• 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/inner_navigation_controller.h"
22 #include "core/components_ng/pattern/navigation/navigation_declaration.h"
23 #include "core/components_ng/pattern/navigation/navigation_event_hub.h"
24 #include "core/components_ng/pattern/navigation/navigation_group_node.h"
25 #include "core/components_ng/pattern/navigation/navigation_layout_algorithm.h"
26 #include "core/components_ng/pattern/navigation/navigation_layout_property.h"
27 #include "core/components_ng/pattern/navigation/navigation_stack.h"
28 #include "core/components_ng/pattern/navigation/title_bar_layout_property.h"
29 #include "core/components_ng/pattern/navigation/title_bar_node.h"
30 #include "core/components_ng/pattern/navigation/navigation_transition_proxy.h"
31 #include "core/components_ng/pattern/navrouter/navdestination_group_node.h"
32 #include "core/components_ng/pattern/pattern.h"
33 
34 namespace OHOS::Ace::NG {
35 
36 using namespace Framework;
37 using OnNavigationAnimation = std::function<NavigationTransition(NavContentInfo, NavContentInfo,
38         NavigationOperation)>;
39 class NavigationPattern : public Pattern {
40     DECLARE_ACE_TYPE(NavigationPattern, Pattern);
41 
42 public:
43     NavigationPattern();
44     ~NavigationPattern() override = default;
45 
IsAtomicNode()46     bool IsAtomicNode() const override
47     {
48         return false;
49     }
50 
CreateLayoutProperty()51     RefPtr<LayoutProperty> CreateLayoutProperty() override
52     {
53         return MakeRefPtr<NavigationLayoutProperty>();
54     }
55 
CreateEventHub()56     RefPtr<EventHub> CreateEventHub() override
57     {
58         return MakeRefPtr<NavigationEventHub>();
59     }
60 
CreateLayoutAlgorithm()61     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
62     {
63         auto layoutAlgorithm = MakeRefPtr<NavigationLayoutAlgorithm>();
64         layoutAlgorithm->SetRealNavBarWidth(realNavBarWidth_);
65         layoutAlgorithm->SetIfNeedInit(ifNeedInit_);
66         return layoutAlgorithm;
67     }
68 
69     void OnAttachToFrameNode() override;
70     void OnDetachFromFrameNode(FrameNode* frameNode) override;
71     void OnModifyDone() override;
72 
73     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
74 
GetFocusPattern()75     FocusPattern GetFocusPattern() const override
76     {
77         return { FocusType::SCOPE, true };
78     }
79 
GetScopeFocusAlgorithm()80     ScopeFocusAlgorithm GetScopeFocusAlgorithm() override
81     {
82         return { false, true, ScopeType::FLEX };
83     }
84 
SetNavDestination(std::function<void (std::string)> && builder)85     void SetNavDestination(std::function<void(std::string)>&& builder)
86     {
87         builder_ = std::move(builder);
88     }
89 
GetNavigationMode()90     NavigationMode GetNavigationMode() const
91     {
92         return navigationMode_;
93     }
94 
SetNavigationMode(NavigationMode navigationMode)95     void SetNavigationMode(NavigationMode navigationMode)
96     {
97         navigationMode_ = navigationMode;
98     }
99 
100     void SetNavigationStack(const RefPtr<NavigationStack>& navigationStack);
101 
GetNavigationStack()102     const RefPtr<NavigationStack>& GetNavigationStack()
103     {
104         return navigationStack_;
105     }
106 
107     // use for navRouter case
AddNavDestinationNode(const std::string & name,const RefPtr<UINode> & navDestinationNode)108     void AddNavDestinationNode(const std::string& name, const RefPtr<UINode>& navDestinationNode)
109     {
110         navigationStack_->Add(name, navDestinationNode);
111     }
112 
AddNavDestinationNode(const std::string & name,const RefPtr<UINode> & navDestinationNode,NavRouteMode mode)113     void AddNavDestinationNode(const std::string& name, const RefPtr<UINode>& navDestinationNode, NavRouteMode mode)
114     {
115         navigationStack_->Add(name, navDestinationNode, mode);
116     }
117 
AddNavDestinationNode(const std::string & name,const RefPtr<UINode> & navDestinationNode,NavRouteMode mode,const RefPtr<RouteInfo> & routeInfo)118     void AddNavDestinationNode(const std::string& name, const RefPtr<UINode>& navDestinationNode, NavRouteMode mode,
119         const RefPtr<RouteInfo>& routeInfo)
120     {
121         navigationStack_->Add(name, navDestinationNode, mode, routeInfo);
122     }
123 
GetNavDestinationNode(const std::string & name)124     RefPtr<UINode> GetNavDestinationNode(const std::string& name)
125     {
126         return navigationStack_->Get(name);
127     }
128 
GetNavDestinationNode()129     RefPtr<UINode> GetNavDestinationNode()
130     {
131         // get NavDestinationNode from the stack top
132         return navigationStack_->Get();
133     }
134 
GetPreNavDestination(const std::string & name,const RefPtr<UINode> & navDestinationNode)135     RefPtr<UINode> GetPreNavDestination(const std::string& name, const RefPtr<UINode>& navDestinationNode)
136     {
137         return navigationStack_->GetPre(name, navDestinationNode);
138     }
139 
GetAllNavDestinationNodes()140     const std::vector<std::pair<std::string, RefPtr<UINode>>>& GetAllNavDestinationNodes()
141     {
142         return navigationStack_->GetAllNavDestinationNodes();
143     }
144 
RemoveIfNeeded(const std::string & name,const RefPtr<UINode> & navDestinationNode)145     void RemoveIfNeeded(const std::string& name, const RefPtr<UINode>& navDestinationNode)
146     {
147         navigationStack_->Remove(name, navDestinationNode);
148     }
149 
RemoveNavDestination()150     void RemoveNavDestination()
151     {
152         navigationStack_->Remove();
153     }
154 
155     void InitDividerMouseEvent(const RefPtr<InputEventHub>& inputHub);
156 
CleanStack()157     void CleanStack()
158     {
159         navigationStack_->RemoveAll();
160     }
161 
UpdateAnimatedValue(bool animated)162     void UpdateAnimatedValue(bool animated)
163     {
164         navigationStack_->UpdateAnimatedValue(animated);
165     }
166 
SetNavigationStackProvided(bool provided)167     void SetNavigationStackProvided(bool provided)
168     {
169         navigationStackProvided_ = provided;
170     }
171 
GetNavigationStackProvided()172     bool GetNavigationStackProvided() const
173     {
174         return navigationStackProvided_;
175     }
176 
177     void OnWindowHide() override;
178     void OnWindowShow() override;
179 
SetNavBarVisibilityChange(bool isChange)180     void SetNavBarVisibilityChange(bool isChange)
181     {
182         navBarVisibilityChange_ = isChange;
183     }
184 
GetNavBarVisibilityChange()185     bool GetNavBarVisibilityChange() const
186     {
187         return navBarVisibilityChange_;
188     }
189 
190     void OnVisibleChange(bool isVisible) override;
191 
192     void OnColorConfigurationUpdate() override;
193 
GetMinNavBarWidthValue()194     Dimension GetMinNavBarWidthValue() const
195     {
196         return minNavBarWidthValue_;
197     }
SetMinNavBarWidthValue(Dimension minNavBarWidthValue)198     void SetMinNavBarWidthValue(Dimension minNavBarWidthValue)
199     {
200         minNavBarWidthValue_ = minNavBarWidthValue;
201     }
202 
GetMaxNavBarWidthValue()203     Dimension GetMaxNavBarWidthValue() const
204     {
205         return maxNavBarWidthValue_;
206     }
SetMaxNavBarWidthValue(Dimension maxNavBarWidthValue)207     void SetMaxNavBarWidthValue(Dimension maxNavBarWidthValue)
208     {
209         maxNavBarWidthValue_ = maxNavBarWidthValue;
210     }
211 
GetMinContentWidthValue()212     Dimension GetMinContentWidthValue() const
213     {
214         return minContentWidthValue_;
215     }
216 
SetMinContentWidthValue(Dimension minContentWidthValue)217     void SetMinContentWidthValue(Dimension minContentWidthValue)
218     {
219         minContentWidthValue_ = minContentWidthValue;
220     }
221 
GetUserSetNavBarRangeFlag()222     bool GetUserSetNavBarRangeFlag() const
223     {
224         return userSetNavBarRangeFlag_;
225     }
226 
SetUserSetNavBarRangeFlag(bool userSetNavBarRangeFlag)227     void SetUserSetNavBarRangeFlag(bool userSetNavBarRangeFlag)
228     {
229         userSetNavBarRangeFlag_ = userSetNavBarRangeFlag;
230     }
231 
GetUserSetMinContentFlag()232     bool GetUserSetMinContentFlag() const
233     {
234         return userSetMinContentFlag_;
235     }
236 
SetUserSetMinContentFlag(bool userSetMinContentFlag)237     void SetUserSetMinContentFlag(bool userSetMinContentFlag)
238     {
239         userSetMinContentFlag_ = userSetMinContentFlag;
240     }
241 
GetUserSetNavBarWidthFlag()242     bool GetUserSetNavBarWidthFlag() const
243     {
244         return userSetNavBarWidthFlag_;
245     }
246 
SetUserSetNavBarWidthFlag(bool userSetNavBarWidthFlag)247     void SetUserSetNavBarWidthFlag(bool userSetNavBarWidthFlag)
248     {
249         userSetNavBarWidthFlag_ = userSetNavBarWidthFlag;
250     }
251 
SetInitNavBarWidth(const Dimension & initNavBarWidth)252     void SetInitNavBarWidth(const Dimension& initNavBarWidth)
253     {
254         realNavBarWidth_ = static_cast<float>(initNavBarWidth.ConvertToPx());
255         initNavBarWidthValue_ = initNavBarWidth;
256     }
257 
GetInitNavBarWidth()258     Dimension GetInitNavBarWidth() const
259     {
260         return initNavBarWidthValue_;
261     }
262 
SetIfNeedInit(bool ifNeedInit)263     void SetIfNeedInit(bool ifNeedInit)
264     {
265         ifNeedInit_ = ifNeedInit;
266     }
267 
268     void UpdateContextRect(
269         const RefPtr<NavDestinationGroupNode>& curDestination, const RefPtr<NavigationGroupNode>& navigation);
270 
271     void OnNavBarStateChange(bool modeChange);
272 
GetNavigationModeChange()273     bool GetNavigationModeChange() const
274     {
275         return navigationModeChange_;
276     }
277 
SetNavigationModeChange(bool modeChange)278     void SetNavigationModeChange(bool modeChange)
279     {
280         navigationModeChange_ = modeChange;
281     }
282 
AddOnStateChangeItem(int32_t nodeId,std::function<void (bool)> callback)283     void AddOnStateChangeItem(int32_t nodeId, std::function<void(bool)> callback)
284     {
285         onStateChangeMap_.emplace(nodeId, callback);
286     }
287 
DeleteOnStateChangeItem(int32_t nodeId)288     void DeleteOnStateChangeItem(int32_t nodeId)
289     {
290         onStateChangeMap_.erase(nodeId);
291     }
292 
GetNavigationController()293     const std::shared_ptr<NavigationController>& GetNavigationController() const
294     {
295         return navigationController_;
296     }
297 
GetOnStateChangeMap()298     const std::map<int32_t, std::function<void(bool)>>& GetOnStateChangeMap()
299     {
300         return onStateChangeMap_;
301     }
302 
303     void OnNavigationModeChange(bool modeChange);
304 
305     static void FireNavigationStateChange(const RefPtr<UINode>& node, bool show);
306 
307     void NotifyDialogChange(bool isShow, bool isNavigationChanged);
308     void NotifyPageHide(const std::string& pageName);
309     void DumpInfo() override;
310 
SetIsCustomAnimation(bool isCustom)311     void SetIsCustomAnimation(bool isCustom)
312     {
313         isCustomAnimation_ = isCustom;
314     }
315 
SetNavigationTransition(const OnNavigationAnimation navigationAnimation)316     void SetNavigationTransition(const OnNavigationAnimation navigationAnimation)
317     {
318         onTransition_ = std::move(navigationAnimation);
319     }
320 
NeedSyncWithJsStackMarked()321     bool NeedSyncWithJsStackMarked() const
322     {
323         return needSyncWithJsStack_;
324     }
325 
MarkNeedSyncWithJsStack()326     void MarkNeedSyncWithJsStack()
327     {
328         needSyncWithJsStack_ = true;
329     }
330 
331     void SyncWithJsStackIfNeeded();
332 
333     void AttachNavigationStackToParent();
334     void DetachNavigationStackFromParent();
335 
336 private:
337     void CheckTopNavPathChange(const std::optional<std::pair<std::string, RefPtr<UINode>>>& preTopNavPath,
338         const std::optional<std::pair<std::string, RefPtr<UINode>>>& newTopNavPath, bool isPopPage);
339     void TransitionWithAnimation(const RefPtr<NavDestinationGroupNode>& preTopNavDestination,
340         const RefPtr<NavDestinationGroupNode>& newTopNavDestination, bool isPopPage);
341     bool TriggerCustomAnimation(const RefPtr<NavDestinationGroupNode>& preTopNavDestination,
342         const RefPtr<NavDestinationGroupNode>& newTopNavDestination, bool isPopPage);
343 
344     void OnCustomAnimationFinish(const RefPtr<NavDestinationGroupNode>& preTopNavDestination,
345         const RefPtr<NavDestinationGroupNode>& newTopNavDestination, bool isPopPage);
346     void TransitionWithOutAnimation(const RefPtr<NavDestinationGroupNode>& preTopNavDestination,
347         const RefPtr<NavDestinationGroupNode>& newTopNavDestination, bool isPopPage, bool needVisible = false);
348     NavigationTransition ExecuteTransition(const RefPtr<NavDestinationGroupNode>& preTopDestination,
349         const RefPtr<NavDestinationGroupNode>& newTopNavDestination, bool isPopPage);
350     RefPtr<RenderContext> GetTitleBarRenderContext();
351     void DoAnimation(NavigationMode usrNavigationMode);
352     RefPtr<UINode> GenerateUINodeByIndex(int32_t index);
353     RefPtr<FrameNode> GetDividerNode() const;
354     void InitDragEvent(const RefPtr<GestureEventHub>& gestureHub);
355     void HandleDragStart();
356     void HandleDragUpdate(float xOffset);
357     void HandleDragEnd();
358     void OnHover(bool isHover);
359     void AddDividerHotZoneRect();
360     void RangeCalculation(
361         const RefPtr<NavigationGroupNode>& hostNode, const RefPtr<NavigationLayoutProperty>& navigationLayoutProperty);
362     bool UpdateTitleModeChangeEventHub(const RefPtr<NavigationGroupNode>& hostNode);
363     void NotifyPageShow(const std::string& pageName);
364     int32_t FireNavDestinationStateChange(bool show);
365     void UpdatePreNavDesZIndex(const RefPtr<FrameNode> &preTopNavDestination,
366         const RefPtr<FrameNode> &newTopNavDestination);
367     void UpdateNavPathList();
368     void RefreshNavDestination();
369     RefPtr<NavigationPattern> GetParentNavigationPattern();
370     void DealTransitionVisibility(const RefPtr<FrameNode>& node, bool isVisible, bool isNavBar);
371 
372     NavigationMode navigationMode_ = NavigationMode::AUTO;
373     std::function<void(std::string)> builder_;
374     RefPtr<NavigationStack> navigationStack_;
375     RefPtr<InputEvent> hoverEvent_;
376     RefPtr<DragEvent> dragEvent_;
377     RefPtr<NavigationTransitionProxy> currentProxy_;
378     RectF dragRect_;
379     bool ifNeedInit_ = true;
380     float preNavBarWidth_ = 0.0f;
381     float realNavBarWidth_ = DEFAULT_NAV_BAR_WIDTH.ConvertToPx();
382     float realDividerWidth_ = 2.0f;
383     bool navigationStackProvided_ = false;
384     bool navBarVisibilityChange_ = false;
385     bool userSetNavBarRangeFlag_ = false;
386     bool userSetMinContentFlag_ = false;
387     bool userSetNavBarWidthFlag_ = false;
388     bool isChanged_ = false; // check navigation top page is change
389     Dimension initNavBarWidthValue_ = DEFAULT_NAV_BAR_WIDTH;
390     Dimension minNavBarWidthValue_ = 0.0_vp;
391     Dimension maxNavBarWidthValue_ = 0.0_vp;
392     Dimension minContentWidthValue_ = 0.0_vp;
393     NavigationTitleMode titleMode_ = NavigationTitleMode::FREE;
394     bool navigationModeChange_ = false;
395     bool isCustomAnimation_ = false; // custom animation
396     bool isInDividerDrag_ = false;
397     bool isDividerDraggable_ = true;
398     std::shared_ptr<NavigationController> navigationController_;
399     std::map<int32_t, std::function<void(bool)>> onStateChangeMap_;
400     OnNavigationAnimation onTransition_;
401     bool needSyncWithJsStack_ = false;
402     std::optional<std::pair<std::string, RefPtr<UINode>>> preTopNavPath_;
403     int32_t preStackSize_ = 0;
404 };
405 
406 } // namespace OHOS::Ace::NG
407 
408 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_PATTERN_H
409