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 LOGE("unsupported event: %{public}s.", event.c_str());
133 return false;
134 }
135 return true;
136 }
137
GetSpecializedComponent()138 RefPtr<Component> DOMMenu::GetSpecializedComponent()
139 {
140 return menuChild_;
141 }
142
BindIdNode(const RefPtr<DOMNode> & idNode)143 void DOMMenu::BindIdNode(const RefPtr<DOMNode>& idNode)
144 {
145 if (SystemProperties::GetDeviceType() == DeviceType::TV) {
146 focusMarkerId_ = BackEndEventManager<void()>::GetInstance().GetAvailableMarker();
147 BackEndEventManager<void()>::GetInstance().BindBackendEvent(focusMarkerId_, [weak = WeakClaim(this), idNode]() {
148 if (!idNode || idNode->IsNodeDisabled()) {
149 return;
150 }
151 auto domMenu = weak.Upgrade();
152 if (!domMenu) {
153 return;
154 }
155 const auto& targetCallback = domMenu->menuChild_->GetTargetCallback();
156 const auto& targetId = idNode->GetRootComponent()->GetId();
157 if (targetCallback) {
158 targetCallback(targetId, Offset(0, 0));
159 }
160 });
161 idNode->SetOnFocusClick(focusMarkerId_);
162 }
163 if (isClickType_) {
164 clickMarkerId_ = BackEndEventManager<void(const ClickInfo&)>::GetInstance().GetAvailableMarker();
165 BackEndEventManager<void(const ClickInfo&)>::GetInstance().BindBackendEvent(
166 clickMarkerId_, [weak = WeakClaim(this), idNode](const ClickInfo& clickInfo) {
167 auto domMenu = weak.Upgrade();
168 if (!domMenu || !idNode) {
169 return;
170 }
171 const auto& targetCallback = domMenu->menuChild_->GetTargetCallback();
172 const auto& targetId = idNode->GetRootComponent()->GetId();
173 if (targetCallback) {
174 targetCallback(targetId, clickInfo.GetGlobalLocation());
175 }
176 });
177 idNode->SetOnClick(clickMarkerId_);
178 } else {
179 longPressMarkerId_ =
180 BackEndEventManager<void(const LongPressInfo&)>::GetInstance().GetAvailableMarker();
181 BackEndEventManager<void(const LongPressInfo&)>::GetInstance().BindBackendEvent(
182 longPressMarkerId_, [weak = WeakClaim(this), idNode](const LongPressInfo& longPressInfo) {
183 auto domMenu = weak.Upgrade();
184 if (!domMenu || !idNode) {
185 return;
186 }
187 const auto& targetCallback = domMenu->menuChild_->GetTargetCallback();
188 const auto& targetId = idNode->GetRootComponent()->GetId();
189 if (targetCallback) {
190 targetCallback(targetId, longPressInfo.GetGlobalLocation());
191 }
192 });
193 idNode->SetOnLongPress(longPressMarkerId_);
194 }
195 }
196
GetJsonDouble(const std::unique_ptr<JsonValue> & json,const std::string & name,double defaultValue) const197 double DOMMenu::GetJsonDouble(
198 const std::unique_ptr<JsonValue>& json, const std::string& name, double defaultValue) const
199 {
200 if (json && json->IsArray() && json->GetArraySize() > 0) {
201 std::unique_ptr<JsonValue> itemValue = json->GetArrayItem(0)->GetValue(name);
202 if (itemValue && itemValue->IsNumber()) {
203 return itemValue->GetDouble();
204 }
205 }
206
207 return defaultValue;
208 }
209
CallSpecializedMethod(const std::string & method,const std::string & args)210 void DOMMenu::CallSpecializedMethod(const std::string& method, const std::string& args)
211 {
212 if (method == "show") {
213 std::unique_ptr<JsonValue> argsValue = JsonUtil::ParseJsonString(args);
214 double x = GetJsonDouble(argsValue, "x", 0);
215 double y = GetJsonDouble(argsValue, "y", 0);
216 const auto& targetCallback = menuChild_->GetTargetCallback();
217 if (targetCallback) {
218 targetCallback("", Offset(x, y));
219 }
220 }
221 }
222
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)223 void DOMMenu::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
224 {
225 if (!child) {
226 return;
227 }
228
229 auto option = AceType::DynamicCast<OptionComponent>(child->GetSpecializedComponent());
230 if (!option) {
231 return;
232 }
233
234 if (slot < 0) {
235 menuChild_->AppendOption(option);
236 } else {
237 menuChild_->InsertOption(option, static_cast<uint32_t>(slot));
238 }
239 }
240
241 } // namespace OHOS::Ace::Framework
242