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/interfaces/native/node/api.h"
21 #include "core/components/common/properties/text_style.h"
22 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
23 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_common_bridge.h"
24 #include "core/components_ng/pattern/button/button_request_data.h"
25 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
26
27 namespace OHOS::Ace::NG {
28 const std::vector<TextOverflow> TEXT_OVERFLOWS = { TextOverflow::NONE, TextOverflow::CLIP, TextOverflow::ELLIPSIS,
29 TextOverflow::MARQUEE };
30 const std::vector<Ace::FontStyle> FONT_STYLES = { Ace::FontStyle::NORMAL, Ace::FontStyle::ITALIC };
31 const std::vector<TextHeightAdaptivePolicy> HEIGHT_ADAPTIVE_POLICY = { TextHeightAdaptivePolicy::MAX_LINES_FIRST,
32 TextHeightAdaptivePolicy::MIN_FONT_SIZE_FIRST, TextHeightAdaptivePolicy::LAYOUT_CONSTRAINT_FIRST };
33 const std::string DEFAULT_FONT_WEIGHT = "400";
34 const std::string NONE_FONT_FAMILY = "NoneFontFamily";
35 constexpr int32_t CALL_ARG_0 = 0;
36 constexpr int32_t CALL_ARG_1 = 1;
37 constexpr int32_t CALL_ARG_2 = 2;
38 constexpr int32_t CALL_ARG_3 = 3;
39 constexpr int32_t CALL_ARG_4 = 4;
40 constexpr int32_t MAX_LINES_ARG_2 = 2;
41 constexpr int32_t MIN_FONT_SIZE_ARG_3 = 3;
42 constexpr int32_t MAX_FONT_SIZE_ARG_4 = 4;
43 constexpr int32_t ADAPT_HEIGHT_ARG_5 = 5;
44 constexpr int32_t FONT_SIZE_ARG_6 = 6;
45 constexpr int32_t FONT_WEIGHT_ARG_7 = 7;
46 constexpr int32_t FONT_STYLE_ARG_8 = 8;
47 constexpr int32_t FONT_FAMILY_ARG_9 = 9;
48
SetType(ArkUIRuntimeCallInfo * runtimeCallInfo)49 ArkUINativeModuleValue ButtonBridge::SetType(ArkUIRuntimeCallInfo* runtimeCallInfo)
50 {
51 EcmaVM* vm = runtimeCallInfo->GetVM();
52 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
53 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
54 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
55 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
56 int type = secondArg->Int32Value(vm);
57 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonType(nativeNode, type);
58 return panda::JSValueRef::Undefined(vm);
59 }
60
ResetType(ArkUIRuntimeCallInfo * runtimeCallInfo)61 ArkUINativeModuleValue ButtonBridge::ResetType(ArkUIRuntimeCallInfo* runtimeCallInfo)
62 {
63 EcmaVM* vm = runtimeCallInfo->GetVM();
64 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
65 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
66 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
67 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonType(nativeNode);
68 return panda::JSValueRef::Undefined(vm);
69 }
70
SetStateEffect(ArkUIRuntimeCallInfo * runtimeCallInfo)71 ArkUINativeModuleValue ButtonBridge::SetStateEffect(ArkUIRuntimeCallInfo* runtimeCallInfo)
72 {
73 EcmaVM* vm = runtimeCallInfo->GetVM();
74 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
75 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
76 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
77 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
78 if (secondArg->IsBoolean()) {
79 bool stateEffect = secondArg->ToBoolean(vm)->Value();
80 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonStateEffect(nativeNode, stateEffect);
81 } else {
82 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonStateEffect(nativeNode);
83 }
84 return panda::JSValueRef::Undefined(vm);
85 }
86
ResetStateEffect(ArkUIRuntimeCallInfo * runtimeCallInfo)87 ArkUINativeModuleValue ButtonBridge::ResetStateEffect(ArkUIRuntimeCallInfo* runtimeCallInfo)
88 {
89 EcmaVM* vm = runtimeCallInfo->GetVM();
90 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
91 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
92 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
93 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonStateEffect(nativeNode);
94 return panda::JSValueRef::Undefined(vm);
95 }
96
SetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)97 ArkUINativeModuleValue ButtonBridge::SetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
98 {
99 EcmaVM* vm = runtimeCallInfo->GetVM();
100 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
101 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
102 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
103 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
104 Color color;
105 if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color)) {
106 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonFontColor(nativeNode);
107 } else {
108 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonFontColor(nativeNode, color.GetValue());
109 }
110 return panda::JSValueRef::Undefined(vm);
111 }
112
ResetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)113 ArkUINativeModuleValue ButtonBridge::ResetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
114 {
115 EcmaVM* vm = runtimeCallInfo->GetVM();
116 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
117 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
118 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
119 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonFontColor(nativeNode);
120 return panda::JSValueRef::Undefined(vm);
121 }
122
SetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)123 ArkUINativeModuleValue ButtonBridge::SetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
124 {
125 EcmaVM* vm = runtimeCallInfo->GetVM();
126 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
127 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
128 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
129 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
130 Ace::CalcDimension fontSize;
131 if (ArkTSUtils::ParseJsDimensionVpNG(vm, secondArg, fontSize) && fontSize.Unit() != DimensionUnit::PERCENT &&
132 GreatOrEqual(fontSize.Value(), 0.0)) {
133 ArkTSUtils::ParseJsDimensionFp(vm, secondArg, fontSize);
134 } else {
135 auto pipeline = PipelineBase::GetCurrentContext();
136 CHECK_NULL_RETURN(pipeline, panda::NativePointerRef::New(vm, nullptr));
137 auto buttonTheme = pipeline->GetTheme<ButtonTheme>();
138 CHECK_NULL_RETURN(buttonTheme, panda::NativePointerRef::New(vm, nullptr));
139 fontSize = buttonTheme->GetTextStyle().GetFontSize();
140 }
141 int fontSizeUnit = static_cast<int>(fontSize.Unit());
142 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonFontSize(nativeNode, fontSize.Value(), fontSizeUnit);
143 return panda::JSValueRef::Undefined(vm);
144 }
145
ResetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)146 ArkUINativeModuleValue ButtonBridge::ResetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
147 {
148 EcmaVM* vm = runtimeCallInfo->GetVM();
149 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
150 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
151 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
152 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonFontSize(nativeNode);
153 return panda::JSValueRef::Undefined(vm);
154 }
155
SetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)156 ArkUINativeModuleValue ButtonBridge::SetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
157 {
158 EcmaVM* vm = runtimeCallInfo->GetVM();
159 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
160 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
161 Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
162 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
163 std::string fontWeight = DEFAULT_FONT_WEIGHT;
164 if (!fontWeightArg->IsNull()) {
165 if (fontWeightArg->IsNumber()) {
166 fontWeight = std::to_string(fontWeightArg->Int32Value(vm));
167 } else if (fontWeightArg->IsString()) {
168 // enum FontWeight is string.
169 fontWeight = fontWeightArg->ToString(vm)->ToString();
170 }
171 }
172 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonFontWeight(nativeNode, fontWeight.c_str());
173 return panda::JSValueRef::Undefined(vm);
174 }
175
ResetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)176 ArkUINativeModuleValue ButtonBridge::ResetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
177 {
178 EcmaVM* vm = runtimeCallInfo->GetVM();
179 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
180 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
181 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
182 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonFontWeight(nativeNode);
183 return panda::JSValueRef::Undefined(vm);
184 }
185
SetFontStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)186 ArkUINativeModuleValue ButtonBridge::SetFontStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
187 {
188 EcmaVM* vm = runtimeCallInfo->GetVM();
189 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
190 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
191 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
192 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
193 int fontStyle = secondArg->Int32Value(vm);
194 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonFontStyle(nativeNode, fontStyle);
195 return panda::JSValueRef::Undefined(vm);
196 }
197
ResetFontStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)198 ArkUINativeModuleValue ButtonBridge::ResetFontStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
199 {
200 EcmaVM* vm = runtimeCallInfo->GetVM();
201 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
202 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
203 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
204 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonFontStyle(nativeNode);
205 return panda::JSValueRef::Undefined(vm);
206 }
207
SetFontFamily(ArkUIRuntimeCallInfo * runtimeCallInfo)208 ArkUINativeModuleValue ButtonBridge::SetFontFamily(ArkUIRuntimeCallInfo* runtimeCallInfo)
209 {
210 EcmaVM* vm = runtimeCallInfo->GetVM();
211 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
212 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
213 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
214 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
215 std::string fontFamily;
216 if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, secondArg, fontFamily)) {
217 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonFontFamily(nativeNode);
218 return panda::JSValueRef::Undefined(vm);
219 }
220
221 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonFontFamily(nativeNode, fontFamily.c_str());
222 return panda::JSValueRef::Undefined(vm);
223 }
224
ResetFontFamily(ArkUIRuntimeCallInfo * runtimeCallInfo)225 ArkUINativeModuleValue ButtonBridge::ResetFontFamily(ArkUIRuntimeCallInfo* runtimeCallInfo)
226 {
227 EcmaVM* vm = runtimeCallInfo->GetVM();
228 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
229 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
230 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
231 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonFontFamily(nativeNode);
232 return panda::JSValueRef::Undefined(vm);
233 }
234
SetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)235 ArkUINativeModuleValue ButtonBridge::SetBackgroundColor(ArkUIRuntimeCallInfo *runtimeCallInfo)
236 {
237 EcmaVM *vm = runtimeCallInfo->GetVM();
238 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
239 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
240 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
241 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
242 Color color;
243 if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color)) {
244 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonBackgroundColor(nativeNode);
245 } else {
246 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonBackgroundColor(nativeNode, color.GetValue());
247 }
248 return panda::JSValueRef::Undefined(vm);
249 }
250
ResetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)251 ArkUINativeModuleValue ButtonBridge::ResetBackgroundColor(ArkUIRuntimeCallInfo *runtimeCallInfo)
252 {
253 EcmaVM *vm = runtimeCallInfo->GetVM();
254 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
255 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
256 void *nativeNode = firstArg->ToNativePointer(vm)->Value();
257 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonBackgroundColor(nativeNode);
258 return panda::JSValueRef::Undefined(vm);
259 }
260
PushValuesVector(const std::optional<int32_t> & value,std::vector<int32_t> & valuesVector)261 void ButtonBridge::PushValuesVector(const std::optional<int32_t>& value, std::vector<int32_t>& valuesVector)
262 {
263 valuesVector.push_back(static_cast<int32_t>(value.has_value()));
264 if (value.has_value()) {
265 valuesVector.push_back(static_cast<int32_t>(value.value()));
266 } else {
267 valuesVector.push_back(0);
268 }
269 }
270
PutButtonValuesParameters(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,std::vector<int32_t> & valuesVector)271 void ButtonBridge::PutButtonValuesParameters(
272 ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm, std::vector<int32_t>& valuesVector)
273 {
274 std::optional<int32_t> overflowOptional = std::nullopt;
275 Local<JSValueRef> overflowArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_1);
276 if (!overflowArg->IsNull() && overflowArg->IsNumber()) {
277 auto overflow = overflowArg->Int32Value(vm);
278 if (overflow >= 0 && overflow < static_cast<int32_t>(TEXT_OVERFLOWS.size())) {
279 overflowOptional = overflow;
280 }
281 }
282 PushValuesVector(overflowOptional, valuesVector);
283
284 std::optional<int32_t> maxLinesOptional = std::nullopt;
285 Local<JSValueRef> maxLinesArg = runtimeCallInfo->GetCallArgRef(MAX_LINES_ARG_2);
286 if (!maxLinesArg->IsNull() && maxLinesArg->IsNumber()) {
287 auto maxLines = maxLinesArg->Int32Value(vm);
288 maxLinesOptional = Positive(maxLines) ? maxLines : 1;
289 }
290 PushValuesVector(maxLinesOptional, valuesVector);
291
292 std::optional<int32_t> adaptHeightOptional = std::nullopt;
293 Local<JSValueRef> adaptHeightArg = runtimeCallInfo->GetCallArgRef(ADAPT_HEIGHT_ARG_5);
294 if (!adaptHeightArg->IsNull() && adaptHeightArg->IsNumber()) {
295 auto adaptHeight = adaptHeightArg->Int32Value(vm);
296 if (adaptHeight >= 0 && adaptHeight < static_cast<double>(HEIGHT_ADAPTIVE_POLICY.size())) {
297 adaptHeightOptional = adaptHeight;
298 }
299 }
300 PushValuesVector(adaptHeightOptional, valuesVector);
301
302 std::optional<int32_t> fontStyleOptional = std::nullopt;
303 Local<JSValueRef> fontStyleArg = runtimeCallInfo->GetCallArgRef(FONT_STYLE_ARG_8);
304 if (!fontStyleArg->IsNull() && fontStyleArg->IsNumber()) {
305 auto fontStyle = fontStyleArg->Int32Value(vm);
306 if (fontStyle >= 0 && fontStyle < static_cast<int32_t>(FONT_STYLES.size())) {
307 fontStyleOptional = fontStyle;
308 }
309 }
310 PushValuesVector(fontStyleOptional, valuesVector);
311 }
312
PushDimensionVector(const std::optional<Dimension> & valueDimen,std::vector<double> & dimensions)313 void ButtonBridge::PushDimensionVector(const std::optional<Dimension>& valueDimen, std::vector<double>& dimensions)
314 {
315 dimensions.push_back(static_cast<double>(valueDimen.has_value()));
316 if (valueDimen.has_value()) {
317 dimensions.push_back(static_cast<double>(valueDimen.value().Value()));
318 dimensions.push_back(static_cast<double>(valueDimen.value().Unit()));
319 } else {
320 dimensions.push_back(0);
321 dimensions.push_back(0);
322 }
323 }
324
PushButtonDimension(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,std::vector<double> & fontSizesVector,int32_t argIndex)325 void ButtonBridge::PushButtonDimension(
326 ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm, std::vector<double>& fontSizesVector, int32_t argIndex)
327 {
328 Local<JSValueRef> arg = runtimeCallInfo->GetCallArgRef(argIndex);
329 std::optional<CalcDimension> dimemstionOptinal = std::nullopt;
330 CalcDimension parsedDimension;
331 if (ArkTSUtils::ParseJsDimensionFp(vm, arg, parsedDimension, false)) {
332 dimemstionOptinal = parsedDimension;
333 }
334 PushDimensionVector(dimemstionOptinal, fontSizesVector);
335 }
336
PutButtonDimensionParameters(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,std::vector<double> & fontSizesVector)337 void ButtonBridge::PutButtonDimensionParameters(
338 ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm, std::vector<double>& fontSizesVector)
339 {
340 std::vector<int32_t> indexVector = { MIN_FONT_SIZE_ARG_3, MAX_FONT_SIZE_ARG_4, FONT_SIZE_ARG_6 };
341 for (size_t index = 0; index < indexVector.size(); index++) {
342 PushButtonDimension(runtimeCallInfo, vm, fontSizesVector, indexVector[index]);
343 }
344 }
345
PutButtonStringParameters(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm,std::vector<const char * > & stringParameters)346 void ButtonBridge::PutButtonStringParameters(
347 ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm, std::vector<const char*>& stringParameters)
348 {
349 std::vector<std::string> stringsVector;
350 Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(FONT_WEIGHT_ARG_7);
351 std::string fontWeight = DEFAULT_FONT_WEIGHT;
352 if (!fontWeightArg->IsNull()) {
353 if (fontWeightArg->IsNumber()) {
354 fontWeight = std::to_string(fontWeightArg->Int32Value(vm));
355 } else if (fontWeightArg->IsString()) {
356 // enum FontWeight is sent as string.
357 fontWeight = fontWeightArg->ToString(vm)->ToString();
358 }
359 }
360 stringsVector.push_back(fontWeight);
361
362 Local<JSValueRef> fontFamilyArg = runtimeCallInfo->GetCallArgRef(FONT_FAMILY_ARG_9);
363 std::string strFontFamilies;
364 if (ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArg, strFontFamilies)) {
365 stringsVector.push_back(strFontFamilies);
366 } else {
367 strFontFamilies = NONE_FONT_FAMILY;
368 stringsVector.push_back(strFontFamilies);
369 }
370
371 for (size_t index = 0; index < stringsVector.size(); index++) {
372 stringParameters.push_back(stringsVector[index].c_str());
373 }
374 }
375
SetLabelStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)376 ArkUINativeModuleValue ButtonBridge::SetLabelStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
377 {
378 EcmaVM* vm = runtimeCallInfo->GetVM();
379 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
380 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
381 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
382 std::vector<int32_t> valuesVector;
383 PutButtonValuesParameters(runtimeCallInfo, vm, valuesVector);
384 std::vector<double> fontSizesVector;
385 PutButtonDimensionParameters(runtimeCallInfo, vm, fontSizesVector);
386 std::vector<const char*> stringParameters;
387 PutButtonStringParameters(runtimeCallInfo, vm, stringParameters);
388 std::vector<size_t> dataCountVector;
389 dataCountVector.push_back(stringParameters.size());
390 dataCountVector.push_back(valuesVector.size());
391 dataCountVector.push_back(fontSizesVector.size());
392 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonLabelStyle(
393 nativeNode, stringParameters.data(), valuesVector.data(), fontSizesVector.data(), dataCountVector.data());
394 return panda::JSValueRef::Undefined(vm);
395 }
396
ResetLabelStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)397 ArkUINativeModuleValue ButtonBridge::ResetLabelStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
398 {
399 EcmaVM* vm = runtimeCallInfo->GetVM();
400 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
401 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(CALL_ARG_0);
402 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
403 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonLabelStyle(nativeNode);
404 return panda::JSValueRef::Undefined(vm);
405 }
406
ParseBorderRadius(EcmaVM * vm,const Local<JSValueRef> & args,std::optional<CalcDimension> & optionalDimention)407 void ParseBorderRadius(EcmaVM* vm, const Local<JSValueRef>& args, std::optional<CalcDimension>& optionalDimention)
408 {
409 CalcDimension valueDimen;
410 if (!args->IsUndefined() && ArkTSUtils::ParseJsDimensionVp(vm, args, valueDimen, false)) {
411 optionalDimention = valueDimen;
412 }
413 }
414
PushBorderRadiusVector(const std::optional<CalcDimension> & valueDimen,std::vector<double> & options)415 void PushBorderRadiusVector(const std::optional<CalcDimension>& valueDimen, std::vector<double> &options)
416 {
417 options.push_back(static_cast<double>(valueDimen.has_value()));
418 if (valueDimen.has_value()) {
419 options.push_back(static_cast<double>(valueDimen.value().Value()));
420 options.push_back(static_cast<double>(valueDimen.value().Unit()));
421 } else {
422 options.push_back(0);
423 options.push_back(0);
424 }
425 }
426
SetButtonBorderRadius(ArkUIRuntimeCallInfo * runtimeCallInfo)427 ArkUINativeModuleValue ButtonBridge::SetButtonBorderRadius(ArkUIRuntimeCallInfo* runtimeCallInfo)
428 {
429 EcmaVM* vm = runtimeCallInfo->GetVM();
430 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
431 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
432 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
433 Local<JSValueRef> topLeftArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_1); // topLeft value
434 Local<JSValueRef> topRightArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_2); // topRight value
435 Local<JSValueRef> bottomLeftArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_3); // bottomLeft value
436 Local<JSValueRef> bottomRightArgs = runtimeCallInfo->GetCallArgRef(CALL_ARG_4); // bottomRight value
437
438 if (topLeftArgs->IsUndefined() && topRightArgs->IsUndefined() && bottomLeftArgs->IsUndefined() &&
439 bottomRightArgs->IsUndefined()) {
440 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonBorderRadius(nativeNode);
441 return panda::JSValueRef::Undefined(vm);
442 }
443
444 std::optional<CalcDimension> radiusTopLeft;
445 std::optional<CalcDimension> radiusTopRight;
446 std::optional<CalcDimension> radiusBottomLeft;
447 std::optional<CalcDimension> radiusBottomRight;
448
449 ParseBorderRadius(vm, topLeftArgs, radiusTopLeft);
450 ParseBorderRadius(vm, topRightArgs, radiusTopRight);
451 ParseBorderRadius(vm, bottomLeftArgs, radiusBottomLeft);
452 ParseBorderRadius(vm, bottomRightArgs, radiusBottomRight);
453
454 std::vector<double> options;
455 PushBorderRadiusVector(radiusTopLeft, options);
456 PushBorderRadiusVector(radiusTopRight, options);
457 PushBorderRadiusVector(radiusBottomLeft, options);
458 PushBorderRadiusVector(radiusBottomRight, options);
459
460 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonBorderRadius(nativeNode, options.data(), options.size());
461 return panda::JSValueRef::Undefined(vm);
462 }
463
ResetButtonBorderRadius(ArkUIRuntimeCallInfo * runtimeCallInfo)464 ArkUINativeModuleValue ButtonBridge::ResetButtonBorderRadius(ArkUIRuntimeCallInfo* runtimeCallInfo)
465 {
466 EcmaVM* vm = runtimeCallInfo->GetVM();
467 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
468 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
469 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
470 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonBorderRadius(nativeNode);
471 return panda::JSValueRef::Undefined(vm);
472 }
473
SetButtonBorder(ArkUIRuntimeCallInfo * runtimeCallInfo)474 ArkUINativeModuleValue ButtonBridge::SetButtonBorder(ArkUIRuntimeCallInfo* runtimeCallInfo)
475 {
476 CommonBridge::SetBorder(runtimeCallInfo);
477 EcmaVM* vm = runtimeCallInfo->GetVM();
478 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
479 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
480 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
481
482 Local<JSValueRef> topLeftArgs = runtimeCallInfo->GetCallArgRef(9); // 9:topLeft value
483 Local<JSValueRef> topRightArgs = runtimeCallInfo->GetCallArgRef(10); // 10:topRight value
484 Local<JSValueRef> bottomLeftArgs = runtimeCallInfo->GetCallArgRef(11); // 11:bottomLeft value
485 Local<JSValueRef> bottomRightArgs = runtimeCallInfo->GetCallArgRef(12); // 12:bottomRight value
486 if (topLeftArgs->IsUndefined() && topRightArgs->IsUndefined() && bottomLeftArgs->IsUndefined() &&
487 bottomRightArgs->IsUndefined()) {
488 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonBorderRadius(nativeNode);
489 return panda::JSValueRef::Undefined(vm);
490 }
491
492 std::optional<CalcDimension> radiusTopLeft;
493 std::optional<CalcDimension> radiusTopRight;
494 std::optional<CalcDimension> radiusBottomLeft;
495 std::optional<CalcDimension> radiusBottomRight;
496
497 ParseBorderRadius(vm, topLeftArgs, radiusTopLeft);
498 ParseBorderRadius(vm, topRightArgs, radiusTopRight);
499 ParseBorderRadius(vm, bottomLeftArgs, radiusBottomLeft);
500 ParseBorderRadius(vm, bottomRightArgs, radiusBottomRight);
501
502 std::vector<double> options;
503 PushBorderRadiusVector(radiusTopLeft, options);
504 PushBorderRadiusVector(radiusTopRight, options);
505 PushBorderRadiusVector(radiusBottomLeft, options);
506 PushBorderRadiusVector(radiusBottomRight, options);
507
508 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonBorderRadius(nativeNode, options.data(), options.size());
509 return panda::JSValueRef::Undefined(vm);
510 }
511
ResetButtonBorder(ArkUIRuntimeCallInfo * runtimeCallInfo)512 ArkUINativeModuleValue ButtonBridge::ResetButtonBorder(ArkUIRuntimeCallInfo* runtimeCallInfo)
513 {
514 EcmaVM* vm = runtimeCallInfo->GetVM();
515 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
516 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
517 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
518 GetArkUIInternalNodeAPI()->GetCommonModifier().ResetBorder(nativeNode);
519 return panda::JSValueRef::Undefined(vm);
520 }
521
SetButtonSize(ArkUIRuntimeCallInfo * runtimeCallInfo)522 ArkUINativeModuleValue ButtonBridge::SetButtonSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
523 {
524 EcmaVM* vm = runtimeCallInfo->GetVM();
525 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
526 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
527 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
528 Local<JSValueRef> widthArgs = runtimeCallInfo->GetCallArgRef(1); // 1:width value
529 Local<JSValueRef> heightArgs = runtimeCallInfo->GetCallArgRef(2); // 2:height value
530 CalcDimension width;
531 std::string widthStr;
532 if (widthArgs->IsUndefined() || widthArgs->IsNull()) {
533 widthStr = "undefined";
534 } else {
535 ArkTSUtils::ParseJsDimensionVp(vm, widthArgs, width);
536 widthStr = std::to_string(width.Value());
537 }
538 CalcDimension height;
539 std::string heightStr;
540 if (heightArgs->IsUndefined() || heightArgs->IsNull()) {
541 heightStr = "undefined";
542 } else {
543 ArkTSUtils::ParseJsDimensionVp(vm, heightArgs, height);
544 heightStr = std::to_string(height.Value());
545 }
546 GetArkUIInternalNodeAPI()->GetButtonModifier().SetButtonSize(nativeNode, widthStr.c_str(),
547 static_cast<int32_t>(width.Unit()), heightStr.c_str(), static_cast<int32_t>(height.Unit()));
548 return panda::JSValueRef::Undefined(vm);
549 }
550
ResetButtonSize(ArkUIRuntimeCallInfo * runtimeCallInfo)551 ArkUINativeModuleValue ButtonBridge::ResetButtonSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
552 {
553 EcmaVM* vm = runtimeCallInfo->GetVM();
554 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
555 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0); // 0:node info
556 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
557 GetArkUIInternalNodeAPI()->GetButtonModifier().ResetButtonSize(nativeNode);
558 return panda::JSValueRef::Undefined(vm);
559 }
560 } // namespace OHOS::Ace::NG
561