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 #include "frameworks/bridge/common/dom/dom_tab_bar.h"
17
18 #include "core/components/tab_bar/tab_theme.h"
19 #include "core/components/theme/theme_manager.h"
20 #include "frameworks/bridge/common/dom/dom_type.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22
23 namespace OHOS::Ace::Framework {
24
DOMTabBar(NodeId nodeId,const std::string & nodeName)25 DOMTabBar::DOMTabBar(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
26 {
27 std::list<RefPtr<Component>> tabBars;
28 RefPtr<TabController> controller;
29 tabBarIndicator_ = AceType::MakeRefPtr<TabBarIndicatorComponent>();
30 tabBarChild_ = AceType::MakeRefPtr<TabBarComponent>(tabBars, controller, tabBarIndicator_);
31 }
32
InitializeStyle()33 void DOMTabBar::InitializeStyle()
34 {
35 RefPtr<TabTheme> theme = GetTheme<TabTheme>();
36 if (!theme) {
37 return;
38 }
39 if (boxComponent_) {
40 boxComponent_->SetColor(theme->GetBackgroundColor());
41 boxComponent_->SetHasBackgroundColor(true);
42 }
43 auto paddingDimension = theme->GetPadding();
44 padding_ = Edge(paddingDimension.Value(), 0.0, paddingDimension.Value(), 0.0, paddingDimension.Unit());
45 }
46
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)47 bool DOMTabBar::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
48 {
49 if (attr.first == DOM_TAB_BAR_MODE) {
50 tabBarMode_ = ConvertStrToTabBarMode(attr.second);
51 return true;
52 }
53 return false;
54 }
55
SetSpecializedStyle(const std::pair<std::string,std::string> & style)56 bool DOMTabBar::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
57 {
58 static const std::unordered_map<std::string, void (*)(const std::string&, DOMTabBar&)> styleOperators = {
59 { DOM_PADDING,
60 [](const std::string& val, DOMTabBar& node) {
61 node.padding_ = node.ParseEdge(val);
62 } },
63 { DOM_PADDING_END,
64 [](const std::string& val, DOMTabBar& node) {
65 if (node.IsRightToLeft()) {
66 node.padding_.SetLeft(node.ParseDimension(val));
67 } else {
68 node.padding_.SetRight(node.ParseDimension(val));
69 }
70 } },
71 { DOM_PADDING_LEFT,
72 [](const std::string& val, DOMTabBar& node) {
73 node.padding_.SetLeft(node.ParseDimension(val));
74 } },
75 { DOM_PADDING_RIGHT,
76 [](const std::string& val, DOMTabBar& node) {
77 node.padding_.SetRight(node.ParseDimension(val));
78 } },
79 { DOM_PADDING_START,
80 [](const std::string& val, DOMTabBar& node) {
81 if (node.IsRightToLeft()) {
82 node.padding_.SetRight(node.ParseDimension(val));
83 } else {
84 node.padding_.SetLeft(node.ParseDimension(val));
85 }
86 } },
87 { DOM_INDICATOR_COLOR,
88 [](const std::string& val, DOMTabBar& node) {
89 node.indicatorColor_= node.ParseColor(val);
90 }},
91 };
92 auto operatorIter = styleOperators.find(style.first);
93 if (operatorIter != styleOperators.end()) {
94 operatorIter->second(style.second, *this);
95 return true;
96 }
97 return false;
98 }
99
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)100 void DOMTabBar::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
101 {
102 if (!child) {
103 return;
104 }
105 if (tabBarChild_) {
106 tabBarChild_->InsertChild(slot, child->GetRootComponent());
107 }
108 }
109
UpdateIndex(uint32_t currentIndex)110 void DOMTabBar::UpdateIndex(uint32_t currentIndex)
111 {
112 uint32_t index = 0;
113 for (const auto& childNode : GetChildList()) {
114 if (index == currentIndex) {
115 OnChildActive(childNode, true);
116 } else if (index == lastIndex_) {
117 OnChildActive(childNode, false);
118 }
119 index++;
120 }
121 lastIndex_ = currentIndex;
122 }
123
OnMounted(const RefPtr<DOMNode> & parentNode)124 void DOMTabBar::OnMounted(const RefPtr<DOMNode>& parentNode)
125 {
126 if (!parentNode) {
127 return;
128 }
129 if (parentNode->GetTag() == DOM_NODE_TAG_TABS) {
130 const auto& parentNodeTmp = AceType::DynamicCast<DOMTabs>(parentNode);
131 if (!parentNodeTmp) {
132 return;
133 }
134 lastIndex_ = parentNodeTmp->GetTabIndex();
135 controllerId_ = parentNodeTmp->GetTabControllerId();
136 const auto& controller = parentNodeTmp->GetTabController();
137 controller->SetIndexWithoutChangeContent(static_cast<int32_t>(lastIndex_));
138 tabBarChild_->SetController(controller);
139 PrepareChangeListener();
140 }
141 }
142
OnChildNodeRemoved(const RefPtr<DOMNode> & child)143 void DOMTabBar::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
144 {
145 if (!child) {
146 return;
147 }
148 if (tabBarChild_) {
149 tabBarChild_->RemoveChild(child->GetRootComponent());
150 }
151 }
152
ResetInitializedStyle()153 void DOMTabBar::ResetInitializedStyle()
154 {
155 if (!boxComponent_) {
156 return;
157 }
158 RefPtr<TabTheme> theme = GetTheme<TabTheme>();
159 if (theme) {
160 if(indicatorColor_.has_value()){
161 tabBarChild_->SetIndicatorColor(indicatorColor_.value());
162 }
163 tabBarChild_->InitStyle(theme);
164 if (vertical_) {
165 if (LessOrEqual(GetWidth().Value(), 0.0)) {
166 boxComponent_->SetWidth(theme->GetDefaultWidth().Value(), theme->GetDefaultWidth().Unit());
167 }
168 } else {
169 if (LessOrEqual(GetHeight().Value(), 0.0)) {
170 boxComponent_->SetHeight(theme->GetDefaultHeight().Value(), theme->GetDefaultHeight().Unit());
171 }
172 }
173 }
174 }
175
PrepareSpecializedComponent()176 void DOMTabBar::PrepareSpecializedComponent()
177 {
178 tabBarChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
179 tabBarChild_->SetMode(tabBarMode_);
180 const auto& parentNodeTmp = AceType::DynamicCast<DOMTabs>(parentNode_.Upgrade());
181 if (parentNodeTmp) {
182 vertical_ = parentNodeTmp->IsVertical();
183 }
184 tabBarChild_->SetVertical(vertical_);
185 ResetInitializedStyle();
186 tabBarChild_->SetPadding(padding_);
187 }
188
PrepareChangeListener()189 void DOMTabBar::PrepareChangeListener()
190 {
191 // used for initalized the active tabBarItem
192 auto weak = AceType::WeakClaim(this);
193 auto changeCallback = [weak](uint32_t currentIndex) {
194 auto tabBarNode = weak.Upgrade();
195 if (!tabBarNode) {
196 return;
197 }
198 tabBarNode->UpdateIndex(currentIndex);
199 };
200 auto changeMarker = BackEndEventManager<void(uint32_t)>::GetInstance().GetAvailableMarker();
201 BackEndEventManager<void(uint32_t)>::GetInstance().BindBackendEvent(changeMarker, changeCallback);
202 tabBarChild_->SetDomChangeEventId(changeMarker);
203 }
204
OnChildActive(const RefPtr<DOMNode> & node,bool active)205 void DOMTabBar::OnChildActive(const RefPtr<DOMNode>& node, bool active)
206 {
207 node->OnActive(active);
208 for (const auto& childNode : node->GetChildList()) {
209 childNode->OnActive(active);
210 }
211 }
212
ParseEdge(const std::string & value) const213 Edge DOMTabBar::ParseEdge(const std::string& value) const
214 {
215 Edge edge;
216 std::vector<std::string> offsets;
217 StringUtils::StringSplitter(value, ' ', offsets);
218 switch (offsets.size()) {
219 case 1:
220 edge.SetLeft(ParseDimension(offsets[0]));
221 edge.SetRight(ParseDimension(offsets[0]));
222 edge.SetTop(ParseDimension(offsets[0]));
223 edge.SetBottom(ParseDimension(offsets[0]));
224 break;
225 case 2:
226 edge.SetLeft(ParseDimension(offsets[0]));
227 edge.SetRight(ParseDimension(offsets[1]));
228 edge.SetTop(ParseDimension(offsets[0]));
229 edge.SetBottom(ParseDimension(offsets[0]));
230 break;
231 case 3:
232 edge.SetLeft(ParseDimension(offsets[1]));
233 edge.SetRight(ParseDimension(offsets[1]));
234 edge.SetTop(ParseDimension(offsets[0]));
235 edge.SetBottom(ParseDimension(offsets[2]));
236 break;
237 case 4:
238 edge.SetLeft(ParseDimension(offsets[3]));
239 edge.SetRight(ParseDimension(offsets[1]));
240 edge.SetTop(ParseDimension(offsets[0]));
241 edge.SetBottom(ParseDimension(offsets[2]));
242 break;
243 default:
244 break;
245 }
246 return edge;
247 }
248
249 } // namespace OHOS::Ace::Framework
250