1 /*
2 * Copyright (c) 2021 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_tool_bar_item.h"
17
18 #include "base/i18n/localization.h"
19 #include "core/components/padding/padding_component.h"
20 #include "core/components/select/select_theme.h"
21 #include "frameworks/bridge/common/dom/dom_type.h"
22 #include "frameworks/bridge/common/utils/utils.h"
23
24 namespace OHOS::Ace::Framework {
25 namespace {
26
27 constexpr int32_t MAX_LINES = 2;
28 const char MORE[] = "common.more";
29
30 }
31
DOMToolBarItem(NodeId nodeId,const std::string & nodeName)32 DOMToolBarItem::DOMToolBarItem(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
33 {
34 toolBarItemChild_ = AceType::MakeRefPtr<ToolBarItemComponent>();
35 textChild_ = AceType::MakeRefPtr<TextComponent>("");
36 imageChild_ = AceType::MakeRefPtr<ImageComponent>("");
37 }
38
InitializeStyle()39 void DOMToolBarItem::InitializeStyle()
40 {
41 ResetInitializedStyle();
42 }
43
ResetInitializedStyle()44 void DOMToolBarItem::ResetInitializedStyle()
45 {
46 theme_ = GetTheme<ToolBarTheme>();
47 if (!theme_) {
48 LOGE("ToolBarTheme is null");
49 return;
50 }
51 InitImageStyle();
52 InitTextStyle();
53 InitializedToolBarItemChild();
54 }
55
InitImageStyle()56 void DOMToolBarItem::InitImageStyle()
57 {
58 const Size& iconSize = theme_->GetIconSize();
59 imageChild_->SetWidth(Dimension(iconSize.Width(), DimensionUnit::VP));
60 imageChild_->SetHeight(Dimension(iconSize.Height(), DimensionUnit::VP));
61 imageChild_->SetMatchTextDirection(true);
62 }
63
InitTextStyle()64 void DOMToolBarItem::InitTextStyle()
65 {
66 textStyle_ = theme_->GetToolBarTextStyle();
67 textStyle_.SetTextAlign(TextAlign::CENTER);
68 textStyle_.SetTextOverflow(TextOverflow::CLIP);
69 textStyle_.SetMaxLines(MAX_LINES);
70 }
71
InitializedToolBarItemChild()72 void DOMToolBarItem::InitializedToolBarItemChild()
73 {
74 toolBarItemChild_->SetPressColor(theme_->GetPressColor());
75 toolBarItemChild_->SetRadius(theme_->GetRadius());
76 toolBarItemChild_->SetFocusColor(theme_->GetFocusColor());
77 toolBarItemChild_->SetHoverColor(theme_->GetHoverColor());
78 RefPtr<SelectTheme> selectTheme = GetTheme<SelectTheme>();
79 if (selectTheme) {
80 toolBarItemChild_->SetMenuMinWidth(selectTheme->GetPopupMinWidth());
81 }
82 if (!declaration_) {
83 return;
84 }
85 declaration_->GetBackDecoration()->SetBackgroundColor(theme_->GetItemBackgroundColor());
86 declaration_->SetHasDecorationStyle(true);
87 }
88
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)89 bool DOMToolBarItem::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
90 {
91 // Operator map for attr
92 static const LinearMapNode<void (*)(DOMToolBarItem&, const std::string&)> attrOperators[] = {
93 { DOM_TOOL_BAR_ITEM_ICON,
94 [](DOMToolBarItem& toolBarItem, const std::string& val) { toolBarItem.icon_ = std::move(val); } },
95 { DOM_TOOL_BAR_ITEM_VALUE,
96 [](DOMToolBarItem& toolBarItem, const std::string& val) { toolBarItem.value_ = std::move(val); } },
97 };
98 auto operatorIter = BinarySearchFindIndex(attrOperators, ArraySize(attrOperators), attr.first.c_str());
99 if (operatorIter != -1) {
100 LOGD("ToolBarItem attrs : %{public}s = %{public}s", attr.first.c_str(), attr.second.c_str());
101 attrOperators[operatorIter].value(*this, attr.second);
102 return true;
103 }
104 return false;
105 }
106
SetSpecializedStyle(const std::pair<std::string,std::string> & style)107 bool DOMToolBarItem::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
108 {
109 // Operator map for style
110 static const LinearMapNode<void (*)(DOMToolBarItem&, const std::string&)> styleOperators[] = {
111 { DOM_TOOL_BAR_ITEM_ALLOW_SCALE,
112 [](DOMToolBarItem& toolBarItem, const std::string& val) {
113 toolBarItem.textStyle_.SetAllowScale(StringToBool(val));
114 } },
115 { DOM_TOOL_BAR_ITEM_COLOR,
116 [](DOMToolBarItem& toolBarItem, const std::string& val) {
117 toolBarItem.textStyle_.SetTextColor(Color::FromString(val));
118 } },
119 { DOM_TOOL_BAR_ITEM_FONT_FAMILY,
120 [](DOMToolBarItem& toolBarItem, const std::string& val) {
121 toolBarItem.textStyle_.SetFontFamilies(ConvertStrToFontFamilies(val));
122 } },
123 { DOM_TOOL_BAR_ITEM_FONT_SIZE,
124 [](DOMToolBarItem& toolBarItem, const std::string& val) {
125 toolBarItem.textStyle_.SetFontSize(StringToDimension(val));
126 } },
127 { DOM_TOOL_BAR_ITEM_FONT_STYLE,
128 [](DOMToolBarItem& toolBarItem, const std::string& val) {
129 toolBarItem.textStyle_.SetFontStyle(ConvertStrToFontStyle(val));
130 } },
131 { DOM_TOOL_BAR_ITEM_FONT_WEIGHT,
132 [](DOMToolBarItem& toolBarItem, const std::string& val) {
133 toolBarItem.textStyle_.SetFontWeight(ConvertStrToFontWeight(val));
134 } },
135 { DOM_TOOL_BAR_ITEM_TEXT_COLOR,
136 [](DOMToolBarItem& toolBarItem, const std::string& val) {
137 toolBarItem.textStyle_.SetTextColor(Color::FromString(val));
138 } },
139 { DOM_TOOL_BAR_ITEM_TEXT_DECORATION,
140 [](DOMToolBarItem& toolBarItem, const std::string& val) {
141 toolBarItem.textStyle_.SetTextDecoration(ConvertStrToTextDecoration(val));
142 } },
143 };
144 auto operatorIter = BinarySearchFindIndex(styleOperators, ArraySize(styleOperators), style.first.c_str());
145 if (operatorIter != -1) {
146 LOGD("ToolBarItem styles : %{public}s = %{public}s", style.first.c_str(), style.second.c_str());
147 styleOperators[operatorIter].value(*this, style.second);
148 return true;
149 }
150 return false;
151 }
152
AddSpecializedEvent(int32_t pageId,const std::string & event)153 bool DOMToolBarItem::AddSpecializedEvent(int32_t pageId, const std::string& event)
154 {
155 static const LinearMapNode<void (*)(DOMToolBarItem&, const EventMarker&)> toolBarItemEventOperators[] = {
156 { DOM_CATCH_BUBBLE_CLICK,
157 [](DOMToolBarItem& toolBarItem, const EventMarker& event) {
158 EventMarker eventMarker(event);
159 eventMarker.SetCatchMode(true);
160 toolBarItem.clickEventId_ = eventMarker;
161 } },
162 { DOM_CLICK,
163 [](DOMToolBarItem& toolBarItem, const EventMarker& event) {
164 EventMarker eventMarker(event);
165 eventMarker.SetCatchMode(false);
166 toolBarItem.clickEventId_ = eventMarker;
167 } },
168 };
169 auto operatorIter =
170 BinarySearchFindIndex(toolBarItemEventOperators, ArraySize(toolBarItemEventOperators), event.c_str());
171 if (operatorIter != -1) {
172 LOGD("ToolBarItem events : %{public}s", event.c_str());
173 toolBarItemEventOperators[operatorIter].value(*this, EventMarker(GetNodeIdForEvent(), event, pageId));
174 return true;
175 }
176 return false;
177 }
178
PrepareSpecializedComponent()179 void DOMToolBarItem::PrepareSpecializedComponent()
180 {
181 toolBarItemChild_->SetChild(nullptr);
182 toolBarItemChild_->SetIsEndItem(isEndItem_);
183
184 std::list<RefPtr<Component>> children;
185 if (isEndItem_) {
186 BuildEndItemComponent(children);
187 } else {
188 BuildCommonComponent(children);
189 toolBarItemChild_->SetClickedEventId(clickEventId_);
190 }
191
192 if (children.empty()) {
193 toolBarItemChild_->SetChild(nullptr);
194 return;
195 }
196 auto column = AceType::MakeRefPtr<ColumnComponent>(FlexAlign::FLEX_START, FlexAlign::CENTER, children);
197 if (static_cast<uint32_t>(icon_.empty()) ^ static_cast<uint32_t>(value_.empty())) {
198 column->SetMainAxisAlign(FlexAlign::CENTER);
199 }
200 toolBarItemChild_->SetChild(column);
201 }
202
BuildCommonComponent(std::list<RefPtr<Component>> & children)203 void DOMToolBarItem::BuildCommonComponent(std::list<RefPtr<Component>>& children)
204 {
205 // Generate common icon
206 if (!icon_.empty()) {
207 if (!imageChild_) {
208 imageChild_ = AceType::MakeRefPtr<ImageComponent>(icon_);
209 InitImageStyle();
210 }
211 imageChild_->SetResourceId(InternalResource::ResourceId::NO_ID);
212 imageChild_->SetSrc(icon_);
213 imageChild_->SetImageFill(GetImageFill());
214 children.emplace_back(SetPadding(imageChild_, Edge(theme_->GetIconEdge())));
215 }
216
217 // Generate common text
218 if (!value_.empty()) {
219 if (!textChild_) {
220 textChild_ = AceType::MakeRefPtr<TextComponent>(value_);
221 InitTextStyle();
222 }
223 textChild_->SetData(value_);
224 textStyle_.SetAdaptMaxFontSize(textStyle_.GetFontSize());
225 textChild_->SetFocusColor(textStyle_.GetTextColor());
226 textChild_->SetTextStyle(textStyle_);
227 children.emplace_back(SetPadding(textChild_, Edge(theme_->GetTextEdge())));
228 }
229 }
230
BuildEndItemComponent(std::list<RefPtr<Component>> & children)231 void DOMToolBarItem::BuildEndItemComponent(std::list<RefPtr<Component>>& children)
232 {
233 // Generate endItem icon
234 if (!imageChild_) {
235 imageChild_ = AceType::MakeRefPtr<ImageComponent>("");
236 InitImageStyle();
237 }
238 imageChild_->SetSrc("");
239 imageChild_->SetResourceId(InternalResource::ResourceId::IC_MORE);
240 imageChild_->SetColor(theme_->GetIconColor());
241 children.emplace_back(SetPadding(imageChild_, Edge(theme_->GetIconEdge())));
242
243 // Generate endItem text
244 if (!textChild_) {
245 textChild_ = AceType::MakeRefPtr<TextComponent>("");
246 InitTextStyle();
247 }
248 textChild_->SetData(Localization::GetInstance()->GetEntryLetters(MORE));
249 textChild_->SetFocusColor(textStyle_.GetTextColor());
250 textChild_->SetTextStyle(textStyle_);
251 children.emplace_back(SetPadding(textChild_, Edge(theme_->GetTextEdge())));
252 }
253
SetPadding(const RefPtr<Component> & component,Edge && edge)254 const RefPtr<Component> DOMToolBarItem::SetPadding(const RefPtr<Component>& component, Edge&& edge)
255 {
256 auto paddingComponent = AceType::MakeRefPtr<PaddingComponent>();
257 paddingComponent->SetPadding(std::move(edge));
258 paddingComponent->SetChild(component);
259
260 return paddingComponent;
261 }
262
BuildOptionComponent()263 RefPtr<OptionComponent> DOMToolBarItem::BuildOptionComponent()
264 {
265 RefPtr<OptionComponent> optionComponent;
266 if (!value_.empty()) {
267 RefPtr<TextComponent> textComponent = AceType::MakeRefPtr<TextComponent>(value_);
268 if (!optionComponent) {
269 optionComponent = AceType::MakeRefPtr<OptionComponent>();
270 }
271 optionComponent->SetText(textComponent);
272 optionComponent->SetValue(value_);
273 optionComponent->SetClickEventForToolBarItem(clickEventId_);
274 }
275 return optionComponent;
276 }
277
278 } // namespace OHOS::Ace::Framework
279