• 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 #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 
32 } // namespace
33 
AddChildToGroup(const RefPtr<UINode> & child,int32_t slot)34 void TabsNode::AddChildToGroup(const RefPtr<UINode>& child, int32_t slot)
35 {
36     if (swiperChildren_.find(child->GetId()) != swiperChildren_.end()) {
37         LOGW("Child has already exist.");
38         return;
39     }
40 
41     swiperChildren_.emplace(child->GetId());
42     auto swiperNode = GetTabs();
43     if (swiperNode) {
44         child->MountToParent(swiperNode);
45     }
46 }
47 
ToJsonValue(std::unique_ptr<JsonValue> & json) const48 void TabsNode::ToJsonValue(std::unique_ptr<JsonValue>& json) const
49 {
50     FrameNode::ToJsonValue(json);
51     json->Put("index", std::to_string(GetIndex()).c_str());
52     json->Put("scrollable", Scrollable());
53     json->Put("animationDuration", GetAnimationDuration());
54     if (GetTabBarMode() == TabBarMode::SCROLLABLE) {
55         auto optionsJson = JsonUtil::Create(true);
56         auto options = GetScrollableBarModeOptions();
57         optionsJson->Put("margin", options.margin.ToString().c_str());
58         if (options.nonScrollableLayoutStyle == LayoutStyle::ALWAYS_AVERAGE_SPLIT) {
59             optionsJson->Put("nonScrollableLayoutStyle", "LayoutStyle.ALWAYS_AVERAGE_SPLIT");
60         } else if (options.nonScrollableLayoutStyle == LayoutStyle::SPACE_BETWEEN_OR_CENTER) {
61             optionsJson->Put("nonScrollableLayoutStyle", "LayoutStyle.SPACE_BETWEEN_OR_CENTER");
62         } else {
63             optionsJson->Put("nonScrollableLayoutStyle", "LayoutStyle.ALWAYS_CENTER");
64         }
65         std::string barMode = "BarMode.Scrollable," + optionsJson->ToString();
66         json->Put("barMode", barMode.c_str());
67     } else {
68         json->Put("barMode", "BarMode.Fixed");
69     }
70     json->Put("barWidth", std::to_string(GetBarWidth().Value()).c_str());
71     json->Put("barHeight", GetBarAdaptiveHeight() ? "auto" : std::to_string(GetBarHeight().Value()).c_str());
72     json->Put("fadingEdge", GetFadingEdge() ? "true" : "false");
73     json->Put("barBackgroundColor", GetBarBackgroundColor().ColorToString().c_str());
74 
75     auto barGridAlignJson = JsonUtil::Create(true);
76     auto barGridAlign = GetBarGridAlign();
77     barGridAlignJson->Put("gutter", barGridAlign.gutter.ToString().c_str());
78     barGridAlignJson->Put("margin", barGridAlign.margin.ToString().c_str());
79     barGridAlignJson->Put("sm", std::to_string(barGridAlign.sm).c_str());
80     barGridAlignJson->Put("md", std::to_string(barGridAlign.md).c_str());
81     barGridAlignJson->Put("lg", std::to_string(barGridAlign.lg).c_str());
82 
83     json->Put("barGridAlign", barGridAlignJson);
84 }
85 
Scrollable() const86 bool TabsNode::Scrollable() const
87 {
88     if (!swiperId_.has_value()) {
89         return true;
90     }
91     auto swiperNode = GetFrameNode(V2::SWIPER_ETS_TAG, swiperId_.value());
92     CHECK_NULL_RETURN(swiperNode, true);
93     auto paintProperty = swiperNode->GetPaintProperty<SwiperPaintProperty>();
94     CHECK_NULL_RETURN(paintProperty, true);
95     return !paintProperty->GetDisableSwipe().value_or(false);
96 }
97 
GetAnimationDuration() const98 int32_t TabsNode::GetAnimationDuration() const
99 {
100     if (!swiperId_.has_value()) {
101         return ANIMATION_DURATION_DEFAULT;
102     }
103     auto swiperNode = GetFrameNode(V2::SWIPER_ETS_TAG, swiperId_.value());
104     CHECK_NULL_RETURN(swiperNode, ANIMATION_DURATION_DEFAULT);
105     auto paintProperty = swiperNode->GetPaintProperty<SwiperPaintProperty>();
106     CHECK_NULL_RETURN(paintProperty, ANIMATION_DURATION_DEFAULT);
107     return paintProperty->GetDuration().value_or(ANIMATION_DURATION_DEFAULT);
108 }
109 
GetIndex() const110 int32_t TabsNode::GetIndex() const
111 {
112     if (!swiperId_.has_value()) {
113         return 0;
114     }
115     auto swiperNode = GetFrameNode(V2::SWIPER_ETS_TAG, swiperId_.value());
116     CHECK_NULL_RETURN(swiperNode, 0);
117     auto layoutProperty = swiperNode->GetLayoutProperty<SwiperLayoutProperty>();
118     CHECK_NULL_RETURN(layoutProperty, 0);
119     return layoutProperty->GetIndex().value_or(0);
120 }
121 
GetTabBarMode() const122 TabBarMode TabsNode::GetTabBarMode() const
123 {
124     if (!tabBarId_.has_value()) {
125         return TabBarMode::FIXED;
126     }
127     auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
128     CHECK_NULL_RETURN(tabBarNode, TabBarMode::FIXED);
129     auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
130     CHECK_NULL_RETURN(tabBarProperty, TabBarMode::FIXED);
131     return tabBarProperty->GetTabBarMode().value_or(TabBarMode::FIXED);
132 }
133 
GetBarWidth() const134 Dimension TabsNode::GetBarWidth() const
135 {
136     if (!tabBarId_.has_value()) {
137         return 0.0_vp;
138     }
139     auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
140     CHECK_NULL_RETURN(tabBarNode, 0.0_vp);
141     auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
142     CHECK_NULL_RETURN(tabBarProperty, 0.0_vp);
143     return tabBarProperty->GetTabBarWidth().value_or(0.0_vp);
144 }
145 
GetBarAdaptiveHeight() const146 bool TabsNode::GetBarAdaptiveHeight() const
147 {
148     if (!tabBarId_.has_value()) {
149         return false;
150     }
151     auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
152     CHECK_NULL_RETURN(tabBarNode, false);
153     auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
154     CHECK_NULL_RETURN(tabBarProperty, false);
155     return tabBarProperty->GetBarAdaptiveHeight().value_or(false);
156 }
157 
GetBarHeight() const158 Dimension TabsNode::GetBarHeight() const
159 {
160     if (!tabBarId_.has_value()) {
161         return 0.0_vp;
162     }
163     auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
164     CHECK_NULL_RETURN(tabBarNode, 0.0_vp);
165     auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
166     CHECK_NULL_RETURN(tabBarProperty, 0.0_vp);
167     return tabBarProperty->GetTabBarHeight().value_or(0.0_vp);
168 }
169 
GetBarBackgroundColor() const170 Color TabsNode::GetBarBackgroundColor() const
171 {
172     auto backgroundColor = Color::BLACK.BlendOpacity(0.0f);
173     if (!tabBarId_.has_value()) {
174         return backgroundColor;
175     }
176     auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
177     CHECK_NULL_RETURN(tabBarNode, backgroundColor);
178     auto tabBarPaintProperty = tabBarNode->GetPaintProperty<TabBarPaintProperty>();
179     CHECK_NULL_RETURN(tabBarPaintProperty, backgroundColor);
180     return tabBarPaintProperty->GetBarBackgroundColor().value_or(backgroundColor);
181 }
182 
GetFadingEdge() const183 bool TabsNode::GetFadingEdge() const
184 {
185     if (!tabBarId_.has_value()) {
186         return true;
187     }
188     auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
189     CHECK_NULL_RETURN(tabBarNode, true);
190     auto tabBarProperty = tabBarNode->GetPaintProperty<TabBarPaintProperty>();
191     CHECK_NULL_RETURN(tabBarProperty, true);
192     return tabBarProperty->GetFadingEdge().value_or(true);
193 }
194 
GetBarGridAlign() const195 BarGridColumnOptions TabsNode::GetBarGridAlign() const
196 {
197     BarGridColumnOptions option;
198     if (!tabBarId_.has_value()) {
199         return option;
200     }
201     auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
202     CHECK_NULL_RETURN(tabBarNode, option);
203     auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
204     CHECK_NULL_RETURN(tabBarProperty, option);
205     return tabBarProperty->GetBarGridAlign().value_or(option);
206 }
207 
GetScrollableBarModeOptions() const208 ScrollableBarModeOptions TabsNode::GetScrollableBarModeOptions() const
209 {
210     ScrollableBarModeOptions option;
211     if (!tabBarId_.has_value()) {
212         return option;
213     }
214     auto tabBarNode = GetFrameNode(V2::TAB_BAR_ETS_TAG, tabBarId_.value());
215     CHECK_NULL_RETURN(tabBarNode, option);
216     auto tabBarProperty = tabBarNode->GetLayoutProperty<TabBarLayoutProperty>();
217     CHECK_NULL_RETURN(tabBarProperty, option);
218     return tabBarProperty->GetScrollableBarModeOptions().value_or(option);
219 }
220 } // namespace OHOS::Ace::NG
221