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_NAVIGATION_GROUP_NODE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_GROUP_NODE_H 18 19 #include <atomic> 20 #include <cstdint> 21 #include <list> 22 23 #include "base/memory/referenced.h" 24 #include "core/animation/page_transition_common.h" 25 #include "core/components_ng/base/frame_node.h" 26 #include "core/components_ng/base/group_node.h" 27 #include "core/components_ng/pattern/navigation/bar_item_node.h" 28 #include "core/components_ng/pattern/navigation/navigation_declaration.h" 29 #include "core/components_ng/pattern/navigation/navigation_stack.h" 30 #include "core/components_ng/pattern/navigation/title_bar_node.h" 31 #include "core/components_ng/pattern/navrouter/navdestination_group_node.h" 32 #include "core/components_ng/pattern/navrouter/navrouter_pattern.h" 33 #include "core/components_ng/property/property.h" 34 35 namespace OHOS::Ace::NG { 36 class InspectorFilter; 37 38 struct TransitionUnitInfo { 39 const RefPtr<FrameNode>& transitionNode; 40 bool isUseCustomTransition = false; 41 int32_t animationId = -1; 42 TransitionUnitInfoTransitionUnitInfo43 TransitionUnitInfo(const RefPtr<FrameNode>& node, bool isUseCustomTransition, int32_t animationId) 44 : transitionNode(node), isUseCustomTransition(isUseCustomTransition), animationId(animationId) 45 {} 46 }; 47 48 class ACE_EXPORT NavigationGroupNode : public GroupNode { DECLARE_ACE_TYPE(NavigationGroupNode,GroupNode)49 DECLARE_ACE_TYPE(NavigationGroupNode, GroupNode) 50 public: 51 NavigationGroupNode(const std::string& tag, int32_t nodeId, const RefPtr<Pattern>& pattern) 52 : GroupNode(tag, nodeId, pattern) 53 {} 54 55 ~NavigationGroupNode() override; 56 57 using AnimationFinishCallback = std::function<void()>; 58 59 void AddChildToGroup(const RefPtr<UINode>& child, int32_t slot = DEFAULT_NODE_SLOT) override; 60 61 // remain child needs to keep to use pop animation 62 void UpdateNavDestinationNodeWithoutMarkDirty(const RefPtr<UINode>& remainChild, bool modeChange = false); 63 static RefPtr<NavigationGroupNode> GetOrCreateGroupNode( 64 const std::string& tag, int32_t nodeId, const std::function<RefPtr<Pattern>(void)>& patternCreator); 65 IsAtomicNode()66 bool IsAtomicNode() const override 67 { 68 return false; 69 } 70 SetNavBarNode(const RefPtr<UINode> & navBarNode)71 void SetNavBarNode(const RefPtr<UINode>& navBarNode) 72 { 73 navBarNode_ = navBarNode; 74 } 75 GetNavBarNode()76 const RefPtr<UINode>& GetNavBarNode() const 77 { 78 return navBarNode_; 79 } 80 SetContentNode(const RefPtr<UINode> & contentNode)81 void SetContentNode(const RefPtr<UINode>& contentNode) 82 { 83 contentNode_ = contentNode; 84 } 85 GetContentNode()86 const RefPtr<UINode>& GetContentNode() const 87 { 88 return contentNode_; 89 } 90 SetDividerNode(const RefPtr<UINode> & dividerNode)91 void SetDividerNode(const RefPtr<UINode>& dividerNode) 92 { 93 dividerNode_ = dividerNode; 94 } 95 GetDividerNode()96 const RefPtr<UINode>& GetDividerNode() const 97 { 98 return dividerNode_; 99 } 100 GetCurId()101 const std::string& GetCurId() const 102 { 103 return curId_; 104 } 105 GetIsModeChange()106 bool GetIsModeChange() const 107 { 108 return isModeChange_; 109 } 110 SetIsModeChange(bool isModeChange)111 void SetIsModeChange(bool isModeChange) 112 { 113 isModeChange_ = isModeChange; 114 } 115 GetNeedSetInvisible()116 bool GetNeedSetInvisible() const 117 { 118 return needSetInvisible_; 119 } 120 SetNeedSetInvisible(bool needSetInvisible)121 void SetNeedSetInvisible(bool needSetInvisible) 122 { 123 needSetInvisible_ = needSetInvisible; 124 } 125 IsOnModeSwitchAnimation()126 bool IsOnModeSwitchAnimation() 127 { 128 return isOnModeSwitchAnimation_; 129 } 130 SetDoingModeSwitchAnimationFlag(bool isOnAnimation)131 void SetDoingModeSwitchAnimationFlag(bool isOnAnimation) 132 { 133 isOnModeSwitchAnimation_ = isOnAnimation; 134 } 135 GetPushAnimations()136 std::list<std::shared_ptr<AnimationUtils::Animation>>& GetPushAnimations() 137 { 138 return pushAnimations_; 139 } 140 GetPopAnimations()141 std::list<std::shared_ptr<AnimationUtils::Animation>>& GetPopAnimations() 142 { 143 return popAnimations_; 144 } 145 CleanPushAnimations()146 void CleanPushAnimations() 147 { 148 pushAnimations_.clear(); 149 } 150 CleanPopAnimations()151 void CleanPopAnimations() 152 { 153 popAnimations_.clear(); 154 } 155 156 bool CheckCanHandleBack(bool& isEntry); 157 158 void OnInspectorIdUpdate(const std::string& id) override; 159 160 bool HandleBack(const RefPtr<FrameNode>& node, bool isLastChild, bool isOverride); 161 162 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override; 163 static RefPtr<UINode> GetNavDestinationNode(RefPtr<UINode> uiNode); 164 void SetBackButtonEvent(const RefPtr<NavDestinationGroupNode>& navDestination); 165 166 void TransitionWithPop(const RefPtr<FrameNode>& preNode, const RefPtr<FrameNode>& curNode, bool isNavBar = false); 167 void TransitionWithPush(const RefPtr<FrameNode>& preNode, const RefPtr<FrameNode>& curNode, bool isNavBar = false); 168 virtual void CreateAnimationWithPop(const TransitionUnitInfo& preInfo, const TransitionUnitInfo& curInfo, 169 const AnimationFinishCallback finishCallback, bool isNavBar = false); 170 virtual void CreateAnimationWithPush(const TransitionUnitInfo& preInfo, const TransitionUnitInfo& curInfo, 171 const AnimationFinishCallback finishCallback, bool isNavBar = false); 172 virtual void ResetSystemAnimationProperties(const RefPtr<FrameNode>& navDestinationNode); 173 174 std::shared_ptr<AnimationUtils::Animation> BackButtonAnimation( 175 const RefPtr<FrameNode>& backButtonNode, bool isTransitionIn); 176 std::shared_ptr<AnimationUtils::Animation> MaskAnimation(const RefPtr<FrameNode>& curNode, bool isTransitionIn); 177 std::shared_ptr<AnimationUtils::Animation> TitleOpacityAnimation( 178 const RefPtr<FrameNode>& node, bool isTransitionOut); 179 void TransitionWithReplace(const RefPtr<FrameNode>& preNode, const RefPtr<FrameNode>& curNode, bool isNavBar); 180 void DealNavigationExit(const RefPtr<FrameNode>& preNode, bool isNavBar, bool isAnimated = true); 181 void NotifyPageHide(); 182 void UpdateLastStandardIndex(); 183 GetPreLastStandardIndex()184 int32_t GetPreLastStandardIndex() const 185 { 186 return preLastStandardIndex_; 187 } 188 189 void PreNodeFinishCallback(const RefPtr<FrameNode>& preNode); 190 void CreateAnimationWithDialogPop(const AnimationFinishCallback callback, 191 const std::vector<WeakPtr<FrameNode>> prevNavList, const std::vector<WeakPtr<FrameNode>> curNavList); 192 void CreateAnimationWithDialogPush(const AnimationFinishCallback callback, 193 const std::vector<WeakPtr<FrameNode>> prevNavList, const std::vector<WeakPtr<FrameNode>> curNavList); 194 void TransitionWithDialogPush(const RefPtr<FrameNode>& preNode, const RefPtr<FrameNode>& curNode, 195 bool isNavBar = false); 196 void TransitionWithDialogPop(const RefPtr<FrameNode>& preNode, const RefPtr<FrameNode>& curNode, 197 bool isNavBar = false); 198 void StartDialogtransition(const RefPtr<FrameNode>& preNode, const RefPtr<FrameNode>& curNode, 199 bool isTransitionIn); 200 201 void InitPopPreList(const RefPtr<FrameNode>& preNode, std::vector<WeakPtr<FrameNode>>& preNavList); 202 void InitPopCurList(const RefPtr<FrameNode>& curNode, std::vector<WeakPtr<FrameNode>>& curNavList, 203 bool isNavbarNeedAnimation); 204 void InitPushPreList(const RefPtr<FrameNode>& preNode, std::vector<WeakPtr<FrameNode>>& prevNavList, 205 bool isNavbarNeedAnimation); 206 void InitPushCurList(const RefPtr<FrameNode>& curNode, std::vector<WeakPtr<FrameNode>>& curNavList); 207 208 std::vector<WeakPtr<NavDestinationGroupNode>> FindNodesPoped(const RefPtr<FrameNode>& preNode, 209 const RefPtr<FrameNode>& curNode); 210 void DialogTransitionPopAnimation(const RefPtr<FrameNode>& preNode, const RefPtr<FrameNode>& curNode, 211 AnimationOption option); 212 void DialogTransitionPushAnimation(const RefPtr<FrameNode>& preNode, const RefPtr<FrameNode>& curNode, 213 AnimationOption option); 214 GetLastStandardIndex()215 int32_t GetLastStandardIndex() const 216 { 217 return lastStandardIndex_; 218 } 219 220 AnimationOption CreateAnimationOption(const RefPtr<Curve>& curve, FillMode mode, 221 int32_t duration, const NavigationGroupNode::AnimationFinishCallback& callback); 222 223 NavigationMode GetNavigationMode(); 224 SetIsOnAnimation(bool isOnAnimation)225 void SetIsOnAnimation(bool isOnAnimation) 226 { 227 isOnAnimation_ = isOnAnimation; 228 } 229 RefPtr<FrameNode> GetTopDestination(); 230 void OnDetachFromMainTree(bool recursive, PipelineContext* context = nullptr) override; 231 void OnAttachToMainTree(bool recursive) override; 232 233 void FireHideNodeChange(NavDestinationLifecycle lifecycle); 234 ReduceModeSwitchAnimationCnt()235 void ReduceModeSwitchAnimationCnt() 236 { 237 --modeSwitchAnimationCnt_; 238 } 239 IncreaseModeSwitchAnimationCnt()240 void IncreaseModeSwitchAnimationCnt() 241 { 242 ++modeSwitchAnimationCnt_; 243 } 244 GetModeSwitchAnimationCnt()245 int32_t GetModeSwitchAnimationCnt() 246 { 247 return modeSwitchAnimationCnt_; 248 } 249 250 float CheckLanguageDirection(); 251 252 void RemoveDialogDestination(bool isReplace = false); 253 void AddDestinationNode(const RefPtr<UINode>& parent); GetParentDestinationNode()254 WeakPtr<NavDestinationGroupNode> GetParentDestinationNode() const 255 { 256 return parentDestinationNode_; 257 } SetNavigationPathInfo(const std::string & moduleName,const std::string & pagePath)258 void SetNavigationPathInfo(const std::string& moduleName, const std::string& pagePath) 259 { 260 navigationPathInfo_ = pagePath; 261 navigationModuleName_ = moduleName; 262 } 263 GetNavigationPathInfo()264 const std::string& GetNavigationPathInfo() const 265 { 266 return navigationPathInfo_; 267 } 268 SetDragBarNode(const RefPtr<UINode> & dragNode)269 void SetDragBarNode(const RefPtr<UINode>& dragNode) 270 { 271 dragBarNode_ = dragNode; 272 } 273 GetDragBarNode()274 const RefPtr<UINode>& GetDragBarNode() const 275 { 276 return dragBarNode_; 277 } 278 CleanHideNodes()279 void CleanHideNodes() 280 { 281 hideNodes_.clear(); 282 } 283 GetHideNodes()284 std::vector<std::pair<RefPtr<NavDestinationGroupNode>, bool>> GetHideNodes() const 285 { 286 return hideNodes_; 287 } 288 SetRecoverable(bool recoverable)289 void SetRecoverable(bool recoverable) 290 { 291 recoverable_ = recoverable; 292 } 293 CanRecovery()294 bool CanRecovery() const 295 { 296 return recoverable_ && !curId_.empty(); 297 } 298 MakeUniqueAnimationId()299 int32_t MakeUniqueAnimationId() 300 { 301 return ++animationId_; 302 } 303 GetAnimationId()304 int32_t GetAnimationId() const 305 { 306 return animationId_; 307 } 308 309 bool CheckAnimationIdValid(const RefPtr<FrameNode>& curNode, const int32_t animationId); 310 311 protected: 312 std::list<std::shared_ptr<AnimationUtils::Animation>> pushAnimations_; 313 std::list<std::shared_ptr<AnimationUtils::Animation>> popAnimations_; 314 private: 315 bool UpdateNavDestinationVisibility(const RefPtr<NavDestinationGroupNode>& navDestination, 316 const RefPtr<UINode>& remainChild, int32_t index, size_t destinationSize, 317 const RefPtr<UINode>& preLastStandardNode); 318 bool ReorderNavDestination( 319 const std::vector<std::pair<std::string, RefPtr<UINode>>>& navDestinationNodes, 320 RefPtr<FrameNode>& navigationContentNode, int32_t& slot, bool& hasChanged); 321 void RemoveRedundantNavDestination(RefPtr<FrameNode>& navigationContentNode, 322 const RefPtr<UINode>& remainChild, int32_t slot, bool& hasChanged, 323 const RefPtr<NavDestinationGroupNode>& preLastStandardNode); 324 void ReorderAnimatingDestination(RefPtr<FrameNode>& navigationContentNode, RefPtr<UINode>& maxAnimatingDestination, 325 RefPtr<UINode>& remainDestination, RefPtr<UINode>& curTopDestination); 326 bool FindNavigationParent(const std::string& parentName); 327 void DealRemoveDestination(const RefPtr<NavDestinationGroupNode>& destination); 328 RefPtr<FrameNode> TransitionAnimationIsValid( 329 const RefPtr<FrameNode>& node, bool isNavBar, bool isUseNavDestCustomTransition); 330 331 RefPtr<UINode> navBarNode_; 332 RefPtr<UINode> contentNode_; 333 RefPtr<UINode> dividerNode_; 334 RefPtr<UINode> dragBarNode_; 335 WeakPtr<NavDestinationGroupNode> parentDestinationNode_; 336 // dialog hideNodes, if is true, nodes need remove 337 std::vector<std::pair<RefPtr<NavDestinationGroupNode>, bool>> hideNodes_; 338 std::vector<RefPtr<NavDestinationGroupNode>> showNodes_; 339 int32_t lastStandardIndex_ = -1; 340 std::atomic_int32_t animationId_ = 0; 341 std::atomic_int32_t modeSwitchAnimationCnt_ = 0; 342 bool isOnAnimation_ { false }; 343 bool isModeChange_ { false }; 344 bool needSetInvisible_ { false }; 345 bool isOnModeSwitchAnimation_ { false }; 346 bool recoverable_ { false }; 347 std::string curId_; 348 std::string navigationPathInfo_; 349 std::string navigationModuleName_; 350 int32_t preLastStandardIndex_ = -1; 351 }; 352 } // namespace OHOS::Ace::NG 353 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_GROUP_NODE_H 354