• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_TABS_TABS_NODE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TABS_TABS_NODE_H
18 
19 #include <optional>
20 
21 #include "base/geometry/dimension.h"
22 #include "core/components_ng/base/group_node.h"
23 #include "core/components_ng/pattern/tabs/tabs_model.h"
24 
25 namespace OHOS::Ace::NG {
26 namespace {
27 constexpr int32_t SWIPER_INDEX = 0;
28 constexpr int32_t DIVIDER_INDEX = 1;
29 constexpr int32_t TAB_BAR_INDEX = 2;
30 } // namespace
31 
32 class ACE_EXPORT TabsNode : public GroupNode {
33     DECLARE_ACE_TYPE(TabsNode, GroupNode);
34 
35 public:
36     TabsNode(const std::string& tag, int32_t nodeId, const RefPtr<Pattern>& pattern, bool isRoot = false)
GroupNode(tag,nodeId,pattern,isRoot)37         : GroupNode(tag, nodeId, pattern, isRoot)
38     {}
39     ~TabsNode() override = default;
40     void AddChildToGroup(const RefPtr<UINode>& child, int32_t slot = DEFAULT_NODE_SLOT) override;
41     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override;
42 
HasSwiperNode()43     bool HasSwiperNode() const
44     {
45         return swiperId_.has_value();
46     }
47 
HasTabBarNode()48     bool HasTabBarNode() const
49     {
50         return tabBarId_.has_value();
51     }
52 
HasDividerNode()53     bool HasDividerNode() const
54     {
55         return dividerId_.has_value();
56     }
57 
HasSelectedMaskNode()58     bool HasSelectedMaskNode() const
59     {
60         return selectedMaskId_.has_value();
61     }
62 
HasUnselectedMaskNode()63     bool HasUnselectedMaskNode() const
64     {
65         return unselectedMaskId_.has_value();
66     }
67 
GetSwiperId()68     int32_t GetSwiperId()
69     {
70         if (!swiperId_.has_value()) {
71             swiperId_ = ElementRegister::GetInstance()->MakeUniqueId();
72         }
73         return swiperId_.value();
74     }
75 
GetDividerId()76     int32_t GetDividerId()
77     {
78         if (!dividerId_.has_value()) {
79             dividerId_ = ElementRegister::GetInstance()->MakeUniqueId();
80         }
81         return dividerId_.value();
82     }
83 
GetTabBarId()84     int32_t GetTabBarId()
85     {
86         if (!tabBarId_.has_value()) {
87             tabBarId_ = ElementRegister::GetInstance()->MakeUniqueId();
88         }
89         return tabBarId_.value();
90     }
91 
GetSelectedMaskId()92     int32_t GetSelectedMaskId()
93     {
94         if (!selectedMaskId_.has_value()) {
95             selectedMaskId_ = ElementRegister::GetInstance()->MakeUniqueId();
96         }
97         return selectedMaskId_.value();
98     }
99 
GetUnselectedMaskId()100     int32_t GetUnselectedMaskId()
101     {
102         if (!unselectedMaskId_.has_value()) {
103             unselectedMaskId_ = ElementRegister::GetInstance()->MakeUniqueId();
104         }
105         return unselectedMaskId_.value();
106     }
107 
GetBuilderByContentId(int32_t tabContentId,const RefPtr<UINode> & builderNode)108     RefPtr<UINode> GetBuilderByContentId(int32_t tabContentId, const RefPtr<UINode>& builderNode)
109     {
110         auto iter = builderNode_.find(tabContentId);
111         if (iter == builderNode_.end()) {
112             builderNode_.try_emplace(tabContentId, builderNode);
113             return nullptr;
114         }
115         auto result = iter->second;
116         iter->second = builderNode;
117         return result;
118     }
119 
RemoveBuilderByContentId(int32_t tabContentId)120     void RemoveBuilderByContentId(int32_t tabContentId)
121     {
122         builderNode_.erase(tabContentId);
123     }
124 
GetTabBar()125     RefPtr<UINode> GetTabBar()
126     {
127         return GetChildAtIndex(TAB_BAR_INDEX);
128     }
129 
GetTabs()130     RefPtr<UINode> GetTabs()
131     {
132         return GetChildAtIndex(SWIPER_INDEX);
133     }
134 
GetDivider()135     RefPtr<UINode> GetDivider()
136     {
137         return GetChildAtIndex(DIVIDER_INDEX);
138     }
139 
140 private:
141     bool Scrollable() const;
142     int32_t GetAnimationDuration() const;
143     TabBarMode GetTabBarMode() const;
144     Dimension GetBarWidth() const;
145     Dimension GetBarHeight() const;
146     bool GetBarAdaptiveHeight() const;
147     Color GetBarBackgroundColor() const;
148     int32_t GetIndex() const;
149     bool GetFadingEdge() const;
150     BarGridColumnOptions GetBarGridAlign() const;
151     ScrollableBarModeOptions GetScrollableBarModeOptions() const;
152 
153     std::optional<int32_t> swiperId_;
154     std::optional<int32_t> tabBarId_;
155     std::optional<int32_t> dividerId_;
156     std::optional<int32_t> selectedMaskId_;
157     std::optional<int32_t> unselectedMaskId_;
158     std::set<int32_t> swiperChildren_;
159     std::map<int32_t, RefPtr<UINode>> builderNode_; // Key is id of TabContent, value is id of builder of TabBar.
160 };
161 
162 } // namespace OHOS::Ace::NG
163 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TABS_TABS_NODE_H