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 #include "core/components_ng/pattern/tabs/tabs_node.h"
17
18 #include "base/utils/utils.h"
19 #include "core/components/common/layout/constants.h"
20 #include "core/components_ng/pattern/swiper/swiper_layout_property.h"
21 #include "core/components_ng/pattern/swiper/swiper_paint_property.h"
22 #include "core/components_ng/pattern/tabs/tab_bar_layout_algorithm.h"
23 #include "core/components_ng/pattern/tabs/tab_bar_pattern.h"
24 #include "core/components_v2/inspector/inspector_constants.h"
25 #include "core/pipeline_ng/pipeline_context.h"
26
27 namespace OHOS::Ace::NG {
28 namespace {
29
30 constexpr int32_t ANIMATION_DURATION_DEFAULT = 200;
31 const std::string BAR_BLURSTYLE[] = {
32 "BlurStyle.NONE",
33 "BlurStyle.Thin",
34 "BlurStyle.Regular",
35 "BlurStyle.Thick",
36 "BlurStyle.BACKGROUND_THIN",
37 "BlurStyle.BACKGROUND_REGULAR",
38 "BlurStyle.BACKGROUND_THICK",
39 "BlurStyle.BACKGROUND_ULTRA_THICK",
40 "BlurStyle.COMPONENT_ULTRA_THIN",
41 "BlurStyle.COMPONENT_THIN",
42 "BlurStyle.COMPONENT_REGULAR",
43 "BlurStyle.COMPONENT_THICK",
44 "BlurStyle.COMPONENT_ULTRA_THICK"
45 };
46
47 } // namespace
48
AddChildToGroup(const RefPtr<UINode> & child,int32_t slot)49 void TabsNode::AddChildToGroup(const RefPtr<UINode>& child, int32_t slot)
50 {
51 if (swiperChildren_.find(child->GetId()) != swiperChildren_.end()) {
52 return;
53 }
54
55 swiperChildren_.emplace(child->GetId());
56 auto swiperNode = GetTabs();
57 if (swiperNode) {
58 child->MountToParent(swiperNode);
59 }
60 }
61
ToJsonValue(std::unique_ptr<JsonValue> & json) const62 void TabsNode::ToJsonValue(std::unique_ptr<JsonValue>& json) const
63 {
64 FrameNode::ToJsonValue(json);
65 json->Put("index", std::to_string(GetIndex()).c_str());
66 json->Put("scrollable", Scrollable());
67 json->Put("animationDuration", GetAnimationDuration());
68 if (GetTabBarMode() == TabBarMode::SCROLLABLE) {
69 auto optionsJson = JsonUtil::Create(true);
70 auto options = GetScrollableBarModeOptions();
71 optionsJson->Put("margin", options.margin.ToString().c_str());
72 if (options.nonScrollableLayoutStyle == LayoutStyle::ALWAYS_AVERAGE_SPLIT) {
73 optionsJson->Put("nonScrollableLayoutStyle", "LayoutStyle.ALWAYS_AVERAGE_SPLIT");
74 } else if (options.nonScrollableLayoutStyle == LayoutStyle::SPACE_BETWEEN_OR_CENTER) {
75 optionsJson->Put("nonScrollableLayoutStyle", "LayoutStyle.SPACE_BETWEEN_OR_CENTER");
76 } else {
77 optionsJson->Put("nonScrollableLayoutStyle", "LayoutStyle.ALWAYS_CENTER");
78 }
79 std::string barMode = "BarMode.Scrollable," + optionsJson->ToString();
80 json->Put("barMode", barMode.c_str());
81 } else {
82 json->Put("barMode", "BarMode.Fixed");
83 }
84 json->Put("barWidth", std::to_string(GetBarWidth().Value()).c_str());
85 json->Put("barHeight", GetBarAdaptiveHeight() ? "auto" : std::to_string(GetBarHeight().Value()).c_str());
86 json->Put("fadingEdge", GetFadingEdge() ? "true" : "false");
87 json->Put("barBackgroundColor", GetBarBackgroundColor().ColorToString().c_str());
88 json->Put("barBackgroundBlurStyle", BAR_BLURSTYLE[static_cast<int32_t>(GetBarBackgroundBlurStyle())].c_str());
89
90 auto barGridAlignJson = JsonUtil::Create(true);
91 auto barGridAlign = GetBarGridAlign();
92 barGridAlignJson->Put("gutter", barGridAlign.gutter.ToString().c_str());
93 barGridAlignJson->Put("margin", barGridAlign.margin.ToString().c_str());
94 barGridAlignJson->Put("sm", std::to_string(barGridAlign.sm).c_str());
95 barGridAlignJson->Put("md", std::to_string(barGridAlign.md).c_str());
96 barGridAlignJson->Put("lg", std::to_string(barGridAlign.lg).c_str());
97
98 json->Put("barGridAlign", barGridAlignJson);
99 }
100
Scrollable() const101 bool TabsNode::Scrollable() const
102 {
103 if (!swiperId_.has_value()) {
104 return true;
105 }
106 auto swiperNode = GetFrameNode(V2::SWIPER_ETS_TAG, swiperId_.value());
107 CHECK_NULL_RETURN(swiperNode, true);
108 auto props = swiperNode->GetLayoutProperty<SwiperLayoutProperty>();
109 CHECK_NULL_RETURN(props, true);
110 return !props->GetDisableSwipe().value_or(false);
111 }
112
GetAnimationDuration() const113 int32_t TabsNode::GetAnimationDuration() const
114 {
115 if (!swiperId_.has_value()) {
116 return ANIMATION_DURATION_DEFAULT;
117 }
118 auto swiperNode = GetFrameNode(V2::SWIPER_ETS_TAG, swiperId_.value());
119 CHECK_NULL_RETURN(swiperNode, ANIMATION_DURATION_DEFAULT);
120 auto paintProperty = swiperNode->GetPaintProperty<SwiperPaintProperty>();
121 CHECK_NULL_RETURN(paintProperty, ANIMATION_DURATION_DEFAULT);
122 return paintProperty->GetDuration().value_or(ANIMATION_DURATION_DEFAULT);
123 }
124
GetIndex() const125 int32_t TabsNode::GetIndex() const
126 {
127 if (!swiperId_.has_value()) {
128 return 0;
129 }
130 auto swiperNode = GetFrameNode(V2::SWIPER_ETS_TAG, swiperId_.value());
131 CHECK_NULL_RETURN(swiperNode, 0);
132 auto layoutProperty = swiperNode->GetLayoutProperty<SwiperLayoutProperty>();
133 CHECK_NULL_RETURN(layoutProperty, 0);
134 return layoutProperty->GetIndex().value_or(0);
135 }
136
GetTabBarMode() const137 TabBarMode TabsNode::GetTabBarMode() const
138 {
139 if (!tabBarId_.has_value()) {
140 return TabBarMode::FIXED;
141 }
142 auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
143 CHECK_NULL_RETURN(tabBarNode, TabBarMode::FIXED);
144 auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
145 CHECK_NULL_RETURN(tabBarProperty, TabBarMode::FIXED);
146 return tabBarProperty->GetTabBarMode().value_or(TabBarMode::FIXED);
147 }
148
GetBarWidth() const149 Dimension TabsNode::GetBarWidth() const
150 {
151 if (!tabBarId_.has_value()) {
152 return 0.0_vp;
153 }
154 auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
155 CHECK_NULL_RETURN(tabBarNode, 0.0_vp);
156 auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
157 CHECK_NULL_RETURN(tabBarProperty, 0.0_vp);
158 return tabBarProperty->GetTabBarWidth().value_or(0.0_vp);
159 }
160
GetBarAdaptiveHeight() const161 bool TabsNode::GetBarAdaptiveHeight() const
162 {
163 if (!tabBarId_.has_value()) {
164 return false;
165 }
166 auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
167 CHECK_NULL_RETURN(tabBarNode, false);
168 auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
169 CHECK_NULL_RETURN(tabBarProperty, false);
170 return tabBarProperty->GetBarAdaptiveHeight().value_or(false);
171 }
172
GetBarHeight() const173 Dimension TabsNode::GetBarHeight() const
174 {
175 if (!tabBarId_.has_value()) {
176 return 0.0_vp;
177 }
178 auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
179 CHECK_NULL_RETURN(tabBarNode, 0.0_vp);
180 auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
181 CHECK_NULL_RETURN(tabBarProperty, 0.0_vp);
182 return tabBarProperty->GetTabBarHeight().value_or(0.0_vp);
183 }
184
GetBarBackgroundColor() const185 Color TabsNode::GetBarBackgroundColor() const
186 {
187 auto backgroundColor = Color::BLACK.BlendOpacity(0.0f);
188 if (!tabBarId_.has_value()) {
189 return backgroundColor;
190 }
191 auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
192 CHECK_NULL_RETURN(tabBarNode, backgroundColor);
193 auto tabBarPaintProperty = tabBarNode->GetPaintProperty<TabBarPaintProperty>();
194 CHECK_NULL_RETURN(tabBarPaintProperty, backgroundColor);
195 return tabBarPaintProperty->GetBarBackgroundColor().value_or(backgroundColor);
196 }
197
GetBarBackgroundBlurStyle() const198 BlurStyle TabsNode::GetBarBackgroundBlurStyle() const
199 {
200 auto barBackgroundBlurStyle = BlurStyle::NO_MATERIAL;
201 if (!tabBarId_.has_value()) {
202 return barBackgroundBlurStyle;
203 }
204 auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
205 CHECK_NULL_RETURN(tabBarNode, barBackgroundBlurStyle);
206 auto tabBarPaintProperty = tabBarNode->GetPaintProperty<TabBarPaintProperty>();
207 CHECK_NULL_RETURN(tabBarPaintProperty, barBackgroundBlurStyle);
208 return tabBarPaintProperty->GetTabBarBlurStyle().value_or(barBackgroundBlurStyle);
209 }
210
GetFadingEdge() const211 bool TabsNode::GetFadingEdge() const
212 {
213 if (!tabBarId_.has_value()) {
214 return true;
215 }
216 auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
217 CHECK_NULL_RETURN(tabBarNode, true);
218 auto tabBarProperty = tabBarNode->GetPaintProperty<TabBarPaintProperty>();
219 CHECK_NULL_RETURN(tabBarProperty, true);
220 return tabBarProperty->GetFadingEdge().value_or(true);
221 }
222
GetBarGridAlign() const223 BarGridColumnOptions TabsNode::GetBarGridAlign() const
224 {
225 BarGridColumnOptions option;
226 if (!tabBarId_.has_value()) {
227 return option;
228 }
229 auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
230 CHECK_NULL_RETURN(tabBarNode, option);
231 auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
232 CHECK_NULL_RETURN(tabBarProperty, option);
233 return tabBarProperty->GetBarGridAlign().value_or(option);
234 }
235
GetScrollableBarModeOptions() const236 ScrollableBarModeOptions TabsNode::GetScrollableBarModeOptions() const
237 {
238 ScrollableBarModeOptions option;
239 if (!tabBarId_.has_value()) {
240 return option;
241 }
242 auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
243 CHECK_NULL_RETURN(tabBarNode, option);
244 auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
245 CHECK_NULL_RETURN(tabBarProperty, option);
246 return tabBarProperty->GetScrollableBarModeOptions().value_or(option);
247 }
248 } // namespace OHOS::Ace::NG
249