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