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_PIECE_PIECE_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_PIECE_PIECE_DECLARATION_H
18 
19 #include "core/components/declaration/common/declaration.h"
20 #include "frameworks/bridge/common/dom/dom_type.h"
21 #include "frameworks/core/components/piece/piece_theme.h"
22 
23 namespace OHOS::Ace {
24 
25 struct PieceAttribute : Attribute {
26     bool showDelete = false;
27     std::string content;
28     std::string icon;
29     Dimension interval; // Interval between text and icon.
30     Dimension iconSize;
31     InternalResource::ResourceId iconResource = InternalResource::ResourceId::NO_ID;
32 };
33 
34 struct PieceStyle : Style {
35     TextStyle textStyle;
36     Edge margin;
37     Border border;
38     Color hoverColor;
39     Color backGroundColor;
40 };
41 
42 struct PieceEvent : Event {
43     EventMarker onDelete;
44 };
45 
46 class PieceDeclaration : public Declaration {
47     DECLARE_ACE_TYPE(PieceDeclaration, Declaration);
48 
49 public:
50     PieceDeclaration() = default;
51     ~PieceDeclaration() override = default;
52 
53     void InitializeStyle(RefPtr<PieceTheme>& theme);
54     void InitializeStyle() override;
GetContent()55     const std::string& GetContent() const
56     {
57         auto& attribute = static_cast<PieceAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
58         return attribute.content;
59     }
SetContent(const std::string & content)60     void SetContent(const std::string& content)
61     {
62         auto& attribute = MaybeResetAttribute<PieceAttribute>(AttributeTag::SPECIALIZED_ATTR);
63         attribute.content = content;
64     }
65 
GetIcon()66     const std::string& GetIcon() const
67     {
68         auto& attribute = static_cast<PieceAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
69         return attribute.icon;
70     }
SetIcon(const std::string & icon)71     void SetIcon(const std::string& icon)
72     {
73         auto& attribute = MaybeResetAttribute<PieceAttribute>(AttributeTag::SPECIALIZED_ATTR);
74         attribute.icon = icon;
75     }
76 
ShowDelete()77     bool ShowDelete() const
78     {
79         auto& attribute = static_cast<PieceAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
80         return attribute.showDelete;
81     }
SetShowDelete(bool showDelete)82     void SetShowDelete(bool showDelete)
83     {
84         auto& attribute = MaybeResetAttribute<PieceAttribute>(AttributeTag::SPECIALIZED_ATTR);
85         attribute.showDelete = showDelete;
86     }
87 
GetInterval()88     const Dimension& GetInterval() const
89     {
90         auto& attribute = static_cast<PieceAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
91         return attribute.interval;
92     }
SetInterval(const Dimension & interval)93     void SetInterval(const Dimension& interval)
94     {
95         auto& attribute = MaybeResetAttribute<PieceAttribute>(AttributeTag::SPECIALIZED_ATTR);
96         attribute.interval = interval;
97     }
98 
GetIconResource()99     InternalResource::ResourceId GetIconResource() const
100     {
101         auto& attribute = static_cast<PieceAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
102         return attribute.iconResource;
103     }
SetIconResource(InternalResource::ResourceId iconResource)104     void SetIconResource(InternalResource::ResourceId iconResource)
105     {
106         auto& attribute = MaybeResetAttribute<PieceAttribute>(AttributeTag::SPECIALIZED_ATTR);
107         attribute.iconResource = iconResource;
108     }
109 
GetIconSize()110     const Dimension& GetIconSize() const
111     {
112         auto& attribute = static_cast<PieceAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
113         return attribute.iconSize;
114     }
SetIconSize(const Dimension & iconSize)115     void SetIconSize(const Dimension& iconSize)
116     {
117         auto& attribute = MaybeResetAttribute<PieceAttribute>(AttributeTag::SPECIALIZED_ATTR);
118         attribute.iconSize = iconSize;
119     }
120 
GetTextStyle()121     const TextStyle& GetTextStyle() const
122     {
123         auto& style = static_cast<PieceStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
124         return style.textStyle;
125     }
SetTextStyle(const TextStyle & textStyle)126     void SetTextStyle(const TextStyle& textStyle)
127     {
128         auto& style = MaybeResetStyle<PieceStyle>(StyleTag::SPECIALIZED_STYLE);
129         style.textStyle = textStyle;
130     }
131 
GetMargin()132     const Edge& GetMargin() const
133     {
134         auto& style = static_cast<PieceStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
135         return style.margin;
136     }
SetMargin(const Edge & margin)137     void SetMargin(const Edge& margin)
138     {
139         auto& style = MaybeResetStyle<PieceStyle>(StyleTag::SPECIALIZED_STYLE);
140         style.margin = margin;
141     }
142 
GetBorder()143     const Border& GetBorder() const
144     {
145         auto& style = static_cast<PieceStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
146         return style.border;
147     }
SetBorder(const Border & border)148     void SetBorder(const Border& border)
149     {
150         auto& style = MaybeResetStyle<PieceStyle>(StyleTag::SPECIALIZED_STYLE);
151         style.border = border;
152     }
153 
GetHoverColor()154     const Color& GetHoverColor() const
155     {
156         auto& style = static_cast<PieceStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
157         return style.hoverColor;
158     }
159 
SetHoverColor(const Color & hoverColor)160     void SetHoverColor(const Color& hoverColor)
161     {
162         auto& style = MaybeResetStyle<PieceStyle>(StyleTag::SPECIALIZED_STYLE);
163         style.hoverColor = hoverColor;
164     }
165 
SetBackGroundColor(const Color & backGroundColor)166     void SetBackGroundColor(const Color& backGroundColor)
167     {
168         auto& style = MaybeResetStyle<PieceStyle>(StyleTag::SPECIALIZED_STYLE);
169         style.backGroundColor = backGroundColor;
170     }
171 
GetBackGroundColor()172     const Color& GetBackGroundColor() const
173     {
174         auto& style = static_cast<PieceStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
175         return style.backGroundColor;
176     }
177 
GetOnDelete()178     const EventMarker& GetOnDelete() const
179     {
180         auto& event = static_cast<PieceEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
181         return event.onDelete;
182     }
SetOnDelete(const EventMarker & onDelete)183     void SetOnDelete(const EventMarker& onDelete)
184     {
185         auto& event = MaybeResetEvent<PieceEvent>(EventTag::SPECIALIZED_EVENT);
186         event.onDelete = onDelete;
187     }
188 
HasContent()189     bool HasContent() const
190     {
191         return hasContent_;
192     }
193 
HasBackground()194     bool HasBackground() const
195     {
196         return hasBackground_;
197     }
198 
GetImageFill()199     const std::optional<Color>& GetImageFill() const
200     {
201         auto& imageStyle = static_cast<CommonImageStyle&>(GetStyle(StyleTag::COMMON_IMAGE_STYLE));
202         return imageStyle.imageFill;
203     }
204 
205 protected:
206     void InitSpecialized() override;
207     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
208     bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override;
209     bool SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event) override;
210 
211 private:
212     bool hasContent_ = false;
213     bool hasBackground_ = false;
214 };
215 
216 } // namespace OHOS::Ace
217 
218 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_PIECE_PIECE_DECLARATION_H
219