1 /*
2  * Copyright (c) 2024 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/badge_modifier.h"
16 
17 #include "core/components_ng/pattern/badge/badge_data.h"
18 #include "core/components_ng/pattern/badge/badge_model_ng.h"
19 
20 namespace OHOS::Ace::NG {
21 namespace {
SetBadgeBaseParam(BadgeParameters & badgeParameters,const ArkUIBadgeParam & style)22 void SetBadgeBaseParam(BadgeParameters& badgeParameters, const ArkUIBadgeParam& style)
23 {
24     badgeParameters.isPositionXy = style.isPositionXy;
25     if (style.isPositionXy) {
26         badgeParameters.badgePositionX =
27             Dimension(style.positionX.value, static_cast<DimensionUnit>(style.positionX.units));
28         badgeParameters.badgePositionY =
29             Dimension(style.positionY.value, static_cast<DimensionUnit>(style.positionY.units));
30     } else {
31         badgeParameters.badgePosition = style.position;
32     }
33     badgeParameters.badgeColor = Color(style.badgeColor);
34     badgeParameters.badgeTextColor = Color(style.textColor);
35     badgeParameters.badgeBorderColor = Color(style.borderColor);
36     badgeParameters.badgeFontWeight = static_cast<FontWeight>(style.fontWeight);
37     badgeParameters.badgeFontSize = Dimension(style.fontSize.value, static_cast<DimensionUnit>(style.fontSize.units));
38     badgeParameters.badgeCircleSize =
39         Dimension(style.badgeSize.value, static_cast<DimensionUnit>(style.badgeSize.units));
40     badgeParameters.badgeBorderWidth =
41         Dimension(style.borderWidth.value, static_cast<DimensionUnit>(style.borderWidth.units));
42 }
43 } // namespace
44 
SetBadgeParamWithNumber(ArkUINodeHandle node,const struct ArkUIBadgeParam * style,ArkUI_Int32 count,ArkUI_Bool countHasValue,ArkUI_Int32 maxCount)45 void SetBadgeParamWithNumber(ArkUINodeHandle node, const struct ArkUIBadgeParam* style, ArkUI_Int32 count,
46     ArkUI_Bool countHasValue, ArkUI_Int32 maxCount)
47 {
48     CHECK_NULL_VOID(style);
49     auto* frameNode = reinterpret_cast<FrameNode*>(node);
50     CHECK_NULL_VOID(frameNode);
51     BadgeParameters badgeParameters;
52     SetBadgeBaseParam(badgeParameters, *style);
53     if (countHasValue) {
54         badgeParameters.badgeCount = count;
55     } else {
56         badgeParameters.badgeCount = std::optional<int>();
57     }
58     badgeParameters.badgeMaxCount = maxCount;
59     BadgeModelNG::SetBadgeParam(frameNode, badgeParameters, style->isDefaultFontSize, style->isDefaultBadgeSize);
60 }
61 
SetBadgeParamWithString(ArkUINodeHandle node,const struct ArkUIBadgeParam * style,ArkUI_CharPtr value)62 void SetBadgeParamWithString(ArkUINodeHandle node, const struct ArkUIBadgeParam* style, ArkUI_CharPtr value)
63 {
64     CHECK_NULL_VOID(style);
65     CHECK_NULL_VOID(value);
66     auto* frameNode = reinterpret_cast<FrameNode*>(node);
67     CHECK_NULL_VOID(frameNode);
68     BadgeParameters badgeParameters;
69     SetBadgeBaseParam(badgeParameters, *style);
70     badgeParameters.badgeValue = value;
71     BadgeModelNG::SetBadgeParam(frameNode, badgeParameters, style->isDefaultFontSize, style->isDefaultBadgeSize);
72 }
73 
74 namespace NodeModifier {
GetBadgeModifier()75 const ArkUIBadgeModifier* GetBadgeModifier()
76 {
77     static const ArkUIBadgeModifier modifier = { nullptr, nullptr, nullptr, nullptr, nullptr, SetBadgeParamWithNumber,
78         SetBadgeParamWithString };
79     return &modifier;
80 }
81 } // namespace NodeModifier
82 } // namespace OHOS::Ace::NG