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 <optional> 21 22 #include "base/memory/referenced.h" 23 #include "core/components_ng/base/ui_node.h" 24 #include "core/components_ng/pattern/navigation/navigation_declaration.h" 25 26 namespace OHOS::Ace::NG { 27 using NavPathList = std::vector<std::pair<std::string, RefPtr<UINode>>>; 28 29 class RouteInfo : public virtual AceType { 30 DECLARE_ACE_TYPE(NG::RouteInfo, AceType) 31 public: 32 RouteInfo() = default; 33 virtual ~RouteInfo() = default; 34 GetName()35 virtual std::string GetName() 36 { 37 return ""; 38 } 39 }; 40 41 class NavigationStack : public virtual AceType { 42 DECLARE_ACE_TYPE(NG::NavigationStack, AceType) 43 public: 44 NavigationStack() = default; 45 ~NavigationStack() override = default; 46 UpdateStackInfo(const RefPtr<NavigationStack> & newStack)47 virtual void UpdateStackInfo(const RefPtr<NavigationStack>& newStack) {} 48 GetAllNavDestinationNodes()49 NavPathList& GetAllNavDestinationNodes() 50 { 51 return navPathList_; 52 } 53 SetNavPathList(const NavPathList & navPathList)54 void SetNavPathList(const NavPathList& navPathList) 55 { 56 navPathList_ = navPathList; 57 preNavPathList_ = navPathList; 58 } 59 Empty()60 bool Empty() const 61 { 62 return navPathList_.empty(); 63 } 64 Size()65 int32_t Size() const 66 { 67 return static_cast<int32_t>(navPathList_.size()); 68 } 69 PreSize()70 int32_t PreSize() const 71 { 72 return static_cast<int32_t>(preNavPathList_.size()); 73 } 74 GetTopNavPath()75 std::optional<std::pair<std::string, RefPtr<UINode>>> GetTopNavPath() const 76 { 77 if (navPathList_.empty()) { 78 return std::nullopt; 79 } 80 return navPathList_.back(); 81 } 82 GetPreTopNavPath()83 std::optional<std::pair<std::string, RefPtr<UINode>>> GetPreTopNavPath() const 84 { 85 if (preNavPathList_.empty()) { 86 return std::nullopt; 87 } 88 return preNavPathList_.back(); 89 } 90 91 void Remove(); 92 void Remove(const std::string& name); 93 void Remove(const std::string& name, const RefPtr<UINode>& navDestinationNode); 94 int32_t RemoveInNavPathList(const std::string& name, const RefPtr<UINode>& navDestinationNode); 95 int32_t RemoveInPreNavPathList(const std::string& name, const RefPtr<UINode>& navDestinationNode); 96 void RemoveAll(); 97 void Add(const std::string& name, const RefPtr<UINode>& navDestinationNode, 98 const RefPtr<RouteInfo>& routeInfo = nullptr); 99 void Add(const std::string& name, const RefPtr<UINode>& navDestinationNode, NavRouteMode mode, 100 const RefPtr<RouteInfo>& routeInfo = nullptr); 101 RefPtr<UINode> Get(); 102 RefPtr<UINode> Get(const std::string& name); 103 RefPtr<UINode> GetFromPreBackup(const std::string& name); 104 RefPtr<UINode> GetPre(const std::string& name, const RefPtr<UINode>& navDestinationNode); 105 virtual bool IsEmpty(); 106 virtual std::vector<std::string> GetAllPathName(); 107 virtual void Pop(); 108 virtual void Push(const std::string& name, const RefPtr<RouteInfo>& routeInfo = nullptr); 109 virtual void Push(const std::string& name, int32_t index); 110 virtual void RemoveName(const std::string& name); 111 virtual void RemoveIndex(int32_t index); 112 virtual void Clear(); 113 virtual RefPtr<UINode> CreateNodeByIndex(int32_t index); 114 virtual RefPtr<UINode> CreateNodeByRouteInfo(const RefPtr<RouteInfo>& routeInfo); 115 int32_t FindIndex(const std::string& name, const RefPtr<UINode>& navDestinationNode, bool isNavPathList); 116 117 private: 118 void MoveToTop(const std::string& name, const RefPtr<UINode>& navDestinationNode); 119 void AddForDefault(const std::string& name, const RefPtr<UINode>& navDestinationNode, 120 const RefPtr<RouteInfo>& routeInfo = nullptr); 121 void AddForReplace(const std::string& name, const RefPtr<UINode>& navDestinationNode, 122 const RefPtr<RouteInfo>& routeInfo = nullptr); 123 124 NavPathList navPathList_; 125 // prev backup NavPathList 126 NavPathList preNavPathList_; 127 }; 128 } // namespace OHOS::Ace::NG 129 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_STACK_H 130