1 /*
2 * Copyright (c) 2023 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 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_radio_bridge.h"
16 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
17 #include "core/components/checkable/checkable_theme.h"
18 #include "core/components_ng/base/frame_node.h"
19 #include "core/components_ng/pattern/radio/radio_model_ng.h"
20
21 namespace OHOS::Ace::NG {
22 constexpr int NUM_0 = 0;
23 constexpr int NUM_1 = 1;
24 constexpr int NUM_2 = 2;
25 constexpr int NUM_3 = 3;
26 constexpr int NUM_4 = 4;
27 constexpr int PARAM_ARR_LENGTH_1 = 1;
28 const char* RADIO_BUILDER_NODEPTR_OF_UINODE = "nodePtr_";
JsRadioChangeCallback(panda::JsiRuntimeCallInfo * runtimeCallInfo)29 panda::Local<panda::JSValueRef> JsRadioChangeCallback(panda::JsiRuntimeCallInfo* runtimeCallInfo)
30 {
31 auto vm = runtimeCallInfo->GetVM();
32 int32_t argc = static_cast<int32_t>(runtimeCallInfo->GetArgsNumber());
33 if (argc != NUM_1) {
34 return panda::JSValueRef::Undefined(vm);
35 }
36 auto firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
37 if (!firstArg->IsBoolean()) {
38 return panda::JSValueRef::Undefined(vm);
39 }
40 bool value = firstArg->ToBoolean(vm)->Value();
41 auto ref = runtimeCallInfo->GetThisRef();
42 auto obj = ref->ToObject(vm);
43 if (obj->GetNativePointerFieldCount(vm) < NUM_1) {
44 return panda::JSValueRef::Undefined(vm);
45 }
46 auto frameNode = static_cast<FrameNode*>(obj->GetNativePointerField(vm, 0));
47 CHECK_NULL_RETURN(frameNode, panda::JSValueRef::Undefined(vm));
48 RadioModelNG::SetChangeValue(frameNode, value);
49 return panda::JSValueRef::Undefined(vm);
50 }
51
SetRadioChecked(ArkUIRuntimeCallInfo * runtimeCallInfo)52 ArkUINativeModuleValue RadioBridge::SetRadioChecked(ArkUIRuntimeCallInfo* runtimeCallInfo)
53 {
54 EcmaVM* vm = runtimeCallInfo->GetVM();
55 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
56 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
57 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
58 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
59 bool isCheck = secondArg->ToBoolean(vm)->Value();
60 GetArkUINodeModifiers()->getRadioModifier()->setRadioChecked(nativeNode, isCheck);
61 return panda::JSValueRef::Undefined(vm);
62 }
63
ResetRadioChecked(ArkUIRuntimeCallInfo * runtimeCallInfo)64 ArkUINativeModuleValue RadioBridge::ResetRadioChecked(ArkUIRuntimeCallInfo* runtimeCallInfo)
65 {
66 EcmaVM* vm = runtimeCallInfo->GetVM();
67 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
68 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
69 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
70 GetArkUINodeModifiers()->getRadioModifier()->resetRadioChecked(nativeNode);
71 return panda::JSValueRef::Undefined(vm);
72 }
73
SetRadioStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)74 ArkUINativeModuleValue RadioBridge::SetRadioStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
75 {
76 EcmaVM* vm = runtimeCallInfo->GetVM();
77 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
78 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
79 Local<JSValueRef> checkedBackgroundColor = runtimeCallInfo->GetCallArgRef(NUM_1);
80 Local<JSValueRef> uncheckedBorderColor = runtimeCallInfo->GetCallArgRef(NUM_2);
81 Local<JSValueRef> indicatorColor = runtimeCallInfo->GetCallArgRef(NUM_3);
82 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
83
84 auto pipeline = PipelineBase::GetCurrentContext();
85 CHECK_NULL_RETURN(pipeline, panda::NativePointerRef::New(vm, nullptr));
86 auto radioTheme = pipeline->GetTheme<RadioTheme>();
87 CHECK_NULL_RETURN(radioTheme, panda::NativePointerRef::New(vm, nullptr));
88
89 Color checkedBackgroundColorVal;
90 if (checkedBackgroundColor->IsNull() || checkedBackgroundColor->IsUndefined() ||
91 !ArkTSUtils::ParseJsColorAlpha(vm, checkedBackgroundColor, checkedBackgroundColorVal)) {
92 checkedBackgroundColorVal = radioTheme->GetActiveColor();
93 }
94 Color uncheckedBorderColorVal;
95 if (uncheckedBorderColor->IsNull() || uncheckedBorderColor->IsUndefined() ||
96 !ArkTSUtils::ParseJsColorAlpha(vm, uncheckedBorderColor, uncheckedBorderColorVal)) {
97 uncheckedBorderColorVal = radioTheme->GetInactiveColor();
98 }
99 Color indicatorColorVal;
100 if (indicatorColor->IsNull() || indicatorColor->IsUndefined() ||
101 !ArkTSUtils::ParseJsColorAlpha(vm, indicatorColor, indicatorColorVal)) {
102 indicatorColorVal = radioTheme->GetPointColor();
103 }
104
105 GetArkUINodeModifiers()->getRadioModifier()->setRadioStyle(nativeNode,
106 checkedBackgroundColorVal.GetValue(), uncheckedBorderColorVal.GetValue(), indicatorColorVal.GetValue());
107 return panda::JSValueRef::Undefined(vm);
108 }
109
ResetRadioStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)110 ArkUINativeModuleValue RadioBridge::ResetRadioStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
111 {
112 EcmaVM* vm = runtimeCallInfo->GetVM();
113 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
114 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
115 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
116 GetArkUINodeModifiers()->getRadioModifier()->resetRadioStyle(nativeNode);
117 return panda::JSValueRef::Undefined(vm);
118 }
119
SetRadioWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)120 ArkUINativeModuleValue RadioBridge::SetRadioWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
121 {
122 EcmaVM* vm = runtimeCallInfo->GetVM();
123 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
124 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
125 Local<JSValueRef> widthArg = runtimeCallInfo->GetCallArgRef(1);
126 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
127 CalcDimension width;
128 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, widthArg, width)) {
129 GetArkUINodeModifiers()->getRadioModifier()->resetRadioWidth(nativeNode);
130 return panda::JSValueRef::Undefined(vm);
131 }
132 if (LessNotEqual(width.Value(), 0.0)) {
133 width.SetValue(0.0);
134 }
135 std::string widthCalc = width.CalcValue();
136 GetArkUINodeModifiers()->getRadioModifier()->setRadioWidth(
137 nativeNode, width.Value(), static_cast<int32_t>(width.Unit()), widthCalc.c_str());
138 return panda::JSValueRef::Undefined(vm);
139 }
140
ResetRadioWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)141 ArkUINativeModuleValue RadioBridge::ResetRadioWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
142 {
143 EcmaVM* vm = runtimeCallInfo->GetVM();
144 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
145 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); //0 is node arguments
146 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
147 GetArkUINodeModifiers()->getRadioModifier()->resetRadioWidth(nativeNode);
148 return panda::JSValueRef::Undefined(vm);
149 }
150
SetRadioHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)151 ArkUINativeModuleValue RadioBridge::SetRadioHeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
152 {
153 EcmaVM* vm = runtimeCallInfo->GetVM();
154 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
155 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
156 Local<JSValueRef> heightArg = runtimeCallInfo->GetCallArgRef(1);
157 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
158 CalcDimension height;
159 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, heightArg, height)) {
160 GetArkUINodeModifiers()->getRadioModifier()->resetRadioHeight(nativeNode);
161 return panda::JSValueRef::Undefined(vm);
162 }
163 if (LessNotEqual(height.Value(), 0.0)) {
164 height.SetValue(0.0);
165 }
166 std::string heightCalc = height.CalcValue();
167 GetArkUINodeModifiers()->getRadioModifier()->setRadioHeight(
168 nativeNode, height.Value(), static_cast<int32_t>(height.Unit()), heightCalc.c_str());
169 return panda::JSValueRef::Undefined(vm);
170 }
171
ResetRadioHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)172 ArkUINativeModuleValue RadioBridge::ResetRadioHeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
173 {
174 EcmaVM* vm = runtimeCallInfo->GetVM();
175 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
176 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); //0 is node arguments
177 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
178 GetArkUINodeModifiers()->getRadioModifier()->resetRadioHeight(nativeNode);
179 return panda::JSValueRef::Undefined(vm);
180 }
181
SetRadioSize(ArkUIRuntimeCallInfo * runtimeCallInfo)182 ArkUINativeModuleValue RadioBridge::SetRadioSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
183 {
184 EcmaVM* vm = runtimeCallInfo->GetVM();
185 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
186 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); //0 is node arguments
187 Local<JSValueRef> widthArg = runtimeCallInfo->GetCallArgRef(1); //1 is width value
188 Local<JSValueRef> heightArg = runtimeCallInfo->GetCallArgRef(2); //2 is height value
189 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
190
191 CalcDimension width;
192 CalcDimension height;
193 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, widthArg, width)) {
194 GetArkUINodeModifiers()->getRadioModifier()->resetRadioWidth(nativeNode);
195 } else {
196 std::string widthCalc = width.CalcValue();
197 if (LessNotEqual(width.Value(), 0.0)) {
198 width.SetValue(0.0);
199 }
200 GetArkUINodeModifiers()->getRadioModifier()->setRadioWidth(
201 nativeNode, width.Value(), static_cast<int>(width.Unit()), widthCalc.c_str());
202 }
203 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, heightArg, height)) {
204 GetArkUINodeModifiers()->getRadioModifier()->resetRadioHeight(nativeNode);
205 } else {
206 std::string heightCalc = height.CalcValue();
207 if (LessNotEqual(height.Value(), 0.0)) {
208 height.SetValue(0.0);
209 }
210 GetArkUINodeModifiers()->getRadioModifier()->setRadioHeight(
211 nativeNode, height.Value(), static_cast<int>(height.Unit()), heightCalc.c_str());
212 }
213 return panda::JSValueRef::Undefined(vm);
214 }
215
ResetRadioSize(ArkUIRuntimeCallInfo * runtimeCallInfo)216 ArkUINativeModuleValue RadioBridge::ResetRadioSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
217 {
218 EcmaVM* vm = runtimeCallInfo->GetVM();
219 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
220 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); //0 is node arguments
221 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
222 GetArkUINodeModifiers()->getRadioModifier()->resetRadioSize(nativeNode);
223 return panda::JSValueRef::Undefined(vm);
224 }
225
SetRadioHoverEffect(ArkUIRuntimeCallInfo * runtimeCallInfo)226 ArkUINativeModuleValue RadioBridge::SetRadioHoverEffect(ArkUIRuntimeCallInfo* runtimeCallInfo)
227 {
228 EcmaVM* vm = runtimeCallInfo->GetVM();
229 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
230 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); //0 is node arguments
231 Local<JSValueRef> valueArg = runtimeCallInfo->GetCallArgRef(1); //1 is Jsvalue
232 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
233
234 if (valueArg->IsUndefined() || !valueArg->IsNumber()) {
235 GetArkUINodeModifiers()->getRadioModifier()->resetRadioHoverEffect(nativeNode);
236 return panda::JSValueRef::Undefined(vm);
237 }
238 int32_t intValue = valueArg->Int32Value(vm);
239 GetArkUINodeModifiers()->getRadioModifier()->setRadioHoverEffect(nativeNode, intValue);
240 return panda::JSValueRef::Undefined(vm);
241 }
242
ResetRadioHoverEffect(ArkUIRuntimeCallInfo * runtimeCallInfo)243 ArkUINativeModuleValue RadioBridge::ResetRadioHoverEffect(ArkUIRuntimeCallInfo* runtimeCallInfo)
244 {
245 EcmaVM* vm = runtimeCallInfo->GetVM();
246 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
247 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); //0 is node arguments
248 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
249 GetArkUINodeModifiers()->getRadioModifier()->resetRadioHoverEffect(nativeNode);
250 return panda::JSValueRef::Undefined(vm);
251 }
252
SetRadioPadding(ArkUIRuntimeCallInfo * runtimeCallInfo)253 ArkUINativeModuleValue RadioBridge::SetRadioPadding(ArkUIRuntimeCallInfo *runtimeCallInfo)
254 {
255 EcmaVM *vm = runtimeCallInfo->GetVM();
256 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
257 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); //0 is node arguments
258 Local<JSValueRef> topArg = runtimeCallInfo->GetCallArgRef(1); //1 is top arguments
259 Local<JSValueRef> rightArg = runtimeCallInfo->GetCallArgRef(2); //2 is right arguments
260 Local<JSValueRef> bottomArg = runtimeCallInfo->GetCallArgRef(3); //3 is bottom arguments
261 Local<JSValueRef> leftArg = runtimeCallInfo->GetCallArgRef(4); //4 is left arguments
262 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
263
264 struct ArkUISizeType top = { 0.0, static_cast<int8_t>(DimensionUnit::VP), nullptr };
265 struct ArkUISizeType right = { 0.0, static_cast<int8_t>(DimensionUnit::VP), nullptr };
266 struct ArkUISizeType bottom = { 0.0, static_cast<int8_t>(DimensionUnit::VP), nullptr };
267 struct ArkUISizeType left = { 0.0, static_cast<int8_t>(DimensionUnit::VP), nullptr };
268
269 CalcDimension topDimen(0, DimensionUnit::VP);
270 CalcDimension rightDimen(0, DimensionUnit::VP);
271 CalcDimension bottomDimen(0, DimensionUnit::VP);
272 CalcDimension leftDimen(0, DimensionUnit::VP);
273 ArkTSUtils::ParsePadding(vm, topArg, topDimen, top);
274 ArkTSUtils::ParsePadding(vm, rightArg, rightDimen, right);
275 ArkTSUtils::ParsePadding(vm, bottomArg, bottomDimen, bottom);
276 ArkTSUtils::ParsePadding(vm, leftArg, leftDimen, left);
277 GetArkUINodeModifiers()->getRadioModifier()->setRadioPadding(nativeNode, &top, &right, &bottom, &left);
278
279 return panda::JSValueRef::Undefined(vm);
280 }
281
ResetRadioPadding(ArkUIRuntimeCallInfo * runtimeCallInfo)282 ArkUINativeModuleValue RadioBridge::ResetRadioPadding(ArkUIRuntimeCallInfo *runtimeCallInfo)
283 {
284 EcmaVM *vm = runtimeCallInfo->GetVM();
285 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
286 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); //0 is node arguments
287 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
288 GetArkUINodeModifiers()->getRadioModifier()->resetRadioPadding(nativeNode);
289 return panda::JSValueRef::Undefined(vm);
290 }
291
SetRadioResponseRegion(ArkUIRuntimeCallInfo * runtimeCallInfo)292 ArkUINativeModuleValue RadioBridge::SetRadioResponseRegion(ArkUIRuntimeCallInfo* runtimeCallInfo)
293 {
294 EcmaVM* vm = runtimeCallInfo->GetVM();
295 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
296 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); //0 is node arguments
297 Local<JSValueRef> valueArg = runtimeCallInfo->GetCallArgRef(1); //1 is JsValue
298 Local<JSValueRef> lengthArg = runtimeCallInfo->GetCallArgRef(2); //2 is length arguments
299 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
300 uint32_t length = static_cast<uint32_t>(lengthArg->Int32Value(vm));
301 ArkUI_Float32 regionArray[length];
302 int32_t regionUnits[length];
303 if (!ArkTSUtils::ParseResponseRegion(vm, valueArg, regionArray, regionUnits, length)) {
304 GetArkUINodeModifiers()->getRadioModifier()->resetRadioResponseRegion(nativeNode);
305 return panda::JSValueRef::Undefined(vm);
306 }
307 GetArkUINodeModifiers()->getRadioModifier()->setRadioResponseRegion(
308 nativeNode, regionArray, regionUnits, length);
309 return panda::JSValueRef::Undefined(vm);
310 }
311
ResetRadioResponseRegion(ArkUIRuntimeCallInfo * runtimeCallInfo)312 ArkUINativeModuleValue RadioBridge::ResetRadioResponseRegion(ArkUIRuntimeCallInfo* runtimeCallInfo)
313 {
314 EcmaVM* vm = runtimeCallInfo->GetVM();
315 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
316 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0); //0 is node arguments
317 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
318 GetArkUINodeModifiers()->getRadioModifier()->resetRadioResponseRegion(nativeNode);
319 return panda::JSValueRef::Undefined(vm);
320 }
321
SetContentModifierBuilder(ArkUIRuntimeCallInfo * runtimeCallInfo)322 ArkUINativeModuleValue RadioBridge::SetContentModifierBuilder(ArkUIRuntimeCallInfo* runtimeCallInfo)
323 {
324 EcmaVM* vm = runtimeCallInfo->GetVM();
325 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
326 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
327 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
328 auto* frameNode = reinterpret_cast<FrameNode*>(firstArg->ToNativePointer(vm)->Value());
329 if (!secondArg->IsObject(vm)) {
330 RadioModelNG::SetBuilderFunc(frameNode, nullptr);
331 return panda::JSValueRef::Undefined(vm);
332 }
333 panda::CopyableGlobal<panda::ObjectRef> obj(vm, secondArg);
334 RadioModelNG::SetBuilderFunc(frameNode,
335 [vm, frameNode, obj = std::move(obj), containerId = Container::CurrentId()](
336 RadioConfiguration config) -> RefPtr<FrameNode> {
337 ContainerScope scope(containerId);
338 auto context = ArkTSUtils::GetContext(vm);
339 CHECK_EQUAL_RETURN(context->IsUndefined(), true, nullptr);
340 const char* keysOfRadio[] = { "value", "checked", "enabled", "triggerChange"};
341 Local<JSValueRef> valuesOfRadio[] = { panda::StringRef::NewFromUtf8(vm, config.value_.c_str()),
342 panda::BooleanRef::New(vm, config.checked_), panda::BooleanRef::New(vm, config.enabled_),
343 panda::FunctionRef::New(vm, JsRadioChangeCallback)};
344 auto radio = panda::ObjectRef::NewWithNamedProperties(vm,
345 ArraySize(keysOfRadio), keysOfRadio, valuesOfRadio);
346 radio->SetNativePointerFieldCount(vm, 1);
347 radio->SetNativePointerField(vm, 0, static_cast<void*>(frameNode));
348 panda::Local<panda::JSValueRef> params[NUM_2] = { context, radio };
349 LocalScope pandaScope(vm);
350 panda::TryCatch trycatch(vm);
351 auto jsObject = obj.ToLocal();
352 auto makeFunc = jsObject->Get(vm, panda::StringRef::NewFromUtf8(vm, "makeContentModifierNode"));
353 CHECK_EQUAL_RETURN(makeFunc->IsFunction(vm), false, nullptr);
354 panda::Local<panda::FunctionRef> func = makeFunc;
355 auto result = func->Call(vm, jsObject, params, NUM_2);
356 JSNApi::ExecutePendingJob(vm);
357 CHECK_EQUAL_RETURN(result.IsEmpty() || trycatch.HasCaught() || !result->IsObject(vm), true, nullptr);
358 auto resultObj = result->ToObject(vm);
359 panda::Local<panda::JSValueRef> nodeptr =
360 resultObj->Get(vm, panda::StringRef::NewFromUtf8(vm, RADIO_BUILDER_NODEPTR_OF_UINODE));
361 CHECK_EQUAL_RETURN(nodeptr.IsEmpty() || nodeptr->IsUndefined() || nodeptr->IsNull(), true, nullptr);
362 auto* frameNode = reinterpret_cast<FrameNode*>(nodeptr->ToNativePointer(vm)->Value());
363 CHECK_NULL_RETURN(frameNode, nullptr);
364 return AceType::Claim(frameNode);
365 });
366 return panda::JSValueRef::Undefined(vm);
367 }
368
SetRadioOptions(ArkUIRuntimeCallInfo * runtimeCallInfo)369 ArkUINativeModuleValue RadioBridge::SetRadioOptions(ArkUIRuntimeCallInfo* runtimeCallInfo)
370 {
371 EcmaVM* vm = runtimeCallInfo->GetVM();
372 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
373 CHECK_EQUAL_RETURN(runtimeCallInfo->GetArgsNumber() != NUM_4, true, panda::JSValueRef::Undefined(vm));
374 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
375 Local<JSValueRef> valueArg = runtimeCallInfo->GetCallArgRef(NUM_1);
376 Local<JSValueRef> groupArg = runtimeCallInfo->GetCallArgRef(NUM_2);
377 Local<JSValueRef> indicatorTypeArg = runtimeCallInfo->GetCallArgRef(NUM_3);
378 std::string value;
379 std::string group;
380 auto indicatorType = RadioIndicatorType::TICK;
381
382 if (!valueArg.IsNull() && valueArg->IsString(vm)) {
383 value = valueArg->ToString(vm)->ToString(vm);
384 }
385 if (!groupArg.IsNull() && groupArg->IsString(vm)) {
386 group = groupArg->ToString(vm)->ToString(vm);
387 }
388 if (!indicatorTypeArg.IsNull() && indicatorTypeArg->IsNumber()) {
389 indicatorType = static_cast<RadioIndicatorType>(indicatorTypeArg->Int32Value(vm));
390 if (indicatorType == RadioIndicatorType::CUSTOM) {
391 indicatorType = RadioIndicatorType::TICK;
392 }
393 }
394 if (!firstArg.IsNull() && !firstArg->IsUndefined()) {
395 auto pointer = firstArg->ToNativePointer(vm);
396 CHECK_EQUAL_RETURN(pointer.IsEmpty(), true, panda::JSValueRef::Undefined(vm));
397 auto nativeNode = nodePtr(pointer->Value());
398 CHECK_NULL_RETURN(nativeNode, panda::JSValueRef::Undefined(vm));
399 GetArkUINodeModifiers()->getRadioModifier()->setRadioOptions(
400 nativeNode, value.c_str(), group.c_str(), static_cast<int32_t>(indicatorType));
401 }
402 return panda::JSValueRef::Undefined(vm);
403 }
404
ResetRadioOnChange(ArkUIRuntimeCallInfo * runtimeCallInfo)405 ArkUINativeModuleValue RadioBridge::ResetRadioOnChange(ArkUIRuntimeCallInfo* runtimeCallInfo)
406 {
407 EcmaVM* vm = runtimeCallInfo->GetVM();
408 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
409 Local<JSValueRef> nativeNodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
410 auto nativeNode = nodePtr(nativeNodeArg->ToNativePointer(vm)->Value());
411 GetArkUINodeModifiers()->getRadioModifier()->resetRadioOnChange(nativeNode);
412 return panda::JSValueRef::Undefined(vm);
413 }
414
SetRadioOnChange(ArkUIRuntimeCallInfo * runtimeCallInfo)415 ArkUINativeModuleValue RadioBridge::SetRadioOnChange(ArkUIRuntimeCallInfo* runtimeCallInfo)
416 {
417 EcmaVM* vm = runtimeCallInfo->GetVM();
418 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
419 int32_t argsNumber = runtimeCallInfo->GetArgsNumber();
420 if (argsNumber != NUM_2) {
421 return panda::JSValueRef::Undefined(vm);
422 }
423 Local<JSValueRef> nativeNodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
424 Local<JSValueRef> callbackArg = runtimeCallInfo->GetCallArgRef(NUM_1);
425 auto nativeNode = nodePtr(nativeNodeArg->ToNativePointer(vm)->Value());
426 auto frameNode = reinterpret_cast<FrameNode*>(nativeNode);
427 CHECK_NULL_RETURN(frameNode, panda::NativePointerRef::New(vm, nullptr));
428 if (callbackArg->IsUndefined() || callbackArg->IsNull() || !callbackArg->IsFunction(vm)) {
429 GetArkUINodeModifiers()->getRadioModifier()->resetRadioOnChange(nativeNode);
430 return panda::JSValueRef::Undefined(vm);
431 }
432 panda::Local<panda::FunctionRef> func = callbackArg->ToObject(vm);
433 std::function<void(bool)> callback = [vm, frameNode, func = panda::CopyableGlobal(vm, func)](bool isOnchange) {
434 panda::LocalScope pandaScope(vm);
435 panda::TryCatch trycatch(vm);
436 PipelineContext::SetCallBackNode(AceType::WeakClaim(frameNode));
437 panda::Local<panda::JSValueRef> params[PARAM_ARR_LENGTH_1] = { panda::BooleanRef::New(vm, isOnchange) };
438 func->Call(vm, func.ToLocal(), params, PARAM_ARR_LENGTH_1);
439 };
440 GetArkUINodeModifiers()->getRadioModifier()->setRadioOnChange(nativeNode, reinterpret_cast<void*>(&callback));
441 return panda::JSValueRef::Undefined(vm);
442 }
443 }
444