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