• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 namespace OHOS::Ace::NG {
28 using NavPathList = std::vector<std::pair<std::string, RefPtr<UINode>>>;
29 
30 class RouteInfo : public virtual AceType {
31     DECLARE_ACE_TYPE(NG::RouteInfo, AceType)
32 public:
33     RouteInfo() = default;
34     virtual ~RouteInfo() = default;
35 
GetName()36     virtual std::string GetName()
37     {
38         return "";
39     }
40 };
41 
42 class NavigationStack : public virtual AceType {
43     DECLARE_ACE_TYPE(NG::NavigationStack, AceType)
44 public:
45     NavigationStack() = default;
46     ~NavigationStack() override = default;
47 
UpdateStackInfo(const RefPtr<NavigationStack> & newStack)48     virtual void UpdateStackInfo(const RefPtr<NavigationStack>& newStack) {}
49 
GetAllNavDestinationNodes()50     NavPathList& GetAllNavDestinationNodes()
51     {
52         return navPathList_;
53     }
54 
SetOnStateChangedCallback(std::function<void ()> callback)55     virtual void SetOnStateChangedCallback(std::function<void()> callback) {}
56 
SetNavPathList(const NavPathList & navPathList)57     void SetNavPathList(const NavPathList& navPathList)
58     {
59         navPathList_ = navPathList;
60         preNavPathList_ = navPathList;
61     }
62 
Empty()63     bool Empty() const
64     {
65         return navPathList_.empty();
66     }
67 
Size()68     int32_t Size() const
69     {
70         return static_cast<int32_t>(navPathList_.size());
71     }
72 
PreSize()73     int32_t PreSize() const
74     {
75         return static_cast<int32_t>(preNavPathList_.size());
76     }
77 
GetTopNavPath()78     std::optional<std::pair<std::string, RefPtr<UINode>>> GetTopNavPath() const
79     {
80         if (navPathList_.empty()) {
81             return std::nullopt;
82         }
83         return navPathList_.back();
84     }
85 
GetPreTopNavPath()86     std::optional<std::pair<std::string, RefPtr<UINode>>> GetPreTopNavPath() const
87     {
88         if (preNavPathList_.empty()) {
89             return std::nullopt;
90         }
91         return preNavPathList_.back();
92     }
93 
94     NavPathList GetAllCacheNodes();
95     void AddCacheNode(const std::string& name, const RefPtr<UINode>& uiNode);
96     RefPtr<UINode> GetFromCacheNode(NavPathList& cacheNodes, const std::string& name);
97     RefPtr<UINode> GetFromCacheNode(const std::string& name);
98     std::optional<std::pair<std::string, RefPtr<UINode>>> GetFromCacheNode(int32_t handle);
99     void RemoveCacheNode(
100         NavPathList& cacheNodes, const std::string& name, const RefPtr<UINode>& navDestinationNode);
101     void RemoveCacheNode(int32_t handle);
102     void ReOrderCache(const std::string& name, const RefPtr<UINode>& navDestinationNode);
103 
104     void Remove();
105     void Remove(const std::string& name);
106     void Remove(const std::string& name, const RefPtr<UINode>& navDestinationNode);
107     int32_t RemoveInNavPathList(const std::string& name, const RefPtr<UINode>& navDestinationNode);
108     int32_t RemoveInPreNavPathList(const std::string& name, const RefPtr<UINode>& navDestinationNode);
109     void RemoveAll();
110     void Add(const std::string& name, const RefPtr<UINode>& navDestinationNode,
111         const RefPtr<RouteInfo>& routeInfo = nullptr);
112     void Add(const std::string& name, const RefPtr<UINode>& navDestinationNode, NavRouteMode mode,
113         const RefPtr<RouteInfo>& routeInfo = nullptr);
114     void UpdateRemovedNavPathList();
115     RefPtr<UINode> Get();
116     RefPtr<UINode> Get(const std::string& name);
117     RefPtr<UINode> GetFromPreBackup(const std::string& name);
118     RefPtr<UINode> GetPre(const std::string& name, const RefPtr<UINode>& navDestinationNode);
119     virtual bool IsEmpty();
120     virtual std::vector<std::string> GetAllPathName();
121     virtual std::vector<int32_t> GetRemoveArray();
122     virtual void Pop();
123     virtual void Push(const std::string& name, const RefPtr<RouteInfo>& routeInfo = nullptr);
124     virtual void Push(const std::string& name, int32_t index);
125     virtual void RemoveName(const std::string& name);
126     virtual void RemoveIndex(int32_t index);
127     virtual void Clear();
128     virtual void ClearRemoveArray();
129     virtual void UpdateReplaceValue(int32_t replaceValue) const;
130     virtual int32_t GetReplaceValue() const;
131     virtual RefPtr<UINode> CreateNodeByIndex(int32_t index);
132     virtual RefPtr<UINode> CreateNodeByRouteInfo(const RefPtr<RouteInfo>& routeInfo);
GetDisableAnimation()133     virtual bool GetDisableAnimation() const
134     {
135         return false;
136     }
GetAnimatedValue()137     virtual bool GetAnimatedValue() const
138     {
139         return animated_;
140     }
UpdateAnimatedValue(bool animated)141     virtual void UpdateAnimatedValue(bool animated)
142     {
143         animated_ = animated;
144     }
145     int32_t FindIndex(const std::string& name, const RefPtr<UINode>& navDestinationNode, bool isNavPathList);
GetRouteParam()146     virtual std::string GetRouteParam() const
147     {
148         return "";
149     }
150 
OnAttachToParent(RefPtr<NavigationStack> parent)151     virtual void OnAttachToParent(RefPtr<NavigationStack> parent) {}
OnDetachFromParent()152     virtual void OnDetachFromParent() {}
ClearPreBuildNodeList()153     virtual void ClearPreBuildNodeList() {}
154 
155 private:
156     void MoveToTop(const std::string& name, const RefPtr<UINode>& navDestinationNode);
157     void AddForDefault(const std::string& name, const RefPtr<UINode>& navDestinationNode,
158         const RefPtr<RouteInfo>& routeInfo = nullptr);
159     void AddForReplace(const std::string& name, const RefPtr<UINode>& navDestinationNode,
160         const RefPtr<RouteInfo>& routeInfo = nullptr);
161 
162     NavPathList navPathList_;
163     // prev backup NavPathList
164     NavPathList preNavPathList_;
165     NavPathList cacheNodes_;
166     bool animated_ = true;
167 };
168 } // namespace OHOS::Ace::NG
169 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_STACK_H
170