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 = GetOrCreateEventHub<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_->SetNavDestinationPattern(WeakClaim(this)); 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 GetNavDestinationState()159 NavDestinationState GetNavDestinationState() const 160 { 161 auto eventHub = GetOrCreateEventHub<NavDestinationEventHub>(); 162 CHECK_NULL_RETURN(eventHub, NavDestinationState::NONE); 163 auto state = eventHub->GetState(); 164 return state; 165 } 166 167 void DumpInfo() override; 168 void DumpInfo(std::unique_ptr<JsonValue>& json) override; DumpSimplifyInfo(std::shared_ptr<JsonValue> & json)169 void DumpSimplifyInfo(std::shared_ptr<JsonValue>& json) override {} 170 GetNavDestinationId()171 uint64_t GetNavDestinationId() const 172 { 173 return navDestinationId_; 174 } 175 176 void OnDetachFromMainTree() override; 177 178 bool OverlayOnBackPressed(); 179 CreateOverlayManager(bool isShow)180 void CreateOverlayManager(bool isShow) 181 { 182 if (!overlayManager_ && isShow) { 183 overlayManager_ = MakeRefPtr<OverlayManager>(GetHost()); 184 } 185 } 186 GetOverlayManager()187 const RefPtr<OverlayManager>& GetOverlayManager() 188 { 189 return overlayManager_; 190 } 191 DeleteOverlayManager()192 void DeleteOverlayManager() 193 { 194 overlayManager_.Reset(); 195 } 196 197 int32_t GetTitlebarZIndex() const; 198 SetNavigationId(const std::string & id)199 void SetNavigationId(const std::string& id) 200 { 201 inspectorId_ = id; 202 } 203 GetNavigationId()204 std::string GetNavigationId() const 205 { 206 return inspectorId_; 207 } 208 SetIsUserDefinedBgColor(bool isUserDefinedBgColor)209 void SetIsUserDefinedBgColor(bool isUserDefinedBgColor) 210 { 211 isUserDefinedBgColor_ = isUserDefinedBgColor; 212 } 213 IsUserDefinedBgColor()214 bool IsUserDefinedBgColor() const 215 { 216 return isUserDefinedBgColor_; 217 } 218 219 void OnLanguageConfigurationUpdate() override; 220 221 bool NeedIgnoreKeyboard(); 222 223 void SetSystemBarStyle(const RefPtr<SystemBarStyle>& style); GetBackupStyle()224 const std::optional<RefPtr<SystemBarStyle>>& GetBackupStyle() const 225 { 226 return backupStyle_; 227 } GetCurrentStyle()228 const std::optional<RefPtr<SystemBarStyle>>& GetCurrentStyle() const 229 { 230 return currStyle_; 231 } 232 233 void OnWindowHide() override; 234 GetScrollableProcessor()235 const RefPtr<NavDestinationScrollableProcessor>& GetScrollableProcessor() const 236 { 237 return scrollableProcessor_; 238 } SetScrollableProcessor(const RefPtr<NavDestinationScrollableProcessor> & processor)239 void SetScrollableProcessor(const RefPtr<NavDestinationScrollableProcessor>& processor) 240 { 241 scrollableProcessor_ = processor; 242 } 243 /** 244 * Respond to the scrolling events of the internal scrolling components of NavDestination, 245 * and hide part of the titleBar&toolBar( translate up or down in Y axis ), while changing 246 * the opacity of the titleBar&toolBar. 247 */ 248 void UpdateTitleAndToolBarHiddenOffset(float offset); 249 // show titleBar&toolBar immediately. 250 void ShowTitleAndToolBar(); 251 // cancel the delayed task if we have started, which will show titleBar&toolBar in the feature time. 252 void CancelShowTitleAndToolBarTask(); 253 // Restore the titleBar&toolBar to its original position (hide or show state). 254 void ResetTitleAndToolBarState(); 255 256 void OnCoordScrollStart() override; 257 float OnCoordScrollUpdate(float offset, float currentOffset) override; 258 void OnCoordScrollEnd() override; 259 void UpdateBackgroundColor(); NeedCoordWithScroll()260 bool NeedCoordWithScroll() override 261 { 262 return IsNeedHandleScroll(); 263 } 264 IsNeedHandleScroll()265 bool IsNeedHandleScroll() const override 266 { 267 auto eventHub = GetOrCreateEventHub<NavDestinationEventHub>(); 268 if (eventHub && eventHub->HasOnCoordScrollStartAction()) { 269 return true; 270 } 271 return false; 272 } 273 GetTitleBarHeightLessThanMaxBarHeight()274 float GetTitleBarHeightLessThanMaxBarHeight() const override 275 { 276 return 0.0; 277 } 278 CanCoordScrollUp(float offset)279 bool CanCoordScrollUp(float offset) const override 280 { 281 return IsNeedHandleScroll(); 282 } 283 SetIsActive(bool isActive)284 void SetIsActive(bool isActive) 285 { 286 isActive_ = isActive; 287 } 288 IsActive()289 bool IsActive() const 290 { 291 return isActive_; 292 } 293 GetSerializedParam()294 std::string GetSerializedParam() const 295 { 296 return serializedParam_; 297 } 298 UpdateSerializedParam(const std::string & param)299 void UpdateSerializedParam(const std::string& param) 300 { 301 serializedParam_ = param; 302 } 303 304 void BeforeCreateLayoutWrapper() override; 305 306 private: 307 struct HideBarOnSwipeContext { 308 CancelableCallback<void()> showBarTask; 309 bool isBarShowing = false; 310 bool isBarHiding = false; 311 }; GetSwipeContext(bool isTitle)312 HideBarOnSwipeContext& GetSwipeContext(bool isTitle) 313 { 314 return isTitle ? titleBarSwipeContext_ : toolBarSwipeContext_; 315 } 316 RefPtr<FrameNode> GetBarNode(const RefPtr<NavDestinationNodeBase>& nodeBase, bool isTitle); 317 bool EnableTitleBarSwipe(const RefPtr<NavDestinationNodeBase>& nodeBase); 318 bool EnableToolBarSwipe(const RefPtr<NavDestinationNodeBase>& nodeBase); 319 void UpdateBarHiddenOffset(const RefPtr<NavDestinationNodeBase>& nodeBase, 320 const RefPtr<FrameNode>& barNode, float offset, bool isTitle); 321 void StartHideOrShowBarInner(const RefPtr<NavDestinationNodeBase>& nodeBase, 322 float barHeight, float curTranslate, bool isTitle, bool isHide); 323 void StopHideBarIfNeeded(float curTranslate, bool isTitle); 324 void PostShowBarDelayedTask(bool isTitle); 325 void ResetBarState(const RefPtr<NavDestinationNodeBase>& nodeBase, 326 const RefPtr<FrameNode>& barNode, bool isTitle); 327 328 void UpdateNameIfNeeded(RefPtr<NavDestinationGroupNode>& hostNode); 329 void UpdateBackgroundColorIfNeeded(RefPtr<NavDestinationGroupNode>& hostNode); 330 void MountTitleBar( 331 RefPtr<NavDestinationGroupNode>& hostNode, bool& needRunTitleBarAnimation); 332 void OnFontScaleConfigurationUpdate() override; 333 void OnAttachToFrameNode() override; 334 void OnDetachFromFrameNode(FrameNode* frameNode) override; 335 void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override; 336 void CloseLongPressDialog(); 337 void CheckIfOrientationChanged(); 338 void StopAnimation(); 339 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 340 void CheckIfStatusBarConfigChanged(); 341 void CheckIfNavigationIndicatorConfigChagned(); 342 343 RefPtr<ShallowBuilder> shallowBuilder_; 344 std::string name_; 345 std::string inspectorId_; 346 RefPtr<NavDestinationContext> navDestinationContext_; 347 RefPtr<UINode> customNode_; 348 RefPtr<OverlayManager> overlayManager_; 349 bool isOnShow_ = false; 350 bool isActive_ = false; 351 bool isUserDefinedBgColor_ = false; 352 bool isRightToLeft_ = false; 353 uint64_t navDestinationId_ = 0; 354 355 std::optional<RefPtr<SystemBarStyle>> backupStyle_; 356 std::optional<RefPtr<SystemBarStyle>> currStyle_; 357 358 RefPtr<NavDestinationScrollableProcessor> scrollableProcessor_; 359 HideBarOnSwipeContext titleBarSwipeContext_; 360 HideBarOnSwipeContext toolBarSwipeContext_; 361 bool isFirstTimeCheckOrientation_ = true; 362 bool isFirstTimeCheckStatusBarConfig_ = true; 363 bool isFirstTimeCheckNavigationIndicatorConfig_ = true; 364 RefPtr<TouchEventImpl> touchListener_ = nullptr; 365 std::string serializedParam_ = ""; 366 }; 367 } // namespace OHOS::Ace::NG 368 369 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVROUTER_NAVDESTINATION_PATTERN_H 370