1 2 /* 3 * Copyright (c) 2023 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_STACK_H 18 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_STACK_H 19 20 #include <functional> 21 #include <optional> 22 23 #include "base/memory/referenced.h" 24 #include "core/components_ng/base/ui_node.h" 25 #include "core/components_ng/pattern/navigation/navigation_declaration.h" 26 #include "core/components_ng/pattern/navrouter/navdestination_context.h" 27 28 namespace OHOS::Ace::NG { 29 using NavPathList = std::vector<std::pair<std::string, RefPtr<UINode>>>; 30 class NavDestinationContext; 31 class RouteInfo : public virtual AceType { 32 DECLARE_ACE_TYPE(NG::RouteInfo, AceType) 33 public: 34 RouteInfo() = default; 35 virtual ~RouteInfo() = default; 36 GetName()37 virtual std::string GetName() 38 { 39 return ""; 40 } 41 }; 42 43 class ACE_FORCE_EXPORT NavigationStack : public virtual AceType { 44 DECLARE_ACE_TYPE(NG::NavigationStack, AceType) 45 public: 46 NavigationStack() = default; 47 ~NavigationStack() override = default; 48 UpdateStackInfo(const RefPtr<NavigationStack> & newStack)49 virtual void UpdateStackInfo(const RefPtr<NavigationStack>& newStack) {} 50 GetAllNavDestinationNodes()51 NavPathList& GetAllNavDestinationNodes() 52 { 53 return navPathList_; 54 } 55 SetOnStateChangedCallback(std::function<void ()> callback)56 virtual void SetOnStateChangedCallback(std::function<void()> callback) {} 57 SavePreNavList()58 void SavePreNavList() 59 { 60 // same navdestination nodes before poped 61 isPreForceSetList_ = isCurForceSetList_; 62 navPathListBeforePoped_.clear(); 63 for (auto iter: preNavPathList_) { 64 navPathListBeforePoped_.emplace_back(std::make_pair(iter.first, WeakPtr<UINode>(iter.second))); 65 } 66 } 67 SetNavPathList(const NavPathList & navPathList)68 void SetNavPathList(const NavPathList& navPathList) 69 { 70 preNavPathList_ = navPathList; 71 //copy nav path 72 navPathList_ = navPathList; 73 } 74 SetIsCurForceSetList(bool isForce)75 void SetIsCurForceSetList(bool isForce) 76 { 77 isCurForceSetList_ = isForce; 78 } 79 GetIsPreForceSetList()80 bool GetIsPreForceSetList() const 81 { 82 return isPreForceSetList_; 83 } 84 GetAllNavDestinationNodesPrev()85 std::vector<std::pair<std::string, WeakPtr<UINode>>>& GetAllNavDestinationNodesPrev() 86 { 87 return navPathListBeforePoped_; 88 } 89 Empty()90 bool Empty() const 91 { 92 return navPathList_.empty(); 93 } 94 Size()95 int32_t Size() const 96 { 97 return static_cast<int32_t>(navPathList_.size()); 98 } 99 PreSize()100 int32_t PreSize() const 101 { 102 return static_cast<int32_t>(preNavPathList_.size()); 103 } 104 GetTopNavPath()105 std::optional<std::pair<std::string, RefPtr<UINode>>> GetTopNavPath() const 106 { 107 if (navPathList_.empty()) { 108 return std::nullopt; 109 } 110 return navPathList_.back(); 111 } 112 GetPreTopNavPath()113 std::optional<std::pair<std::string, RefPtr<UINode>>> GetPreTopNavPath() const 114 { 115 if (preNavPathList_.empty()) { 116 return std::nullopt; 117 } 118 return preNavPathList_.back(); 119 } 120 RemoveStack()121 void RemoveStack() 122 { 123 navPathList_.clear(); 124 } 125 126 NavPathList GetAllCacheNodes(); 127 void AddCacheNode(const std::string& name, const RefPtr<UINode>& uiNode); 128 RefPtr<UINode> GetFromCacheNode(NavPathList& cacheNodes, const std::string& name); 129 RefPtr<UINode> GetFromCacheNode(const std::string& name); 130 std::optional<std::pair<std::string, RefPtr<UINode>>> GetFromCacheNode(int32_t handle); 131 void RemoveCacheNode( 132 NavPathList& cacheNodes, const std::string& name, const RefPtr<UINode>& navDestinationNode); 133 void RemoveCacheNode(int32_t handle); 134 void ReOrderCache(const std::string& name, const RefPtr<UINode>& navDestinationNode); 135 136 void Remove(); 137 void Remove(const std::string& name); 138 void Remove(const std::string& name, const RefPtr<UINode>& navDestinationNode); 139 int32_t RemoveInNavPathList(const std::string& name, const RefPtr<UINode>& navDestinationNode); 140 int32_t RemoveInPreNavPathList(const std::string& name, const RefPtr<UINode>& navDestinationNode); 141 void RemoveAll(); 142 void Add(const std::string& name, const RefPtr<UINode>& navDestinationNode, 143 const RefPtr<RouteInfo>& routeInfo = nullptr); 144 void Add(const std::string& name, const RefPtr<UINode>& navDestinationNode, NavRouteMode mode, 145 const RefPtr<RouteInfo>& routeInfo = nullptr); 146 RefPtr<UINode> Get(); 147 bool Get(const std::string& name, RefPtr<UINode>& navDestinationNode, int32_t& index); 148 bool GetFromPreBackup(const std::string& name, RefPtr<UINode>& navDestinationNode, int32_t& index); 149 std::string GetNavDesNameByIndex(int32_t index); 150 RefPtr<UINode> Get(int32_t index); 151 RefPtr<UINode> GetPre(const std::string& name, const RefPtr<UINode>& navDestinationNode); 152 virtual bool IsEmpty(); 153 virtual std::vector<std::string> GetAllPathName(); 154 virtual std::vector<int32_t> GetAllPathIndex(); InitNavPathIndex(const std::vector<std::string> & pathNames)155 virtual void InitNavPathIndex(const std::vector<std::string>& pathNames) {} SetDestinationIdToJsStack(int32_t index,const std::string & navDestinationId)156 virtual void SetDestinationIdToJsStack(int32_t index, const std::string& navDestinationId) {} 157 virtual void Pop(); 158 virtual void Push(const std::string& name, const RefPtr<RouteInfo>& routeInfo = nullptr); 159 virtual void Push(const std::string& name, int32_t index); 160 virtual void RemoveName(const std::string& name); 161 virtual void RemoveIndex(int32_t index); 162 virtual void Clear(); 163 virtual void UpdateReplaceValue(int32_t replaceValue) const; 164 virtual int32_t GetReplaceValue() const; 165 virtual bool CreateNodeByIndex(int32_t index, const WeakPtr<UINode>& customNode, RefPtr<UINode>& node); 166 virtual RefPtr<UINode> CreateNodeByRouteInfo(const RefPtr<RouteInfo>& routeInfo, const WeakPtr<UINode>& node); GetDisableAnimation()167 virtual bool GetDisableAnimation() const 168 { 169 return false; 170 } GetAnimatedValue()171 virtual bool GetAnimatedValue() const 172 { 173 return animated_; 174 } UpdateAnimatedValue(bool animated)175 virtual void UpdateAnimatedValue(bool animated) 176 { 177 animated_ = animated; 178 } 179 int32_t FindIndex(const std::string& name, const RefPtr<UINode>& navDestinationNode, bool isNavPathList); GetRouteParam()180 virtual std::string GetRouteParam() const 181 { 182 return ""; 183 } 184 FireNavigationInterception(bool isBefore,const RefPtr<NG::NavDestinationContext> & from,const RefPtr<NG::NavDestinationContext> & to,NavigationOperation operation,bool isAnimated)185 virtual void FireNavigationInterception(bool isBefore, const RefPtr<NG::NavDestinationContext>& from, 186 const RefPtr<NG::NavDestinationContext>& to, NavigationOperation operation, bool isAnimated) {} 187 FireNavigationModeChange(NavigationMode mode)188 virtual void FireNavigationModeChange(NavigationMode mode) {} 189 OnAttachToParent(RefPtr<NavigationStack> parent)190 virtual void OnAttachToParent(RefPtr<NavigationStack> parent) {} OnDetachFromParent()191 virtual void OnDetachFromParent() {} 192 193 virtual std::vector<std::string> DumpStackInfo() const; 194 GetJsIndexFromNativeIndex(int32_t index)195 virtual int32_t GetJsIndexFromNativeIndex(int32_t index) { return -1; } MoveIndexToTop(int32_t index)196 virtual void MoveIndexToTop(int32_t index) {} 197 GetStringifyParamByIndex(int32_t index)198 virtual std::string GetStringifyParamByIndex(int32_t index) const { return ""; } SetPathArray(const std::vector<NavdestinationRecoveryInfo> & navdestinationsInfo)199 virtual void SetPathArray(const std::vector<NavdestinationRecoveryInfo>& navdestinationsInfo) {} SetFromRecovery(int32_t index,bool fromRecovery)200 virtual void SetFromRecovery(int32_t index, bool fromRecovery) {} IsFromRecovery(int32_t index)201 virtual bool IsFromRecovery(int32_t index) { return false; } GetRecoveredDestinationMode(int32_t index)202 virtual int32_t GetRecoveredDestinationMode(int32_t index) { return false; } GetSize()203 virtual int32_t GetSize() const { return -1; } 204 GetNavDestinationIdInt(int32_t index)205 virtual uint64_t GetNavDestinationIdInt(int32_t index) { return -1; } GetIsForceSet(int32_t index)206 virtual bool GetIsForceSet(int32_t index) { return false; } ResetIsForceSetFlag(int32_t index)207 virtual void ResetIsForceSetFlag(int32_t index) {} 208 209 // could be optimized... HasSingletonMoved()210 virtual bool HasSingletonMoved() 211 { 212 return false; 213 } 214 IsTopFromSingletonMoved()215 virtual bool IsTopFromSingletonMoved() 216 { 217 return false; 218 } 219 ResetSingletonMoved()220 virtual void ResetSingletonMoved() {} 221 GetNavigationNode()222 const WeakPtr<UINode>& GetNavigationNode() 223 { 224 return navigationNode_; 225 } 226 SetNavigationNode(const WeakPtr<UINode> & navigationNode)227 void SetNavigationNode(const WeakPtr<UINode>& navigationNode) 228 { 229 navigationNode_ = navigationNode; 230 } 231 232 #if defined(ENABLE_NAV_SPLIT_MODE) SetLastNavPathList(const NavPathList & navPathList)233 void SetLastNavPathList(const NavPathList& navPathList) 234 { 235 lastNavPathList_ = navPathList; 236 } 237 bool isLastListContains(const std::string& name, const RefPtr<UINode>& navDestinationNode); 238 #endif 239 UpdatePathInfoIfNeeded(RefPtr<UINode> & uiNode,int32_t index)240 virtual void UpdatePathInfoIfNeeded(RefPtr<UINode>& uiNode, int32_t index) {} RecoveryNavigationStack()241 virtual void RecoveryNavigationStack() {} NeedBuildNewInstance(int32_t index)242 virtual bool NeedBuildNewInstance(int32_t index) { return false; } SetNeedBuildNewInstance(int32_t index,bool need)243 virtual void SetNeedBuildNewInstance(int32_t index, bool need) {} SetRecoveryFromReplaceDestination(int32_t index,bool value)244 virtual void SetRecoveryFromReplaceDestination(int32_t index, bool value) {} CheckIsReplacedDestination(int32_t index,std::string & replacedName,int32_t & replacedIndex)245 virtual bool CheckIsReplacedDestination(int32_t index, std::string& replacedName, int32_t& replacedIndex) 246 { 247 return false; 248 } 249 UpdateRecoveryList()250 void UpdateRecoveryList() 251 { 252 recoveryList_ = navPathList_; 253 } 254 ClearRecoveryList()255 void ClearRecoveryList() 256 { 257 recoveryList_.clear(); 258 } 259 GetRecoveryList()260 NavPathList GetRecoveryList() 261 { 262 return recoveryList_; 263 } 264 SetIsEntryByIndex(int32_t index,bool isEntry)265 virtual void SetIsEntryByIndex(int32_t index, bool isEntry) {} 266 267 protected: 268 void MoveToTop(const std::string& name, const RefPtr<UINode>& navDestinationNode); 269 void AddForDefault(const std::string& name, const RefPtr<UINode>& navDestinationNode, 270 const RefPtr<RouteInfo>& routeInfo = nullptr); 271 void AddForReplace(const std::string& name, const RefPtr<UINode>& navDestinationNode, 272 const RefPtr<RouteInfo>& routeInfo = nullptr); 273 274 NavPathList navPathList_; 275 // prev backup NavPathList 276 NavPathList preNavPathList_; 277 #if defined(ENABLE_NAV_SPLIT_MODE) 278 // backup NavPathList before push or pop 279 NavPathList lastNavPathList_; 280 #endif 281 // recovery NavPathList 282 NavPathList recoveryList_; 283 NavPathList cacheNodes_; 284 bool animated_ = true; 285 WeakPtr<UINode> navigationNode_; 286 std::vector<std::pair<std::string, WeakPtr<UINode>>> navPathListBeforePoped_; 287 bool isCurForceSetList_ = false; 288 bool isPreForceSetList_ = false; 289 }; 290 } // namespace OHOS::Ace::NG 291 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_STACK_H 292