• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     RefPtr<ResourceObject> colorResObj;
225     auto nodeInfo = ArkTSUtils::MakeNativeNodeInfo(nativeNode);
226     if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color, colorResObj, nodeInfo)) {
227         GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontColor(nativeNode);
228     } else {
229         auto colorRawPtr = AceType::RawPtr(colorResObj);
230         GetArkUINodeModifiers()->getButtonModifier()->setButtonFontColorPtr(nativeNode, color.GetValue(), colorRawPtr);
231     }
232     return panda::JSValueRef::Undefined(vm);
233 }
234 
ResetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)235 ArkUINativeModuleValue ButtonBridge::ResetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
236 {
237     EcmaVM* vm = runtimeCallInfo->GetVM();
238     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
239     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
240     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
241     GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontColor(nativeNode);
242     return panda::JSValueRef::Undefined(vm);
243 }
244 
SetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)245 ArkUINativeModuleValue ButtonBridge::SetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
246 {
247     EcmaVM* vm = runtimeCallInfo->GetVM();
248     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
249     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
250     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
251     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
252     Ace::CalcDimension fontSize;
253     if (ArkTSUtils::ParseJsDimensionVpNG(vm, secondArg, fontSize) && fontSize.Unit() != DimensionUnit::PERCENT &&
254         GreatOrEqual(fontSize.Value(), 0.0)) {
255         ArkTSUtils::ParseJsDimensionFp(vm, secondArg, fontSize);
256     } else {
257         auto pipeline = PipelineBase::GetCurrentContext();
258         CHECK_NULL_RETURN(pipeline, panda::NativePointerRef::New(vm, nullptr));
259         auto buttonTheme = pipeline->GetTheme<ButtonTheme>();
260         CHECK_NULL_RETURN(buttonTheme, panda::NativePointerRef::New(vm, nullptr));
261         fontSize = buttonTheme->GetTextStyle().GetFontSize();
262     }
263     int fontSizeUnit = static_cast<int>(fontSize.Unit());
264     GetArkUINodeModifiers()->getButtonModifier()->setButtonFontSize(nativeNode, fontSize.Value(), fontSizeUnit);
265     return panda::JSValueRef::Undefined(vm);
266 }
267 
ResetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)268 ArkUINativeModuleValue ButtonBridge::ResetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
269 {
270     EcmaVM* vm = runtimeCallInfo->GetVM();
271     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
272     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
273     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
274     GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontSize(nativeNode);
275     return panda::JSValueRef::Undefined(vm);
276 }
277 
SetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)278 ArkUINativeModuleValue ButtonBridge::SetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
279 {
280     EcmaVM* vm = runtimeCallInfo->GetVM();
281     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
282     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
283     Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
284     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
285     std::string fontWeight = DEFAULT_FONT_WEIGHT;
286     if (!fontWeightArg->IsNull()) {
287         if (fontWeightArg->IsNumber()) {
288             fontWeight = std::to_string(fontWeightArg->Int32Value(vm));
289         } else if (fontWeightArg->IsString(vm)) {
290             // enum FontWeight is string.
291             fontWeight = fontWeightArg->ToString(vm)->ToString(vm);
292         }
293     }
294     GetArkUINodeModifiers()->getButtonModifier()->setButtonFontWeight(nativeNode, fontWeight.c_str());
295     return panda::JSValueRef::Undefined(vm);
296 }
297 
ResetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)298 ArkUINativeModuleValue ButtonBridge::ResetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
299 {
300     EcmaVM* vm = runtimeCallInfo->GetVM();
301     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
302     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
303     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
304     GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontWeight(nativeNode);
305     return panda::JSValueRef::Undefined(vm);
306 }
307 
SetFontStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)308 ArkUINativeModuleValue ButtonBridge::SetFontStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
309 {
310     EcmaVM* vm = runtimeCallInfo->GetVM();
311     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
312     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
313     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
314     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
315     int fontStyle = secondArg->Int32Value(vm);
316     GetArkUINodeModifiers()->getButtonModifier()->setButtonFontStyle(nativeNode, fontStyle);
317     return panda::JSValueRef::Undefined(vm);
318 }
319 
ResetFontStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)320 ArkUINativeModuleValue ButtonBridge::ResetFontStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
321 {
322     EcmaVM* vm = runtimeCallInfo->GetVM();
323     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
324     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
325     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
326     GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontStyle(nativeNode);
327     return panda::JSValueRef::Undefined(vm);
328 }
329 
SetFontFamily(ArkUIRuntimeCallInfo * runtimeCallInfo)330 ArkUINativeModuleValue ButtonBridge::SetFontFamily(ArkUIRuntimeCallInfo* runtimeCallInfo)
331 {
332     EcmaVM* vm = runtimeCallInfo->GetVM();
333     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
334     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
335     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
336     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
337     std::string fontFamily;
338     RefPtr<ResourceObject> familiesResObj;
339     if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, secondArg, fontFamily, familiesResObj)) {
340         GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontFamily(nativeNode);
341         return panda::JSValueRef::Undefined(vm);
342     }
343     auto familiesRawPtr = AceType::RawPtr(familiesResObj);
344     GetArkUINodeModifiers()->getButtonModifier()->setButtonFontFamilyPtr(
345         nativeNode, fontFamily.c_str(), familiesRawPtr);
346     return panda::JSValueRef::Undefined(vm);
347 }
348 
ResetFontFamily(ArkUIRuntimeCallInfo * runtimeCallInfo)349 ArkUINativeModuleValue ButtonBridge::ResetFontFamily(ArkUIRuntimeCallInfo* runtimeCallInfo)
350 {
351     EcmaVM* vm = runtimeCallInfo->GetVM();
352     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
353     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
354     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
355     GetArkUINodeModifiers()->getButtonModifier()->resetButtonFontFamily(nativeNode);
356     return panda::JSValueRef::Undefined(vm);
357 }
358 
SetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)359 ArkUINativeModuleValue ButtonBridge::SetBackgroundColor(ArkUIRuntimeCallInfo *runtimeCallInfo)
360 {
361     EcmaVM *vm = runtimeCallInfo->GetVM();
362     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
363     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
364     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
365     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
366     Color color;
367     RefPtr<ResourceObject> colorResObj;
368     auto nodeInfo = ArkTSUtils::MakeNativeNodeInfo(nativeNode);
369     if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color, colorResObj, nodeInfo)) {
370         GetArkUINodeModifiers()->getButtonModifier()->resetButtonBackgroundColor(nativeNode);
371     } else {
372         auto colorRawPtr = AceType::RawPtr(colorResObj);
373         GetArkUINodeModifiers()->getButtonModifier()->setButtonBackgroundColorWithColorSpacePtr(
374             nativeNode, color.GetValue(), color.GetColorSpace(), colorRawPtr);
375     }
376     return panda::JSValueRef::Undefined(vm);
377 }
378 
ResetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)379 ArkUINativeModuleValue ButtonBridge::ResetBackgroundColor(ArkUIRuntimeCallInfo *runtimeCallInfo)
380 {
381     EcmaVM *vm = runtimeCallInfo->GetVM();
382     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
383     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
384     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
385     GetArkUINodeModifiers()->getButtonModifier()->resetButtonBackgroundColor(nativeNode);
386     return panda::JSValueRef::Undefined(vm);
387 }
388 
PushValuesVector(const std::optional<int32_t> & value,std::vector<int32_t> & valuesVector)389 void ButtonBridge::PushValuesVector(const std::optional<int32_t>& value, std::vector<int32_t>& valuesVector)
390 {
391     valuesVector.push_back(static_cast<int32_t>(value.has_value()));
392     if (value.has_value()) {
393         valuesVector.push_back(static_cast<int32_t>(value.value()));
394     } else {
395         valuesVector.push_back(0);
396     }
397 }
398 
PutButtonValuesParameters(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,std::vector<int32_t> & valuesVector)399 void ButtonBridge::PutButtonValuesParameters(
400     ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm, std::vector<int32_t>& valuesVector)
401 {
402     std::optional<int32_t> overflowOptional = std::nullopt;
403     Local<JSValueRef> overflowArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
404     if (!overflowArg->IsNull() && overflowArg->IsNumber()) {
405         auto overflow = overflowArg->Int32Value(vm);
406         if (overflow >= 0 && overflow < static_cast<int32_t>(TEXT_OVERFLOWS.size())) {
407             overflowOptional = overflow;
408         }
409     }
410     PushValuesVector(overflowOptional, valuesVector);
411 
412     std::optional<int32_t> maxLinesOptional = std::nullopt;
413     Local<JSValueRef> maxLinesArg = runtimeCallInfo->GetCallArgRef(MAX_LINES_ARG_2);
414     if (!maxLinesArg->IsNull() && maxLinesArg->IsNumber()) {
415         auto maxLines = maxLinesArg->Int32Value(vm);
416         maxLinesOptional = Positive(maxLines) ? maxLines : 1;
417     }
418     PushValuesVector(maxLinesOptional, valuesVector);
419 
420     std::optional<int32_t> adaptHeightOptional = std::nullopt;
421     Local<JSValueRef> adaptHeightArg = runtimeCallInfo->GetCallArgRef(ADAPT_HEIGHT_ARG_5);
422     if (!adaptHeightArg->IsNull() && adaptHeightArg->IsNumber()) {
423         auto adaptHeight = adaptHeightArg->Int32Value(vm);
424         if (adaptHeight >= 0 && adaptHeight < static_cast<double>(HEIGHT_ADAPTIVE_POLICY.size())) {
425             adaptHeightOptional = adaptHeight;
426         }
427     }
428     PushValuesVector(adaptHeightOptional, valuesVector);
429 
430     std::optional<int32_t> fontStyleOptional = std::nullopt;
431     Local<JSValueRef> fontStyleArg = runtimeCallInfo->GetCallArgRef(FONT_STYLE_ARG_8);
432     if (!fontStyleArg->IsNull() && fontStyleArg->IsNumber()) {
433         auto fontStyle = fontStyleArg->Int32Value(vm);
434         if (fontStyle >= 0 && fontStyle < static_cast<int32_t>(FONT_STYLES.size())) {
435             fontStyleOptional = fontStyle;
436         }
437     }
438     PushValuesVector(fontStyleOptional, valuesVector);
439 }
440 
PushDimensionVector(const std::optional<Dimension> & valueDim,std::vector<ArkUI_Float32> & dimensions)441 void ButtonBridge::PushDimensionVector(const std::optional<Dimension>& valueDim, std::vector<ArkUI_Float32>& dimensions)
442 {
443     dimensions.push_back(static_cast<ArkUI_Float32>(valueDim.has_value()));
444     if (valueDim.has_value()) {
445         dimensions.push_back(static_cast<ArkUI_Float32>(valueDim.value().Value()));
446         dimensions.push_back(static_cast<ArkUI_Float32>(valueDim.value().Unit()));
447     } else {
448         dimensions.push_back(0);
449         dimensions.push_back(0);
450     }
451 }
452 
PushButtonDimension(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,std::vector<ArkUI_Float32> & fontSizesVector,int32_t argIndex)453 void ButtonBridge::PushButtonDimension(
454     ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm, std::vector<ArkUI_Float32>& fontSizesVector, int32_t argIndex)
455 {
456     Local<JSValueRef> arg = runtimeCallInfo->GetCallArgRef(argIndex);
457     std::optional<CalcDimension> dimensionOptional = std::nullopt;
458     CalcDimension parsedDimension;
459     if (ArkTSUtils::ParseJsDimensionFp(vm, arg, parsedDimension, false)) {
460         dimensionOptional = parsedDimension;
461     }
462     PushDimensionVector(dimensionOptional, fontSizesVector);
463 }
464 
PutButtonDimensionParameters(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,std::vector<ArkUI_Float32> & fontSizesVector)465 void ButtonBridge::PutButtonDimensionParameters(
466     ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm, std::vector<ArkUI_Float32>& fontSizesVector)
467 {
468     std::vector<int32_t> indexVector = { MIN_FONT_SIZE_ARG_3, MAX_FONT_SIZE_ARG_4, FONT_SIZE_ARG_6 };
469     for (size_t index = 0; index < indexVector.size(); index++) {
470         PushButtonDimension(runtimeCallInfo, vm, fontSizesVector, indexVector[index]);
471     }
472 }
473 
SetLabelStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)474 ArkUINativeModuleValue ButtonBridge::SetLabelStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
475 {
476     EcmaVM* vm = runtimeCallInfo->GetVM();
477     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
478     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
479     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
480     std::vector<ArkUI_Int32> valuesVector;
481     PutButtonValuesParameters(runtimeCallInfo, vm, valuesVector);
482     std::vector<ArkUI_Float32> fontSizesVector;
483     PutButtonDimensionParameters(runtimeCallInfo, vm, fontSizesVector);
484     std::vector<ArkUI_CharPtr> stringParameters;
485     Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(FONT_WEIGHT_ARG_7);
486     std::string fontWeight = DEFAULT_FONT_WEIGHT;
487     if (!fontWeightArg->IsNull()) {
488         if (fontWeightArg->IsNumber()) {
489             fontWeight = std::to_string(fontWeightArg->Int32Value(vm));
490         } else if (fontWeightArg->IsString(vm)) {
491             // enum FontWeight is sent as string.
492             fontWeight = fontWeightArg->ToString(vm)->ToString(vm);
493         }
494     }
495     stringParameters.push_back(fontWeight.c_str());
496 
497     Local<JSValueRef> fontFamilyArg = runtimeCallInfo->GetCallArgRef(FONT_FAMILY_ARG_9);
498     std::string strFontFamilies;
499     if (ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArg, strFontFamilies)) {
500         stringParameters.push_back(strFontFamilies.c_str());
501     } else {
502         strFontFamilies = NONE_FONT_FAMILY;
503         stringParameters.push_back(strFontFamilies.c_str());
504     }
505     std::vector<ArkUI_Uint32> dataCountVector;
506     dataCountVector.push_back(stringParameters.size());
507     dataCountVector.push_back(valuesVector.size());
508     dataCountVector.push_back(fontSizesVector.size());
509 
510     ArkUIButtonSizeStruct sizeResObj;
511     auto maxFontSizeResObj = ParseFontSizeResObj(runtimeCallInfo, vm, MAX_FONT_SIZE_ARG_4);
512     sizeResObj.maxFontSize = AceType::RawPtr(maxFontSizeResObj);
513     auto minFontSizeResObj = ParseFontSizeResObj(runtimeCallInfo, vm, MIN_FONT_SIZE_ARG_3);
514     sizeResObj.minFontSize = AceType::RawPtr(minFontSizeResObj);
515 
516     GetArkUINodeModifiers()->getButtonModifier()->setButtonLabelStylePtr(
517         nativeNode, stringParameters.data(), valuesVector.data(), fontSizesVector.data(),
518         dataCountVector.data(), sizeResObj);
519     return panda::JSValueRef::Undefined(vm);
520 }
521 
ParseFontSizeResObj(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,int32_t argIndex)522 RefPtr<ResourceObject> ButtonBridge::ParseFontSizeResObj(ArkUIRuntimeCallInfo* runtimeCallInfo,
523     EcmaVM* vm, int32_t argIndex)
524 {
525     Local<JSValueRef> arg = runtimeCallInfo->GetCallArgRef(argIndex);
526     CalcDimension parsedDimension;
527     RefPtr<ResourceObject> resObj;
528     ArkTSUtils::ParseJsDimensionFp(vm, arg, parsedDimension, resObj, false);
529     return resObj;
530 }
531 
ResetLabelStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)532 ArkUINativeModuleValue ButtonBridge::ResetLabelStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
533 {
534     EcmaVM* vm = runtimeCallInfo->GetVM();
535     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
536     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
537     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
538     GetArkUINodeModifiers()->getButtonModifier()->resetButtonLabelStyle(nativeNode);
539     return panda::JSValueRef::Undefined(vm);
540 }
541 
ParseBorderRadius(EcmaVM * vm,const Local<JSValueRef> & args,std::optional<CalcDimension> & optionalDimension)542 void ParseBorderRadius(EcmaVM* vm, const Local<JSValueRef>& args, std::optional<CalcDimension>& optionalDimension)
543 {
544     CalcDimension valueDim;
545     if (!args->IsUndefined() && ArkTSUtils::ParseJsDimensionVp(vm, args, valueDim, false)) {
546         optionalDimension = valueDim;
547     }
548 }
549 
PushBorderRadiusVector(const std::optional<CalcDimension> & valueDim,std::vector<ArkUI_Float32> & options)550 void PushBorderRadiusVector(const std::optional<CalcDimension>& valueDim, std::vector<ArkUI_Float32> &options)
551 {
552     options.push_back(static_cast<ArkUI_Float32>(valueDim.has_value()));
553     if (valueDim.has_value()) {
554         options.push_back(static_cast<ArkUI_Float32>(valueDim.value().Value()));
555         options.push_back(static_cast<ArkUI_Float32>(valueDim.value().Unit()));
556     } else {
557         options.push_back(0);
558         options.push_back(0);
559     }
560 }
561 
SetButtonBorderRadius(ArkUIRuntimeCallInfo * runtimeCallInfo)562 ArkUINativeModuleValue ButtonBridge::SetButtonBorderRadius(ArkUIRuntimeCallInfo* runtimeCallInfo)
563 {
564     EcmaVM* vm = runtimeCallInfo->GetVM();
565     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
566     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
567     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
568     Local<JSValueRef> topLeftArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);     // topLeft value
569     Local<JSValueRef> topRightArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_2);    // topRight value
570     Local<JSValueRef> bottomLeftArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_3);  // bottomLeft value
571     Local<JSValueRef> bottomRightArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_4); // bottomRight value
572 
573     if (topLeftArgs->IsUndefined() && topRightArgs->IsUndefined() && bottomLeftArgs->IsUndefined() &&
574         bottomRightArgs->IsUndefined()) {
575         GetArkUINodeModifiers()->getButtonModifier()->resetButtonBorderRadius(nativeNode);
576         return panda::JSValueRef::Undefined(vm);
577     }
578 
579     std::optional<CalcDimension> radiusTopLeft;
580     std::optional<CalcDimension> radiusTopRight;
581     std::optional<CalcDimension> radiusBottomLeft;
582     std::optional<CalcDimension> radiusBottomRight;
583 
584     ParseBorderRadius(vm, topLeftArgs, radiusTopLeft);
585     ParseBorderRadius(vm, topRightArgs, radiusTopRight);
586     ParseBorderRadius(vm, bottomLeftArgs, radiusBottomLeft);
587     ParseBorderRadius(vm, bottomRightArgs, radiusBottomRight);
588 
589     std::vector<ArkUI_Float32> options;
590     PushBorderRadiusVector(radiusTopLeft, options);
591     PushBorderRadiusVector(radiusTopRight, options);
592     PushBorderRadiusVector(radiusBottomLeft, options);
593     PushBorderRadiusVector(radiusBottomRight, options);
594 
595     GetArkUINodeModifiers()->getButtonModifier()->setButtonBorderRadius(nativeNode, options.data(), options.size());
596     return panda::JSValueRef::Undefined(vm);
597 }
598 
ResetButtonBorderRadius(ArkUIRuntimeCallInfo * runtimeCallInfo)599 ArkUINativeModuleValue ButtonBridge::ResetButtonBorderRadius(ArkUIRuntimeCallInfo* runtimeCallInfo)
600 {
601     EcmaVM* vm = runtimeCallInfo->GetVM();
602     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
603     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
604     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
605     GetArkUINodeModifiers()->getButtonModifier()->resetButtonBorderRadius(nativeNode);
606     return panda::JSValueRef::Undefined(vm);
607 }
608 
SetButtonBorder(ArkUIRuntimeCallInfo * runtimeCallInfo)609 ArkUINativeModuleValue ButtonBridge::SetButtonBorder(ArkUIRuntimeCallInfo* runtimeCallInfo)
610 {
611     CommonBridge::SetBorder(runtimeCallInfo);
612     EcmaVM* vm = runtimeCallInfo->GetVM();
613     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
614     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
615     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
616 
617     Local<JSValueRef> topLeftArgs = runtimeCallInfo->GetCallArgRef(9);      // 9:topLeft value
618     Local<JSValueRef> topRightArgs = runtimeCallInfo->GetCallArgRef(10);    // 10:topRight value
619     Local<JSValueRef> bottomLeftArgs = runtimeCallInfo->GetCallArgRef(11);  // 11:bottomLeft value
620     Local<JSValueRef> bottomRightArgs = runtimeCallInfo->GetCallArgRef(12); // 12:bottomRight value
621     if (topLeftArgs->IsUndefined() && topRightArgs->IsUndefined() && bottomLeftArgs->IsUndefined() &&
622         bottomRightArgs->IsUndefined()) {
623         GetArkUINodeModifiers()->getButtonModifier()->resetButtonBorderRadius(nativeNode);
624         return panda::JSValueRef::Undefined(vm);
625     }
626 
627     std::optional<CalcDimension> radiusTopLeft;
628     std::optional<CalcDimension> radiusTopRight;
629     std::optional<CalcDimension> radiusBottomLeft;
630     std::optional<CalcDimension> radiusBottomRight;
631 
632     ParseBorderRadius(vm, topLeftArgs, radiusTopLeft);
633     ParseBorderRadius(vm, topRightArgs, radiusTopRight);
634     ParseBorderRadius(vm, bottomLeftArgs, radiusBottomLeft);
635     ParseBorderRadius(vm, bottomRightArgs, radiusBottomRight);
636 
637     std::vector<ArkUI_Float32> options;
638     PushBorderRadiusVector(radiusTopLeft, options);
639     PushBorderRadiusVector(radiusTopRight, options);
640     PushBorderRadiusVector(radiusBottomLeft, options);
641     PushBorderRadiusVector(radiusBottomRight, options);
642 
643     GetArkUINodeModifiers()->getButtonModifier()->setButtonBorderRadius(nativeNode, options.data(), options.size());
644     return panda::JSValueRef::Undefined(vm);
645 }
646 
ResetButtonBorder(ArkUIRuntimeCallInfo * runtimeCallInfo)647 ArkUINativeModuleValue ButtonBridge::ResetButtonBorder(ArkUIRuntimeCallInfo* runtimeCallInfo)
648 {
649     EcmaVM* vm = runtimeCallInfo->GetVM();
650     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
651     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
652     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
653     GetArkUINodeModifiers()->getCommonModifier()->resetBorder(nativeNode);
654     return panda::JSValueRef::Undefined(vm);
655 }
656 
SetButtonSize(ArkUIRuntimeCallInfo * runtimeCallInfo)657 ArkUINativeModuleValue ButtonBridge::SetButtonSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
658 {
659     EcmaVM* vm = runtimeCallInfo->GetVM();
660     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
661     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
662     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
663     Local<JSValueRef> widthArgs = runtimeCallInfo->GetCallArgRef(1); // 1:width value
664     Local<JSValueRef> heightArgs = runtimeCallInfo->GetCallArgRef(2); // 2:height value
665     CalcDimension width;
666     std::string widthStr;
667     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, widthArgs, width)) {
668         widthStr = "undefined";
669     } else {
670         ArkTSUtils::ParseJsDimensionVp(vm, widthArgs, width);
671         widthStr = std::to_string(width.Value());
672     }
673     CalcDimension height;
674     std::string heightStr;
675     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, heightArgs, height)) {
676         heightStr = "undefined";
677     } else {
678         heightStr = std::to_string(height.Value());
679     }
680     GetArkUINodeModifiers()->getButtonModifier()->setButtonSize(nativeNode, widthStr.c_str(),
681         static_cast<int32_t>(width.Unit()), heightStr.c_str(), static_cast<int32_t>(height.Unit()));
682     return panda::JSValueRef::Undefined(vm);
683 }
684 
SetButtonRole(ArkUIRuntimeCallInfo * runtimeCallInfo)685 ArkUINativeModuleValue ButtonBridge::SetButtonRole(ArkUIRuntimeCallInfo* runtimeCallInfo)
686 {
687     EcmaVM* vm = runtimeCallInfo->GetVM();
688     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
689     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
690     Local<JSValueRef> buttonRoleArgs = runtimeCallInfo->GetCallArgRef(1);
691     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
692     if (buttonRoleArgs->IsUndefined() || buttonRoleArgs->IsNull() || !buttonRoleArgs->IsNumber()) {
693         GetArkUINodeModifiers()->getButtonModifier()->resetButtonRole(nativeNode);
694         return panda::JSValueRef::Undefined(vm);
695     }
696     uint32_t buttonRole = buttonRoleArgs->Uint32Value(vm);
697     GetArkUINodeModifiers()->getButtonModifier()->setButtonRole(nativeNode, buttonRole);
698     return panda::JSValueRef::Undefined(vm);
699 }
700 
ResetButtonRole(ArkUIRuntimeCallInfo * runtimeCallInfo)701 ArkUINativeModuleValue ButtonBridge::ResetButtonRole(ArkUIRuntimeCallInfo* runtimeCallInfo)
702 {
703     EcmaVM* vm = runtimeCallInfo->GetVM();
704     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
705     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
706     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
707     GetArkUINodeModifiers()->getButtonModifier()->resetButtonRole(nativeNode);
708     return panda::JSValueRef::Undefined(vm);
709 }
SetButtonStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)710 ArkUINativeModuleValue ButtonBridge::SetButtonStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
711 {
712     EcmaVM* vm = runtimeCallInfo->GetVM();
713     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
714     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
715     Local<JSValueRef> buttonStyleArgs = runtimeCallInfo->GetCallArgRef(1);
716     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
717     if (buttonStyleArgs->IsUndefined() || buttonStyleArgs->IsNull() || !buttonStyleArgs->IsNumber()) {
718         GetArkUINodeModifiers()->getButtonModifier()->resetButtonStyle(nativeNode);
719         return panda::JSValueRef::Undefined(vm);
720     }
721     uint32_t buttonStyle = buttonStyleArgs->Uint32Value(vm);
722     GetArkUINodeModifiers()->getButtonModifier()->setButtonStyle(nativeNode, buttonStyle);
723     return panda::JSValueRef::Undefined(vm);
724 }
725 
ResetButtonStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)726 ArkUINativeModuleValue ButtonBridge::ResetButtonStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
727 {
728     EcmaVM* vm = runtimeCallInfo->GetVM();
729     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
730     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
731     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
732     GetArkUINodeModifiers()->getButtonModifier()->resetButtonStyle(nativeNode);
733     return panda::JSValueRef::Undefined(vm);
734 }
735 
SetButtonControlSize(ArkUIRuntimeCallInfo * runtimeCallInfo)736 ArkUINativeModuleValue ButtonBridge::SetButtonControlSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
737 {
738     EcmaVM* vm = runtimeCallInfo->GetVM();
739     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
740     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
741     Local<JSValueRef> controlSizeArgs = runtimeCallInfo->GetCallArgRef(1);
742     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
743     if (controlSizeArgs->IsUndefined() || controlSizeArgs->IsNull()) {
744         GetArkUINodeModifiers()->getButtonModifier()->resetButtonControlSize(nativeNode);
745         return panda::JSValueRef::Undefined(vm);
746     }
747     uint32_t controlSize = controlSizeArgs->Uint32Value(vm);
748     GetArkUINodeModifiers()->getButtonModifier()->setButtonControlSize(nativeNode, controlSize);
749     return panda::JSValueRef::Undefined(vm);
750 }
751 
ResetButtonControlSize(ArkUIRuntimeCallInfo * runtimeCallInfo)752 ArkUINativeModuleValue ButtonBridge::ResetButtonControlSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
753 {
754     EcmaVM* vm = runtimeCallInfo->GetVM();
755     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
756     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
757     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
758     GetArkUINodeModifiers()->getButtonModifier()->resetButtonControlSize(nativeNode);
759     return panda::JSValueRef::Undefined(vm);
760 }
761 
SetContentModifierBuilder(ArkUIRuntimeCallInfo * runtimeCallInfo)762 ArkUINativeModuleValue ButtonBridge::SetContentModifierBuilder(ArkUIRuntimeCallInfo* runtimeCallInfo)
763 {
764     EcmaVM* vm = runtimeCallInfo->GetVM();
765     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
766     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
767     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
768     auto* frameNode = reinterpret_cast<FrameNode*>(firstArg->ToNativePointer(vm)->Value());
769     if (!secondArg->IsObject(vm)) {
770         ButtonModelNG::SetBuilderFunc(frameNode, nullptr);
771         return panda::JSValueRef::Undefined(vm);
772     }
773     panda::CopyableGlobal<panda::ObjectRef> obj(vm, secondArg);
774     auto containerId = Container::CurrentId();
775     ButtonModelNG::SetBuilderFunc(frameNode, [vm, frameNode, obj = std::move(obj), containerId](
776             ButtonConfiguration config) -> RefPtr<FrameNode> {
777             ContainerScope scope(containerId);
778             auto context = ArkTSUtils::GetContext(vm);
779             const char* keyOfButton[] = { "label", "pressed", "enabled", "triggerClick" };
780             Local<JSValueRef> valuesOfButton[] = { panda::StringRef::NewFromUtf8(vm, config.label_.c_str()),
781                 panda::BooleanRef::New(vm, config.pressed_), panda::BooleanRef::New(vm, config.enabled_),
782                 panda::FunctionRef::New(vm, JsButtonClickCallback) };
783             auto button = panda::ObjectRef::NewWithNamedProperties(vm, ArraySize(keyOfButton),
784                 keyOfButton, valuesOfButton);
785             button->SetNativePointerFieldCount(vm, 1);
786             button->SetNativePointerField(vm, 0, static_cast<void*>(frameNode));
787             panda::Local<panda::JSValueRef> params[CALL_ARG_2] = { context, button };
788             LocalScope pandaScope(vm);
789             panda::TryCatch trycatch(vm);
790             auto jsObject = obj.ToLocal();
791             auto makeFunc = jsObject->Get(vm, panda::StringRef::NewFromUtf8(vm, "makeContentModifierNode"));
792             CHECK_EQUAL_RETURN(makeFunc->IsFunction(vm), false, nullptr);
793             panda::Local<panda::FunctionRef> func = makeFunc;
794             auto result = func->Call(vm, jsObject, params, CALL_ARG_2);
795             JSNApi::ExecutePendingJob(vm);
796             CHECK_EQUAL_RETURN(result.IsEmpty() || trycatch.HasCaught() || !result->IsObject(vm), true, nullptr);
797             auto resultObj = result->ToObject(vm);
798             panda::Local<panda::JSValueRef> nodeptr =
799                 resultObj->Get(vm, panda::StringRef::NewFromUtf8(vm, BUTTON_NODEPTR_OF_UINODE));
800             CHECK_EQUAL_RETURN(nodeptr.IsEmpty() || nodeptr->IsUndefined() || nodeptr->IsNull(), true, nullptr);
801             auto* frameNode = reinterpret_cast<FrameNode*>(nodeptr->ToNativePointer(vm)->Value());
802             CHECK_NULL_RETURN(frameNode, nullptr);
803             return AceType::Claim(frameNode);
804         });
805     return panda::JSValueRef::Undefined(vm);
806 }
807 
ResetButtonSize(ArkUIRuntimeCallInfo * runtimeCallInfo)808 ArkUINativeModuleValue ButtonBridge::ResetButtonSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
809 {
810     EcmaVM* vm = runtimeCallInfo->GetVM();
811     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
812     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
813     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
814     GetArkUINodeModifiers()->getButtonModifier()->resetButtonSize(nativeNode);
815     return panda::JSValueRef::Undefined(vm);
816 }
817 
SetMinFontScale(ArkUIRuntimeCallInfo * runtimeCallInfo)818 ArkUINativeModuleValue ButtonBridge::SetMinFontScale(ArkUIRuntimeCallInfo* runtimeCallInfo)
819 {
820     EcmaVM* vm = runtimeCallInfo->GetVM();
821     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
822     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
823     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
824     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
825     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
826     double minFontScale;
827     RefPtr<ResourceObject> doubleResObj;
828     if (!ArkTSUtils::ParseJsDouble(vm, secondArg, minFontScale, doubleResObj)) {
829         return panda::JSValueRef::Undefined(vm);
830     }
831     auto doubleRawPtr = AceType::RawPtr(doubleResObj);
832     if (LessOrEqual(minFontScale, 0.0f)) {
833         GetArkUINodeModifiers()->getButtonModifier()->setButtonMinFontScalePtr(
834             nativeNode, static_cast<float>(minFontScale), doubleRawPtr);
835         minFontScale = 0.0f;
836     } else if (GreatOrEqual(minFontScale, 1.0f)) {
837         minFontScale = 1.0f;
838     }
839     GetArkUINodeModifiers()->getButtonModifier()->setButtonMinFontScalePtr(
840         nativeNode, static_cast<float>(minFontScale), doubleRawPtr);
841     return panda::JSValueRef::Undefined(vm);
842 }
843 
ResetMinFontScale(ArkUIRuntimeCallInfo * runtimeCallInfo)844 ArkUINativeModuleValue ButtonBridge::ResetMinFontScale(ArkUIRuntimeCallInfo* runtimeCallInfo)
845 {
846     EcmaVM* vm = runtimeCallInfo->GetVM();
847     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
848     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
849     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
850     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
851     GetArkUINodeModifiers()->getButtonModifier()->resetButtonMinFontScale(nativeNode);
852     return panda::JSValueRef::Undefined(vm);
853 }
854 
SetMaxFontScale(ArkUIRuntimeCallInfo * runtimeCallInfo)855 ArkUINativeModuleValue ButtonBridge::SetMaxFontScale(ArkUIRuntimeCallInfo* runtimeCallInfo)
856 {
857     EcmaVM* vm = runtimeCallInfo->GetVM();
858     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
859     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
860     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
861     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
862     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
863     double maxFontScale;
864     RefPtr<ResourceObject> doubleResObj;
865     if (!ArkTSUtils::ParseJsDouble(vm, secondArg, maxFontScale, doubleResObj)) {
866         return panda::JSValueRef::Undefined(vm);
867     }
868     if (LessOrEqual(maxFontScale, 1.0f)) {
869         maxFontScale = 1.0f;
870     }
871     auto doubleRawPtr = AceType::RawPtr(doubleResObj);
872     GetArkUINodeModifiers()->getButtonModifier()->setButtonMaxFontScalePtr(
873         nativeNode, static_cast<float>(maxFontScale), doubleRawPtr);
874     return panda::JSValueRef::Undefined(vm);
875 }
876 
ResetMaxFontScale(ArkUIRuntimeCallInfo * runtimeCallInfo)877 ArkUINativeModuleValue ButtonBridge::ResetMaxFontScale(ArkUIRuntimeCallInfo* runtimeCallInfo)
878 {
879     EcmaVM* vm = runtimeCallInfo->GetVM();
880     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
881     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
882     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
883     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
884     GetArkUINodeModifiers()->getButtonModifier()->resetButtonMaxFontScale(nativeNode);
885     return panda::JSValueRef::Undefined(vm);
886 }
887 
888 } // namespace OHOS::Ace::NG
889