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         JSRef<JSObject> paddingObj = JSRef<JSObject>::Cast(info[0]);
254         CommonCalcDimension commonCalcDimension;
255         JSViewAbstract::ParseCommonMarginOrPaddingCorner(paddingObj, commonCalcDimension);
256         if (commonCalcDimension.left.has_value() || commonCalcDimension.right.has_value() ||
257             commonCalcDimension.top.has_value() || commonCalcDimension.bottom.has_value()) {
258             return GetPadding(commonCalcDimension.top, commonCalcDimension.bottom, commonCalcDimension.left,
259                 commonCalcDimension.right);
260         }
261     }
262     CalcDimension length;
263     if (!ParseJsDimensionVp(info[0], length)) {
264         length.Reset();
265     }
266 
267     padding.SetEdges(NG::CalcLength(length.IsNonNegative() ? length : CalcDimension()));
268     return padding;
269 }
270 
GetPadding(const std::optional<CalcDimension>& top, const std::optional<CalcDimension>& bottom, const std::optional<CalcDimension>& left, const std::optional<CalcDimension>& right)271 NG::PaddingProperty JSRadio::GetPadding(const std::optional<CalcDimension>& top,
272     const std::optional<CalcDimension>& bottom, const std::optional<CalcDimension>& left,
273     const std::optional<CalcDimension>& right)
274 {
275     NG::PaddingProperty padding({
276         NG::CalcLength(0.0_vp), NG::CalcLength(0.0_vp), NG::CalcLength(0.0_vp), NG::CalcLength(0.0_vp)
277     });
278     if (left.has_value() && left.value().IsNonNegative()) {
279         padding.left = NG::CalcLength(left.value());
280     }
281     if (right.has_value() && right.value().IsNonNegative()) {
282         padding.right = NG::CalcLength(right.value());
283     }
284     if (top.has_value() && top.value().IsNonNegative()) {
285         padding.top = NG::CalcLength(top.value());
286     }
287     if (bottom.has_value() && bottom.value().IsNonNegative()) {
288         padding.bottom = NG::CalcLength(bottom.value());
289     }
290     return padding;
291 }
292 
JsRadioStyle(const JSCallbackInfo& info)293 void JSRadio::JsRadioStyle(const JSCallbackInfo& info)
294 {
295     auto theme = GetTheme<RadioTheme>();
296     if (!info[0]->IsObject()) {
297         RadioModel::GetInstance()->SetCheckedBackgroundColor(theme->GetActiveColor());
298         RadioModel::GetInstance()->SetUncheckedBorderColor(theme->GetInactiveColor());
299         RadioModel::GetInstance()->SetIndicatorColor(theme->GetPointColor());
300         return;
301     }
302     JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
303     JSRef<JSVal> checkedBackgroundColor = obj->GetProperty("checkedBackgroundColor");
304     JSRef<JSVal> uncheckedBorderColor = obj->GetProperty("uncheckedBorderColor");
305     JSRef<JSVal> indicatorColor = obj->GetProperty("indicatorColor");
306     Color checkedBackgroundColorVal;
307     if (!ParseJsColor(checkedBackgroundColor, checkedBackgroundColorVal)) {
308         if (!JSRadioTheme::ObtainCheckedBackgroundColor(checkedBackgroundColorVal)) {
309             checkedBackgroundColorVal = theme->GetActiveColor();
310         }
311     }
312     RadioModel::GetInstance()->SetCheckedBackgroundColor(checkedBackgroundColorVal);
313     Color uncheckedBorderColorVal;
314     if (!ParseJsColor(uncheckedBorderColor, uncheckedBorderColorVal)) {
315         if (!JSRadioTheme::ObtainUncheckedBorderColor(uncheckedBorderColorVal)) {
316             uncheckedBorderColorVal = theme->GetInactiveColor();
317         }
318     }
319     RadioModel::GetInstance()->SetUncheckedBorderColor(uncheckedBorderColorVal);
320     Color indicatorColorVal;
321     if (!ParseJsColor(indicatorColor, indicatorColorVal)) {
322         if (!JSRadioTheme::ObtainIndicatorColor(indicatorColorVal)) {
323             indicatorColorVal = theme->GetPointColor();
324         }
325     }
326     RadioModel::GetInstance()->SetIndicatorColor(indicatorColorVal);
327 }
328 
JsResponseRegion(const JSCallbackInfo& info)329 void JSRadio::JsResponseRegion(const JSCallbackInfo& info)
330 {
331     if (info.Length() < 1) {
332         return;
333     }
334 
335     std::vector<DimensionRect> result;
336     if (!JSViewAbstract::ParseJsResponseRegionArray(info[0], result)) {
337         return;
338     }
339 
340     RadioModel::GetInstance()->SetResponseRegion(result);
341 }
342 
OnChange(const JSCallbackInfo& args)343 void JSRadio::OnChange(const JSCallbackInfo& args)
344 {
345     if (!args[0]->IsFunction()) {
346         return;
347     }
348     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
349     WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
350     auto onChange = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](bool check) {
351         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
352         ACE_SCORING_EVENT("Radio.onChange");
353         PipelineContext::SetCallBackNode(node);
354         auto newJSVal = JSRef<JSVal>::Make(ToJSValue(check));
355         func->ExecuteJS(1, &newJSVal);
356 #if !defined(PREVIEW) && defined(OHOS_PLATFORM)
357         UiSessionManager::GetInstance().ReportComponentChangeEvent("event", "Radio.onChange");
358 #endif
359     };
360     RadioModel::GetInstance()->SetOnChange(std::move(onChange));
361     args.ReturnSelf();
362 }
363 
JsOnClick(const JSCallbackInfo& args)364 void JSRadio::JsOnClick(const JSCallbackInfo& args)
365 {
366     if (Container::IsCurrentUseNewPipeline()) {
367         JSViewAbstract::JsOnClick(args);
368         return;
369     }
370     if (!args[0]->IsFunction()) {
371         return;
372     }
373     RadioModel::GetInstance()->SetOnClickEvent(
374         JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
375 
376     args.ReturnSelf();
377 }
378 
JsHoverEffect(const JSCallbackInfo& info)379 void JSRadio::JsHoverEffect(const JSCallbackInfo& info)
380 {
381     if (info[0]->IsNumber()) {
382         RadioModel::GetInstance()->SetHoverEffect(static_cast<HoverEffectType>(info[0]->ToNumber<int32_t>()));
383     }
384 }
385 } // namespace OHOS::Ace::Framework
386