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_button_bridge.h"
16 
17 #include <string>
18 
19 #include "base/geometry/dimension.h"
20 #include "core/components/common/properties/text_style.h"
21 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
22 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_common_bridge.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/pattern/button/button_model_ng.h"
25 #include "core/components_ng/pattern/button/button_request_data.h"
26 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
27 
28 namespace OHOS::Ace::NG {
29 const std::vector<TextOverflow> TEXT_OVERFLOWS = { TextOverflow::NONE, TextOverflow::CLIP, TextOverflow::ELLIPSIS,
30     TextOverflow::MARQUEE };
31 const std::vector<Ace::FontStyle> FONT_STYLES = { Ace::FontStyle::NORMAL, Ace::FontStyle::ITALIC };
32 const std::vector<TextHeightAdaptivePolicy> HEIGHT_ADAPTIVE_POLICY = { TextHeightAdaptivePolicy::MAX_LINES_FIRST,
33     TextHeightAdaptivePolicy::MIN_FONT_SIZE_FIRST, TextHeightAdaptivePolicy::LAYOUT_CONSTRAINT_FIRST };
34 const std::string DEFAULT_FONT_WEIGHT = "400";
35 const std::string NONE_FONT_FAMILY = "NoneFontFamily";
36 constexpr int32_t DEFAULT_BUTTON_TYPE = 1;
37 constexpr bool DEFAULT_STATE_EFFECT = true;
38 constexpr uint32_t DEFAULT_CONTROL_SIZE = 1;
39 constexpr uint32_t DEFAULT_STYLE = 1;
40 constexpr uint32_t DEFAULT_ROLE = 0;
41 constexpr int32_t CALL_ARG_0 = 0;
42 constexpr int32_t CALL_ARG_1 = 1;
43 constexpr int32_t CALL_ARG_2 = 2;
44 constexpr int32_t CALL_ARG_3 = 3;
45 constexpr int32_t CALL_ARG_4 = 4;
46 constexpr int32_t CALL_ARG_5 = 5;
47 constexpr int32_t MAX_LINES_ARG_2 = 2;
48 constexpr int32_t MIN_FONT_SIZE_ARG_3 = 3;
49 constexpr int32_t MAX_FONT_SIZE_ARG_4 = 4;
50 constexpr int32_t ADAPT_HEIGHT_ARG_5 = 5;
51 constexpr int32_t FONT_SIZE_ARG_6 = 6;
52 constexpr int32_t FONT_WEIGHT_ARG_7 = 7;
53 constexpr int32_t FONT_STYLE_ARG_8 = 8;
54 constexpr int32_t FONT_FAMILY_ARG_9 = 9;
55 const char* BUTTON_NODEPTR_OF_UINODE = "nodePtr_";
JsButtonClickCallback(panda::JsiRuntimeCallInfo * runtimeCallInfo)56 panda::Local<panda::JSValueRef> JsButtonClickCallback(panda::JsiRuntimeCallInfo* runtimeCallInfo)
57 {
58     auto vm = runtimeCallInfo->GetVM();
59     int32_t argc = static_cast<int32_t>(runtimeCallInfo->GetArgsNumber());
60     if (argc != CALL_ARG_2) {
61         return panda::JSValueRef::Undefined(vm);
62     }
63     auto firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
64     auto secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
65     if (!firstArg->IsNumber() || !secondArg->IsNumber()) {
66         return panda::JSValueRef::Undefined(vm);
67     }
68     double xPos = firstArg->ToNumber(vm)->Value();
69     double yPos = secondArg->ToNumber(vm)->Value();
70     auto ref = runtimeCallInfo->GetThisRef();
71     auto obj = ref->ToObject(vm);
72     if (obj->GetNativePointerFieldCount(vm) < CALL_ARG_1) {
73         return panda::JSValueRef::Undefined(vm);
74     }
75     auto frameNode = static_cast<FrameNode*>(obj->GetNativePointerField(vm, 0));
76     CHECK_NULL_RETURN(frameNode, panda::JSValueRef::Undefined(vm));
77     ButtonModelNG::TriggerClick(frameNode, xPos, yPos);
78     return panda::JSValueRef::Undefined(vm);
79 }
80 
SetCreateWithLabel(ArkUIRuntimeCallInfo * runtimeCallInfo)81 ArkUINativeModuleValue ButtonBridge::SetCreateWithLabel(ArkUIRuntimeCallInfo* runtimeCallInfo)
82 {
83     EcmaVM* vm = runtimeCallInfo->GetVM();
84     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
85     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
86     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
87     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
88     bool createWithLabel = false;
89     if (secondArg->IsBoolean()) {
90         createWithLabel = secondArg->ToBoolean(vm)->Value();
91     }
92     GetArkUINodeModifiers()->getButtonModifier()->setCreateWithLabel(nativeNode, createWithLabel);
93     return panda::JSValueRef::Undefined(vm);
94 }
95 
SetLabel(ArkUIRuntimeCallInfo * runtimeCallInfo)96 ArkUINativeModuleValue ButtonBridge::SetLabel(ArkUIRuntimeCallInfo* runtimeCallInfo)
97 {
98     EcmaVM* vm = runtimeCallInfo->GetVM();
99     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
100     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
101     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
102     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
103     std::string value;
104     ArkTSUtils::ParseJsString(vm, secondArg, value);
105     GetArkUINodeModifiers()->getButtonModifier()->setButtonLabelWithCheck(nativeNode, value.c_str());
106     return panda::JSValueRef::Undefined(vm);
107 }
108 
ResetLabel(ArkUIRuntimeCallInfo * runtimeCallInfo)109 ArkUINativeModuleValue ButtonBridge::ResetLabel(ArkUIRuntimeCallInfo* runtimeCallInfo)
110 {
111     EcmaVM* vm = runtimeCallInfo->GetVM();
112     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
113     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
114     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
115     GetArkUINodeModifiers()->getButtonModifier()->resetButtonLabelWithCheck(nativeNode);
116     return panda::JSValueRef::Undefined(vm);
117 }
118 
SetOptions(ArkUIRuntimeCallInfo * runtimeCallInfo)119 ArkUINativeModuleValue ButtonBridge::SetOptions(ArkUIRuntimeCallInfo* runtimeCallInfo)
120 {
121     EcmaVM* vm = runtimeCallInfo->GetVM();
122     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
123     Local<JSValueRef> typeArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
124     Local<JSValueRef> stateEffectArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_2);
125     Local<JSValueRef> buttonStyleArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_3);
126     Local<JSValueRef> controlSizeArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_4);
127     Local<JSValueRef> roleArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_5);
128     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
129     int32_t type = DEFAULT_BUTTON_TYPE;
130     if (typeArg->IsNumber()) {
131         type = typeArg->Int32Value(vm);
132     }
133     bool stateEffect = DEFAULT_STATE_EFFECT;
134     if (stateEffectArg->IsBoolean()) {
135         stateEffect = stateEffectArg->ToBoolean(vm)->Value();
136     }
137     uint32_t controlSize = DEFAULT_CONTROL_SIZE;
138     if (controlSizeArg->IsNumber()) {
139         controlSize = controlSizeArg->Uint32Value(vm);
140     }
141     uint32_t buttonStyle = DEFAULT_STYLE;
142     if (buttonStyleArg->IsNumber()) {
143         buttonStyle = buttonStyleArg->Uint32Value(vm);
144     }
145     uint32_t buttonRole = DEFAULT_ROLE;
146     if (roleArg->IsNumber()) {
147         buttonRole = roleArg->Uint32Value(vm);
148     }
149     GetArkUINodeModifiers()->getButtonModifier()->setButtonType(nativeNode, type);
150     GetArkUINodeModifiers()->getButtonModifier()->setButtonStateEffect(nativeNode, stateEffect);
151     GetArkUINodeModifiers()->getButtonModifier()->setButtonControlSize(nativeNode, controlSize);
152     GetArkUINodeModifiers()->getButtonModifier()->setButtonStyle(nativeNode, buttonStyle);
153     GetArkUINodeModifiers()->getButtonModifier()->setButtonRole(nativeNode, buttonRole);
154     GetArkUINodeModifiers()->getButtonModifier()->setButtonOptions(nativeNode, buttonStyle, buttonRole);
155     return panda::JSValueRef::Undefined(vm);
156 }
157 
ResetOptions(ArkUIRuntimeCallInfo * runtimeCallInfo)158 ArkUINativeModuleValue ButtonBridge::ResetOptions(ArkUIRuntimeCallInfo* runtimeCallInfo)
159 {
160     EcmaVM* vm = runtimeCallInfo->GetVM();
161     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
162     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
163     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
164     GetArkUINodeModifiers()->getButtonModifier()->resetButtonOptions(nativeNode);
165     return panda::JSValueRef::Undefined(vm);
166 }
167 
SetType(ArkUIRuntimeCallInfo * runtimeCallInfo)168 ArkUINativeModuleValue ButtonBridge::SetType(ArkUIRuntimeCallInfo* runtimeCallInfo)
169 {
170     EcmaVM* vm = runtimeCallInfo->GetVM();
171     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
172     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
173     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
174     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
175     int type = secondArg->Int32Value(vm);
176     GetArkUINodeModifiers()->getButtonModifier()->setButtonType(nativeNode, type);
177     return panda::JSValueRef::Undefined(vm);
178 }
179 
ResetType(ArkUIRuntimeCallInfo * runtimeCallInfo)180 ArkUINativeModuleValue ButtonBridge::ResetType(ArkUIRuntimeCallInfo* runtimeCallInfo)
181 {
182     EcmaVM* vm = runtimeCallInfo->GetVM();
183     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
184     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
185     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
186     GetArkUINodeModifiers()->getButtonModifier()->resetButtonType(nativeNode);
187     return panda::JSValueRef::Undefined(vm);
188 }
189 
SetStateEffect(ArkUIRuntimeCallInfo * runtimeCallInfo)190 ArkUINativeModuleValue ButtonBridge::SetStateEffect(ArkUIRuntimeCallInfo* runtimeCallInfo)
191 {
192     EcmaVM* vm = runtimeCallInfo->GetVM();
193     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
194     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
195     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
196     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
197     if (secondArg->IsBoolean()) {
198         bool stateEffect = secondArg->ToBoolean(vm)->Value();
199         GetArkUINodeModifiers()->getButtonModifier()->setButtonStateEffect(nativeNode, stateEffect);
200     } else {
201         GetArkUINodeModifiers()->getButtonModifier()->resetButtonStateEffect(nativeNode);
202     }
203     return panda::JSValueRef::Undefined(vm);
204 }
205 
ResetStateEffect(ArkUIRuntimeCallInfo * runtimeCallInfo)206 ArkUINativeModuleValue ButtonBridge::ResetStateEffect(ArkUIRuntimeCallInfo* runtimeCallInfo)
207 {
208     EcmaVM* vm = runtimeCallInfo->GetVM();
209     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
210     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
211     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
212     GetArkUINodeModifiers()->getButtonModifier()->resetButtonStateEffect(nativeNode);
213     return panda::JSValueRef::Undefined(vm);
214 }
215 
SetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)216 ArkUINativeModuleValue ButtonBridge::SetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
217 {
218     EcmaVM* vm = runtimeCallInfo->GetVM();
219     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
220     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
221     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
222     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
223     Color color;
224     if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color)) {
225         GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontColor(nativeNode);
226     } else {
227         GetArkUINodeModifiers()->getButtonModifier()->setButtonFontColor(nativeNode, color.GetValue());
228     }
229     return panda::JSValueRef::Undefined(vm);
230 }
231 
ResetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)232 ArkUINativeModuleValue ButtonBridge::ResetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
233 {
234     EcmaVM* vm = runtimeCallInfo->GetVM();
235     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
236     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
237     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
238     GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontColor(nativeNode);
239     return panda::JSValueRef::Undefined(vm);
240 }
241 
SetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)242 ArkUINativeModuleValue ButtonBridge::SetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
243 {
244     EcmaVM* vm = runtimeCallInfo->GetVM();
245     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
246     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
247     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
248     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
249     Ace::CalcDimension fontSize;
250     if (ArkTSUtils::ParseJsDimensionVpNG(vm, secondArg, fontSize) && fontSize.Unit() != DimensionUnit::PERCENT &&
251         GreatOrEqual(fontSize.Value(), 0.0)) {
252         ArkTSUtils::ParseJsDimensionFp(vm, secondArg, fontSize);
253     } else {
254         auto pipeline = PipelineBase::GetCurrentContext();
255         CHECK_NULL_RETURN(pipeline, panda::NativePointerRef::New(vm, nullptr));
256         auto buttonTheme = pipeline->GetTheme<ButtonTheme>();
257         CHECK_NULL_RETURN(buttonTheme, panda::NativePointerRef::New(vm, nullptr));
258         fontSize = buttonTheme->GetTextStyle().GetFontSize();
259     }
260     int fontSizeUnit = static_cast<int>(fontSize.Unit());
261     GetArkUINodeModifiers()->getButtonModifier()->setButtonFontSize(nativeNode, fontSize.Value(), fontSizeUnit);
262     return panda::JSValueRef::Undefined(vm);
263 }
264 
ResetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)265 ArkUINativeModuleValue ButtonBridge::ResetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
266 {
267     EcmaVM* vm = runtimeCallInfo->GetVM();
268     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
269     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
270     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
271     GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontSize(nativeNode);
272     return panda::JSValueRef::Undefined(vm);
273 }
274 
SetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)275 ArkUINativeModuleValue ButtonBridge::SetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
276 {
277     EcmaVM* vm = runtimeCallInfo->GetVM();
278     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
279     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
280     Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
281     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
282     std::string fontWeight = DEFAULT_FONT_WEIGHT;
283     if (!fontWeightArg->IsNull()) {
284         if (fontWeightArg->IsNumber()) {
285             fontWeight = std::to_string(fontWeightArg->Int32Value(vm));
286         } else if (fontWeightArg->IsString(vm)) {
287             // enum FontWeight is string.
288             fontWeight = fontWeightArg->ToString(vm)->ToString(vm);
289         }
290     }
291     GetArkUINodeModifiers()->getButtonModifier()->setButtonFontWeight(nativeNode, fontWeight.c_str());
292     return panda::JSValueRef::Undefined(vm);
293 }
294 
ResetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)295 ArkUINativeModuleValue ButtonBridge::ResetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
296 {
297     EcmaVM* vm = runtimeCallInfo->GetVM();
298     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
299     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
300     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
301     GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontWeight(nativeNode);
302     return panda::JSValueRef::Undefined(vm);
303 }
304 
SetFontStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)305 ArkUINativeModuleValue ButtonBridge::SetFontStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
306 {
307     EcmaVM* vm = runtimeCallInfo->GetVM();
308     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
309     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
310     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
311     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
312     int fontStyle = secondArg->Int32Value(vm);
313     GetArkUINodeModifiers()->getButtonModifier()->setButtonFontStyle(nativeNode, fontStyle);
314     return panda::JSValueRef::Undefined(vm);
315 }
316 
ResetFontStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)317 ArkUINativeModuleValue ButtonBridge::ResetFontStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
318 {
319     EcmaVM* vm = runtimeCallInfo->GetVM();
320     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
321     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
322     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
323     GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontStyle(nativeNode);
324     return panda::JSValueRef::Undefined(vm);
325 }
326 
SetFontFamily(ArkUIRuntimeCallInfo * runtimeCallInfo)327 ArkUINativeModuleValue ButtonBridge::SetFontFamily(ArkUIRuntimeCallInfo* runtimeCallInfo)
328 {
329     EcmaVM* vm = runtimeCallInfo->GetVM();
330     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
331     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
332     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
333     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
334     std::string fontFamily;
335     if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, secondArg, fontFamily)) {
336         GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontFamily(nativeNode);
337         return panda::JSValueRef::Undefined(vm);
338     }
339 
340     GetArkUINodeModifiers()->getButtonModifier()->setButtonFontFamily(nativeNode, fontFamily.c_str());
341     return panda::JSValueRef::Undefined(vm);
342 }
343 
ResetFontFamily(ArkUIRuntimeCallInfo * runtimeCallInfo)344 ArkUINativeModuleValue ButtonBridge::ResetFontFamily(ArkUIRuntimeCallInfo* runtimeCallInfo)
345 {
346     EcmaVM* vm = runtimeCallInfo->GetVM();
347     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
348     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
349     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
350     GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontFamily(nativeNode);
351     return panda::JSValueRef::Undefined(vm);
352 }
353 
SetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)354 ArkUINativeModuleValue ButtonBridge::SetBackgroundColor(ArkUIRuntimeCallInfo *runtimeCallInfo)
355 {
356     EcmaVM *vm = runtimeCallInfo->GetVM();
357     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
358     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
359     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
360     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
361     Color color;
362     if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color)) {
363         GetArkUINodeModifiers()->getButtonModifier()->resetButtonBackgroundColor(nativeNode);
364     } else {
365         GetArkUINodeModifiers()->getButtonModifier()->setButtonBackgroundColor(nativeNode, color.GetValue());
366     }
367     return panda::JSValueRef::Undefined(vm);
368 }
369 
ResetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)370 ArkUINativeModuleValue ButtonBridge::ResetBackgroundColor(ArkUIRuntimeCallInfo *runtimeCallInfo)
371 {
372     EcmaVM *vm = runtimeCallInfo->GetVM();
373     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
374     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
375     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
376     GetArkUINodeModifiers()->getButtonModifier()->resetButtonBackgroundColor(nativeNode);
377     return panda::JSValueRef::Undefined(vm);
378 }
379 
PushValuesVector(const std::optional<int32_t> & value,std::vector<int32_t> & valuesVector)380 void ButtonBridge::PushValuesVector(const std::optional<int32_t>& value, std::vector<int32_t>& valuesVector)
381 {
382     valuesVector.push_back(static_cast<int32_t>(value.has_value()));
383     if (value.has_value()) {
384         valuesVector.push_back(static_cast<int32_t>(value.value()));
385     } else {
386         valuesVector.push_back(0);
387     }
388 }
389 
PutButtonValuesParameters(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,std::vector<int32_t> & valuesVector)390 void ButtonBridge::PutButtonValuesParameters(
391     ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm, std::vector<int32_t>& valuesVector)
392 {
393     std::optional<int32_t> overflowOptional = std::nullopt;
394     Local<JSValueRef> overflowArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
395     if (!overflowArg->IsNull() && overflowArg->IsNumber()) {
396         auto overflow = overflowArg->Int32Value(vm);
397         if (overflow >= 0 && overflow < static_cast<int32_t>(TEXT_OVERFLOWS.size())) {
398             overflowOptional = overflow;
399         }
400     }
401     PushValuesVector(overflowOptional, valuesVector);
402 
403     std::optional<int32_t> maxLinesOptional = std::nullopt;
404     Local<JSValueRef> maxLinesArg = runtimeCallInfo->GetCallArgRef(MAX_LINES_ARG_2);
405     if (!maxLinesArg->IsNull() && maxLinesArg->IsNumber()) {
406         auto maxLines = maxLinesArg->Int32Value(vm);
407         maxLinesOptional = Positive(maxLines) ? maxLines : 1;
408     }
409     PushValuesVector(maxLinesOptional, valuesVector);
410 
411     std::optional<int32_t> adaptHeightOptional = std::nullopt;
412     Local<JSValueRef> adaptHeightArg = runtimeCallInfo->GetCallArgRef(ADAPT_HEIGHT_ARG_5);
413     if (!adaptHeightArg->IsNull() && adaptHeightArg->IsNumber()) {
414         auto adaptHeight = adaptHeightArg->Int32Value(vm);
415         if (adaptHeight >= 0 && adaptHeight < static_cast<double>(HEIGHT_ADAPTIVE_POLICY.size())) {
416             adaptHeightOptional = adaptHeight;
417         }
418     }
419     PushValuesVector(adaptHeightOptional, valuesVector);
420 
421     std::optional<int32_t> fontStyleOptional = std::nullopt;
422     Local<JSValueRef> fontStyleArg = runtimeCallInfo->GetCallArgRef(FONT_STYLE_ARG_8);
423     if (!fontStyleArg->IsNull() && fontStyleArg->IsNumber()) {
424         auto fontStyle = fontStyleArg->Int32Value(vm);
425         if (fontStyle >= 0 && fontStyle < static_cast<int32_t>(FONT_STYLES.size())) {
426             fontStyleOptional = fontStyle;
427         }
428     }
429     PushValuesVector(fontStyleOptional, valuesVector);
430 }
431 
PushDimensionVector(const std::optional<Dimension> & valueDim,std::vector<ArkUI_Float32> & dimensions)432 void ButtonBridge::PushDimensionVector(const std::optional<Dimension>& valueDim, std::vector<ArkUI_Float32>& dimensions)
433 {
434     dimensions.push_back(static_cast<ArkUI_Float32>(valueDim.has_value()));
435     if (valueDim.has_value()) {
436         dimensions.push_back(static_cast<ArkUI_Float32>(valueDim.value().Value()));
437         dimensions.push_back(static_cast<ArkUI_Float32>(valueDim.value().Unit()));
438     } else {
439         dimensions.push_back(0);
440         dimensions.push_back(0);
441     }
442 }
443 
PushButtonDimension(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,std::vector<ArkUI_Float32> & fontSizesVector,int32_t argIndex)444 void ButtonBridge::PushButtonDimension(
445     ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm, std::vector<ArkUI_Float32>& fontSizesVector, int32_t argIndex)
446 {
447     Local<JSValueRef> arg = runtimeCallInfo->GetCallArgRef(argIndex);
448     std::optional<CalcDimension> dimensionOptional = std::nullopt;
449     CalcDimension parsedDimension;
450     if (ArkTSUtils::ParseJsDimensionFp(vm, arg, parsedDimension, false)) {
451         dimensionOptional = parsedDimension;
452     }
453     PushDimensionVector(dimensionOptional, fontSizesVector);
454 }
455 
PutButtonDimensionParameters(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,std::vector<ArkUI_Float32> & fontSizesVector)456 void ButtonBridge::PutButtonDimensionParameters(
457     ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm, std::vector<ArkUI_Float32>& fontSizesVector)
458 {
459     std::vector<int32_t> indexVector = { MIN_FONT_SIZE_ARG_3, MAX_FONT_SIZE_ARG_4, FONT_SIZE_ARG_6 };
460     for (size_t index = 0; index < indexVector.size(); index++) {
461         PushButtonDimension(runtimeCallInfo, vm, fontSizesVector, indexVector[index]);
462     }
463 }
464 
SetLabelStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)465 ArkUINativeModuleValue ButtonBridge::SetLabelStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
466 {
467     EcmaVM* vm = runtimeCallInfo->GetVM();
468     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
469     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
470     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
471     std::vector<ArkUI_Int32> valuesVector;
472     PutButtonValuesParameters(runtimeCallInfo, vm, valuesVector);
473     std::vector<ArkUI_Float32> fontSizesVector;
474     PutButtonDimensionParameters(runtimeCallInfo, vm, fontSizesVector);
475     std::vector<ArkUI_CharPtr> stringParameters;
476     Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(FONT_WEIGHT_ARG_7);
477     std::string fontWeight = DEFAULT_FONT_WEIGHT;
478     if (!fontWeightArg->IsNull()) {
479         if (fontWeightArg->IsNumber()) {
480             fontWeight = std::to_string(fontWeightArg->Int32Value(vm));
481         } else if (fontWeightArg->IsString(vm)) {
482             // enum FontWeight is sent as string.
483             fontWeight = fontWeightArg->ToString(vm)->ToString(vm);
484         }
485     }
486     stringParameters.push_back(fontWeight.c_str());
487 
488     Local<JSValueRef> fontFamilyArg = runtimeCallInfo->GetCallArgRef(FONT_FAMILY_ARG_9);
489     std::string strFontFamilies;
490     if (ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArg, strFontFamilies)) {
491         stringParameters.push_back(strFontFamilies.c_str());
492     } else {
493         strFontFamilies = NONE_FONT_FAMILY;
494         stringParameters.push_back(strFontFamilies.c_str());
495     }
496     std::vector<ArkUI_Uint32> dataCountVector;
497     dataCountVector.push_back(stringParameters.size());
498     dataCountVector.push_back(valuesVector.size());
499     dataCountVector.push_back(fontSizesVector.size());
500     GetArkUINodeModifiers()->getButtonModifier()->setButtonLabelStyle(
501         nativeNode, stringParameters.data(), valuesVector.data(), fontSizesVector.data(), dataCountVector.data());
502     return panda::JSValueRef::Undefined(vm);
503 }
504 
ResetLabelStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)505 ArkUINativeModuleValue ButtonBridge::ResetLabelStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
506 {
507     EcmaVM* vm = runtimeCallInfo->GetVM();
508     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
509     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
510     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
511     GetArkUINodeModifiers()->getButtonModifier()->resetButtonLabelStyle(nativeNode);
512     return panda::JSValueRef::Undefined(vm);
513 }
514 
ParseBorderRadius(EcmaVM * vm,const Local<JSValueRef> & args,std::optional<CalcDimension> & optionalDimension)515 void ParseBorderRadius(EcmaVM* vm, const Local<JSValueRef>& args, std::optional<CalcDimension>& optionalDimension)
516 {
517     CalcDimension valueDim;
518     if (!args->IsUndefined() && ArkTSUtils::ParseJsDimensionVp(vm, args, valueDim, false)) {
519         optionalDimension = valueDim;
520     }
521 }
522 
PushBorderRadiusVector(const std::optional<CalcDimension> & valueDim,std::vector<ArkUI_Float32> & options)523 void PushBorderRadiusVector(const std::optional<CalcDimension>& valueDim, std::vector<ArkUI_Float32> &options)
524 {
525     options.push_back(static_cast<ArkUI_Float32>(valueDim.has_value()));
526     if (valueDim.has_value()) {
527         options.push_back(static_cast<ArkUI_Float32>(valueDim.value().Value()));
528         options.push_back(static_cast<ArkUI_Float32>(valueDim.value().Unit()));
529     } else {
530         options.push_back(0);
531         options.push_back(0);
532     }
533 }
534 
SetButtonBorderRadius(ArkUIRuntimeCallInfo * runtimeCallInfo)535 ArkUINativeModuleValue ButtonBridge::SetButtonBorderRadius(ArkUIRuntimeCallInfo* runtimeCallInfo)
536 {
537     EcmaVM* vm = runtimeCallInfo->GetVM();
538     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
539     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
540     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
541     Local<JSValueRef> topLeftArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);     // topLeft value
542     Local<JSValueRef> topRightArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_2);    // topRight value
543     Local<JSValueRef> bottomLeftArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_3);  // bottomLeft value
544     Local<JSValueRef> bottomRightArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_4); // bottomRight value
545 
546     if (topLeftArgs->IsUndefined() && topRightArgs->IsUndefined() && bottomLeftArgs->IsUndefined() &&
547         bottomRightArgs->IsUndefined()) {
548         GetArkUINodeModifiers()->getButtonModifier()->resetButtonBorderRadius(nativeNode);
549         return panda::JSValueRef::Undefined(vm);
550     }
551 
552     std::optional<CalcDimension> radiusTopLeft;
553     std::optional<CalcDimension> radiusTopRight;
554     std::optional<CalcDimension> radiusBottomLeft;
555     std::optional<CalcDimension> radiusBottomRight;
556 
557     ParseBorderRadius(vm, topLeftArgs, radiusTopLeft);
558     ParseBorderRadius(vm, topRightArgs, radiusTopRight);
559     ParseBorderRadius(vm, bottomLeftArgs, radiusBottomLeft);
560     ParseBorderRadius(vm, bottomRightArgs, radiusBottomRight);
561 
562     std::vector<ArkUI_Float32> options;
563     PushBorderRadiusVector(radiusTopLeft, options);
564     PushBorderRadiusVector(radiusTopRight, options);
565     PushBorderRadiusVector(radiusBottomLeft, options);
566     PushBorderRadiusVector(radiusBottomRight, options);
567 
568     GetArkUINodeModifiers()->getButtonModifier()->setButtonBorderRadius(nativeNode, options.data(), options.size());
569     return panda::JSValueRef::Undefined(vm);
570 }
571 
ResetButtonBorderRadius(ArkUIRuntimeCallInfo * runtimeCallInfo)572 ArkUINativeModuleValue ButtonBridge::ResetButtonBorderRadius(ArkUIRuntimeCallInfo* runtimeCallInfo)
573 {
574     EcmaVM* vm = runtimeCallInfo->GetVM();
575     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
576     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
577     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
578     GetArkUINodeModifiers()->getButtonModifier()->resetButtonBorderRadius(nativeNode);
579     return panda::JSValueRef::Undefined(vm);
580 }
581 
SetButtonBorder(ArkUIRuntimeCallInfo * runtimeCallInfo)582 ArkUINativeModuleValue ButtonBridge::SetButtonBorder(ArkUIRuntimeCallInfo* runtimeCallInfo)
583 {
584     CommonBridge::SetBorder(runtimeCallInfo);
585     EcmaVM* vm = runtimeCallInfo->GetVM();
586     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
587     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
588     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
589 
590     Local<JSValueRef> topLeftArgs = runtimeCallInfo->GetCallArgRef(9);      // 9:topLeft value
591     Local<JSValueRef> topRightArgs = runtimeCallInfo->GetCallArgRef(10);    // 10:topRight value
592     Local<JSValueRef> bottomLeftArgs = runtimeCallInfo->GetCallArgRef(11);  // 11:bottomLeft value
593     Local<JSValueRef> bottomRightArgs = runtimeCallInfo->GetCallArgRef(12); // 12:bottomRight value
594     if (topLeftArgs->IsUndefined() && topRightArgs->IsUndefined() && bottomLeftArgs->IsUndefined() &&
595         bottomRightArgs->IsUndefined()) {
596         GetArkUINodeModifiers()->getButtonModifier()->resetButtonBorderRadius(nativeNode);
597         return panda::JSValueRef::Undefined(vm);
598     }
599 
600     std::optional<CalcDimension> radiusTopLeft;
601     std::optional<CalcDimension> radiusTopRight;
602     std::optional<CalcDimension> radiusBottomLeft;
603     std::optional<CalcDimension> radiusBottomRight;
604 
605     ParseBorderRadius(vm, topLeftArgs, radiusTopLeft);
606     ParseBorderRadius(vm, topRightArgs, radiusTopRight);
607     ParseBorderRadius(vm, bottomLeftArgs, radiusBottomLeft);
608     ParseBorderRadius(vm, bottomRightArgs, radiusBottomRight);
609 
610     std::vector<ArkUI_Float32> options;
611     PushBorderRadiusVector(radiusTopLeft, options);
612     PushBorderRadiusVector(radiusTopRight, options);
613     PushBorderRadiusVector(radiusBottomLeft, options);
614     PushBorderRadiusVector(radiusBottomRight, options);
615 
616     GetArkUINodeModifiers()->getButtonModifier()->setButtonBorderRadius(nativeNode, options.data(), options.size());
617     return panda::JSValueRef::Undefined(vm);
618 }
619 
ResetButtonBorder(ArkUIRuntimeCallInfo * runtimeCallInfo)620 ArkUINativeModuleValue ButtonBridge::ResetButtonBorder(ArkUIRuntimeCallInfo* runtimeCallInfo)
621 {
622     EcmaVM* vm = runtimeCallInfo->GetVM();
623     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
624     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
625     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
626     GetArkUINodeModifiers()->getCommonModifier()->resetBorder(nativeNode);
627     return panda::JSValueRef::Undefined(vm);
628 }
629 
SetButtonSize(ArkUIRuntimeCallInfo * runtimeCallInfo)630 ArkUINativeModuleValue ButtonBridge::SetButtonSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
631 {
632     EcmaVM* vm = runtimeCallInfo->GetVM();
633     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
634     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
635     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
636     Local<JSValueRef> widthArgs = runtimeCallInfo->GetCallArgRef(1); // 1:width value
637     Local<JSValueRef> heightArgs = runtimeCallInfo->GetCallArgRef(2); // 2:height value
638     CalcDimension width;
639     std::string widthStr;
640     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, widthArgs, width)) {
641         widthStr = "undefined";
642     } else {
643         ArkTSUtils::ParseJsDimensionVp(vm, widthArgs, width);
644         widthStr = std::to_string(width.Value());
645     }
646     CalcDimension height;
647     std::string heightStr;
648     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, heightArgs, height)) {
649         heightStr = "undefined";
650     } else {
651         heightStr = std::to_string(height.Value());
652     }
653     GetArkUINodeModifiers()->getButtonModifier()->setButtonSize(nativeNode, widthStr.c_str(),
654         static_cast<int32_t>(width.Unit()), heightStr.c_str(), static_cast<int32_t>(height.Unit()));
655     return panda::JSValueRef::Undefined(vm);
656 }
657 
SetButtonRole(ArkUIRuntimeCallInfo * runtimeCallInfo)658 ArkUINativeModuleValue ButtonBridge::SetButtonRole(ArkUIRuntimeCallInfo* runtimeCallInfo)
659 {
660     EcmaVM* vm = runtimeCallInfo->GetVM();
661     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
662     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
663     Local<JSValueRef> buttonRoleArgs = runtimeCallInfo->GetCallArgRef(1);
664     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
665     if (buttonRoleArgs->IsUndefined() || buttonRoleArgs->IsNull() || !buttonRoleArgs->IsNumber()) {
666         GetArkUINodeModifiers()->getButtonModifier()->resetButtonRole(nativeNode);
667         return panda::JSValueRef::Undefined(vm);
668     }
669     uint32_t buttonRole = buttonRoleArgs->Uint32Value(vm);
670     GetArkUINodeModifiers()->getButtonModifier()->setButtonRole(nativeNode, buttonRole);
671     return panda::JSValueRef::Undefined(vm);
672 }
673 
ResetButtonRole(ArkUIRuntimeCallInfo * runtimeCallInfo)674 ArkUINativeModuleValue ButtonBridge::ResetButtonRole(ArkUIRuntimeCallInfo* runtimeCallInfo)
675 {
676     EcmaVM* vm = runtimeCallInfo->GetVM();
677     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
678     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
679     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
680     GetArkUINodeModifiers()->getButtonModifier()->resetButtonRole(nativeNode);
681     return panda::JSValueRef::Undefined(vm);
682 }
SetButtonStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)683 ArkUINativeModuleValue ButtonBridge::SetButtonStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
684 {
685     EcmaVM* vm = runtimeCallInfo->GetVM();
686     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
687     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
688     Local<JSValueRef> buttonStyleArgs = runtimeCallInfo->GetCallArgRef(1);
689     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
690     if (buttonStyleArgs->IsUndefined() || buttonStyleArgs->IsNull() || !buttonStyleArgs->IsNumber()) {
691         GetArkUINodeModifiers()->getButtonModifier()->resetButtonStyle(nativeNode);
692         return panda::JSValueRef::Undefined(vm);
693     }
694     uint32_t buttonStyle = buttonStyleArgs->Uint32Value(vm);
695     GetArkUINodeModifiers()->getButtonModifier()->setButtonStyle(nativeNode, buttonStyle);
696     return panda::JSValueRef::Undefined(vm);
697 }
698 
ResetButtonStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)699 ArkUINativeModuleValue ButtonBridge::ResetButtonStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
700 {
701     EcmaVM* vm = runtimeCallInfo->GetVM();
702     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
703     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
704     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
705     GetArkUINodeModifiers()->getButtonModifier()->resetButtonStyle(nativeNode);
706     return panda::JSValueRef::Undefined(vm);
707 }
708 
SetButtonControlSize(ArkUIRuntimeCallInfo * runtimeCallInfo)709 ArkUINativeModuleValue ButtonBridge::SetButtonControlSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
710 {
711     EcmaVM* vm = runtimeCallInfo->GetVM();
712     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
713     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
714     Local<JSValueRef> controlSizeArgs = runtimeCallInfo->GetCallArgRef(1);
715     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
716     if (controlSizeArgs->IsUndefined() || controlSizeArgs->IsNull()) {
717         GetArkUINodeModifiers()->getButtonModifier()->resetButtonControlSize(nativeNode);
718         return panda::JSValueRef::Undefined(vm);
719     }
720     uint32_t controlSize = controlSizeArgs->Uint32Value(vm);
721     GetArkUINodeModifiers()->getButtonModifier()->setButtonControlSize(nativeNode, controlSize);
722     return panda::JSValueRef::Undefined(vm);
723 }
724 
ResetButtonControlSize(ArkUIRuntimeCallInfo * runtimeCallInfo)725 ArkUINativeModuleValue ButtonBridge::ResetButtonControlSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
726 {
727     EcmaVM* vm = runtimeCallInfo->GetVM();
728     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
729     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
730     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
731     GetArkUINodeModifiers()->getButtonModifier()->resetButtonControlSize(nativeNode);
732     return panda::JSValueRef::Undefined(vm);
733 }
734 
SetContentModifierBuilder(ArkUIRuntimeCallInfo * runtimeCallInfo)735 ArkUINativeModuleValue ButtonBridge::SetContentModifierBuilder(ArkUIRuntimeCallInfo* runtimeCallInfo)
736 {
737     EcmaVM* vm = runtimeCallInfo->GetVM();
738     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
739     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
740     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
741     auto* frameNode = reinterpret_cast<FrameNode*>(firstArg->ToNativePointer(vm)->Value());
742     if (!secondArg->IsObject(vm)) {
743         ButtonModelNG::SetBuilderFunc(frameNode, nullptr);
744         return panda::JSValueRef::Undefined(vm);
745     }
746     panda::CopyableGlobal<panda::ObjectRef> obj(vm, secondArg);
747     auto containerId = Container::CurrentId();
748     ButtonModelNG::SetBuilderFunc(frameNode, [vm, frameNode, obj = std::move(obj), containerId](
749             ButtonConfiguration config) -> RefPtr<FrameNode> {
750             ContainerScope scope(containerId);
751             auto context = ArkTSUtils::GetContext(vm);
752             const char* keyOfButton[] = { "label", "pressed", "enabled", "triggerClick" };
753             Local<JSValueRef> valuesOfButton[] = { panda::StringRef::NewFromUtf8(vm, config.label_.c_str()),
754                 panda::BooleanRef::New(vm, config.pressed_), panda::BooleanRef::New(vm, config.enabled_),
755                 panda::FunctionRef::New(vm, JsButtonClickCallback) };
756             auto button = panda::ObjectRef::NewWithNamedProperties(vm, ArraySize(keyOfButton),
757                 keyOfButton, valuesOfButton);
758             button->SetNativePointerFieldCount(vm, 1);
759             button->SetNativePointerField(vm, 0, static_cast<void*>(frameNode));
760             panda::Local<panda::JSValueRef> params[CALL_ARG_2] = { context, button };
761             LocalScope pandaScope(vm);
762             panda::TryCatch trycatch(vm);
763             auto jsObject = obj.ToLocal();
764             auto makeFunc = jsObject->Get(vm, panda::StringRef::NewFromUtf8(vm, "makeContentModifierNode"));
765             CHECK_EQUAL_RETURN(makeFunc->IsFunction(vm), false, nullptr);
766             panda::Local<panda::FunctionRef> func = makeFunc;
767             auto result = func->Call(vm, jsObject, params, CALL_ARG_2);
768             JSNApi::ExecutePendingJob(vm);
769             CHECK_EQUAL_RETURN(result.IsEmpty() || trycatch.HasCaught() || !result->IsObject(vm), true, nullptr);
770             auto resultObj = result->ToObject(vm);
771             panda::Local<panda::JSValueRef> nodeptr =
772                 resultObj->Get(vm, panda::StringRef::NewFromUtf8(vm, BUTTON_NODEPTR_OF_UINODE));
773             CHECK_EQUAL_RETURN(nodeptr.IsEmpty() || nodeptr->IsUndefined() || nodeptr->IsNull(), true, nullptr);
774             auto* frameNode = reinterpret_cast<FrameNode*>(nodeptr->ToNativePointer(vm)->Value());
775             CHECK_NULL_RETURN(frameNode, nullptr);
776             return AceType::Claim(frameNode);
777         });
778     return panda::JSValueRef::Undefined(vm);
779 }
780 
ResetButtonSize(ArkUIRuntimeCallInfo * runtimeCallInfo)781 ArkUINativeModuleValue ButtonBridge::ResetButtonSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
782 {
783     EcmaVM* vm = runtimeCallInfo->GetVM();
784     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
785     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
786     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
787     GetArkUINodeModifiers()->getButtonModifier()->resetButtonSize(nativeNode);
788     return panda::JSValueRef::Undefined(vm);
789 }
790 } // namespace OHOS::Ace::NG
791