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_menu.h"
17
18 #include <string>
19
20 #include "base/utils/linear_map.h"
21 #include "base/utils/utils.h"
22 #include "core/components/common/properties/color.h"
23 #include "core/components/option/option_component.h"
24 #include "core/components/select/select_theme.h"
25 #include "core/components/text/text_component.h"
26 #include "core/components/theme/theme_manager.h"
27 #include "frameworks/bridge/common/dom/dom_text.h"
28 #include "frameworks/bridge/common/utils/utils.h"
29
30 namespace OHOS::Ace::Framework {
31
DOMMenu(NodeId nodeId,const std::string & nodeName)32 DOMMenu::DOMMenu(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
33 {
34 menuChild_ = AceType::MakeRefPtr<MenuComponent>(std::to_string(nodeId), nodeName);
35 }
36
~DOMMenu()37 DOMMenu::~DOMMenu()
38 {
39 if (!clickMarkerId_.IsEmpty()) {
40 BackEndEventManager<void()>::GetInstance().RemoveBackEndEvent(clickMarkerId_);
41 }
42 if (!focusMarkerId_.IsEmpty()) {
43 BackEndEventManager<void()>::GetInstance().RemoveBackEndEvent(focusMarkerId_);
44 }
45 if (!longPressMarkerId_.IsEmpty()) {
46 BackEndEventManager<void()>::GetInstance().RemoveBackEndEvent(longPressMarkerId_);
47 }
48 }
49
InitializeStyle()50 void DOMMenu::InitializeStyle()
51 {
52 ResetInitializedStyle();
53 }
54
ResetInitializedStyle()55 void DOMMenu::ResetInitializedStyle()
56 {
57 menuChild_->InitTheme(GetThemeManager());
58 titleStyle_ = menuChild_->GetTitleStyle();
59 }
60
OnChildNodeRemoved(const RefPtr<DOMNode> & child)61 void DOMMenu::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
62 {
63 if (!child) {
64 return;
65 }
66
67 auto option = AceType::DynamicCast<OptionComponent>(child->GetSpecializedComponent());
68 if (!option) {
69 return;
70 }
71
72 menuChild_->RemoveOption(option);
73 }
74
PrepareSpecializedComponent()75 void DOMMenu::PrepareSpecializedComponent()
76 {
77 menuChild_->SetTextDirection((IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR));
78 menuChild_->SetTitleStyle(titleStyle_);
79 }
80
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)81 bool DOMMenu::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
82 {
83 if (attr.first == DOM_TYPE) {
84 isClickType_ = (attr.second == DOM_CLICK);
85 return true;
86 }
87
88 if (attr.first == DOM_TITLE && !attr.second.empty()) {
89 menuChild_->SetTitle(attr.second);
90 }
91
92 return true;
93 }
94
SetSpecializedStyle(const std::pair<std::string,std::string> & style)95 bool DOMMenu::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
96 {
97 static const LinearMapNode<void (*)(TextStyle&, const std::string&, const DOMMenu&)> menuStyleOperators[] = {
98 { DOM_TEXT_ALLOW_SCALE, [](TextStyle& textStyle, const std::string& val,
99 const DOMMenu&) { textStyle.SetAllowScale(StringToBool(val)); } },
100 { DOM_TEXT_FONT_FAMILY, [](TextStyle& textStyle, const std::string& val,
101 const DOMMenu& node) { textStyle.SetFontFamilies(node.ParseFontFamilies(val)); } },
102 { DOM_TEXT_FONT_SIZE, [](TextStyle& textStyle, const std::string& val,
103 const DOMMenu& node) { textStyle.SetFontSize(node.ParseDimension(val)); } },
104 { DOM_TEXT_FONT_STYLE, [](TextStyle& textStyle, const std::string& val,
105 const DOMMenu&) { textStyle.SetFontStyle(ConvertStrToFontStyle(val)); } },
106 { DOM_TEXT_FONT_WEIGHT, [](TextStyle& textStyle, const std::string& val,
107 const DOMMenu&) { textStyle.SetFontWeight(ConvertStrToFontWeight(val)); } },
108 { DOM_TEXT_LETTER_SPACING, [](TextStyle& textStyle, const std::string& val,
109 const DOMMenu& node) { textStyle.SetLetterSpacing(node.ParseDimension(val)); } },
110 { DOM_PICKER_TEXT_COLOR, // use define of picker which is the same "text-color"
111 [](TextStyle& textStyle, const std::string& val, const DOMMenu& node) {
112 textStyle.SetTextColor(node.ParseColor(val));
113 } },
114 { DOM_TEXT_DECORATION, [](TextStyle& textStyle, const std::string& val,
115 const DOMMenu&) { textStyle.SetTextDecoration(ConvertStrToTextDecoration(val)); } },
116 };
117 auto operatorIter = BinarySearchFindIndex(menuStyleOperators, ArraySize(menuStyleOperators), style.first.c_str());
118 if (operatorIter != -1) {
119 menuStyleOperators[operatorIter].value(titleStyle_, style.second, *this);
120 return true;
121 }
122 return false;
123 }
124
AddSpecializedEvent(int32_t pageId,const std::string & event)125 bool DOMMenu::AddSpecializedEvent(int32_t pageId, const std::string& event)
126 {
127 if (event == DOM_SELECTED) {
128 menuChild_->SetOnSuccess(EventMarker(GetNodeIdForEvent(), event, pageId));
129 } else if (event == DOM_CANCEL) {
130 menuChild_->SetOnCancel(EventMarker(GetNodeIdForEvent(), event, pageId));
131 } else {
132 return false;
133 }
134 return true;
135 }
136
GetSpecializedComponent()137 RefPtr<Component> DOMMenu::GetSpecializedComponent()
138 {
139 return menuChild_;
140 }
141
BindIdNode(const RefPtr<DOMNode> & idNode)142 void DOMMenu::BindIdNode(const RefPtr<DOMNode>& idNode)
143 {
144 if (SystemProperties::GetDeviceType() == DeviceType::TV) {
145 focusMarkerId_ = BackEndEventManager<void()>::GetInstance().GetAvailableMarker();
146 BackEndEventManager<void()>::GetInstance().BindBackendEvent(focusMarkerId_, [weak = WeakClaim(this), idNode]() {
147 if (!idNode || idNode->IsNodeDisabled()) {
148 return;
149 }
150 auto domMenu = weak.Upgrade();
151 if (!domMenu) {
152 return;
153 }
154 const auto& targetCallback = domMenu->menuChild_->GetTargetCallback();
155 const auto& targetId = idNode->GetRootComponent()->GetId();
156 if (targetCallback) {
157 targetCallback(targetId, Offset(0, 0));
158 }
159 });
160 idNode->SetOnFocusClick(focusMarkerId_);
161 }
162 if (isClickType_) {
163 clickMarkerId_ = BackEndEventManager<void(const ClickInfo&)>::GetInstance().GetAvailableMarker();
164 BackEndEventManager<void(const ClickInfo&)>::GetInstance().BindBackendEvent(
165 clickMarkerId_, [weak = WeakClaim(this), idNode](const ClickInfo& clickInfo) {
166 auto domMenu = weak.Upgrade();
167 if (!domMenu || !idNode) {
168 return;
169 }
170 const auto& targetCallback = domMenu->menuChild_->GetTargetCallback();
171 const auto& targetId = idNode->GetRootComponent()->GetId();
172 if (targetCallback) {
173 targetCallback(targetId, clickInfo.GetGlobalLocation());
174 }
175 });
176 idNode->SetOnClick(clickMarkerId_);
177 } else {
178 longPressMarkerId_ =
179 BackEndEventManager<void(const LongPressInfo&)>::GetInstance().GetAvailableMarker();
180 BackEndEventManager<void(const LongPressInfo&)>::GetInstance().BindBackendEvent(
181 longPressMarkerId_, [weak = WeakClaim(this), idNode](const LongPressInfo& longPressInfo) {
182 auto domMenu = weak.Upgrade();
183 if (!domMenu || !idNode) {
184 return;
185 }
186 const auto& targetCallback = domMenu->menuChild_->GetTargetCallback();
187 const auto& targetId = idNode->GetRootComponent()->GetId();
188 if (targetCallback) {
189 targetCallback(targetId, longPressInfo.GetGlobalLocation());
190 }
191 });
192 idNode->SetOnLongPress(longPressMarkerId_);
193 }
194 }
195
GetJsonDouble(const std::unique_ptr<JsonValue> & json,const std::string & name,double defaultValue) const196 double DOMMenu::GetJsonDouble(
197 const std::unique_ptr<JsonValue>& json, const std::string& name, double defaultValue) const
198 {
199 if (json && json->IsArray() && json->GetArraySize() > 0) {
200 std::unique_ptr<JsonValue> itemValue = json->GetArrayItem(0)->GetValue(name);
201 if (itemValue && itemValue->IsNumber()) {
202 return itemValue->GetDouble();
203 }
204 }
205
206 return defaultValue;
207 }
208
CallSpecializedMethod(const std::string & method,const std::string & args)209 void DOMMenu::CallSpecializedMethod(const std::string& method, const std::string& args)
210 {
211 if (method == "show") {
212 std::unique_ptr<JsonValue> argsValue = JsonUtil::ParseJsonString(args);
213 double x = GetJsonDouble(argsValue, "x", 0);
214 double y = GetJsonDouble(argsValue, "y", 0);
215 const auto& targetCallback = menuChild_->GetTargetCallback();
216 if (targetCallback) {
217 targetCallback("", Offset(x, y));
218 }
219 }
220 }
221
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)222 void DOMMenu::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
223 {
224 if (!child) {
225 return;
226 }
227
228 auto option = AceType::DynamicCast<OptionComponent>(child->GetSpecializedComponent());
229 if (!option) {
230 return;
231 }
232
233 if (slot < 0) {
234 menuChild_->AppendOption(option);
235 } else {
236 menuChild_->InsertOption(option, static_cast<uint32_t>(slot));
237 }
238 }
239
240 } // namespace OHOS::Ace::Framework
241