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 "core/interfaces/native/node/button_modifier.h"
16
17 #include <unordered_map>
18
19 #include "bridge/common/utils/utils.h"
20 #include "core/components/button/button_theme.h"
21 #include "core/components/common/layout/constants.h"
22 #include "core/components/common/properties/text_style.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/base/view_abstract.h"
25 #include "core/components_ng/pattern/button/button_model_ng.h"
26 #include "core/components_ng/pattern/button/button_request_data.h"
27 #include "core/pipeline/base/element_register.h"
28 #include "frameworks/core/components/button/button_theme.h"
29
30 namespace OHOS::Ace::NG {
31 namespace {
32 constexpr int32_t DEFAULT_BUTTON_TYPE = (int32_t)ButtonType::CAPSULE;
33 constexpr bool DEFAULT_STATE_EFFECT = true;
34 constexpr Ace::FontWeight DEFAULT_FONT_WEIGHT = Ace::FontWeight::NORMAL;
35 constexpr Ace::FontStyle DEFAULT_FONT_STYLE = Ace::FontStyle::NORMAL;
36 constexpr uint32_t INDEX_STRING_FONT_WEIGHT_0 = 0;
37 constexpr uint32_t INDEX_STRING_FONT_FAMILY_1 = 1;
38 constexpr uint32_t INDEX_VALUE_TEXT_OVERFLOW_0 = 0;
39 constexpr uint32_t INDEX_VALUE_MAX_LINES_1 = 1;
40 constexpr uint32_t INDEX_VALUE_ADAPT_HEIGHT_2 = 2;
41 constexpr uint32_t INDEX_VALUE_FONT_STYLE_3 = 3;
42 constexpr uint32_t INDEX_DIMENSION_MIN_FONT_SIZE_0 = 0;
43 constexpr uint32_t INDEX_DIMENSION_MAX_FONT_SIZE_1 = 1;
44 constexpr uint32_t INDEX_DIMENSION_FONT_SIZE_2 = 2;
45 constexpr uint32_t INDEX_STRING_ARRAY_COUNT = 0;
46 constexpr uint32_t INDEX_VALUE_ARRAY_COUNT = 1;
47 constexpr uint32_t INDEX_DIMENSION_ARRAY_COUNT = 2;
48 constexpr uint32_t INDEX_INVALID_FONT_FAMILY_0 = 0;
49 const std::string DEFAULT_FONT_FAMILY = "HarmonyOS Sans";
50 constexpr int32_t OFFSET_1 = 1;
51 constexpr int32_t OFFSET_2 = 2;
52 constexpr int32_t OFFSET_3 = 3;
53 constexpr int32_t BORDER_RADIUS_SIZE = 12; // BorderRadius array size
54 const std::vector<TextOverflow> TEXT_OVERFLOWS = { TextOverflow::NONE, TextOverflow::CLIP, TextOverflow::ELLIPSIS,
55 TextOverflow::MARQUEE };
56 const std::vector<Ace::FontStyle> FONT_STYLES = { Ace::FontStyle::NORMAL, Ace::FontStyle::ITALIC };
57 const std::vector<TextHeightAdaptivePolicy> HEIGHT_ADAPTIVE_POLICY = { TextHeightAdaptivePolicy::MAX_LINES_FIRST,
58 TextHeightAdaptivePolicy::MIN_FONT_SIZE_FIRST, TextHeightAdaptivePolicy::LAYOUT_CONSTRAINT_FIRST };
59 const std::string NONE_FONT_FAMILY = "NoneFontFamily";
60 const uint32_t ERROR_UINT_CODE = -1;
61 const float ERROR_FLOAT_CODE = -1.0f;
62 const int32_t ERROR_INT_CODE = -1;
63 std::string g_strValue;
64
65 const std::unordered_map<int, DimensionUnit> DIMENSION_UNIT_MAP = {
66 { -2, DimensionUnit::INVALID },
67 { -1, DimensionUnit::NONE },
68 { 0, DimensionUnit::PX },
69 { 1, DimensionUnit::VP },
70 { 2, DimensionUnit::FP },
71 { 3, DimensionUnit::PERCENT },
72 { 4, DimensionUnit::LPX },
73 { 5, DimensionUnit::AUTO },
74 { 6, DimensionUnit::CALC },
75 };
76
77 const std::vector<FontWeight> BUTTON_FONT_WEIGHTS = {
78 FontWeight::W100,
79 FontWeight::W200,
80 FontWeight::W300,
81 FontWeight::W400,
82 FontWeight::W500,
83 FontWeight::W600,
84 FontWeight::W700,
85 FontWeight::W800,
86 FontWeight::W900,
87 FontWeight::BOLD,
88 FontWeight::BOLDER,
89 FontWeight::LIGHTER,
90 FontWeight::MEDIUM,
91 FontWeight::NORMAL,
92 FontWeight::REGULAR,
93 };
94 } // namespace
95
SetOptionalBorderRadius(std::optional<Dimension> & optionalDimension,const ArkUI_Float32 * values,int32_t valuesSize,int32_t & offset)96 void SetOptionalBorderRadius(
97 std::optional<Dimension>& optionalDimension, const ArkUI_Float32* values, int32_t valuesSize, int32_t& offset)
98 {
99 bool hasValue = static_cast<bool>(values[offset]);
100 if (hasValue) {
101 optionalDimension =
102 Dimension(values[offset + OFFSET_1], static_cast<OHOS::Ace::DimensionUnit>(values[offset + OFFSET_2]));
103 }
104 offset = offset + OFFSET_3;
105 }
106
SetButtonLabel(ArkUINodeHandle node,ArkUI_CharPtr label)107 void SetButtonLabel(ArkUINodeHandle node, ArkUI_CharPtr label)
108 {
109 auto* frameNode = reinterpret_cast<FrameNode*>(node);
110 CHECK_NULL_VOID(frameNode);
111 ButtonModelNG::SetLabel(frameNode, label);
112 }
113
ResetButtonLabel(ArkUINodeHandle node)114 void ResetButtonLabel(ArkUINodeHandle node)
115 {
116 auto* frameNode = reinterpret_cast<FrameNode*>(node);
117 CHECK_NULL_VOID(frameNode);
118 ButtonModelNG::SetLabel(frameNode, "");
119 }
120
SetButtonType(ArkUINodeHandle node,int type)121 void SetButtonType(ArkUINodeHandle node, int type)
122 {
123 auto* frameNode = reinterpret_cast<FrameNode*>(node);
124 CHECK_NULL_VOID(frameNode);
125 if ((ButtonType)type == ButtonType::CAPSULE || (ButtonType)type == ButtonType::CIRCLE ||
126 (ButtonType)type == ButtonType::ARC || (ButtonType)type == ButtonType::NORMAL ||
127 (ButtonType)type == ButtonType::ROUNDED_RECTANGLE) {
128 ButtonModelNG::SetType(frameNode, type);
129 }
130 }
131
ResetButtonType(ArkUINodeHandle node)132 void ResetButtonType(ArkUINodeHandle node)
133 {
134 auto* frameNode = reinterpret_cast<FrameNode*>(node);
135 CHECK_NULL_VOID(frameNode);
136 ButtonModelNG::SetType(frameNode, DEFAULT_BUTTON_TYPE);
137 return;
138 }
139
SetButtonStateEffect(ArkUINodeHandle node,ArkUI_Bool stateEffect)140 void SetButtonStateEffect(ArkUINodeHandle node, ArkUI_Bool stateEffect)
141 {
142 auto* frameNode = reinterpret_cast<FrameNode*>(node);
143 CHECK_NULL_VOID(frameNode);
144 ButtonModelNG::SetStateEffect(frameNode, stateEffect);
145 }
146
ResetButtonStateEffect(ArkUINodeHandle node)147 void ResetButtonStateEffect(ArkUINodeHandle node)
148 {
149 auto* frameNode = reinterpret_cast<FrameNode*>(node);
150 CHECK_NULL_VOID(frameNode);
151 ButtonModelNG::SetStateEffect(frameNode, DEFAULT_STATE_EFFECT);
152 }
153
SetButtonFontColor(ArkUINodeHandle node,uint32_t fontColor)154 void SetButtonFontColor(ArkUINodeHandle node, uint32_t fontColor)
155 {
156 auto* frameNode = reinterpret_cast<FrameNode*>(node);
157 CHECK_NULL_VOID(frameNode);
158 ButtonModelNG::SetFontColor(frameNode, Color(fontColor));
159 }
160
ResetButtonFontColor(ArkUINodeHandle node)161 void ResetButtonFontColor(ArkUINodeHandle node)
162 {
163 auto* frameNode = reinterpret_cast<FrameNode*>(node);
164 CHECK_NULL_VOID(frameNode);
165 auto pipeline = PipelineBase::GetCurrentContext();
166 CHECK_NULL_VOID(pipeline);
167 auto buttonTheme = pipeline->GetTheme<ButtonTheme>();
168 CHECK_NULL_VOID(buttonTheme);
169 Color textColor = buttonTheme->GetTextStyle().GetTextColor();
170 ButtonModelNG::SetFontColor(frameNode, textColor);
171 }
172
ResetButtonFontSizeInternal(ArkUINodeHandle node)173 void ResetButtonFontSizeInternal(ArkUINodeHandle node)
174 {
175 auto* frameNode = reinterpret_cast<FrameNode*>(node);
176 CHECK_NULL_VOID(frameNode);
177 auto pipeline = PipelineBase::GetCurrentContext();
178 CHECK_NULL_VOID(pipeline);
179 auto buttonTheme = pipeline->GetTheme<ButtonTheme>();
180 CHECK_NULL_VOID(buttonTheme);
181 CalcDimension fontSize = buttonTheme->GetTextStyle().GetFontSize();
182 ButtonModelNG::SetFontSize(frameNode, fontSize);
183 }
184
SetButtonFontSize(ArkUINodeHandle node,ArkUI_Float32 fontSizeValue,int fontSizeUnit)185 void SetButtonFontSize(ArkUINodeHandle node, ArkUI_Float32 fontSizeValue, int fontSizeUnit)
186 {
187 auto* frameNode = reinterpret_cast<FrameNode*>(node);
188 CHECK_NULL_VOID(frameNode);
189 if (LessNotEqual(fontSizeValue, 0.0)) {
190 ResetButtonFontSizeInternal(node);
191 } else {
192 ButtonModelNG::SetFontSize(frameNode, CalcDimension(fontSizeValue, (DimensionUnit)fontSizeUnit));
193 }
194 }
195
ResetButtonFontSize(ArkUINodeHandle node)196 void ResetButtonFontSize(ArkUINodeHandle node)
197 {
198 ResetButtonFontSizeInternal(node);
199 }
200
SetButtonFontWeight(ArkUINodeHandle node,ArkUI_CharPtr fontWeight)201 void SetButtonFontWeight(ArkUINodeHandle node, ArkUI_CharPtr fontWeight)
202 {
203 auto* frameNode = reinterpret_cast<FrameNode*>(node);
204 CHECK_NULL_VOID(frameNode);
205 std::string fontWeightStr = fontWeight;
206 ButtonModelNG::SetFontWeight(frameNode, Framework::ConvertStrToFontWeight(fontWeightStr));
207 }
208
SetButtonFontWeightEnum(ArkUINodeHandle node,int fontWeight)209 void SetButtonFontWeightEnum(ArkUINodeHandle node, int fontWeight)
210 {
211 auto* frameNode = reinterpret_cast<FrameNode*>(node);
212 CHECK_NULL_VOID(frameNode);
213 ButtonModelNG::SetFontWeight(frameNode, static_cast<FontWeight>(fontWeight));
214 }
215
ResetButtonFontWeight(ArkUINodeHandle node)216 void ResetButtonFontWeight(ArkUINodeHandle node)
217 {
218 auto* frameNode = reinterpret_cast<FrameNode*>(node);
219 CHECK_NULL_VOID(frameNode);
220 ButtonModelNG::SetFontWeight(frameNode, DEFAULT_FONT_WEIGHT);
221 }
222
SetButtonFontStyle(ArkUINodeHandle node,int32_t fontStyle)223 void SetButtonFontStyle(ArkUINodeHandle node, int32_t fontStyle)
224 {
225 auto* frameNode = reinterpret_cast<FrameNode*>(node);
226 CHECK_NULL_VOID(frameNode);
227 if (fontStyle < 0 || fontStyle >= static_cast<int32_t>(FONT_STYLES.size())) {
228 ButtonModelNG::SetFontStyle(frameNode, DEFAULT_FONT_STYLE);
229 } else {
230 ButtonModelNG::SetFontStyle(frameNode, FONT_STYLES[fontStyle]);
231 }
232 }
233
ResetButtonFontStyle(ArkUINodeHandle node)234 void ResetButtonFontStyle(ArkUINodeHandle node)
235 {
236 auto* frameNode = reinterpret_cast<FrameNode*>(node);
237 CHECK_NULL_VOID(frameNode);
238 ButtonModelNG::SetFontStyle(frameNode, DEFAULT_FONT_STYLE);
239 }
240
SetButtonFontFamily(ArkUINodeHandle node,ArkUI_CharPtr fontFamily)241 void SetButtonFontFamily(ArkUINodeHandle node, ArkUI_CharPtr fontFamily)
242 {
243 auto* frameNode = reinterpret_cast<FrameNode*>(node);
244 CHECK_NULL_VOID(frameNode);
245 std::string familiesStr = fontFamily;
246 std::vector<std::string> fontFamilyResult = Framework::ConvertStrToFontFamilies(familiesStr);
247 ButtonModelNG::SetFontFamily(frameNode, fontFamilyResult);
248 }
249
ResetButtonFontFamily(ArkUINodeHandle node)250 void ResetButtonFontFamily(ArkUINodeHandle node)
251 {
252 auto* frameNode = reinterpret_cast<FrameNode*>(node);
253 CHECK_NULL_VOID(frameNode);
254 std::string familiesStr = DEFAULT_FONT_FAMILY;
255 std::vector<std::string> fontFamilyResult = Framework::ConvertStrToFontFamilies(familiesStr);
256 ButtonModelNG::SetFontFamily(frameNode, fontFamilyResult);
257 }
258
ButtonCompleteParameters(ButtonParameters & buttonParameters)259 void ButtonCompleteParameters(ButtonParameters& buttonParameters)
260 {
261 auto pipeline = PipelineBase::GetCurrentContext();
262 CHECK_NULL_VOID(pipeline);
263 auto buttonTheme = pipeline->GetTheme<ButtonTheme>();
264 if (!buttonTheme) {
265 return;
266 }
267 auto textStyle = buttonTheme->GetTextStyle();
268 if (!buttonParameters.textOverflow.has_value()) {
269 buttonParameters.textOverflow = TextOverflow::CLIP;
270 }
271 if (!buttonParameters.maxLines.has_value()) {
272 buttonParameters.maxLines = buttonTheme->GetTextMaxLines();
273 }
274 if (!buttonParameters.fontSize.has_value()) {
275 buttonParameters.fontSize = textStyle.GetFontSize();
276 }
277 if (!buttonParameters.fontWeight.has_value()) {
278 buttonParameters.fontWeight = textStyle.GetFontWeight();
279 }
280 if (!buttonParameters.fontStyle.has_value()) {
281 buttonParameters.fontStyle = textStyle.GetFontStyle();
282 }
283 if (!buttonParameters.heightAdaptivePolicy.has_value()) {
284 buttonParameters.heightAdaptivePolicy = TextHeightAdaptivePolicy::MAX_LINES_FIRST;
285 }
286 }
287
SetButtonDimension(const ArkUI_Float32 * dimensionArray,uint32_t offset,const size_t dataCount,std::optional<CalcDimension> & optDimension)288 bool SetButtonDimension(
289 const ArkUI_Float32* dimensionArray, uint32_t offset, const size_t dataCount,
290 std::optional<CalcDimension>& optDimension)
291 {
292 CHECK_NULL_RETURN(dimensionArray, false);
293 auto hasValue = dimensionArray[offset];
294 if (!static_cast<bool>(hasValue)) {
295 return false;
296 }
297 uint32_t valueIndex = offset + 1;
298 uint32_t unitIndex = offset + 2;
299 if (unitIndex >= dataCount) {
300 return false;
301 }
302 auto value = dimensionArray[valueIndex];
303 auto unit = dimensionArray[unitIndex];
304 DimensionUnit unitValue = static_cast<DimensionUnit>(unit);
305 CalcDimension dimensionValue = CalcDimension(value, unitValue);
306 optDimension = dimensionValue;
307 return true;
308 }
309
SetButtonDimensionParameters(const ArkUI_Float32 * dimensionArray,const size_t dataCount,ButtonParameters & buttonParameters)310 void SetButtonDimensionParameters(
311 const ArkUI_Float32* dimensionArray, const size_t dataCount, ButtonParameters& buttonParameters)
312 {
313 CHECK_NULL_VOID(dimensionArray);
314 uint32_t step = 3;
315 uint32_t minFontSizeIndex = INDEX_DIMENSION_MIN_FONT_SIZE_0;
316 std::optional<CalcDimension> minFontSizeOptional = std::nullopt;
317 if (SetButtonDimension(dimensionArray, minFontSizeIndex, dataCount, minFontSizeOptional)) {
318 buttonParameters.minFontSize = minFontSizeOptional;
319 }
320 uint32_t maxFontSizeIndex = INDEX_DIMENSION_MAX_FONT_SIZE_1 * step;
321 std::optional<CalcDimension> maxFontSizeOptional = std::nullopt;
322 if (SetButtonDimension(dimensionArray, maxFontSizeIndex, dataCount, maxFontSizeOptional)) {
323 buttonParameters.maxFontSize = maxFontSizeOptional;
324 }
325 uint32_t fontSizeIndex = INDEX_DIMENSION_FONT_SIZE_2 * step;
326 std::optional<CalcDimension> fontSizeOptional = std::nullopt;
327 if (SetButtonDimension(dimensionArray, fontSizeIndex, dataCount, fontSizeOptional)) {
328 buttonParameters.fontSize = fontSizeOptional;
329 }
330 }
331
SetButtonValue(const int32_t * valueArray,uint32_t index,const size_t dataCount,int32_t & result)332 bool SetButtonValue(const int32_t* valueArray, uint32_t index, const size_t dataCount, int32_t& result)
333 {
334 CHECK_NULL_RETURN(valueArray, false);
335 uint32_t step = 2;
336 auto hasValueIndex = index * step;
337 auto valueIndex = hasValueIndex + 1;
338 if (valueIndex >= dataCount) {
339 return false;
340 }
341 if (static_cast<bool>(valueArray[hasValueIndex])) {
342 result = valueArray[valueIndex];
343 return true;
344 }
345 return false;
346 }
347
SetButtonValueParameters(const int32_t * valueArray,const size_t dataCount,ButtonParameters & buttonParameters)348 void SetButtonValueParameters(const int32_t* valueArray, const size_t dataCount, ButtonParameters& buttonParameters)
349 {
350 CHECK_NULL_VOID(valueArray);
351 int32_t result = 0;
352 buttonParameters.textOverflow = TextOverflow::ELLIPSIS;
353 if (SetButtonValue(valueArray, INDEX_VALUE_TEXT_OVERFLOW_0, dataCount, result) && result >= 0 &&
354 result < static_cast<int32_t>(TEXT_OVERFLOWS.size())) {
355 buttonParameters.textOverflow = TEXT_OVERFLOWS[result];
356 }
357 buttonParameters.maxLines = std::nullopt;
358 if (SetButtonValue(valueArray, INDEX_VALUE_MAX_LINES_1, dataCount, result) && result >= 0) {
359 buttonParameters.maxLines = Positive(result) ? result : 1;
360 }
361 buttonParameters.heightAdaptivePolicy = std::nullopt;
362 if (SetButtonValue(valueArray, INDEX_VALUE_ADAPT_HEIGHT_2, dataCount, result) && result >= 0 &&
363 result < static_cast<int32_t>(HEIGHT_ADAPTIVE_POLICY.size())) {
364 buttonParameters.heightAdaptivePolicy = HEIGHT_ADAPTIVE_POLICY[result];
365 }
366 buttonParameters.fontStyle = std::nullopt;
367 if (SetButtonValue(valueArray, INDEX_VALUE_FONT_STYLE_3, dataCount, result) && result >= 0 &&
368 result < static_cast<int32_t>(FONT_STYLES.size())) {
369 buttonParameters.fontStyle = FONT_STYLES[result];
370 }
371 }
372
SetButtonStringParameters(ArkUI_CharPtr * stringParameters,const size_t dataCount,ButtonParameters & buttonParameters)373 void SetButtonStringParameters(
374 ArkUI_CharPtr* stringParameters, const size_t dataCount, ButtonParameters& buttonParameters)
375 {
376 CHECK_NULL_VOID(stringParameters);
377 buttonParameters.fontWeight = std::nullopt;
378 if (INDEX_STRING_FONT_WEIGHT_0 < dataCount && stringParameters[INDEX_STRING_FONT_WEIGHT_0] != nullptr) {
379 std::string fontWeightStr = stringParameters[INDEX_STRING_FONT_WEIGHT_0];
380 buttonParameters.fontWeight = Framework::ConvertStrToFontWeight(fontWeightStr);
381 }
382 buttonParameters.fontFamily = std::nullopt;
383 if (INDEX_STRING_FONT_FAMILY_1 < dataCount && stringParameters[INDEX_STRING_FONT_FAMILY_1] != nullptr) {
384 std::string familiesStr = stringParameters[INDEX_STRING_FONT_FAMILY_1];
385 std::vector<std::string> fontFamilyResult = Framework::ConvertStrToFontFamilies(familiesStr);
386 if (fontFamilyResult.size() == 1 &&
387 fontFamilyResult[INDEX_INVALID_FONT_FAMILY_0].compare(NONE_FONT_FAMILY) == 0) {
388 return;
389 }
390 buttonParameters.fontFamily = fontFamilyResult;
391 }
392 }
393
SetButtonLabelStyle(ArkUINodeHandle node,ArkUI_CharPtr * stringParameters,const ArkUI_Int32 * valueArray,const ArkUI_Float32 * dimensionArray,const ArkUI_Uint32 * dataCountArray)394 void SetButtonLabelStyle(ArkUINodeHandle node, ArkUI_CharPtr* stringParameters, const ArkUI_Int32* valueArray,
395 const ArkUI_Float32* dimensionArray, const ArkUI_Uint32* dataCountArray)
396 {
397 auto* frameNode = reinterpret_cast<FrameNode*>(node);
398 CHECK_NULL_VOID(frameNode);
399 ButtonParameters buttonParameters;
400 SetButtonStringParameters(stringParameters, dataCountArray[INDEX_STRING_ARRAY_COUNT], buttonParameters);
401 SetButtonValueParameters(valueArray, dataCountArray[INDEX_VALUE_ARRAY_COUNT], buttonParameters);
402 SetButtonDimensionParameters(dimensionArray, dataCountArray[INDEX_DIMENSION_ARRAY_COUNT], buttonParameters);
403 ButtonCompleteParameters(buttonParameters);
404 ButtonModelNG::SetLabelStyle(frameNode, buttonParameters);
405 }
406
ResetButtonLabelStyle(ArkUINodeHandle node)407 void ResetButtonLabelStyle(ArkUINodeHandle node)
408 {}
409
SetButtonBackgroundColor(ArkUINodeHandle node,uint32_t color)410 void SetButtonBackgroundColor(ArkUINodeHandle node, uint32_t color)
411 {
412 auto* frameNode = reinterpret_cast<FrameNode*>(node);
413 CHECK_NULL_VOID(frameNode);
414 ButtonModelNG::BackgroundColor(frameNode, Color(color), true);
415 }
416
ResetButtonBackgroundColor(ArkUINodeHandle node)417 void ResetButtonBackgroundColor(ArkUINodeHandle node)
418 {
419 auto* frameNode = reinterpret_cast<FrameNode*>(node);
420 CHECK_NULL_VOID(frameNode);
421 Color backgroundColor;
422 auto pipeline = PipelineBase::GetCurrentContext();
423 CHECK_NULL_VOID(pipeline);
424 auto buttonTheme = pipeline->GetTheme<ButtonTheme>();
425 CHECK_NULL_VOID(buttonTheme);
426 backgroundColor = buttonTheme->GetBgColor();
427 ButtonModelNG::BackgroundColor(frameNode, backgroundColor, false);
428 }
429
430 /**
431 * @param src source borderRadius
432 * @param options option value
433 * values[offset + 0], option[offset + 1], option[offset + 2]: borderRadius topLeft(hasValue, value, unit)
434 * values[offset + 3], option[offset + 4], option[offset + 5]: borderRadius topRight(hasValue, value, unit)
435 * values[offset + 6], option[offset + 7], option[offset + 8]: borderRadius bottomLeft(hasValue, value, unit)
436 * values[offset + 9], option[offset + 10], option[offset + 11]: borderRadius bottomRight(hasValue, value, unit)
437 * @param optionsLength options valuesSize
438 */
SetButtonBorderRadius(ArkUINodeHandle node,const ArkUI_Float32 * values,int32_t valuesSize)439 void SetButtonBorderRadius(ArkUINodeHandle node, const ArkUI_Float32* values, int32_t valuesSize)
440 {
441 if ((values == nullptr) || (valuesSize != BORDER_RADIUS_SIZE)) {
442 return;
443 }
444 auto* frameNode = reinterpret_cast<FrameNode*>(node);
445 CHECK_NULL_VOID(frameNode);
446 int32_t offset = 0;
447 std::optional<Dimension> radiusTopLeft;
448 std::optional<Dimension> radiusTopRight;
449 std::optional<Dimension> radiusBottomLeft;
450 std::optional<Dimension> radiusBottomRight;
451
452 SetOptionalBorderRadius(radiusTopLeft, values, valuesSize, offset);
453 SetOptionalBorderRadius(radiusTopRight, values, valuesSize, offset);
454 SetOptionalBorderRadius(radiusBottomLeft, values, valuesSize, offset);
455 SetOptionalBorderRadius(radiusBottomRight, values, valuesSize, offset);
456 ButtonModelNG::SetBorderRadius(frameNode, radiusTopLeft, radiusTopRight, radiusBottomLeft, radiusBottomRight);
457 }
458
ResetButtonBorderRadius(ArkUINodeHandle node)459 void ResetButtonBorderRadius(ArkUINodeHandle node)
460 {
461 auto* frameNode = reinterpret_cast<FrameNode*>(node);
462 CHECK_NULL_VOID(frameNode);
463 OHOS::Ace::Dimension reset;
464 ButtonModelNG::SetBorderRadius(frameNode, reset);
465 }
466
SetButtonSize(ArkUINodeHandle node,ArkUI_CharPtr widthValue,int32_t widthUnit,ArkUI_CharPtr heightValue,int32_t heightUnit)467 void SetButtonSize(
468 ArkUINodeHandle node, ArkUI_CharPtr widthValue, int32_t widthUnit, ArkUI_CharPtr heightValue, int32_t heightUnit)
469 {
470 auto* frameNode = reinterpret_cast<FrameNode*>(node);
471 CHECK_NULL_VOID(frameNode);
472 std::string widthValueStr = std::string(widthValue);
473 std::string heightValueStr = std::string(heightValue);
474 std::optional<CalcDimension> widthInfo;
475 std::optional<CalcDimension> heightInfo;
476 if (widthValueStr != "undefined") {
477 widthInfo = CalcDimension(StringUtils::StringToDouble(widthValueStr), (DimensionUnit)widthUnit);
478 } else {
479 ViewAbstract::ClearWidthOrHeight(frameNode, true);
480 }
481 if (heightValueStr != "undefined") {
482 heightInfo = CalcDimension(StringUtils::StringToDouble(heightValueStr), (DimensionUnit)heightUnit);
483 } else {
484 ViewAbstract::ClearWidthOrHeight(frameNode, false);
485 }
486 ButtonModelNG::SetSize(frameNode, widthInfo, heightInfo);
487 }
488
ResetButtonSize(ArkUINodeHandle node)489 void ResetButtonSize(ArkUINodeHandle node)
490 {
491 auto* frameNode = reinterpret_cast<FrameNode*>(node);
492 CHECK_NULL_VOID(frameNode);
493 ViewAbstract::ClearWidthOrHeight(frameNode, true);
494 ViewAbstract::ClearWidthOrHeight(frameNode, false);
495 }
496
GetButtonLabel(ArkUINodeHandle node)497 ArkUI_CharPtr GetButtonLabel(ArkUINodeHandle node)
498 {
499 auto *frameNode = reinterpret_cast<FrameNode *>(node);
500 CHECK_NULL_RETURN(frameNode, "");
501 g_strValue = ButtonModelNG::GetLabel(frameNode);
502 return g_strValue.c_str();
503 }
504
GetButtonFontSize(ArkUINodeHandle node,ArkUI_Int32 unit)505 ArkUI_Float32 GetButtonFontSize(ArkUINodeHandle node, ArkUI_Int32 unit)
506 {
507 auto *frameNode = reinterpret_cast<FrameNode *>(node);
508 CHECK_NULL_RETURN(frameNode, ERROR_FLOAT_CODE);
509 return ButtonModelNG::GetFontSize(frameNode).GetNativeValue(static_cast<DimensionUnit>(unit));
510 }
511
GetButtonFontWeight(ArkUINodeHandle node)512 ArkUI_Int32 GetButtonFontWeight(ArkUINodeHandle node)
513 {
514 auto *frameNode = reinterpret_cast<FrameNode *>(node);
515 CHECK_NULL_RETURN(frameNode, ERROR_INT_CODE);
516 return static_cast<ArkUI_Int32>(ButtonModelNG::GetFontWeight(frameNode));
517 }
518
GetButtonFontColor(ArkUINodeHandle node)519 ArkUI_Uint32 GetButtonFontColor(ArkUINodeHandle node)
520 {
521 auto *frameNode = reinterpret_cast<FrameNode *>(node);
522 CHECK_NULL_RETURN(frameNode, ERROR_UINT_CODE);
523 return ButtonModelNG::GetFontColor(frameNode).GetValue();
524 }
525
SetButtonRole(ArkUINodeHandle node,ArkUI_Uint32 buttonRole)526 void SetButtonRole(ArkUINodeHandle node, ArkUI_Uint32 buttonRole)
527 {
528 auto* frameNode = reinterpret_cast<FrameNode*>(node);
529 CHECK_NULL_VOID(frameNode);
530 ButtonRole role = ButtonRole::NORMAL;
531 if (buttonRole >= static_cast<uint32_t>(ButtonRole::NORMAL) && buttonRole <=
532 static_cast<uint32_t>(ButtonRole::ERROR)) {
533 role = static_cast<ButtonRole>(buttonRole);
534 }
535 ButtonModelNG::SetRole(frameNode, role);
536 }
537
ResetButtonRole(ArkUINodeHandle node)538 void ResetButtonRole(ArkUINodeHandle node)
539 {
540 auto* frameNode = reinterpret_cast<FrameNode*>(node);
541 CHECK_NULL_VOID(frameNode);
542 ButtonModelNG::SetRole(frameNode, ButtonRole::NORMAL);
543 }
544
SetButtonStyle(ArkUINodeHandle node,ArkUI_Uint32 buttonStyle)545 void SetButtonStyle(ArkUINodeHandle node, ArkUI_Uint32 buttonStyle)
546 {
547 auto* frameNode = reinterpret_cast<FrameNode*>(node);
548 CHECK_NULL_VOID(frameNode);
549 ButtonStyleMode style = ButtonStyleMode::EMPHASIZE;
550 if (buttonStyle >= static_cast<uint32_t>(ButtonStyleMode::NORMAL) && buttonStyle <=
551 static_cast<uint32_t>(ButtonStyleMode::TEXT)) {
552 style = static_cast<ButtonStyleMode>(buttonStyle);
553 }
554 ButtonModelNG::SetButtonStyle(frameNode, style);
555 }
556
ResetButtonStyle(ArkUINodeHandle node)557 void ResetButtonStyle(ArkUINodeHandle node)
558 {
559 auto* frameNode = reinterpret_cast<FrameNode*>(node);
560 CHECK_NULL_VOID(frameNode);
561 ButtonModelNG::SetButtonStyle(frameNode, ButtonStyleMode::EMPHASIZE);
562 }
563
SetButtonControlSize(ArkUINodeHandle node,ArkUI_Uint32 controlSize)564 void SetButtonControlSize(ArkUINodeHandle node, ArkUI_Uint32 controlSize)
565 {
566 auto* frameNode = reinterpret_cast<FrameNode*>(node);
567 CHECK_NULL_VOID(frameNode);
568 ControlSize size = ControlSize::NORMAL;
569 if (controlSize >= static_cast<uint32_t>(ControlSize::SMALL) && controlSize <=
570 static_cast<uint32_t>(ControlSize::NORMAL)) {
571 size = static_cast<ControlSize>(controlSize);
572 }
573 ButtonModelNG::SetControlSize(frameNode, size);
574 }
575
ResetButtonControlSize(ArkUINodeHandle node)576 void ResetButtonControlSize(ArkUINodeHandle node)
577 {
578 auto* frameNode = reinterpret_cast<FrameNode*>(node);
579 CHECK_NULL_VOID(frameNode);
580 ButtonModelNG::SetControlSize(frameNode, ControlSize::NORMAL);
581 }
582
GetButtonType(ArkUINodeHandle node)583 ArkUI_Int32 GetButtonType(ArkUINodeHandle node)
584 {
585 auto* frameNode = reinterpret_cast<FrameNode*>(node);
586 CHECK_NULL_RETURN(frameNode, ERROR_INT_CODE);
587 return static_cast<ArkUI_Int32>(ButtonModelNG::GetType(frameNode));
588 }
589
SetButtonLabelWithCheck(ArkUINodeHandle node,ArkUI_CharPtr value)590 void SetButtonLabelWithCheck(ArkUINodeHandle node, ArkUI_CharPtr value)
591 {
592 auto* frameNode = reinterpret_cast<FrameNode*>(node);
593 CHECK_NULL_VOID(frameNode);
594 ButtonModelNG::SetLabelWithCheck(frameNode, value);
595 }
596
ResetButtonLabelWithCheck(ArkUINodeHandle node)597 void ResetButtonLabelWithCheck(ArkUINodeHandle node)
598 {
599 auto* frameNode = reinterpret_cast<FrameNode*>(node);
600 CHECK_NULL_VOID(frameNode);
601 ButtonModelNG::SetLabelWithCheck(frameNode, "");
602 }
603
SetButtonOptions(ArkUINodeHandle node,ArkUI_Uint32 buttonStyle,ArkUI_Uint32 buttonRole)604 void SetButtonOptions(ArkUINodeHandle node, ArkUI_Uint32 buttonStyle, ArkUI_Uint32 buttonRole)
605 {
606 auto* frameNode = reinterpret_cast<FrameNode*>(node);
607 CHECK_NULL_VOID(frameNode);
608 ButtonStyleMode style = ButtonStyleMode::EMPHASIZE;
609 if (buttonStyle >= static_cast<uint32_t>(ButtonStyleMode::NORMAL) && buttonStyle <=
610 static_cast<uint32_t>(ButtonStyleMode::TEXT)) {
611 style = static_cast<ButtonStyleMode>(buttonStyle);
612 }
613 ButtonRole role = ButtonRole::NORMAL;
614 if (buttonRole >= static_cast<uint32_t>(ButtonRole::NORMAL) && buttonRole <=
615 static_cast<uint32_t>(ButtonRole::ERROR)) {
616 role = static_cast<ButtonRole>(buttonRole);
617 }
618 ButtonModelNG::ApplyTheme(frameNode, style, role);
619 }
620
ResetButtonOptions(ArkUINodeHandle node)621 void ResetButtonOptions(ArkUINodeHandle node)
622 {
623 auto* frameNode = reinterpret_cast<FrameNode*>(node);
624 CHECK_NULL_VOID(frameNode);
625 ButtonModelNG::SetType(frameNode, DEFAULT_BUTTON_TYPE);
626 ButtonModelNG::SetStateEffect(frameNode, DEFAULT_STATE_EFFECT);
627 ButtonModelNG::SetButtonStyle(frameNode, ButtonStyleMode::EMPHASIZE);
628 ButtonModelNG::SetControlSize(frameNode, ControlSize::NORMAL);
629 ButtonModelNG::SetRole(frameNode, ButtonRole::NORMAL);
630 }
631
SetCreateWithLabel(ArkUINodeHandle node,bool createWithLabel)632 void SetCreateWithLabel(ArkUINodeHandle node, bool createWithLabel)
633 {
634 auto* frameNode = reinterpret_cast<FrameNode*>(node);
635 CHECK_NULL_VOID(frameNode);
636 ButtonModelNG::SetCreateWithLabel(frameNode, createWithLabel);
637 }
638
639 namespace NodeModifier {
GetButtonModifier()640 const ArkUIButtonModifier* GetButtonModifier()
641 {
642 static const ArkUIButtonModifier modifier = { SetButtonLabel, ResetButtonLabel, SetButtonType, ResetButtonType,
643 SetButtonStateEffect, ResetButtonStateEffect, SetButtonFontColor, ResetButtonFontColor, SetButtonFontSize,
644 ResetButtonFontSize, SetButtonFontWeight, ResetButtonFontWeight, SetButtonFontStyle, ResetButtonFontStyle,
645 SetButtonFontFamily, ResetButtonFontFamily, SetButtonLabelStyle, ResetButtonLabelStyle,
646 SetButtonBackgroundColor, ResetButtonBackgroundColor, SetButtonBorderRadius, ResetButtonBorderRadius,
647 SetButtonFontWeightEnum, SetButtonSize, ResetButtonSize, GetButtonLabel, GetButtonFontSize, GetButtonFontWeight,
648 GetButtonFontColor, SetButtonRole, ResetButtonRole, SetButtonStyle, ResetButtonStyle, SetButtonControlSize,
649 ResetButtonControlSize, GetButtonType, SetButtonLabelWithCheck, ResetButtonLabelWithCheck,
650 SetButtonOptions, ResetButtonOptions, SetCreateWithLabel };
651 return &modifier;
652 }
653
GetCJUIButtonModifier()654 const CJUIButtonModifier* GetCJUIButtonModifier()
655 {
656 static const CJUIButtonModifier modifier = { SetButtonLabel, ResetButtonLabel, SetButtonType, ResetButtonType,
657 SetButtonStateEffect, ResetButtonStateEffect, SetButtonFontColor, ResetButtonFontColor, SetButtonFontSize,
658 ResetButtonFontSize, SetButtonFontWeight, ResetButtonFontWeight, SetButtonFontStyle, ResetButtonFontStyle,
659 SetButtonFontFamily, ResetButtonFontFamily, SetButtonLabelStyle, ResetButtonLabelStyle,
660 SetButtonBackgroundColor, ResetButtonBackgroundColor, SetButtonBorderRadius, ResetButtonBorderRadius,
661 SetButtonFontWeightEnum, SetButtonSize, ResetButtonSize, GetButtonLabel, GetButtonFontSize, GetButtonFontWeight,
662 GetButtonFontColor, SetButtonRole, ResetButtonRole, SetButtonStyle, ResetButtonStyle, SetButtonControlSize,
663 ResetButtonControlSize, GetButtonType, SetButtonLabelWithCheck, ResetButtonLabelWithCheck,
664 SetButtonOptions, ResetButtonOptions, SetCreateWithLabel };
665 return &modifier;
666 }
667 } // namespace NodeModifier
668 } // namespace OHOS::Ace::NG