• 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_NAVIGATION_NAVIGATION_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_PATTERN_H
18 
19 #include "base/memory/referenced.h"
20 #include "core/components_ng/base/ui_node.h"
21 #include "core/components_ng/pattern/navigation/navigation_declaration.h"
22 #include "core/components_ng/pattern/navigation/navigation_event_hub.h"
23 #include "core/components_ng/pattern/navigation/navigation_group_node.h"
24 #include "core/components_ng/pattern/navigation/navigation_layout_algorithm.h"
25 #include "core/components_ng/pattern/navigation/navigation_layout_property.h"
26 #include "core/components_ng/pattern/navigation/navigation_stack.h"
27 #include "core/components_ng/pattern/navigation/title_bar_layout_property.h"
28 #include "core/components_ng/pattern/navigation/title_bar_node.h"
29 #include "core/components_ng/pattern/navrouter/navdestination_group_node.h"
30 #include "core/components_ng/pattern/pattern.h"
31 
32 namespace OHOS::Ace::NG {
33 
34 using namespace Framework;
35 class NavigationPattern : public Pattern {
36     DECLARE_ACE_TYPE(NavigationPattern, Pattern);
37 
38 public:
39     NavigationPattern() = default;
40     ~NavigationPattern() override = default;
41 
IsAtomicNode()42     bool IsAtomicNode() const override
43     {
44         return false;
45     }
46 
CreateLayoutProperty()47     RefPtr<LayoutProperty> CreateLayoutProperty() override
48     {
49         return MakeRefPtr<NavigationLayoutProperty>();
50     }
51 
CreateEventHub()52     RefPtr<EventHub> CreateEventHub() override
53     {
54         return MakeRefPtr<NavigationEventHub>();
55     }
56 
CreateLayoutAlgorithm()57     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
58     {
59         auto layoutAlgorithm = MakeRefPtr<NavigationLayoutAlgorithm>();
60         layoutAlgorithm->SetRealNavBarWidth(realNavBarWidth_);
61         layoutAlgorithm->SetIfNeedInit(ifNeedInit_);
62         return layoutAlgorithm;
63     }
64 
65     void OnAttachToFrameNode() override;
66     void OnDetachFromFrameNode(FrameNode* frameNode) override;
67     void OnModifyDone() override;
68 
69     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
70 
GetFocusPattern()71     FocusPattern GetFocusPattern() const override
72     {
73         return { FocusType::SCOPE, true };
74     }
75 
SetNavDestination(std::function<void (std::string)> && builder)76     void SetNavDestination(std::function<void(std::string)>&& builder)
77     {
78         builder_ = std::move(builder);
79     }
80 
GetNavigationMode()81     NavigationMode GetNavigationMode() const
82     {
83         return navigationMode_;
84     }
85 
SetNavigationMode(NavigationMode navigationMode)86     void SetNavigationMode(NavigationMode navigationMode)
87     {
88         navigationMode_ = navigationMode;
89     }
90 
SetNavigationStack(RefPtr<NavigationStack> && navigationStack)91     void SetNavigationStack(RefPtr<NavigationStack>&& navigationStack)
92     {
93         navigationStack_ = std::move(navigationStack);
94     }
95 
GetNavigationStack()96     const RefPtr<NavigationStack>& GetNavigationStack()
97     {
98         return navigationStack_;
99     }
100 
101     // use for navRouter case
AddNavDestinationNode(const std::string & name,const RefPtr<UINode> & navDestinationNode)102     void AddNavDestinationNode(const std::string& name, const RefPtr<UINode>& navDestinationNode)
103     {
104         navigationStack_->Add(name, navDestinationNode);
105     }
106 
AddNavDestinationNode(const std::string & name,const RefPtr<UINode> & navDestinationNode,NavRouteMode mode)107     void AddNavDestinationNode(const std::string& name, const RefPtr<UINode>& navDestinationNode, NavRouteMode mode)
108     {
109         navigationStack_->Add(name, navDestinationNode, mode);
110     }
111 
AddNavDestinationNode(const std::string & name,RefPtr<UINode> navDestinationNode,NavRouteMode mode,const RefPtr<RouteInfo> & routeInfo)112     void AddNavDestinationNode(const std::string& name, RefPtr<UINode> navDestinationNode, NavRouteMode mode,
113         const RefPtr<RouteInfo>& routeInfo)
114     {
115         navigationStack_->Add(name, navDestinationNode, mode, routeInfo);
116     }
117 
GetNavDestinationNode(const std::string & name)118     RefPtr<UINode> GetNavDestinationNode(const std::string& name)
119     {
120         return navigationStack_->Get(name);
121     }
122 
GetNavDestinationNode()123     RefPtr<UINode> GetNavDestinationNode()
124     {
125         // get NavDestinationNode from the stack top
126         return navigationStack_->Get();
127     }
128 
GetPreNavDestination(const std::string & name,const RefPtr<UINode> & navDestinationNode)129     RefPtr<UINode> GetPreNavDestination(const std::string& name, const RefPtr<UINode>& navDestinationNode)
130     {
131         return navigationStack_->GetPre(name, navDestinationNode);
132     }
133 
GetAllNavDestinationNodes()134     const std::vector<std::pair<std::string, RefPtr<UINode>>>& GetAllNavDestinationNodes()
135     {
136         return navigationStack_->GetAllNavDestinationNodes();
137     }
138 
RemoveIfNeeded(const std::string & name,const RefPtr<UINode> & navDestinationNode)139     void RemoveIfNeeded(const std::string& name, const RefPtr<UINode>& navDestinationNode)
140     {
141         navigationStack_->Remove(name, navDestinationNode);
142     }
143 
RemoveNavDestination()144     void RemoveNavDestination()
145     {
146         navigationStack_->Remove();
147     }
148 
149     void InitDividerMouseEvent(const RefPtr<InputEventHub>& inputHub);
150 
CleanStack()151     void CleanStack()
152     {
153         navigationStack_->RemoveAll();
154     }
155 
SetNavigationStackProvided(bool provided)156     void SetNavigationStackProvided(bool provided)
157     {
158         navigationStackProvided_ = provided;
159     }
160 
GetNavigationStackProvided()161     bool GetNavigationStackProvided() const
162     {
163         return navigationStackProvided_;
164     }
165 
166     void OnWindowHide() override;
167     void OnWindowShow() override;
168 
SetNavBarVisibilityChange(bool isChange)169     void SetNavBarVisibilityChange(bool isChange)
170     {
171         navBarVisibilityChange_ = isChange;
172     }
173 
GetNavBarVisibilityChange()174     bool GetNavBarVisibilityChange() const
175     {
176         return navBarVisibilityChange_;
177     }
178 
179     void OnVisibleChange(bool isVisible) override;
180 
GetMinNavBarWidthValue()181     Dimension GetMinNavBarWidthValue() const
182     {
183         return minNavBarWidthValue_;
184     }
SetMinNavBarWidthValue(Dimension minNavBarWidthValue)185     void SetMinNavBarWidthValue(Dimension minNavBarWidthValue)
186     {
187         minNavBarWidthValue_ = minNavBarWidthValue;
188     }
189 
GetMaxNavBarWidthValue()190     Dimension GetMaxNavBarWidthValue() const
191     {
192         return maxNavBarWidthValue_;
193     }
SetMaxNavBarWidthValue(Dimension maxNavBarWidthValue)194     void SetMaxNavBarWidthValue(Dimension maxNavBarWidthValue)
195     {
196         maxNavBarWidthValue_ = maxNavBarWidthValue;
197     }
198 
GetMinContentWidthValue()199     Dimension GetMinContentWidthValue() const
200     {
201         return minContentWidthValue_;
202     }
203 
SetMinContentWidthValue(Dimension minContentWidthValue)204     void SetMinContentWidthValue(Dimension minContentWidthValue)
205     {
206         minContentWidthValue_ = minContentWidthValue;
207     }
208 
GetUserSetNavBarRangeFlag()209     bool GetUserSetNavBarRangeFlag() const
210     {
211         return userSetNavBarRangeFlag_;
212     }
213 
SetUserSetNavBarRangeFlag(bool userSetNavBarRangeFlag)214     void SetUserSetNavBarRangeFlag(bool userSetNavBarRangeFlag)
215     {
216         userSetNavBarRangeFlag_ = userSetNavBarRangeFlag;
217     }
218 
GetUserSetMinContentFlag()219     bool GetUserSetMinContentFlag() const
220     {
221         return userSetMinContentFlag_;
222     }
223 
SetUserSetMinContentFlag(bool userSetMinContentFlag)224     void SetUserSetMinContentFlag(bool userSetMinContentFlag)
225     {
226         userSetMinContentFlag_ = userSetMinContentFlag;
227     }
228 
GetUserSetNavBarWidthFlag()229     bool GetUserSetNavBarWidthFlag() const
230     {
231         return userSetNavBarWidthFlag_;
232     }
233 
SetUserSetNavBarWidthFlag(bool userSetNavBarWidthFlag)234     void SetUserSetNavBarWidthFlag(bool userSetNavBarWidthFlag)
235     {
236         userSetNavBarWidthFlag_ = userSetNavBarWidthFlag;
237     }
238 
239     void UpdateContextRect(const RefPtr<NavDestinationGroupNode>& curDestination,
240         const RefPtr<NavigationGroupNode>& navigation);
241 
242 private:
243     void CheckTopNavPathChange(const std::optional<std::pair<std::string, RefPtr<UINode>>>& preTopNavPath,
244         const std::optional<std::pair<std::string, RefPtr<UINode>>>& newTopNavPath, bool isPopPage);
245     void DoNavigationTransitionAnimation(const RefPtr<NavDestinationGroupNode>& preTopNavDestination,
246         const RefPtr<NavDestinationGroupNode>& newTopNavDestination, bool isPopPage);
247     RefPtr<RenderContext> GetTitleBarRenderContext();
248     void DoAnimation(NavigationMode usrNavigationMode);
249     RefPtr<UINode> GenerateUINodeByIndex(int32_t index);
250     RefPtr<FrameNode> GetDividerNode() const;
251     void InitDragEvent(const RefPtr<GestureEventHub>& gestureHub);
252     void HandleDragStart();
253     void HandleDragUpdate(float xOffset);
254     void HandleDragEnd();
255     void OnHover(bool isHover);
256     void AddDividerHotZoneRect(const RefPtr<NavigationLayoutAlgorithm>& layoutAlgorithm);
257     void RangeCalculation(
258         const RefPtr<NavigationGroupNode>& hostNode, const RefPtr<NavigationLayoutProperty>& navigationLayoutProperty);
259     void OnNavBarStateChange(bool modeChange);
260     bool UpdateTitleModeChangeEventHub(const RefPtr<NavigationGroupNode>& hostNode);
261     NavigationMode navigationMode_ = NavigationMode::AUTO;
262     std::function<void(std::string)> builder_;
263     RefPtr<NavigationStack> navigationStack_;
264     RefPtr<InputEvent> hoverEvent_;
265     RefPtr<DragEvent> dragEvent_;
266     RectF dragRect_;
267     bool ifNeedInit_ = true;
268     float preNavBarWidth_ = 0.0f;
269     float realNavBarWidth_ = 360.0f;
270     float realDividerWidth_ = 2.0f;
271     bool navigationStackProvided_ = false;
272     bool navBarVisibilityChange_ = false;
273     bool userSetNavBarRangeFlag_ = false;
274     bool userSetMinContentFlag_ = false;
275     bool userSetNavBarWidthFlag_ = false;
276     Dimension minNavBarWidthValue_ = 0.0_vp;
277     Dimension maxNavBarWidthValue_ = 0.0_vp;
278     Dimension minContentWidthValue_ = 0.0_vp;
279     NavigationTitleMode titleMode_ = NavigationTitleMode::FREE;
280 };
281 
282 } // namespace OHOS::Ace::NG
283 
284 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_PATTERN_H
285