1 /*
2  * Copyright (c) 2021-2022 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_TAB_BAR_TAB_BAR_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TAB_BAR_TAB_BAR_COMPONENT_H
18 
19 #include "core/components/box/box_component.h"
20 #include "core/components/box/render_box.h"
21 #include "core/components/common/properties/decoration.h"
22 #include "core/components/common/properties/edge.h"
23 #include "core/components/tab_bar/tab_bar_indicator_component.h"
24 #include "core/components/tab_bar/tab_bar_item_component.h"
25 #include "core/components/tab_bar/tab_controller.h"
26 #include "core/components/tab_bar/tab_theme.h"
27 #include "core/components/text/text_component.h"
28 #include "core/pipeline/base/component_group.h"
29 #include "core/pipeline/base/element.h"
30 #include "core/pipeline/base/render_node.h"
31 
32 namespace OHOS::Ace {
33 
34 enum class TabBarIndicatorType {
35     TAB,
36     LABEL,
37 };
38 
39 class ACE_EXPORT TabBarComponent : public ComponentGroup {
40     DECLARE_ACE_TYPE(TabBarComponent, ComponentGroup);
41 
42 public:
43     TabBarComponent(const std::list<RefPtr<Component>>& tabItems, const RefPtr<TabController>& controller,
44         const RefPtr<BoxComponent>& indicator = MakeRefPtr<TabBarIndicatorComponent>());
45     ~TabBarComponent() override = default;
46 
47     RefPtr<Element> CreateElement() override;
48     RefPtr<RenderNode> CreateRenderNode() override;
49 
50     void InitStyle(const RefPtr<TabTheme>& theme);
51 
SetVertical(bool isVertical)52     void SetVertical(bool isVertical)
53     {
54         vertical_ = isVertical;
55     }
56 
IsVertical()57     bool IsVertical() const
58     {
59         return vertical_;
60     }
61 
SetIndex(int32_t index)62     void SetIndex(int32_t index)
63     {
64         if (controller_) {
65             auto size = static_cast<int32_t>(GetChildren().size());
66             if (index < 0 || index >= size) {
67                 index = 0;
68             }
69             controller_->SetIndexWithoutChangeContent(index);
70         }
71     }
72 
SetController(const RefPtr<TabController> & controller)73     void SetController(const RefPtr<TabController>& controller)
74     {
75         controller_ = controller;
76     }
77 
GetController()78     RefPtr<TabController> GetController() const
79     {
80         return controller_;
81     }
82 
GetIndicator()83     const RefPtr<BoxComponent>& GetIndicator() const
84     {
85         return indicator_;
86     }
87 
GetFocusIndicator()88     const RefPtr<BoxComponent>& GetFocusIndicator() const
89     {
90         return focusIndicator_;
91     }
92 
GetMode()93     TabBarMode GetMode() const
94     {
95         return mode_;
96     }
97 
SetMode(TabBarMode mode)98     void SetMode(TabBarMode mode)
99     {
100         mode_ = mode;
101     }
102 
GetIndicatorSize()103     TabBarIndicatorType GetIndicatorSize() const
104     {
105         return indicatorSize_;
106     }
107 
SetIndicatorSize(TabBarIndicatorType indicatorSize)108     void SetIndicatorSize(TabBarIndicatorType indicatorSize)
109     {
110         indicatorSize_ = indicatorSize;
111     }
112 
GetLabelPadding()113     const Edge& GetLabelPadding() const
114     {
115         return labelPadding_;
116     }
117 
SetLabelPadding(const Edge & labelPadding)118     void SetLabelPadding(const Edge& labelPadding)
119     {
120         labelPadding_ = labelPadding;
121     }
122 
GetPadding()123     const Edge& GetPadding() const
124     {
125         return padding_;
126     }
127 
SetPadding(const Edge & padding)128     void SetPadding(const Edge& padding)
129     {
130         padding_ = padding;
131     }
132 
GetFocusAnimationColor()133     const Color& GetFocusAnimationColor() const
134     {
135         return focusAnimationColor_;
136     }
137 
GetFocusRadiusDimension()138     const Dimension& GetFocusRadiusDimension() const
139     {
140         return focusRadiusDimension_;
141     }
142 
GetGradientWidth()143     const Dimension& GetGradientWidth() const
144     {
145         return gradientWidth_;
146     }
147 
GetActiveIndicatorMinWidth()148     const Dimension& GetActiveIndicatorMinWidth() const
149     {
150         return activeIndicatorMinWidth_;
151     }
152 
GetDomChangeEventId()153     const EventMarker& GetDomChangeEventId() const
154     {
155         return domChangeEventId_;
156     }
157 
SetDomChangeEventId(const EventMarker & domChangeEventId)158     void SetDomChangeEventId(const EventMarker& domChangeEventId)
159     {
160         domChangeEventId_ = domChangeEventId;
161     }
162 
SetBarPosition(BarPosition barPosition)163     void SetBarPosition(BarPosition barPosition)
164     {
165         barPosition_ = barPosition;
166     }
GetBarPosition()167     BarPosition GetBarPosition() const
168     {
169         return barPosition_;
170     }
171 
172     void InitNavigationBarStyle();
173     void InitBottomTabStyle(const RefPtr<TabTheme>& theme);
174     // Called by element on perform build process.
175     void BuildItems(std::list<RefPtr<TabBarItemComponent>>& items);
176     void UpdateItemStyle(const RefPtr<TabBarItemComponent>& item);
177 
ResetIndicator()178     void ResetIndicator()
179     {
180         indicator_.Reset();
181         focusIndicator_.Reset();
182     }
183 
SetAlignment(Alignment alignment)184     void SetAlignment(Alignment alignment)
185     {
186         itemAlignment_ = alignment;
187     }
188 
GetIndicatorColor()189     const Color& GetIndicatorColor() const
190     {
191         return indicatorColor_.value();
192     }
193 
SetIndicatorColor(const Color & color)194     void SetIndicatorColor(const Color& color)
195     {
196         indicatorColor_ = color;
197     }
198 
HasIndicatorColor()199     bool HasIndicatorColor() const
200     {
201         return indicatorColor_.has_value();
202     }
203 
204 private:
205     bool vertical_ { false }; // the tab is vertical or not, default value is false
206 
207     TabBarIndicatorType indicatorSize_ { TabBarIndicatorType::LABEL };
208     TabBarMode mode_ { TabBarMode::FIXED };
209     Edge labelPadding_;
210     Edge padding_;
211 
212     Color focusAnimationColor_ = Color::WHITE;
213     Dimension focusRadiusDimension_;
214     Dimension gradientWidth_;
215     Dimension activeIndicatorMinWidth_;
216 
217     RefPtr<BoxComponent> indicator_;
218     RefPtr<BoxComponent> focusIndicator_;
219     RefPtr<TabController> controller_;
220 
221     bool usingDefaultStyle_ { false };
222     TextStyle activeTextStyle_;
223     TextStyle inactiveTextStyle_;
224     Color activeColor_;
225     Color inactiveColor_;
226     std::optional<Color> indicatorColor_;
227     Alignment itemAlignment_ = Alignment::CENTER;
228     EventMarker domChangeEventId_;
229     BarPosition barPosition_ = BarPosition::START;
230 };
231 
232 } // namespace OHOS::Ace
233 
234 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TAB_BAR_TAB_BAR_COMPONENT_H
235