• 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     void OnColorConfigurationUpdate() override;
69 
70     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
71 
GetFocusPattern()72     FocusPattern GetFocusPattern() const override
73     {
74         return { FocusType::SCOPE, true };
75     }
76 
SetNavDestination(std::function<void (std::string)> && builder)77     void SetNavDestination(std::function<void(std::string)>&& builder)
78     {
79         builder_ = std::move(builder);
80     }
81 
GetNavigationMode()82     NavigationMode GetNavigationMode() const
83     {
84         return navigationMode_;
85     }
86 
SetNavigationMode(NavigationMode navigationMode)87     void SetNavigationMode(NavigationMode navigationMode)
88     {
89         navigationMode_ = navigationMode;
90     }
91 
SetNavigationStack(RefPtr<NavigationStack> && navigationStack)92     void SetNavigationStack(RefPtr<NavigationStack>&& navigationStack)
93     {
94         navigationStack_ = std::move(navigationStack);
95     }
96 
GetNavigationStack()97     const RefPtr<NavigationStack>& GetNavigationStack()
98     {
99         return navigationStack_;
100     }
101 
102     // use for navRouter case
AddNavDestinationNode(const std::string & name,const RefPtr<UINode> & navDestinationNode)103     void AddNavDestinationNode(const std::string& name, const RefPtr<UINode>& navDestinationNode)
104     {
105         navigationStack_->Add(name, navDestinationNode);
106     }
107 
AddNavDestinationNode(const std::string & name,const RefPtr<UINode> & navDestinationNode,NavRouteMode mode)108     void AddNavDestinationNode(const std::string& name, const RefPtr<UINode>& navDestinationNode, NavRouteMode mode)
109     {
110         navigationStack_->Add(name, navDestinationNode, mode);
111     }
112 
AddNavDestinationNode(const std::string & name,RefPtr<UINode> navDestinationNode,NavRouteMode mode,const RefPtr<RouteInfo> & routeInfo)113     void AddNavDestinationNode(const std::string& name, RefPtr<UINode> navDestinationNode, NavRouteMode mode,
114         const RefPtr<RouteInfo>& routeInfo)
115     {
116         navigationStack_->Add(name, navDestinationNode, mode, routeInfo);
117     }
118 
GetNavDestinationNode(const std::string & name)119     RefPtr<UINode> GetNavDestinationNode(const std::string& name)
120     {
121         return navigationStack_->Get(name);
122     }
123 
GetNavDestinationNode()124     RefPtr<UINode> GetNavDestinationNode()
125     {
126         // get NavDestinationNode from the stack top
127         return navigationStack_->Get();
128     }
129 
GetPreNavDestination(const std::string & name,const RefPtr<UINode> & navDestinationNode)130     RefPtr<UINode> GetPreNavDestination(const std::string& name, const RefPtr<UINode>& navDestinationNode)
131     {
132         return navigationStack_->GetPre(name, navDestinationNode);
133     }
134 
GetAllNavDestinationNodes()135     const std::vector<std::pair<std::string, RefPtr<UINode>>>& GetAllNavDestinationNodes()
136     {
137         return navigationStack_->GetAllNavDestinationNodes();
138     }
139 
RemoveIfNeeded(const std::string & name,const RefPtr<UINode> & navDestinationNode)140     void RemoveIfNeeded(const std::string& name, const RefPtr<UINode>& navDestinationNode)
141     {
142         navigationStack_->Remove(name, navDestinationNode);
143     }
144 
RemoveNavDestination()145     void RemoveNavDestination()
146     {
147         navigationStack_->Remove();
148     }
149 
150     void InitDividerMouseEvent(const RefPtr<InputEventHub>& inputHub);
151 
CleanStack()152     void CleanStack()
153     {
154         navigationStack_->RemoveAll();
155     }
156 
SetNavigationStackProvided(bool provided)157     void SetNavigationStackProvided(bool provided)
158     {
159         navigationStackProvided_ = provided;
160     }
161 
GetNavigationStackProvided()162     bool GetNavigationStackProvided() const
163     {
164         return navigationStackProvided_;
165     }
166 
167     void OnWindowHide() override;
168     void OnWindowShow() override;
169 
SetNavBarVisibilityChange(bool isChange)170     void SetNavBarVisibilityChange(bool isChange)
171     {
172         navBarVisibilityChange_ = isChange;
173     }
174 
GetNavBarVisibilityChange()175     bool GetNavBarVisibilityChange() const
176     {
177         return navBarVisibilityChange_;
178     }
179 
180     void OnVisibleChange(bool isVisible) override;
181 
GetMinNavBarWidthValue()182     Dimension GetMinNavBarWidthValue() const
183     {
184         return minNavBarWidthValue_;
185     }
SetMinNavBarWidthValue(Dimension minNavBarWidthValue)186     void SetMinNavBarWidthValue(Dimension minNavBarWidthValue)
187     {
188         minNavBarWidthValue_ = minNavBarWidthValue;
189     }
190 
GetMaxNavBarWidthValue()191     Dimension GetMaxNavBarWidthValue() const
192     {
193         return maxNavBarWidthValue_;
194     }
SetMaxNavBarWidthValue(Dimension maxNavBarWidthValue)195     void SetMaxNavBarWidthValue(Dimension maxNavBarWidthValue)
196     {
197         maxNavBarWidthValue_ = maxNavBarWidthValue;
198     }
199 
GetMinContentWidthValue()200     Dimension GetMinContentWidthValue() const
201     {
202         return minContentWidthValue_;
203     }
204 
SetMinContentWidthValue(Dimension minContentWidthValue)205     void SetMinContentWidthValue(Dimension minContentWidthValue)
206     {
207         minContentWidthValue_ = minContentWidthValue;
208     }
209 
GetUserSetNavBarRangeFlag()210     bool GetUserSetNavBarRangeFlag() const
211     {
212         return userSetNavBarRangeFlag_;
213     }
214 
SetUserSetNavBarRangeFlag(bool userSetNavBarRangeFlag)215     void SetUserSetNavBarRangeFlag(bool userSetNavBarRangeFlag)
216     {
217         userSetNavBarRangeFlag_ = userSetNavBarRangeFlag;
218     }
219 
GetUserSetMinContentFlag()220     bool GetUserSetMinContentFlag() const
221     {
222         return userSetMinContentFlag_;
223     }
224 
SetUserSetMinContentFlag(bool userSetMinContentFlag)225     void SetUserSetMinContentFlag(bool userSetMinContentFlag)
226     {
227         userSetMinContentFlag_ = userSetMinContentFlag;
228     }
229 
GetUserSetNavBarWidthFlag()230     bool GetUserSetNavBarWidthFlag() const
231     {
232         return userSetNavBarWidthFlag_;
233     }
234 
SetUserSetNavBarWidthFlag(bool userSetNavBarWidthFlag)235     void SetUserSetNavBarWidthFlag(bool userSetNavBarWidthFlag)
236     {
237         userSetNavBarWidthFlag_ = userSetNavBarWidthFlag;
238     }
239 
240     void UpdateContextRect(const RefPtr<NavDestinationGroupNode>& curDestination,
241         const RefPtr<NavigationGroupNode>& navigation);
242     void OnNavBarStateChange(bool modeChange);
243 
244 private:
245     void CheckTopNavPathChange(const std::optional<std::pair<std::string, RefPtr<UINode>>>& preTopNavPath,
246         const std::optional<std::pair<std::string, RefPtr<UINode>>>& newTopNavPath, bool isPopPage);
247     void DoNavigationTransitionAnimation(const RefPtr<NavDestinationGroupNode>& preTopNavDestination,
248         const RefPtr<NavDestinationGroupNode>& newTopNavDestination, bool isPopPage);
249     RefPtr<RenderContext> GetTitleBarRenderContext();
250     void DoAnimation(NavigationMode usrNavigationMode);
251     RefPtr<UINode> GenerateUINodeByIndex(int32_t index);
252     RefPtr<FrameNode> GetDividerNode() const;
253     void InitDragEvent(const RefPtr<GestureEventHub>& gestureHub);
254     void HandleDragStart();
255     void HandleDragUpdate(float xOffset);
256     void HandleDragEnd();
257     void OnHover(bool isHover);
258     void AddDividerHotZoneRect(const RefPtr<NavigationLayoutAlgorithm>& layoutAlgorithm);
259     void RangeCalculation(
260         const RefPtr<NavigationGroupNode>& hostNode, const RefPtr<NavigationLayoutProperty>& navigationLayoutProperty);
261     bool UpdateTitleModeChangeEventHub(const RefPtr<NavigationGroupNode>& hostNode);
262     NavigationMode navigationMode_ = NavigationMode::AUTO;
263     std::function<void(std::string)> builder_;
264     RefPtr<NavigationStack> navigationStack_;
265     RefPtr<InputEvent> hoverEvent_;
266     RefPtr<DragEvent> dragEvent_;
267     RectF dragRect_;
268     bool ifNeedInit_ = true;
269     float preNavBarWidth_ = 0.0f;
270     float realNavBarWidth_ = 360.0f;
271     float realDividerWidth_ = 2.0f;
272     bool navigationStackProvided_ = false;
273     bool navBarVisibilityChange_ = false;
274     bool userSetNavBarRangeFlag_ = false;
275     bool userSetMinContentFlag_ = false;
276     bool userSetNavBarWidthFlag_ = false;
277     Dimension minNavBarWidthValue_ = 0.0_vp;
278     Dimension maxNavBarWidthValue_ = 0.0_vp;
279     Dimension minContentWidthValue_ = 0.0_vp;
280     NavigationTitleMode titleMode_ = NavigationTitleMode::FREE;
281 };
282 
283 } // namespace OHOS::Ace::NG
284 
285 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_PATTERN_H
286