1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_STAGE_STAGE_ELEMENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_STAGE_STAGE_ELEMENT_H
18 
19 #include "core/animation/animator.h"
20 #include "core/animation/page_transition_listener.h"
21 #include "core/components/page/page_element.h"
22 #include "core/components/page_transition/page_transition_element.h"
23 #include "core/components/stack/stack_element.h"
24 
25 namespace OHOS::Ace {
26 
27 enum class StackOperation { NONE, PUSH_PAGE, POP, POP_TO_PAGE, REPLACE, CLEAR, RESTORE };
28 
29 class StageElement : public StackElement, public PageTransitionListenable, public FlushEvent {
30     DECLARE_ACE_TYPE(StageElement, StackElement, FlushEvent);
31 
32 public:
33     void PerformBuild() override;
34 
35     virtual void PushPage(const RefPtr<Component>& newComponent);
36     void Pop();
37     void PopToPage(int32_t pageId);
38     void RestorePopPage(const RefPtr<Component>& newComponent);
39     virtual void Replace(const RefPtr<Component>& newComponent);
40     void Replace(const RefPtr<Component>& newComponent, const std::function<void()>& listener);
41     bool ClearOffStage(const std::function<void()>& listener);
42     bool CanPopPage();
43     bool CanPushPage();
44     bool CanReplacePage();
45     bool CanRouterPage();
46     void SetSinglePageId(int32_t pageId);
47     void PostponePageTransition();
48     void LaunchPageTransition();
49     void RefreshFocus();
50     bool IsFocusable() const override;
51     bool InitTransition(const RefPtr<PageTransitionElement>& transitionIn,
52         const RefPtr<PageTransitionElement>& transitionOut, TransitionEvent event);
53     RefPtr<PageElement> GetTopPage() const;
GetStackOperation()54     StackOperation GetStackOperation() const
55     {
56         return operation_;
57     }
58 
59     bool IsTransitionStop() const;
60 
isPageElement()61     bool isPageElement() override
62     {
63         return true;
64     }
65 
66 protected:
67     void MarkDirty() override;
68     bool OnKeyEvent(const KeyEvent& keyEvent) override;
69 
70 private:
71     void PerformPushPage();
72     void PerformPop();
73     void PerformReplace();
74     void PerformPopToPage();
75     void PerformClear();
76 #ifndef WEARABLE_PRODUCT
77     void PerformPushMultimodalScene(int32_t pageId);
78     void PerformPopMultimodalScene(int32_t poppedPageId, int32_t incomingPageId);
79     void PerformReplaceActiveScene(int32_t newPageId, int32_t replaceId);
80     void PerformRemoveInactiveScene(int32_t pageId);
81 #endif
82     bool PerformPushPageTransition(const RefPtr<Element>& elementIn, const RefPtr<Element>& elementOut);
83     bool PerformPopPageTransition(const RefPtr<Element>& elementIn, const RefPtr<Element>& elementOut);
84     void AddListenerForPopPage(const WeakPtr<PageElement>& pageInWeak, const WeakPtr<PageElement>& pageOutWeak);
85     static bool CheckPageTransitionElement(
86         const RefPtr<PageTransitionElement>& transitionIn, const RefPtr<PageTransitionElement>& transitionOut);
87     bool InitTransition(const RefPtr<PageTransitionElement>& transition, TransitionDirection direction,
88         TransitionEvent event, const RRect& cardRect);
89     void PerformPushPageInStage(const WeakPtr<PageElement>& pageOutWeak);
90     void PerformPopPageInStage(const RefPtr<PageElement>& pageOut, const RefPtr<PageTransitionElement>& transitionOut);
91     void ProcessStageInPageTransition();
92     // Page transition parameters depend on the existence of shared element transitions,
93     // Only after PerformBuild can we determine if there is a shared element transition.
94     void OnPostFlush() override;
95     void MakeTopPageTouchable();
96 
97     void RestorePop();
AddListener(const std::function<void ()> & listener)98     void AddListener(const std::function<void()>& listener)
99     {
100         routerListener_ = listener;
101     }
102     void RecycleSinglePage();
103 
104     StackOperation operation_ { StackOperation::NONE };
105     StackOperation pendingOperation_ { StackOperation::NONE };
106     bool postponePageTransition_ { false };
107 
108     RefPtr<Component> newComponent_;
109     RefPtr<Animator> controllerIn_;  // Controller for transition in.
110     RefPtr<Animator> controllerOut_; // Controller for transition out.
111     int32_t directedPageId_ = 0;
112     bool isWaitingForBuild_ = false;
113     int32_t singlePageId_ = -1;
114 
115     std::function<void()> routerListener_ = nullptr;
116 };
117 
118 class SectionStageElement : public StageElement {
119     DECLARE_ACE_TYPE(SectionStageElement, StageElement);
120 
121 public:
122     void PushPage(const RefPtr<Component>& newComponent) override;
123     void Replace(const RefPtr<Component>& newComponent) override;
124 
125 private:
126     void AddAsOnlyPage(const RefPtr<Component>& newComponent);
127 };
128 
129 } // namespace OHOS::Ace
130 
131 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_STAGE_STAGE_ELEMENT_H
132