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 "bridge/declarative_frontend/jsview/js_radio.h"
17 #if !defined(PREVIEW) && defined(OHOS_PLATFORM)
18 #include "interfaces/inner_api/ui_session/ui_session_manager.h"
19 #endif
20
21 #include "base/log/ace_scoring_log.h"
22 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
23 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
24 #include "bridge/declarative_frontend/engine/jsi/js_ui_index.h"
25 #include "bridge/declarative_frontend/jsview/models/radio_model_impl.h"
26 #include "core/components/checkable/checkable_theme.h"
27 #include "core/components_ng/base/view_abstract.h"
28 #include "core/components_ng/base/view_stack_model.h"
29 #include "core/components_ng/base/view_stack_processor.h"
30 #include "core/components_ng/pattern/radio/radio_model_ng.h"
31 #include "bridge/declarative_frontend/ark_theme/theme_apply/js_radio_theme.h"
32
33 namespace OHOS::Ace {
34
35 std::unique_ptr<RadioModel> RadioModel::instance_ = nullptr;
36 std::mutex RadioModel::mutex_;
37
38 enum class RadioIndicatorType {
39 TICK = 0,
40 DOT,
41 CUSTOM,
42 };
43
GetInstance()44 RadioModel* RadioModel::GetInstance()
45 {
46 if (!instance_) {
47 std::lock_guard<std::mutex> lock(mutex_);
48 if (!instance_) {
49 #ifdef NG_BUILD
50 instance_.reset(new NG::RadioModelNG());
51 #else
52 if (Container::IsCurrentUseNewPipeline()) {
53 instance_.reset(new NG::RadioModelNG());
54 } else {
55 instance_.reset(new Framework::RadioModelImpl());
56 }
57 #endif
58 }
59 }
60 return instance_.get();
61 }
62
63 } // namespace OHOS::Ace
64 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)65 void JSRadio::Create(const JSCallbackInfo& info)
66 {
67 if (info.Length() < 1) {
68 return;
69 }
70
71 std::optional<std::string> value;
72 std::optional<std::string> group;
73 std::optional<int32_t> indicator;
74 std::function<void()> customBuilderFunc = nullptr;
75 if ((info.Length() >= 1) && info[0]->IsObject()) {
76 auto paramObject = JSRef<JSObject>::Cast(info[0]);
77 auto valueTemp = paramObject->GetProperty("value");
78 auto groupTemp = paramObject->GetProperty("group");
79 auto indicatorTemp = paramObject->GetProperty("indicatorType");
80 auto builderObject = paramObject->GetProperty("indicatorBuilder");
81 if (valueTemp->IsString()) {
82 value = valueTemp->ToString();
83 } else {
84 value = "";
85 }
86 if (groupTemp->IsString()) {
87 group = groupTemp->ToString();
88 } else {
89 group = "";
90 }
91 indicator = indicatorTemp->ToNumber<int32_t>();
92 ParseIndicator(info, indicator, customBuilderFunc, builderObject);
93 }
94 RadioModel::GetInstance()->Create(value, group, indicator);
95 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
96 RadioModel::GetInstance()->SetBuilder(std::move(customBuilderFunc));
97 }
98 JSRadioTheme::ApplyTheme();
99 }
100
ParseIndicator(const JSCallbackInfo & info,std::optional<int32_t> & indicator,std::function<void ()> & customBuilderFunc,JSRef<JSVal> & builderObject)101 void JSRadio::ParseIndicator(const JSCallbackInfo& info, std::optional<int32_t>& indicator,
102 std::function<void()>& customBuilderFunc, JSRef<JSVal>& builderObject)
103 {
104 if (indicator.value() == static_cast<int32_t>(RadioIndicatorType::CUSTOM)) {
105 if (builderObject->IsFunction()) {
106 auto builderFunc = AceType::MakeRefPtr<JsFunction>(info.This(), JSRef<JSFunc>::Cast(builderObject));
107 CHECK_NULL_VOID(builderFunc);
108 auto targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
109 customBuilderFunc = [execCtx = info.GetExecutionContext(), func = std::move(builderFunc),
110 node = targetNode]() {
111 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
112 ACE_SCORING_EVENT("Radio.builder");
113 PipelineContext::SetCallBackNode(node);
114 func->Execute();
115 };
116 } else {
117 indicator = static_cast<int32_t>(RadioIndicatorType::TICK);
118 }
119 }
120 }
121
JSBind(BindingTarget globalObj)122 void JSRadio::JSBind(BindingTarget globalObj)
123 {
124 JSClass<JSRadio>::Declare("Radio");
125
126 JSClass<JSRadio>::StaticMethod("create", &JSRadio::Create);
127 JSClass<JSRadio>::StaticMethod("checked", &JSRadio::Checked);
128 JSClass<JSRadio>::StaticMethod("size", &JSRadio::JsSize);
129 JSClass<JSRadio>::StaticMethod("padding", &JSRadio::JsPadding);
130 JSClass<JSRadio>::StaticMethod("radioStyle", &JSRadio::JsRadioStyle);
131 JSClass<JSRadio>::StaticMethod("responseRegion", &JSRadio::JsResponseRegion);
132 JSClass<JSRadio>::StaticMethod("hoverEffect", &JSRadio::JsHoverEffect);
133 JSClass<JSRadio>::StaticMethod("onChange", &JSRadio::OnChange);
134 JSClass<JSRadio>::StaticMethod("onClick", &JSRadio::JsOnClick);
135 JSClass<JSRadio>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
136 JSClass<JSRadio>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
137 JSClass<JSRadio>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
138 JSClass<JSRadio>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
139 JSClass<JSRadio>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
140 JSClass<JSRadio>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
141 JSClass<JSRadio>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
142 JSClass<JSRadio>::InheritAndBind<JSViewAbstract>(globalObj);
143 }
144
ParseCheckedObject(const JSCallbackInfo & args,const JSRef<JSVal> & changeEventVal)145 void ParseCheckedObject(const JSCallbackInfo& args, const JSRef<JSVal>& changeEventVal)
146 {
147 CHECK_NULL_VOID(changeEventVal->IsFunction());
148
149 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(changeEventVal));
150 WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
151 auto onChecked = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](bool check) {
152 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
153 ACE_SCORING_EVENT("Radio.onChangeEvent");
154 PipelineContext::SetCallBackNode(node);
155 auto newJSVal = JSRef<JSVal>::Make(ToJSValue(check));
156 func->ExecuteJS(1, &newJSVal);
157 };
158 RadioModel::GetInstance()->SetOnChangeEvent(std::move(onChecked));
159 }
160
Checked(const JSCallbackInfo & info)161 void JSRadio::Checked(const JSCallbackInfo& info)
162 {
163 if (info.Length() < 1 || info.Length() > 2) {
164 return;
165 }
166
167 if (info.Length() > 0 && info[0]->IsBoolean()) {
168 RadioModel::GetInstance()->SetChecked(info[0]->ToBoolean());
169 } else {
170 RadioModel::GetInstance()->SetChecked(false);
171 }
172
173 if (info.Length() > 1 && info[1]->IsFunction()) {
174 ParseCheckedObject(info, info[1]);
175 }
176 }
177
JsSize(const JSCallbackInfo & info)178 void JSRadio::JsSize(const JSCallbackInfo& info)
179 {
180 if (!info[0]->IsObject()) {
181 JSViewAbstract::JsWidth(JSVal::Undefined());
182 JSViewAbstract::JsHeight(JSVal::Undefined());
183 return;
184 }
185 JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
186 JSViewAbstract::JsWidth(sizeObj->GetProperty(static_cast<int32_t>(ArkUIIndex::WIDTH)));
187 JSViewAbstract::JsHeight(sizeObj->GetProperty(static_cast<int32_t>(ArkUIIndex::HEIGHT)));
188 }
189
JsPadding(const JSCallbackInfo & info)190 void JSRadio::JsPadding(const JSCallbackInfo& info)
191 {
192 if (info.Length() < 1) {
193 return;
194 }
195 NG::PaddingPropertyF oldPadding = GetOldPadding(info);
196 NG::PaddingProperty newPadding = GetNewPadding(info);
197 RadioModel::GetInstance()->SetPadding(oldPadding, newPadding);
198 }
199
GetOldPadding(const JSCallbackInfo & info)200 NG::PaddingPropertyF JSRadio::GetOldPadding(const JSCallbackInfo& info)
201 {
202 NG::PaddingPropertyF padding({ 0.0f, 0.0f, 0.0f, 0.0f });
203 if (info[0]->IsObject()) {
204 JSRef<JSObject> jsObj = JSRef<JSObject>::Cast(info[0]);
205 if (jsObj->HasProperty(static_cast<int32_t>(ArkUIIndex::TOP)) ||
206 jsObj->HasProperty(static_cast<int32_t>(ArkUIIndex::BOTTOM)) ||
207 jsObj->HasProperty(static_cast<int32_t>(ArkUIIndex::LEFT)) ||
208 jsObj->HasProperty(static_cast<int32_t>(ArkUIIndex::RIGHT))) {
209 CalcDimension topDimen = CalcDimension(0.0, DimensionUnit::VP);
210 CalcDimension leftDimen = CalcDimension(0.0, DimensionUnit::VP);
211 CalcDimension rightDimen = CalcDimension(0.0, DimensionUnit::VP);
212 CalcDimension bottomDimen = CalcDimension(0.0, DimensionUnit::VP);
213 ParseJsDimensionVp(jsObj->GetProperty(static_cast<int32_t>(ArkUIIndex::TOP)), topDimen);
214 ParseJsDimensionVp(jsObj->GetProperty(static_cast<int32_t>(ArkUIIndex::LEFT)), leftDimen);
215 ParseJsDimensionVp(jsObj->GetProperty(static_cast<int32_t>(ArkUIIndex::RIGHT)), rightDimen);
216 ParseJsDimensionVp(jsObj->GetProperty(static_cast<int32_t>(ArkUIIndex::BOTTOM)), bottomDimen);
217 if (leftDimen == 0.0_vp) {
218 leftDimen = rightDimen;
219 }
220 if (topDimen == 0.0_vp) {
221 topDimen = bottomDimen;
222 }
223 if (leftDimen == 0.0_vp) {
224 leftDimen = topDimen;
225 }
226
227 padding.left = leftDimen.ConvertToPx();
228 padding.right = rightDimen.ConvertToPx();
229 padding.top = topDimen.ConvertToPx();
230 padding.bottom = bottomDimen.ConvertToPx();
231 return padding;
232 }
233 }
234
235 CalcDimension length;
236 if (!ParseJsDimensionVp(info[0], length)) {
237 return padding;
238 }
239
240 padding.left = length.ConvertToPx();
241 padding.right = length.ConvertToPx();
242 padding.top = length.ConvertToPx();
243 padding.bottom = length.ConvertToPx();
244 return padding;
245 }
246
GetNewPadding(const JSCallbackInfo & info)247 NG::PaddingProperty JSRadio::GetNewPadding(const JSCallbackInfo& info)
248 {
249 NG::PaddingProperty padding({
250 NG::CalcLength(0.0_vp), NG::CalcLength(0.0_vp), NG::CalcLength(0.0_vp), NG::CalcLength(0.0_vp)
251 });
252 if (info[0]->IsObject()) {
253 std::optional<CalcDimension> left;
254 std::optional<CalcDimension> right;
255 std::optional<CalcDimension> top;
256 std::optional<CalcDimension> bottom;
257 JSRef<JSObject> paddingObj = JSRef<JSObject>::Cast(info[0]);
258
259 CalcDimension leftDimen;
260 if (ParseJsDimensionVp(paddingObj->GetProperty(static_cast<int32_t>(ArkUIIndex::LEFT)), leftDimen)) {
261 left = leftDimen;
262 }
263 CalcDimension rightDimen;
264 if (ParseJsDimensionVp(paddingObj->GetProperty(static_cast<int32_t>(ArkUIIndex::RIGHT)), rightDimen)) {
265 right = rightDimen;
266 }
267 CalcDimension topDimen;
268 if (ParseJsDimensionVp(paddingObj->GetProperty(static_cast<int32_t>(ArkUIIndex::TOP)), topDimen)) {
269 top = topDimen;
270 }
271 CalcDimension bottomDimen;
272 if (ParseJsDimensionVp(paddingObj->GetProperty(static_cast<int32_t>(ArkUIIndex::BOTTOM)), bottomDimen)) {
273 bottom = bottomDimen;
274 }
275 if (left.has_value() || right.has_value() || top.has_value() || bottom.has_value()) {
276 padding = GetPadding(top, bottom, left, right);
277 return padding;
278 }
279 }
280 CalcDimension length;
281 if (!ParseJsDimensionVp(info[0], length)) {
282 length.Reset();
283 }
284
285 padding.SetEdges(NG::CalcLength(length.IsNonNegative() ? length : CalcDimension()));
286 return padding;
287 }
288
GetPadding(const std::optional<CalcDimension> & top,const std::optional<CalcDimension> & bottom,const std::optional<CalcDimension> & left,const std::optional<CalcDimension> & right)289 NG::PaddingProperty JSRadio::GetPadding(const std::optional<CalcDimension>& top,
290 const std::optional<CalcDimension>& bottom, const std::optional<CalcDimension>& left,
291 const std::optional<CalcDimension>& right)
292 {
293 NG::PaddingProperty padding({
294 NG::CalcLength(0.0_vp), NG::CalcLength(0.0_vp), NG::CalcLength(0.0_vp), NG::CalcLength(0.0_vp)
295 });
296 if (left.has_value() && left.value().IsNonNegative()) {
297 padding.left = NG::CalcLength(left.value());
298 }
299 if (right.has_value() && right.value().IsNonNegative()) {
300 padding.right = NG::CalcLength(right.value());
301 }
302 if (top.has_value() && top.value().IsNonNegative()) {
303 padding.top = NG::CalcLength(top.value());
304 }
305 if (bottom.has_value() && bottom.value().IsNonNegative()) {
306 padding.bottom = NG::CalcLength(bottom.value());
307 }
308 return padding;
309 }
310
JsRadioStyle(const JSCallbackInfo & info)311 void JSRadio::JsRadioStyle(const JSCallbackInfo& info)
312 {
313 auto theme = GetTheme<RadioTheme>();
314 if (!info[0]->IsObject()) {
315 RadioModel::GetInstance()->SetCheckedBackgroundColor(theme->GetActiveColor());
316 RadioModel::GetInstance()->SetUncheckedBorderColor(theme->GetInactiveColor());
317 RadioModel::GetInstance()->SetIndicatorColor(theme->GetPointColor());
318 return;
319 }
320 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
321 JSRef<JSVal> checkedBackgroundColor = obj->GetProperty("checkedBackgroundColor");
322 JSRef<JSVal> uncheckedBorderColor = obj->GetProperty("uncheckedBorderColor");
323 JSRef<JSVal> indicatorColor = obj->GetProperty("indicatorColor");
324 Color checkedBackgroundColorVal;
325 if (!ParseJsColor(checkedBackgroundColor, checkedBackgroundColorVal)) {
326 if (!JSRadioTheme::ObtainCheckedBackgroundColor(checkedBackgroundColorVal)) {
327 checkedBackgroundColorVal = theme->GetActiveColor();
328 }
329 }
330 RadioModel::GetInstance()->SetCheckedBackgroundColor(checkedBackgroundColorVal);
331 Color uncheckedBorderColorVal;
332 if (!ParseJsColor(uncheckedBorderColor, uncheckedBorderColorVal)) {
333 if (!JSRadioTheme::ObtainUncheckedBorderColor(uncheckedBorderColorVal)) {
334 uncheckedBorderColorVal = theme->GetInactiveColor();
335 }
336 }
337 RadioModel::GetInstance()->SetUncheckedBorderColor(uncheckedBorderColorVal);
338 Color indicatorColorVal;
339 if (!ParseJsColor(indicatorColor, indicatorColorVal)) {
340 if (!JSRadioTheme::ObtainIndicatorColor(indicatorColorVal)) {
341 indicatorColorVal = theme->GetPointColor();
342 }
343 }
344 RadioModel::GetInstance()->SetIndicatorColor(indicatorColorVal);
345 }
346
JsResponseRegion(const JSCallbackInfo & info)347 void JSRadio::JsResponseRegion(const JSCallbackInfo& info)
348 {
349 if (info.Length() < 1) {
350 return;
351 }
352
353 std::vector<DimensionRect> result;
354 if (!JSViewAbstract::ParseJsResponseRegionArray(info[0], result)) {
355 return;
356 }
357
358 RadioModel::GetInstance()->SetResponseRegion(result);
359 }
360
OnChange(const JSCallbackInfo & args)361 void JSRadio::OnChange(const JSCallbackInfo& args)
362 {
363 if (!args[0]->IsFunction()) {
364 return;
365 }
366 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
367 WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
368 auto onChange = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](bool check) {
369 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
370 ACE_SCORING_EVENT("Radio.onChange");
371 PipelineContext::SetCallBackNode(node);
372 auto newJSVal = JSRef<JSVal>::Make(ToJSValue(check));
373 func->ExecuteJS(1, &newJSVal);
374 #if !defined(PREVIEW) && defined(OHOS_PLATFORM)
375 UiSessionManager::GetInstance().ReportComponentChangeEvent("event", "Radio.onChange");
376 #endif
377 };
378 RadioModel::GetInstance()->SetOnChange(std::move(onChange));
379 args.ReturnSelf();
380 }
381
JsOnClick(const JSCallbackInfo & args)382 void JSRadio::JsOnClick(const JSCallbackInfo& args)
383 {
384 if (Container::IsCurrentUseNewPipeline()) {
385 JSViewAbstract::JsOnClick(args);
386 return;
387 }
388 if (!args[0]->IsFunction()) {
389 return;
390 }
391 RadioModel::GetInstance()->SetOnClickEvent(
392 JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
393
394 args.ReturnSelf();
395 }
396
JsHoverEffect(const JSCallbackInfo & info)397 void JSRadio::JsHoverEffect(const JSCallbackInfo& info)
398 {
399 if (info[0]->IsNumber()) {
400 RadioModel::GetInstance()->SetHoverEffect(static_cast<HoverEffectType>(info[0]->ToNumber<int32_t>()));
401 }
402 }
403 } // namespace OHOS::Ace::Framework
404