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