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 return; 91 } 92 auto oldInfo = navDestinationContext_->GetNavPathInfo(); 93 if (oldInfo) { 94 oldInfo->UpdateNavPathInfo(pathInfo); 95 } else { 96 navDestinationContext_->SetNavPathInfo(pathInfo); 97 } 98 } 99 GetNavPathInfo()100 RefPtr<NavPathInfo> GetNavPathInfo() const 101 { 102 return navDestinationContext_ ? navDestinationContext_->GetNavPathInfo() : nullptr; 103 } 104 SetNavigationStack(const WeakPtr<NavigationStack> & stack)105 void SetNavigationStack(const WeakPtr<NavigationStack>& stack) 106 { 107 if (navDestinationContext_) { 108 navDestinationContext_->SetNavigationStack(stack); 109 } 110 } 111 GetNavigationStack()112 WeakPtr<NavigationStack> GetNavigationStack() const 113 { 114 return navDestinationContext_ ? navDestinationContext_->GetNavigationStack() : nullptr; 115 } 116 SetIndex(int32_t index)117 void SetIndex(int32_t index) 118 { 119 if (navDestinationContext_) { 120 navDestinationContext_->SetIndex(index); 121 } 122 } 123 SetNavDestinationContext(const RefPtr<NavDestinationContext> & context)124 void SetNavDestinationContext(const RefPtr<NavDestinationContext>& context) 125 { 126 navDestinationContext_ = context; 127 if (navDestinationContext_) { 128 navDestinationContext_->SetNavDestinationId(navDestinationId_); 129 } 130 } 131 GetNavDestinationContext()132 RefPtr<NavDestinationContext> GetNavDestinationContext() const 133 { 134 return navDestinationContext_; 135 } 136 SetCustomNode(const RefPtr<UINode> & customNode)137 void SetCustomNode(const RefPtr<UINode>& customNode) 138 { 139 customNode_ = customNode; 140 } 141 GetCustomNode()142 RefPtr<UINode> GetCustomNode() 143 { 144 return customNode_; 145 } 146 SetIsOnShow(bool isOnShow)147 void SetIsOnShow(bool isOnShow) 148 { 149 isOnShow_ = isOnShow; 150 } 151 GetIsOnShow()152 bool GetIsOnShow() 153 { 154 return isOnShow_; 155 } 156 157 bool GetBackButtonState(); 158 GetNavigationNode()159 RefPtr<UINode> GetNavigationNode() 160 { 161 return navigationNode_.Upgrade(); 162 } 163 GetNavDestinationState()164 NavDestinationState GetNavDestinationState() const 165 { 166 auto eventHub = GetEventHub<NavDestinationEventHub>(); 167 CHECK_NULL_RETURN(eventHub, NavDestinationState::NONE); 168 auto state = eventHub->GetState(); 169 return state; 170 } 171 172 void DumpInfo() override; 173 GetNavDestinationId()174 uint64_t GetNavDestinationId() const 175 { 176 return navDestinationId_; 177 } 178 SetNavigationNode(const RefPtr<UINode> & navigationNode)179 void SetNavigationNode(const RefPtr<UINode>& navigationNode) 180 { 181 navigationNode_ = AceType::WeakClaim(RawPtr(navigationNode)); 182 } 183 OnDetachFromMainTree()184 void OnDetachFromMainTree() override 185 { 186 backupStyle_.reset(); 187 currStyle_.reset(); 188 } 189 190 bool OverlayOnBackPressed(); 191 CreateOverlayManager(bool isShow)192 void CreateOverlayManager(bool isShow) 193 { 194 if (!overlayManager_ && isShow) { 195 overlayManager_ = MakeRefPtr<OverlayManager>(GetHost()); 196 } 197 } 198 GetOverlayManager()199 const RefPtr<OverlayManager>& GetOverlayManager() 200 { 201 return overlayManager_; 202 } 203 DeleteOverlayManager()204 void DeleteOverlayManager() 205 { 206 overlayManager_.Reset(); 207 } 208 209 int32_t GetTitlebarZIndex() const; 210 SetNavigationId(const std::string & id)211 void SetNavigationId(const std::string& id) 212 { 213 inspectorId_= id; 214 } 215 GetNavigationId()216 std::string GetNavigationId() const 217 { 218 return inspectorId_; 219 } 220 SetIsUserDefinedBgColor(bool isUserDefinedBgColor)221 void SetIsUserDefinedBgColor(bool isUserDefinedBgColor) 222 { 223 isUserDefinedBgColor_ = isUserDefinedBgColor; 224 } 225 IsUserDefinedBgColor()226 bool IsUserDefinedBgColor() const 227 { 228 return isUserDefinedBgColor_; 229 } 230 231 void OnLanguageConfigurationUpdate() override; 232 233 bool NeedIgnoreKeyboard(); 234 235 void SetSystemBarStyle(const RefPtr<SystemBarStyle>& style); GetBackupStyle()236 const std::optional<RefPtr<SystemBarStyle>>& GetBackupStyle() const 237 { 238 return backupStyle_; 239 } GetCurrentStyle()240 const std::optional<RefPtr<SystemBarStyle>>& GetCurrentStyle() const 241 { 242 return currStyle_; 243 } 244 245 void OnWindowHide() override; 246 GetScrollableProcessor()247 const RefPtr<NavDestinationScrollableProcessor>& GetScrollableProcessor() const 248 { 249 return scrollableProcessor_; 250 } SetScrollableProcessor(const RefPtr<NavDestinationScrollableProcessor> & processor)251 void SetScrollableProcessor(const RefPtr<NavDestinationScrollableProcessor>& processor) 252 { 253 scrollableProcessor_ = processor; 254 } 255 /** 256 * Respond to the scrolling events of the internal scrolling components of NavDestination, 257 * and hide part of the titleBar&toolBar( translate up or down in Y axis ), while changing 258 * the opacity of the titleBar&toolBar. 259 */ 260 void UpdateTitleAndToolBarHiddenOffset(float offset); 261 // show titleBar&toolBar immediately. 262 void ShowTitleAndToolBar(); 263 // cancel the delayed task if we have started, which will show titleBar&toolBar in the feature time. 264 void CancelShowTitleAndToolBarTask(); 265 // Restore the titleBar&toolBar to its original position (hide or show state). 266 void ResetTitleAndToolBarState(); 267 268 private: 269 struct HideBarOnSwipeContext { 270 CancelableCallback<void()> showBarTask; 271 bool isBarShowing = false; 272 bool isBarHiding = false; 273 }; GetSwipeContext(bool isTitle)274 HideBarOnSwipeContext& GetSwipeContext(bool isTitle) 275 { 276 return isTitle ? titleBarSwipeContext_ : toolBarSwipeContext_; 277 } 278 RefPtr<FrameNode> GetBarNode(const RefPtr<NavDestinationNodeBase>& nodeBase, bool isTitle); 279 bool EnableTitleBarSwipe(const RefPtr<NavDestinationNodeBase>& nodeBase); 280 bool EnableToolBarSwipe(const RefPtr<NavDestinationNodeBase>& nodeBase); 281 void UpdateBarHiddenOffset(const RefPtr<NavDestinationNodeBase>& nodeBase, 282 const RefPtr<FrameNode>& barNode, float offset, bool isTitle); 283 void StartHideOrShowBarInner(const RefPtr<NavDestinationNodeBase>& nodeBase, 284 float barHeight, float curTranslate, bool isTitle, bool isHide); 285 void StopHideBarIfNeeded(float curTranslate, bool isTitle); 286 void PostShowBarDelayedTask(bool isTitle); 287 void ResetBarState(const RefPtr<NavDestinationNodeBase>& nodeBase, 288 const RefPtr<FrameNode>& barNode, bool isTitle); 289 290 void UpdateNameIfNeeded(RefPtr<NavDestinationGroupNode>& hostNode); 291 void UpdateBackgroundColorIfNeeded(RefPtr<NavDestinationGroupNode>& hostNode); 292 void MountTitleBar( 293 RefPtr<NavDestinationGroupNode>& hostNode, bool& needRunTitleBarAnimation); 294 void OnFontScaleConfigurationUpdate() override; 295 void OnAttachToFrameNode() override; 296 void OnDetachFromFrameNode(FrameNode* frameNode) override; 297 void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override; 298 299 RefPtr<ShallowBuilder> shallowBuilder_; 300 std::string name_; 301 std::string inspectorId_; 302 RefPtr<NavDestinationContext> navDestinationContext_; 303 RefPtr<UINode> customNode_; 304 WeakPtr<UINode> navigationNode_; 305 RefPtr<OverlayManager> overlayManager_; 306 bool isOnShow_ = false; 307 bool isUserDefinedBgColor_ = false; 308 bool isRightToLeft_ = false; 309 uint64_t navDestinationId_ = 0; 310 311 std::optional<RefPtr<SystemBarStyle>> backupStyle_; 312 std::optional<RefPtr<SystemBarStyle>> currStyle_; 313 314 RefPtr<NavDestinationScrollableProcessor> scrollableProcessor_; 315 HideBarOnSwipeContext titleBarSwipeContext_; 316 HideBarOnSwipeContext toolBarSwipeContext_; 317 }; 318 } // namespace OHOS::Ace::NG 319 320 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVROUTER_NAVDESTINATION_PATTERN_H 321