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