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