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