• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_NG_PATTERNS_STAGE_PAGE_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STAGE_PAGE_PATTERN_H
18 
19 #include <functional>
20 
21 #include "base/memory/referenced.h"
22 #include "base/utils/noncopyable.h"
23 #include "core/animation/animator_info.h"
24 #include "core/animation/page_transition_common.h"
25 #include "core/components_ng/pattern/pattern.h"
26 #include "core/components_ng/pattern/stage/page_event_hub.h"
27 #include "core/components_ng/pattern/stage/page_info.h"
28 #include "core/components_ng/pattern/stage/page_transition_effect.h"
29 
30 namespace OHOS::Ace::NG {
31 
32 using SharedTransitionMap = std::unordered_map<ShareId, WeakPtr<FrameNode>>;
33 using JSAnimatorMap = std::unordered_map<std::string, RefPtr<Framework::AnimatorInfo>>;
34 // PagePattern is the base class for page root render node.
35 class ACE_EXPORT PagePattern : public Pattern {
36     DECLARE_ACE_TYPE(PagePattern, Pattern);
37 
38 public:
PagePattern(const RefPtr<PageInfo> & pageInfo)39     explicit PagePattern(const RefPtr<PageInfo>& pageInfo) : pageInfo_(pageInfo) {}
40     ~PagePattern() override = default;
41 
IsMeasureBoundary()42     bool IsMeasureBoundary() const override
43     {
44         return true;
45     }
46 
IsAtomicNode()47     bool IsAtomicNode() const override
48     {
49         return false;
50     }
51 
GetPageInfo()52     const RefPtr<PageInfo>& GetPageInfo() const
53     {
54         return pageInfo_;
55     }
56 
57     void OnShow();
58 
59     void OnHide();
60 
OnBackPressed()61     bool OnBackPressed() const
62     {
63         if (isPageInTransition_) {
64             return true;
65         }
66         if (OnBackPressed_) {
67             return OnBackPressed_();
68         }
69         return false;
70     }
71 
SetOnPageShow(std::function<void ()> && onPageShow)72     void SetOnPageShow(std::function<void()>&& onPageShow)
73     {
74         onPageShow_ = std::move(onPageShow);
75     }
76 
SetOnPageHide(std::function<void ()> && onPageHide)77     void SetOnPageHide(std::function<void()>&& onPageHide)
78     {
79         onPageHide_ = std::move(onPageHide);
80     }
81 
SetOnBackPressed(std::function<bool ()> && OnBackPressed)82     void SetOnBackPressed(std::function<bool()>&& OnBackPressed)
83     {
84         OnBackPressed_ = std::move(OnBackPressed);
85     }
86 
SetPageTransitionFunc(std::function<void ()> && pageTransitionFunc)87     void SetPageTransitionFunc(std::function<void()>&& pageTransitionFunc)
88     {
89         pageTransitionFunc_ = std::move(pageTransitionFunc);
90     }
91 
92     // find pageTransition effect according to transition type
93     RefPtr<PageTransitionEffect> FindPageTransitionEffect(PageTransitionType type);
94 
95     void ClearPageTransitionEffect();
96 
97     RefPtr<PageTransitionEffect> GetTopTransition() const;
98 
99     void AddPageTransition(const RefPtr<PageTransitionEffect>& effect);
100 
CreateEventHub()101     RefPtr<EventHub> CreateEventHub() override
102     {
103         return MakeRefPtr<PageEventHub>();
104     }
105 
106     bool TriggerPageTransition(PageTransitionType type, const std::function<void()>& onFinish);
107 
GetFocusPattern()108     FocusPattern GetFocusPattern() const override
109     {
110         return { FocusType::SCOPE, true };
111     }
112 
GetSharedTransitionMap()113     const SharedTransitionMap& GetSharedTransitionMap() const
114     {
115         return sharedTransitionMap_;
116     }
117 
118     void AddJsAnimator(const std::string& animatorId, const RefPtr<Framework::AnimatorInfo>& animatorInfo);
119     RefPtr<Framework::AnimatorInfo> GetJsAnimator(const std::string& animatorId);
120 
121     void BuildSharedTransitionMap();
122 
123     void ReloadPage();
124 
125     void SetFirstBuildCallback(std::function<void()>&& buildCallback);
126 
SetPageInTransition(bool pageTransition)127     void SetPageInTransition(bool pageTransition)
128     {
129         isPageInTransition_ = pageTransition;
130     }
131 
132     // Mark current page node invisible in render tree.
133     void ProcessHideState();
134     // Mark current page node visible in render tree.
135     void ProcessShowState();
136 
137     void StopPageTransition();
138 
MarkRenderDone()139     void MarkRenderDone()
140     {
141         isRenderDone_ = true;
142     }
143 
144 private:
145     void OnAttachToFrameNode() override;
146     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& wrapper, const DirtySwapConfig& config) override;
147     void FirePageTransitionFinish();
148 
149     RefPtr<PageInfo> pageInfo_;
150     RefPtr<Animator> controller_;
151 
152     std::function<void()> onPageShow_;
153     std::function<void()> onPageHide_;
154     std::function<bool()> OnBackPressed_;
155     std::function<void()> pageTransitionFunc_;
156     std::function<void()> firstBuildCallback_;
157     std::shared_ptr<std::function<void()>> pageTransitionFinish_;
158     std::list<RefPtr<PageTransitionEffect>> pageTransitionEffects_;
159 
160     bool isOnShow_ = false;
161     bool isFirstLoad_ = true;
162     bool isPageInTransition_ = false;
163     bool isRenderDone_ = false;
164 
165     SharedTransitionMap sharedTransitionMap_;
166     JSAnimatorMap jsAnimatorMap_;
167 
168     ACE_DISALLOW_COPY_AND_MOVE(PagePattern);
169 };
170 } // namespace OHOS::Ace::NG
171 
172 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STAGE_PAGE_PATTERN_H
173