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 GetPreNavPathList()56 NavPathList& GetPreNavPathList() 57 { 58 return preNavPathList_; 59 } 60 SetOnStateChangedCallback(std::function<void ()> callback)61 virtual void SetOnStateChangedCallback(std::function<void()> callback) {} 62 SavePreNavList()63 void SavePreNavList() 64 { 65 // same navdestination nodes before poped 66 isPreForceSetList_ = isCurForceSetList_; 67 navPathListBeforePoped_.clear(); 68 for (auto iter: preNavPathList_) { 69 navPathListBeforePoped_.emplace_back(std::make_pair(iter.first, WeakPtr<UINode>(iter.second))); 70 } 71 } 72 SetNavPathList(const NavPathList & navPathList)73 void SetNavPathList(const NavPathList& navPathList) 74 { 75 preNavPathList_ = navPathList; 76 //copy nav path 77 navPathList_ = navPathList; 78 } 79 SetIsCurForceSetList(bool isForce)80 void SetIsCurForceSetList(bool isForce) 81 { 82 isCurForceSetList_ = isForce; 83 } 84 GetIsPreForceSetList()85 bool GetIsPreForceSetList() const 86 { 87 return isPreForceSetList_; 88 } 89 GetAllNavDestinationNodesPrev()90 std::vector<std::pair<std::string, WeakPtr<UINode>>>& GetAllNavDestinationNodesPrev() 91 { 92 return navPathListBeforePoped_; 93 } 94 Empty()95 bool Empty() const 96 { 97 return navPathList_.empty(); 98 } 99 Size()100 int32_t Size() const 101 { 102 return static_cast<int32_t>(navPathList_.size()); 103 } 104 PreSize()105 int32_t PreSize() const 106 { 107 return static_cast<int32_t>(preNavPathList_.size()); 108 } 109 GetTopNavPath()110 std::optional<std::pair<std::string, RefPtr<UINode>>> GetTopNavPath() const 111 { 112 if (navPathList_.empty()) { 113 return std::nullopt; 114 } 115 return navPathList_.back(); 116 } 117 GetPreTopNavPath()118 std::optional<std::pair<std::string, RefPtr<UINode>>> GetPreTopNavPath() const 119 { 120 if (preNavPathList_.empty()) { 121 return std::nullopt; 122 } 123 return preNavPathList_.back(); 124 } 125 RemoveStack()126 void RemoveStack() 127 { 128 navPathList_.clear(); 129 } 130 131 NavPathList GetAllCacheNodes(); 132 void AddCacheNode(const std::string& name, const RefPtr<UINode>& uiNode); 133 RefPtr<UINode> GetFromCacheNode(NavPathList& cacheNodes, const std::string& name); 134 RefPtr<UINode> GetFromCacheNode(const std::string& name); 135 std::optional<std::pair<std::string, RefPtr<UINode>>> GetFromCacheNode(int32_t handle); 136 void RemoveCacheNode( 137 NavPathList& cacheNodes, const std::string& name, const RefPtr<UINode>& navDestinationNode); 138 void RemoveCacheNode(int32_t handle); 139 void ReOrderCache(const std::string& name, const RefPtr<UINode>& navDestinationNode); 140 141 void Remove(); 142 void Remove(const std::string& name); 143 void Remove(const std::string& name, const RefPtr<UINode>& navDestinationNode); 144 int32_t RemoveInNavPathList(const std::string& name, const RefPtr<UINode>& navDestinationNode); 145 int32_t RemoveInPreNavPathList(const std::string& name, const RefPtr<UINode>& navDestinationNode); 146 void RemoveAll(); 147 void Add(const std::string& name, const RefPtr<UINode>& navDestinationNode, 148 const RefPtr<RouteInfo>& routeInfo = nullptr); 149 void Add(const std::string& name, const RefPtr<UINode>& navDestinationNode, NavRouteMode mode, 150 const RefPtr<RouteInfo>& routeInfo = nullptr); 151 RefPtr<UINode> Get(); 152 bool Get(const std::string& name, RefPtr<UINode>& navDestinationNode, int32_t& index); 153 bool GetFromPreBackup(const std::string& name, RefPtr<UINode>& navDestinationNode, int32_t& index); 154 std::string GetNavDesNameByIndex(int32_t index); 155 RefPtr<UINode> Get(int32_t index); 156 RefPtr<UINode> GetPre(const std::string& name, const RefPtr<UINode>& navDestinationNode); 157 virtual bool IsEmpty(); 158 virtual std::vector<std::string> GetAllPathName(); 159 virtual std::vector<int32_t> GetAllPathIndex(); InitNavPathIndex(const std::vector<std::string> & pathNames)160 virtual void InitNavPathIndex(const std::vector<std::string>& pathNames) {} SetDestinationIdToJsStack(int32_t index,const std::string & navDestinationId)161 virtual void SetDestinationIdToJsStack(int32_t index, const std::string& navDestinationId) {} 162 virtual void Pop(); 163 virtual void Push(const std::string& name, const RefPtr<RouteInfo>& routeInfo = nullptr); 164 virtual void Push(const std::string& name, int32_t index); 165 virtual void RemoveName(const std::string& name); 166 virtual void RemoveIndex(int32_t index); 167 virtual void Clear(); 168 virtual void UpdateReplaceValue(int32_t replaceValue) const; 169 virtual int32_t GetReplaceValue() const; 170 virtual bool CreateNodeByIndex(int32_t index, const WeakPtr<UINode>& customNode, RefPtr<UINode>& node); 171 virtual RefPtr<UINode> CreateNodeByRouteInfo(const RefPtr<RouteInfo>& routeInfo, const WeakPtr<UINode>& node); GetDisableAnimation()172 virtual bool GetDisableAnimation() const 173 { 174 return false; 175 } GetAnimatedValue()176 virtual bool GetAnimatedValue() const 177 { 178 return animated_; 179 } UpdateAnimatedValue(bool animated)180 virtual void UpdateAnimatedValue(bool animated) 181 { 182 animated_ = animated; 183 } 184 int32_t FindIndex(const std::string& name, const RefPtr<UINode>& navDestinationNode, bool isNavPathList); GetRouteParam()185 virtual std::string GetRouteParam() const 186 { 187 return ""; 188 } 189 FireNavigationInterception(bool isBefore,const RefPtr<NG::NavDestinationContext> & from,const RefPtr<NG::NavDestinationContext> & to,NavigationOperation operation,bool isAnimated)190 virtual void FireNavigationInterception(bool isBefore, const RefPtr<NG::NavDestinationContext>& from, 191 const RefPtr<NG::NavDestinationContext>& to, NavigationOperation operation, bool isAnimated) {} 192 FireNavigationModeChange(NavigationMode mode)193 virtual void FireNavigationModeChange(NavigationMode mode) {} 194 OnAttachToParent(RefPtr<NavigationStack> parent)195 virtual void OnAttachToParent(RefPtr<NavigationStack> parent) {} OnDetachFromParent()196 virtual void OnDetachFromParent() {} 197 198 virtual std::vector<std::string> DumpStackInfo() const; 199 GetJsIndexFromNativeIndex(int32_t index)200 virtual int32_t GetJsIndexFromNativeIndex(int32_t index) { return -1; } MoveIndexToTop(int32_t index)201 virtual void MoveIndexToTop(int32_t index) {} 202 GetStringifyParamByIndex(int32_t index)203 virtual std::string GetStringifyParamByIndex(int32_t index) const { return ""; } GetSerializedParamSafely(int32_t index)204 virtual std::string GetSerializedParamSafely(int32_t index) const { return ""; } SetPathArray(const std::vector<NavdestinationRecoveryInfo> & navdestinationsInfo)205 virtual void SetPathArray(const std::vector<NavdestinationRecoveryInfo>& navdestinationsInfo) {} SetFromRecovery(int32_t index,bool fromRecovery)206 virtual void SetFromRecovery(int32_t index, bool fromRecovery) {} IsFromRecovery(int32_t index)207 virtual bool IsFromRecovery(int32_t index) { return false; } GetRecoveredDestinationMode(int32_t index)208 virtual int32_t GetRecoveredDestinationMode(int32_t index) { return false; } GetSize()209 virtual int32_t GetSize() const { return -1; } 210 GetNavDestinationIdInt(int32_t index)211 virtual uint64_t GetNavDestinationIdInt(int32_t index) { return -1; } GetIsForceSet(int32_t index)212 virtual bool GetIsForceSet(int32_t index) { return false; } ResetIsForceSetFlag(int32_t index)213 virtual void ResetIsForceSetFlag(int32_t index) {} 214 215 // could be optimized... HasSingletonMoved()216 virtual bool HasSingletonMoved() 217 { 218 return false; 219 } 220 IsTopFromSingletonMoved()221 virtual bool IsTopFromSingletonMoved() 222 { 223 return false; 224 } 225 ResetSingletonMoved()226 virtual void ResetSingletonMoved() {} 227 GetNavigationNode()228 const WeakPtr<UINode>& GetNavigationNode() 229 { 230 return navigationNode_; 231 } 232 SetNavigationNode(const WeakPtr<UINode> & navigationNode)233 void SetNavigationNode(const WeakPtr<UINode>& navigationNode) 234 { 235 navigationNode_ = navigationNode; 236 } 237 238 #if defined(ENABLE_NAV_SPLIT_MODE) SetLastNavPathList(const NavPathList & navPathList)239 void SetLastNavPathList(const NavPathList& navPathList) 240 { 241 lastNavPathList_ = navPathList; 242 } 243 bool isLastListContains(const std::string& name, const RefPtr<UINode>& navDestinationNode); 244 #endif 245 UpdatePathInfoIfNeeded(RefPtr<UINode> & uiNode,int32_t index)246 virtual void UpdatePathInfoIfNeeded(RefPtr<UINode>& uiNode, int32_t index) {} RecoveryNavigationStack()247 virtual void RecoveryNavigationStack() {} NeedBuildNewInstance(int32_t index)248 virtual bool NeedBuildNewInstance(int32_t index) { return false; } SetNeedBuildNewInstance(int32_t index,bool need)249 virtual void SetNeedBuildNewInstance(int32_t index, bool need) {} SetRecoveryFromReplaceDestination(int32_t index,bool value)250 virtual void SetRecoveryFromReplaceDestination(int32_t index, bool value) {} CheckIsReplacedDestination(int32_t index,std::string & replacedName,int32_t & replacedIndex)251 virtual bool CheckIsReplacedDestination(int32_t index, std::string& replacedName, int32_t& replacedIndex) 252 { 253 return false; 254 } 255 UpdateRecoveryList()256 void UpdateRecoveryList() 257 { 258 recoveryList_ = navPathList_; 259 } 260 ClearRecoveryList()261 void ClearRecoveryList() 262 { 263 recoveryList_.clear(); 264 } 265 GetRecoveryList()266 NavPathList GetRecoveryList() 267 { 268 return recoveryList_; 269 } 270 SetIsEntryByIndex(int32_t index,bool isEntry)271 virtual void SetIsEntryByIndex(int32_t index, bool isEntry) {} 272 RemoveByIndexes(const std::vector<int32_t> & indexes)273 virtual void RemoveByIndexes(const std::vector<int32_t>& indexes) {} 274 PushIntentNavDestination(const std::string & name,const std::string & params,bool needTransition)275 virtual void PushIntentNavDestination(const std::string& name, const std::string& params, bool needTransition) {} 276 CallPushDestinationInner(const NavdestinationRecoveryInfo & navdestinationsInfo)277 virtual void CallPushDestinationInner(const NavdestinationRecoveryInfo& navdestinationsInfo) {} 278 CreateHomeDestination(const WeakPtr<UINode> & customNode,RefPtr<UINode> & node)279 virtual bool CreateHomeDestination(const WeakPtr<UINode>& customNode, RefPtr<UINode>& node) 280 { 281 return false; 282 } 283 284 protected: 285 void MoveToTop(const std::string& name, const RefPtr<UINode>& navDestinationNode); 286 void AddForDefault(const std::string& name, const RefPtr<UINode>& navDestinationNode, 287 const RefPtr<RouteInfo>& routeInfo = nullptr); 288 void AddForReplace(const std::string& name, const RefPtr<UINode>& navDestinationNode, 289 const RefPtr<RouteInfo>& routeInfo = nullptr); 290 291 NavPathList navPathList_; 292 // prev backup NavPathList 293 NavPathList preNavPathList_; 294 #if defined(ENABLE_NAV_SPLIT_MODE) 295 // backup NavPathList before push or pop 296 NavPathList lastNavPathList_; 297 #endif 298 // recovery NavPathList 299 NavPathList recoveryList_; 300 NavPathList cacheNodes_; 301 bool animated_ = true; 302 WeakPtr<UINode> navigationNode_; 303 std::vector<std::pair<std::string, WeakPtr<UINode>>> navPathListBeforePoped_; 304 bool isCurForceSetList_ = false; 305 bool isPreForceSetList_ = false; 306 }; 307 } // namespace OHOS::Ace::NG 308 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_STACK_H 309