1 /*
2  * Copyright (c) 2021 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 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_BADGE_BADGE_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_BADGE_BADGE_DECLARATION_H
18 
19 #include "core/components/declaration/common/declaration.h"
20 #include "frameworks/bridge/common/dom/dom_type.h"
21 
22 namespace OHOS::Ace {
23 
24 struct BadgeAttribute : Attribute {
25     bool showMessage = true;
26     bool isPositionXy = false;
27     BadgePosition badgePosition { BadgePosition::RIGHT_TOP };
28     Dimension badgePositionX;
29     Dimension badgePositionY;
30     int64_t messageCount = 0;
31     int64_t maxCount = 99;
32     std::optional<std::string> badgeLabel;
33 };
34 
35 struct BadgeStyle : Style {
36     Color badgeColor;
37     Color badgeTextColor;
38     Edge padding;
39     Dimension badgeFontSize;
40     Dimension badgeCircleSize;
41     bool badgeCircleSizeDefined = false;
42 };
43 
44 struct BadgeEvent : Event {
45     EventMarker clickEvent;
46 };
47 
48 class BadgeDeclaration : public Declaration {
49     DECLARE_ACE_TYPE(BadgeDeclaration, Declaration);
50 
51 public:
52     BadgeDeclaration() = default;
53     ~BadgeDeclaration() override = default;
54 
55     void InitializeStyle() override;
56 
GetBadgePosition()57     BadgePosition GetBadgePosition() const
58     {
59         auto& attribute = static_cast<BadgeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
60         return attribute.badgePosition;
61     }
62 
GetBadgePositionX()63     const Dimension& GetBadgePositionX() const
64     {
65         auto& attribute = static_cast<BadgeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
66         return attribute.badgePositionX;
67     }
68 
GetBadgePositionY()69     const Dimension& GetBadgePositionY() const
70     {
71         auto& attribute = static_cast<BadgeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
72         return attribute.badgePositionY;
73     }
74 
IsPositionXy()75     bool IsPositionXy() const
76     {
77         auto& attribute = static_cast<BadgeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
78         return attribute.isPositionXy;
79     }
80 
GetBadgeLabel()81     const std::optional<std::string>& GetBadgeLabel() const
82     {
83         auto& attribute = static_cast<BadgeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
84         return attribute.badgeLabel;
85     }
86 
GetMessageCount()87     int64_t GetMessageCount() const
88     {
89         auto& attribute = static_cast<BadgeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
90         return attribute.messageCount;
91     }
92 
IsShowMessage()93     bool IsShowMessage() const
94     {
95         auto& attribute = static_cast<BadgeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
96         return attribute.showMessage;
97     }
98 
GetMaxCount()99     int64_t GetMaxCount() const
100     {
101         auto& attribute = static_cast<BadgeAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
102         return attribute.maxCount;
103     }
104 
GetBadgeColor()105     const Color& GetBadgeColor() const
106     {
107         auto& style = static_cast<BadgeStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
108         return style.badgeColor;
109     }
110 
GetBadgeTextColor()111     const Color& GetBadgeTextColor() const
112     {
113         auto& style = static_cast<BadgeStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
114         return style.badgeTextColor;
115     }
116 
GetBadgeFontSize()117     const Dimension& GetBadgeFontSize() const
118     {
119         auto& style = static_cast<BadgeStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
120         return style.badgeFontSize;
121     }
122 
GetPadding()123     const Edge& GetPadding() const
124     {
125         auto& style = static_cast<BadgeStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
126         return style.padding;
127     }
128 
GetBadgeCircleSize()129     const Dimension& GetBadgeCircleSize() const
130     {
131         auto& style = static_cast<BadgeStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
132         return style.badgeCircleSize;
133     }
134 
IsBadgeCircleSizeDefined()135     bool IsBadgeCircleSizeDefined() const
136     {
137         auto& style = static_cast<BadgeStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
138         return style.badgeCircleSizeDefined;
139     }
140 
GetClickEvent()141     const EventMarker& GetClickEvent() const
142     {
143         auto& event = static_cast<BadgeEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
144         return event.clickEvent;
145     }
146 
SetMaxCount(int64_t maxCount)147     void SetMaxCount(int64_t maxCount)
148     {
149         auto& attribute = MaybeResetAttribute<BadgeAttribute>(AttributeTag::SPECIALIZED_ATTR);
150         attribute.maxCount = maxCount;
151     }
152 
SetShowMessage(bool showMessage)153     void SetShowMessage(bool showMessage)
154     {
155         auto& attribute = MaybeResetAttribute<BadgeAttribute>(AttributeTag::SPECIALIZED_ATTR);
156         attribute.showMessage = showMessage;
157     }
158 
SetMessageCount(int64_t messageCount)159     void SetMessageCount(int64_t messageCount)
160     {
161         auto& attribute = MaybeResetAttribute<BadgeAttribute>(AttributeTag::SPECIALIZED_ATTR);
162         attribute.messageCount = messageCount;
163     }
164 
SetBadgePosition(BadgePosition badgePostion)165     void SetBadgePosition(BadgePosition badgePostion)
166     {
167         auto& attribute = MaybeResetAttribute<BadgeAttribute>(AttributeTag::SPECIALIZED_ATTR);
168         attribute.badgePosition = badgePostion;
169     }
170 
SetBadgePositionX(const Dimension & badgePostionX)171     void SetBadgePositionX(const Dimension& badgePostionX)
172     {
173         auto& attribute = MaybeResetAttribute<BadgeAttribute>(AttributeTag::SPECIALIZED_ATTR);
174         attribute.badgePositionX = badgePostionX;
175     }
176 
SetBadgePositionY(const Dimension & badgePostionY)177     void SetBadgePositionY(const Dimension& badgePostionY)
178     {
179         auto& attribute = MaybeResetAttribute<BadgeAttribute>(AttributeTag::SPECIALIZED_ATTR);
180         attribute.badgePositionY = badgePostionY;
181     }
182 
SetIsPositionXy(bool isPositionXy)183     void SetIsPositionXy(bool isPositionXy)
184     {
185         auto& attribute = MaybeResetAttribute<BadgeAttribute>(AttributeTag::SPECIALIZED_ATTR);
186         attribute.isPositionXy = isPositionXy;
187     }
188 
SetBadgeCircleSizeDefined(bool badgeCircleSizeDefined)189     void SetBadgeCircleSizeDefined(bool badgeCircleSizeDefined)
190     {
191         auto& style = MaybeResetStyle<BadgeStyle>(StyleTag::SPECIALIZED_STYLE);
192         style.badgeCircleSizeDefined = badgeCircleSizeDefined;
193     }
194 
SetPadding(const Edge & padding)195     void SetPadding(const Edge& padding)
196     {
197         auto& style = MaybeResetStyle<BadgeStyle>(StyleTag::SPECIALIZED_STYLE);
198         style.padding = padding;
199     }
200 
SetBadgeTextColor(const Color & badgeTextColor)201     void SetBadgeTextColor(const Color& badgeTextColor)
202     {
203         auto& style = MaybeResetStyle<BadgeStyle>(StyleTag::SPECIALIZED_STYLE);
204         style.badgeTextColor = badgeTextColor;
205     }
206 
SetBadgeFontSize(const Dimension & badgeFontSize)207     void SetBadgeFontSize(const Dimension& badgeFontSize)
208     {
209         auto& style = MaybeResetStyle<BadgeStyle>(StyleTag::SPECIALIZED_STYLE);
210         style.badgeFontSize = badgeFontSize;
211     }
212 
SetBadgeColor(const Color & color)213     void SetBadgeColor(const Color& color)
214     {
215         auto& style = MaybeResetStyle<BadgeStyle>(StyleTag::SPECIALIZED_STYLE);
216         style.badgeColor = color;
217     }
218 
SetBadgeCircleSize(const Dimension & badgeCircleSize)219     void SetBadgeCircleSize(const Dimension& badgeCircleSize)
220     {
221         auto& style = MaybeResetStyle<BadgeStyle>(StyleTag::SPECIALIZED_STYLE);
222         style.badgeCircleSize = badgeCircleSize;
223         style.badgeCircleSizeDefined = true;
224     }
225 
SetBadgeLabel(const std::string & badgeLabel)226     void SetBadgeLabel(const std::string& badgeLabel)
227     {
228         auto& attribute = MaybeResetAttribute<BadgeAttribute>(AttributeTag::SPECIALIZED_ATTR);
229         attribute.badgeLabel = badgeLabel;
230     }
231 
SetClickEvent(const EventMarker & event)232     void SetClickEvent(const EventMarker& event)
233     {
234         auto& badgeEvent = MaybeResetEvent<BadgeEvent>(EventTag::SPECIALIZED_EVENT);
235         badgeEvent.clickEvent = event;
236     }
237 
238 protected:
239     void InitSpecialized() override;
240     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
241     bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override;
242     bool SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event) override;
243 };
244 
245 } // namespace OHOS::Ace
246 
247 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_BADGE_BADGE_DECLARATION_H
248