• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BUTTON_BUTTON_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_BUTTON_THEME_H
18 
19 #include "core/components/common/properties/color.h"
20 #include "core/components/common/properties/text_style.h"
21 #include "core/components/theme/theme.h"
22 #include "core/components/theme/theme_constants.h"
23 #include "core/components/theme/theme_constants_defines.h"
24 
25 namespace OHOS::Ace {
26 
27 /**
28  * ButtonTheme defines color and styles of ButtonComponent. ButtonTheme should be built
29  * using ButtonTheme::Builder.
30  */
31 class ButtonTheme : public virtual Theme {
32     DECLARE_ACE_TYPE(ButtonTheme, Theme);
33 
34 public:
35     class Builder {
36     public:
37         Builder() = default;
38         ~Builder() = default;
39 
Build(const RefPtr<ThemeConstants> & themeConstants)40         RefPtr<ButtonTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
41         {
42             RefPtr<ButtonTheme> theme = AceType::Claim(new ButtonTheme());
43             if (!themeConstants) {
44                 return theme;
45             }
46             theme->radius_ = themeConstants->GetDimension(THEME_BUTTON_RADIUS);
47             theme->bgColor_ = themeConstants->GetColor(THEME_BUTTON_BACKGROUND_COLOR);
48             theme->bgFocusColor_ = themeConstants->GetColor(THEME_BUTTON_FOCUS_COLOR);
49             theme->clickedColor_ = themeConstants->GetColor(THEME_BUTTON_CLICKED_COLOR);
50             theme->disabledColor_ = themeConstants->GetColor(THEME_BUTTON_DISABLED_COLOR);
51             theme->bgDisabledAlpha_ = themeConstants->GetDouble(THEME_BUTTON_DISABLED_ALPHA);
52             theme->hoverColor_ = themeConstants->GetColor(THEME_BUTTON_HOVER_COLOR);
53             theme->borderColor_ = themeConstants->GetColor(THEME_BUTTON_BORDER_COLOR);
54             theme->borderWidth_ = themeConstants->GetDimension(THEME_BUTTON_BORDER_WIDTH);
55             theme->textFocusColor_ = themeConstants->GetColor(THEME_BUTTON_TEXT_FOCUS_COLOR);
56             theme->textDisabledColor_ = themeConstants->GetColor(THEME_BUTTON_TEXT_DISABLED_COLOR);
57             theme->textWaitingColor_ = themeConstants->GetColor(THEME_BUTTON_TEXT_WAITING_COLOR);
58             theme->textStyle_.SetTextColor(themeConstants->GetColor(THEME_BUTTON_TEXT_COLOR));
59             theme->textStyle_.SetFontSize(themeConstants->GetDimension(THEME_BUTTON_TEXT_FONTSIZE));
60             theme->textStyle_.SetFontWeight(FontWeight(themeConstants->GetInt(THEME_BUTTON_TEXT_FONTWEIGHT)));
61             theme->minWidth_ = themeConstants->GetDimension(THEME_BUTTON_MIN_WIDTH);
62             theme->height_ = themeConstants->GetDimension(THEME_BUTTON_HEIGHT);
63             theme->downloadHeight_ = themeConstants->GetDimension(THEME_BUTTON_DOWNLOAD_HEIGHT);
64             theme->padding_ = Edge(themeConstants->GetDimension(THEME_BUTTON_PADDING_HORIZONTAL).Value(),
65                 themeConstants->GetDimension(THEME_BUTTON_PADDING_VERTICAL).Value(),
66                 themeConstants->GetDimension(THEME_BUTTON_PADDING_HORIZONTAL).Value(),
67                 themeConstants->GetDimension(THEME_BUTTON_PADDING_VERTICAL).Value(),
68                 themeConstants->GetDimension(THEME_BUTTON_PADDING_VERTICAL).Unit());
69             theme->minFontSize_ = themeConstants->GetDimension(THEME_BUTTON_TEXT_FONTSIZE_MIN);
70             int32_t maxlines = themeConstants->GetInt(THEME_BUTTON_TEXT_MAX_LINES);
71             theme->textMaxLines_ = maxlines < 0 ? theme->textMaxLines_ : static_cast<uint32_t>(maxlines);
72             theme->minCircleButtonDiameter_ = themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MIN_DIAMETER);
73             theme->minCircleButtonIcon_ = themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MIN_ICON_SIZE);
74             theme->minCircleButtonPadding_ = Edge(themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MIN_PADDING));
75             theme->maxCircleButtonDiameter_ = themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MAX_DIAMETER);
76             theme->maxCircleButtonIcon_ = themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MAX_ICON_SIZE);
77             theme->maxCircleButtonPadding_ = Edge(themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MAX_PADDING));
78             theme->progressFocusColor_ = themeConstants->GetColor(THEME_BUTTON_PROGRESS_FOCUS_COLOR);
79             theme->downloadBorderColor_ = themeConstants->GetColor(THEME_BUTTON_DOWNLOAD_BORDER_COLOR);
80             theme->normalTextColor_ = themeConstants->GetColor(THEME_BUTTON_NORMAL_TEXT_COLOR);
81             theme->downloadBackgroundColor_ = themeConstants->GetColor(THEME_BUTTON_DOWNLOAD_BG_COLOR);
82             theme->downloadTextColor_ = themeConstants->GetColor(THEME_BUTTON_DOWNLOAD_TEXT_COLOR);
83             theme->downloadFontSize_ = themeConstants->GetDimension(THEME_BUTTON_DOWNLOAD_TEXT_FONTSIZE);
84             theme->progressColor_ = themeConstants->GetColor(THEME_BUTTON_PROGRESS_COLOR);
85             theme->progressDiameter_ = themeConstants->GetDimension(THEME_BUTTON_PROGRESS_DIAMETER);
86             theme->downloadProgressColor_ = themeConstants->GetColor(THEME_BUTTON_DOWNLOAD_PROGRESS_COLOR);
87             theme->innerPadding_ = themeConstants->GetDimension(THEME_BUTTON_INNER_PADDING);
88             ParsePattern(themeConstants->GetThemeStyle(), theme);
89             return theme;
90         }
91 
92     private:
ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<ButtonTheme> & theme)93         void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<ButtonTheme>& theme) const
94         {
95             if (!themeStyle) {
96                 return;
97             }
98             auto buttonPattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_BUTTON, nullptr);
99             if (!buttonPattern) {
100                 return;
101             }
102             theme->bgColor_ = buttonPattern->GetAttr<Color>(PATTERN_BG_COLOR, Color());
103             theme->clickedColor_ = buttonPattern->GetAttr<Color>(BUTTON_CLICK_BLEND_COLOR, Color());
104             theme->disabledColor_ = buttonPattern->GetAttr<Color>(PATTERN_BG_COLOR, Color())
105                 .BlendOpacity(themeStyle->GetAttr<double>(THEME_ATTR_DISABLED_ALPHA, 0.0));
106             theme->hoverColor_ = themeStyle->GetAttr<Color>(BUTTON_HOVER_COLOR, Color());
107             theme->borderColor_ = buttonPattern->GetAttr<Color>(BUTTON_BORDER_COLOR, Color());
108             theme->borderWidth_ = buttonPattern->GetAttr<Dimension>(BUTTON_BORDER_WIDTH, 0.0_vp);
109             theme->textStyle_.SetTextColor(buttonPattern->GetAttr<Color>(BUTTON_TEXT_COLOR, Color()));
110             theme->textDisabledColor_ = buttonPattern->GetAttr<Color>(BUTTON_TEXT_COLOR, Color())
111                 .BlendOpacity(themeStyle->GetAttr<double>(THEME_ATTR_DISABLED_ALPHA, 0.0));
112             theme->textWaitingColor_ = buttonPattern->GetAttr<Color>(BUTTON_TEXT_COLOR, Color());
113             theme->normalTextColor_ = buttonPattern->GetAttr<Color>(BUTTON_NORMAL_TEXT_COLOR, Color());
114             theme->downloadBackgroundColor_ = themeStyle->GetAttr<Color>(BUTTON_DOWNLOAD_BG_COLOR, Color())
115                 .BlendOpacity(themeStyle->GetAttr<double>(THEME_ATTR_FOURTH_CONTENT_ALPHA, 0.0));
116             theme->downloadBorderColor_ = buttonPattern->GetAttr<Color>(BUTTON_DOWNLOAD_BORDER_COLOR, Color())
117                 .BlendOpacity(themeStyle->GetAttr<double>(THEME_ATTR_HIGHLIGHT_BACKGROUND_ALPHA, 0.0));
118             theme->downloadProgressColor_ = buttonPattern->GetAttr<Color>(BUTTON_DOWNLOAD_BORDER_COLOR, Color())
119                 .BlendOpacity(themeStyle->GetAttr<double>(THEME_ATTR_HIGHLIGHT_BACKGROUND_ALPHA, 0.0));
120             theme->downloadTextColor_ = buttonPattern->GetAttr<Color>(BUTTON_DOWNLOAD_TEXT_COLOR, Color());
121             theme->progressColor_ = buttonPattern->GetAttr<Color>(BUTTON_TEXT_COLOR, Color());
122         }
123     };
124 
125     ~ButtonTheme() override = default;
126 
GetRadius()127     const Dimension& GetRadius() const
128     {
129         return radius_;
130     }
131 
GetBgColor()132     const Color& GetBgColor() const
133     {
134         return bgColor_;
135     }
136 
GetBgFocusColor()137     const Color& GetBgFocusColor() const
138     {
139         return bgFocusColor_;
140     }
141 
GetClickedColor()142     const Color& GetClickedColor() const
143     {
144         return clickedColor_;
145     }
146 
GetDisabledColor()147     const Color& GetDisabledColor() const
148     {
149         return disabledColor_;
150     }
151 
GetHoverColor()152     const Color& GetHoverColor() const
153     {
154         return hoverColor_;
155     }
156 
GetBorderColor()157     const Color& GetBorderColor() const
158     {
159         return borderColor_;
160     }
161 
GetBorderWidth()162     const Dimension& GetBorderWidth() const
163     {
164         return borderWidth_;
165     }
166 
GetBgDisabledAlpha()167     double GetBgDisabledAlpha() const
168     {
169         return bgDisabledAlpha_;
170     }
171 
GetTextFocusColor()172     const Color& GetTextFocusColor() const
173     {
174         return textFocusColor_;
175     }
176 
GetTextDisabledColor()177     const Color& GetTextDisabledColor() const
178     {
179         return textDisabledColor_;
180     }
181 
GetNormalTextColor()182     const Color& GetNormalTextColor() const
183     {
184         return normalTextColor_;
185     }
186 
GetDownloadBackgroundColor()187     const Color& GetDownloadBackgroundColor() const
188     {
189         return downloadBackgroundColor_;
190     }
191 
GetDownloadTextColor()192     const Color& GetDownloadTextColor() const
193     {
194         return downloadTextColor_;
195     }
196 
GetTextWaitingColor()197     const Color& GetTextWaitingColor() const
198     {
199         return textWaitingColor_;
200     }
201 
GetTextStyle()202     const TextStyle& GetTextStyle() const
203     {
204         return textStyle_;
205     }
206 
GetMinWidth()207     const Dimension& GetMinWidth() const
208     {
209         return minWidth_;
210     }
211 
GetHeight()212     const Dimension& GetHeight() const
213     {
214         return height_;
215     }
216 
GetDownloadHeight()217     const Dimension& GetDownloadHeight() const
218     {
219         return downloadHeight_;
220     }
221 
GetPadding()222     const Edge& GetPadding() const
223     {
224         return padding_;
225     }
226 
GetMinFontSize()227     const Dimension& GetMinFontSize() const
228     {
229         return minFontSize_;
230     }
231 
GetDownloadFontSize()232     const Dimension& GetDownloadFontSize() const
233     {
234         return downloadFontSize_;
235     }
236 
GetMaxFontSize()237     const Dimension& GetMaxFontSize() const
238     {
239         return textStyle_.GetFontSize();
240     }
241 
GetTextMaxLines()242     uint32_t GetTextMaxLines() const
243     {
244         return textMaxLines_;
245     }
246 
GetMinCircleButtonDiameter()247     const Dimension& GetMinCircleButtonDiameter() const
248     {
249         return minCircleButtonDiameter_;
250     }
251 
GetMinCircleButtonIcon()252     const Dimension& GetMinCircleButtonIcon() const
253     {
254         return minCircleButtonIcon_;
255     }
256 
GetMinCircleButtonPadding()257     const Edge& GetMinCircleButtonPadding() const
258     {
259         return minCircleButtonPadding_;
260     }
261 
GetMaxCircleButtonDiameter()262     const Dimension& GetMaxCircleButtonDiameter() const
263     {
264         return maxCircleButtonDiameter_;
265     }
266 
GetMaxCircleButtonIcon()267     const Dimension& GetMaxCircleButtonIcon() const
268     {
269         return maxCircleButtonIcon_;
270     }
271 
GetMaxCircleButtonPadding()272     const Edge& GetMaxCircleButtonPadding() const
273     {
274         return maxCircleButtonPadding_;
275     }
276 
GetProgressFocusColor()277     const Color& GetProgressFocusColor() const
278     {
279         return progressFocusColor_;
280     }
281 
GetDownloadBorderColor()282     const Color& GetDownloadBorderColor() const
283     {
284         return downloadBorderColor_;
285     }
286 
GetProgressColor()287     const Color& GetProgressColor() const
288     {
289         return progressColor_;
290     }
291 
GetProgressDiameter()292     const Dimension& GetProgressDiameter() const
293     {
294         return progressDiameter_;
295     }
296 
GetDownloadProgressColor()297     const Color& GetDownloadProgressColor() const
298     {
299         return downloadProgressColor_;
300     }
301 
GetInnerPadding()302     const Dimension& GetInnerPadding() const
303     {
304         return innerPadding_;
305     }
306 
307 protected:
308     ButtonTheme() = default;
309 
310 private:
311     Color bgColor_;
312     Color bgFocusColor_;
313     Color clickedColor_;
314     Color disabledColor_;
315     Color hoverColor_;
316     Color borderColor_;
317     Color textFocusColor_;
318     Color textDisabledColor_;
319     Color textWaitingColor_;
320     Color progressColor_;
321     Color progressFocusColor_;
322     Color normalTextColor_;
323     Color downloadBackgroundColor_;
324     Color downloadTextColor_;
325     Color downloadBorderColor_;
326     Color downloadProgressColor_;
327     TextStyle textStyle_;
328     Edge padding_;
329     Edge minCircleButtonPadding_;
330     Edge maxCircleButtonPadding_;
331 
332     Dimension radius_;
333     Dimension minWidth_;
334     Dimension height_;
335     Dimension progressDiameter_;
336     Dimension innerPadding_;
337     Dimension minFontSize_;
338     Dimension downloadFontSize_;
339     Dimension minCircleButtonDiameter_;
340     Dimension minCircleButtonIcon_;
341     Dimension maxCircleButtonDiameter_;
342     Dimension maxCircleButtonIcon_;
343     Dimension borderWidth_;
344     Dimension downloadHeight_;
345 
346     double bgDisabledAlpha_ = 1.0;
347     uint32_t textMaxLines_ = 1;
348 };
349 
350 } // namespace OHOS::Ace
351 
352 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_BUTTON_THEME_H
353