• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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_NAVROUTER_NAVDESTINATION_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVROUTER_NAVDESTINATION_PATTERN_H
18 
19 #include "base/memory/referenced.h"
20 #include "base/system_bar/system_bar_style.h"
21 #include "base/utils/utils.h"
22 #include "core/common/autofill/auto_fill_trigger_state_holder.h"
23 #include "core/components_ng/base/ui_node.h"
24 #include "core/components_ng/pattern/navigation/navdestination_pattern_base.h"
25 #include "core/components_ng/pattern/navigation/navigation_event_hub.h"
26 #include "core/components_ng/pattern/navigation/navigation_stack.h"
27 #include "core/components_ng/pattern/navrouter/navdestination_context.h"
28 #include "core/components_ng/pattern/navrouter/navdestination_event_hub.h"
29 #include "core/components_ng/pattern/navrouter/navdestination_group_node.h"
30 #include "core/components_ng/pattern/navrouter/navdestination_layout_algorithm.h"
31 #include "core/components_ng/pattern/navrouter/navdestination_layout_property.h"
32 #include "core/components_ng/pattern/navrouter/navdestination_scrollable_processor.h"
33 #include "core/components_ng/pattern/pattern.h"
34 #include "core/components_ng/syntax/shallow_builder.h"
35 #include "core/components_v2/inspector/inspector_constants.h"
36 
37 namespace OHOS::Ace::NG {
38 
39 class NavDestinationPattern : public NavDestinationPatternBase, public AutoFillTriggerStateHolder {
40     DECLARE_ACE_TYPE(NavDestinationPattern, NavDestinationPatternBase, AutoFillTriggerStateHolder);
41 
42 public:
43     explicit NavDestinationPattern(const RefPtr<ShallowBuilder>& shallowBuilder);
44     NavDestinationPattern();
45     ~NavDestinationPattern() override;
46 
CreateLayoutProperty()47     RefPtr<LayoutProperty> CreateLayoutProperty() override
48     {
49         return MakeRefPtr<NavDestinationLayoutProperty>();
50     }
51 
CreateLayoutAlgorithm()52     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
53     {
54         auto layout = MakeRefPtr<NavDestinationLayoutAlgorithm>();
55         layout->SetIsShown(isOnShow_);
56         return layout;
57     }
58 
CreateEventHub()59     RefPtr<EventHub> CreateEventHub() override
60     {
61         return MakeRefPtr<NavDestinationEventHub>();
62     }
63 
64     void OnActive() override;
65 
66     void OnModifyDone() override;
67 
GetShallowBuilder()68     const RefPtr<ShallowBuilder>& GetShallowBuilder() const
69     {
70         return shallowBuilder_;
71     }
72 
SetName(const std::string & name)73     void SetName(const std::string& name)
74     {
75         name_ = name;
76         auto eventHub = GetEventHub<NavDestinationEventHub>();
77         CHECK_NULL_VOID(eventHub);
78         eventHub->SetName(name);
79     }
80 
GetName()81     const std::string& GetName()
82     {
83         return name_;
84     }
85 
SetNavPathInfo(const RefPtr<NavPathInfo> & pathInfo)86     void SetNavPathInfo(const RefPtr<NavPathInfo>& pathInfo)
87     {
88         if (!navDestinationContext_) {
89             return;
90         }
91         auto oldInfo = navDestinationContext_->GetNavPathInfo();
92         if (oldInfo) {
93             oldInfo->UpdateNavPathInfo(pathInfo);
94         } else {
95             navDestinationContext_->SetNavPathInfo(pathInfo);
96         }
97     }
98 
GetNavPathInfo()99     RefPtr<NavPathInfo> GetNavPathInfo() const
100     {
101         return navDestinationContext_ ? navDestinationContext_->GetNavPathInfo() : nullptr;
102     }
103 
SetNavigationStack(const WeakPtr<NavigationStack> & stack)104     void SetNavigationStack(const WeakPtr<NavigationStack>& stack)
105     {
106         if (navDestinationContext_) {
107             navDestinationContext_->SetNavigationStack(stack);
108         }
109     }
110 
GetNavigationStack()111     WeakPtr<NavigationStack> GetNavigationStack() const
112     {
113         return navDestinationContext_ ? navDestinationContext_->GetNavigationStack() : nullptr;
114     }
115 
SetIndex(int32_t index)116     void SetIndex(int32_t index)
117     {
118         if (navDestinationContext_) {
119             navDestinationContext_->SetIndex(index);
120         }
121     }
122 
SetNavDestinationContext(const RefPtr<NavDestinationContext> & context)123     void SetNavDestinationContext(const RefPtr<NavDestinationContext>& context)
124     {
125         navDestinationContext_ = context;
126         if (navDestinationContext_) {
127             navDestinationContext_->SetNavDestinationId(navDestinationId_);
128         }
129     }
130 
GetNavDestinationContext()131     RefPtr<NavDestinationContext> GetNavDestinationContext() const
132     {
133         return navDestinationContext_;
134     }
135 
SetCustomNode(const RefPtr<UINode> & customNode)136     void SetCustomNode(const RefPtr<UINode>& customNode)
137     {
138         customNode_ = customNode;
139     }
140 
GetCustomNode()141     RefPtr<UINode> GetCustomNode()
142     {
143         return customNode_;
144     }
145 
SetIsOnShow(bool isOnShow)146     void SetIsOnShow(bool isOnShow)
147     {
148         isOnShow_ = isOnShow;
149     }
150 
GetIsOnShow()151     bool GetIsOnShow()
152     {
153         return isOnShow_;
154     }
155 
156     bool GetBackButtonState();
157 
GetNavigationNode()158     RefPtr<UINode> GetNavigationNode()
159     {
160         return navigationNode_.Upgrade();
161     }
162 
GetNavDestinationState()163     NavDestinationState GetNavDestinationState() const
164     {
165         auto eventHub = GetEventHub<NavDestinationEventHub>();
166         CHECK_NULL_RETURN(eventHub, NavDestinationState::NONE);
167         auto state = eventHub->GetState();
168         return state;
169     }
170 
171     void DumpInfo() override;
172     void DumpInfo(std::unique_ptr<JsonValue>& json) override;
DumpSimplifyInfo(std::unique_ptr<JsonValue> & json)173     void DumpSimplifyInfo(std::unique_ptr<JsonValue>& json) override {}
174 
GetNavDestinationId()175     uint64_t GetNavDestinationId() const
176     {
177         return navDestinationId_;
178     }
179 
SetNavigationNode(const RefPtr<UINode> & navigationNode)180     void SetNavigationNode(const RefPtr<UINode>& navigationNode)
181     {
182         navigationNode_ = AceType::WeakClaim(RawPtr(navigationNode));
183     }
184 
OnDetachFromMainTree()185     void OnDetachFromMainTree() override
186     {
187         backupStyle_.reset();
188         currStyle_.reset();
189     }
190 
191     bool OverlayOnBackPressed();
192 
CreateOverlayManager(bool isShow)193     void CreateOverlayManager(bool isShow)
194     {
195         if (!overlayManager_ && isShow) {
196             overlayManager_ = MakeRefPtr<OverlayManager>(GetHost());
197         }
198     }
199 
GetOverlayManager()200     const RefPtr<OverlayManager>& GetOverlayManager()
201     {
202         return overlayManager_;
203     }
204 
DeleteOverlayManager()205     void DeleteOverlayManager()
206     {
207         overlayManager_.Reset();
208     }
209 
210     int32_t GetTitlebarZIndex() const;
211 
SetNavigationId(const std::string & id)212     void SetNavigationId(const std::string& id)
213     {
214         inspectorId_ = id;
215     }
216 
GetNavigationId()217     std::string GetNavigationId() const
218     {
219         return inspectorId_;
220     }
221 
SetIsUserDefinedBgColor(bool isUserDefinedBgColor)222     void SetIsUserDefinedBgColor(bool isUserDefinedBgColor)
223     {
224         isUserDefinedBgColor_ = isUserDefinedBgColor;
225     }
226 
IsUserDefinedBgColor()227     bool IsUserDefinedBgColor() const
228     {
229         return isUserDefinedBgColor_;
230     }
231 
232     void OnLanguageConfigurationUpdate() override;
233 
234     bool NeedIgnoreKeyboard();
235 
236     void SetSystemBarStyle(const RefPtr<SystemBarStyle>& style);
GetBackupStyle()237     const std::optional<RefPtr<SystemBarStyle>>& GetBackupStyle() const
238     {
239         return backupStyle_;
240     }
GetCurrentStyle()241     const std::optional<RefPtr<SystemBarStyle>>& GetCurrentStyle() const
242     {
243         return currStyle_;
244     }
245 
246     void OnWindowHide() override;
247 
GetScrollableProcessor()248     const RefPtr<NavDestinationScrollableProcessor>& GetScrollableProcessor() const
249     {
250         return scrollableProcessor_;
251     }
SetScrollableProcessor(const RefPtr<NavDestinationScrollableProcessor> & processor)252     void SetScrollableProcessor(const RefPtr<NavDestinationScrollableProcessor>& processor)
253     {
254         scrollableProcessor_ = processor;
255     }
256     /**
257      * Respond to the scrolling events of the internal scrolling components of NavDestination,
258      * and hide part of the titleBar&toolBar( translate up or down in Y axis ), while changing
259      * the opacity of the titleBar&toolBar.
260      */
261     void UpdateTitleAndToolBarHiddenOffset(float offset);
262     // show titleBar&toolBar immediately.
263     void ShowTitleAndToolBar();
264     // cancel the delayed task if we have started, which will show titleBar&toolBar in the feature time.
265     void CancelShowTitleAndToolBarTask();
266     // Restore the titleBar&toolBar to its original position (hide or show state).
267     void ResetTitleAndToolBarState();
268 
269     void OnCoordScrollStart() override;
270     float OnCoordScrollUpdate(float offset, float currentOffset) override;
271     void OnCoordScrollEnd() override;
NeedCoordWithScroll()272     bool NeedCoordWithScroll() override
273     {
274         return true;
275     }
276 
GetTitleBarHeightLessThanMaxBarHeight()277     float GetTitleBarHeightLessThanMaxBarHeight() const override
278     {
279         return 0.0;
280     }
281 
CanCoordScrollUp(float offset)282     bool CanCoordScrollUp(float offset) const override
283     {
284         return true;
285     }
286 
SetIsActive(bool isActive)287     void SetIsActive(bool isActive)
288     {
289         isActive_ = isActive;
290     }
291 
IsActive()292     bool IsActive() const
293     {
294         return isActive_;
295     }
296 
297 private:
298     struct HideBarOnSwipeContext {
299         CancelableCallback<void()> showBarTask;
300         bool isBarShowing = false;
301         bool isBarHiding = false;
302     };
GetSwipeContext(bool isTitle)303     HideBarOnSwipeContext& GetSwipeContext(bool isTitle)
304     {
305         return isTitle ? titleBarSwipeContext_ : toolBarSwipeContext_;
306     }
307     RefPtr<FrameNode> GetBarNode(const RefPtr<NavDestinationNodeBase>& nodeBase, bool isTitle);
308     bool EnableTitleBarSwipe(const RefPtr<NavDestinationNodeBase>& nodeBase);
309     bool EnableToolBarSwipe(const RefPtr<NavDestinationNodeBase>& nodeBase);
310     void UpdateBarHiddenOffset(const RefPtr<NavDestinationNodeBase>& nodeBase,
311         const RefPtr<FrameNode>& barNode, float offset, bool isTitle);
312     void StartHideOrShowBarInner(const RefPtr<NavDestinationNodeBase>& nodeBase,
313         float barHeight, float curTranslate, bool isTitle, bool isHide);
314     void StopHideBarIfNeeded(float curTranslate, bool isTitle);
315     void PostShowBarDelayedTask(bool isTitle);
316     void ResetBarState(const RefPtr<NavDestinationNodeBase>& nodeBase,
317         const RefPtr<FrameNode>& barNode, bool isTitle);
318 
319     void UpdateNameIfNeeded(RefPtr<NavDestinationGroupNode>& hostNode);
320     void UpdateBackgroundColorIfNeeded(RefPtr<NavDestinationGroupNode>& hostNode);
321     void MountTitleBar(
322         RefPtr<NavDestinationGroupNode>& hostNode, bool& needRunTitleBarAnimation);
323     void OnFontScaleConfigurationUpdate() override;
324     void OnAttachToFrameNode() override;
325     void OnDetachFromFrameNode(FrameNode* frameNode) override;
326     void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override;
327     void CloseLongPressDialog();
328 
329     RefPtr<ShallowBuilder> shallowBuilder_;
330     std::string name_;
331     std::string inspectorId_;
332     RefPtr<NavDestinationContext> navDestinationContext_;
333     RefPtr<UINode> customNode_;
334     WeakPtr<UINode> navigationNode_;
335     RefPtr<OverlayManager> overlayManager_;
336     bool isOnShow_ = false;
337     bool isActive_ = false;
338     bool isUserDefinedBgColor_ = false;
339     bool isRightToLeft_ = false;
340     uint64_t navDestinationId_ = 0;
341 
342     std::optional<RefPtr<SystemBarStyle>> backupStyle_;
343     std::optional<RefPtr<SystemBarStyle>> currStyle_;
344 
345     RefPtr<NavDestinationScrollableProcessor> scrollableProcessor_;
346     HideBarOnSwipeContext titleBarSwipeContext_;
347     HideBarOnSwipeContext toolBarSwipeContext_;
348 };
349 } // namespace OHOS::Ace::NG
350 
351 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVROUTER_NAVDESTINATION_PATTERN_H
352