• 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_TOAST_TOAST_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOAST_TOAST_THEME_H
18 
19 #include "core/components/common/properties/color.h"
20 #include "core/components/common/properties/edge.h"
21 #include "core/components/common/properties/text_style.h"
22 #include "core/components/theme/theme.h"
23 #include "core/components/theme/theme_constants.h"
24 #include "core/components/theme/theme_constants_defines.h"
25 
26 namespace OHOS::Ace {
27 
28 /**
29  * ToastTheme defines color and styles of Toast. ToastTheme should be built
30  * using ToastTheme::Builder.
31  */
32 class ToastTheme : public virtual Theme {
33     DECLARE_ACE_TYPE(ToastTheme, Theme);
34 
35 public:
36     class Builder {
37     public:
38         Builder() = default;
39         ~Builder() = default;
40 
Build(const RefPtr<ThemeConstants> & themeConstants)41         RefPtr<ToastTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
42         {
43             RefPtr<ToastTheme> theme = AceType::Claim(new ToastTheme());
44             if (!themeConstants) {
45                 return theme;
46             }
47             // init theme from global data
48             theme->padding_ = Edge(themeConstants->GetDimension(THEME_TOAST_PADDING_HORIZONTAL).Value(),
49                 themeConstants->GetDimension(THEME_TOAST_PADDING_VERTICAL).Value(),
50                 themeConstants->GetDimension(THEME_TOAST_PADDING_HORIZONTAL).Value(),
51                 themeConstants->GetDimension(THEME_TOAST_PADDING_VERTICAL).Value(),
52                 themeConstants->GetDimension(THEME_TOAST_PADDING_VERTICAL).Unit());
53             theme->maxWidth_ = themeConstants->GetDimension(THEME_TOAST_CONTENT_MAX_WIDTH);
54             theme->minWidth_ = themeConstants->GetDimension(THEME_TOAST_CONTENT_MIN_WIDTH);
55             theme->minHeight_ = themeConstants->GetDimension(THEME_TOAST_CONTENT_MIN_HEIGHT);
56             theme->backgroundColor_ = themeConstants->GetColor(THEME_TOAST_BACKGROUND_COLOR);
57             theme->textStyle_.SetFontWeight(FontWeight(themeConstants->GetInt(THEME_TOAST_TEXT_TEXT_FONTWEIGHT)));
58             theme->textStyle_.SetTextColor(themeConstants->GetColor(THEME_TOAST_TEXT_COLOR));
59             theme->textStyle_.SetFontSize(themeConstants->GetDimension(THEME_TOAST_TEXT_FONTSIZE));
60             theme->radius_ = Radius(
61                 themeConstants->GetDimension(THEME_TOAST_RADIUS), themeConstants->GetDimension(THEME_TOAST_RADIUS));
62             theme->bottom_ = themeConstants->GetDimension(THEME_TOAST_BOTTOM);
63             theme->marging_ = Edge(themeConstants->GetDimension(THEME_TOAST_MARGIN).Value(), 0.0,
64                 themeConstants->GetDimension(THEME_TOAST_MARGIN).Value(), 0.0,
65                 themeConstants->GetDimension(THEME_TOAST_MARGIN).Unit());
66             theme->minFontSize_ = themeConstants->GetDimension(THEME_TOAST_TEXT_FONTSIZE_MIN);
67             auto textMaxLines = themeConstants->GetInt(THEME_TOAST_TEXT_MAX_LINES);
68             theme->textMaxLines_ = textMaxLines < 0 ? theme->textMaxLines_ : static_cast<uint32_t>(textMaxLines);
69             ParsePattern(themeConstants->GetThemeStyle(), theme);
70             return theme;
71         }
72     private:
ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<ToastTheme> & theme)73         void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<ToastTheme>& theme) const
74         {
75             if (!themeStyle) {
76                 return;
77             }
78             auto toastPattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_TOAST, nullptr);
79             if (!toastPattern) {
80                 return;
81             }
82             theme->textStyle_.SetFontSize(toastPattern->GetAttr<Dimension>(TOAST_FONT_SIZE, 0.0_vp));
83             theme->textStyle_.SetTextColor(toastPattern->GetAttr<Color>(TOAST_TEXT_COLOR, Color()));
84             theme->backgroundColor_ = toastPattern->GetAttr<Color>(TOAST_BACKGROUND_COLOR, Color());
85         }
86     };
87 
88     ~ToastTheme() override = default;
89 
GetPadding()90     const Edge& GetPadding() const
91     {
92         return padding_;
93     }
94 
GetMaxWidth()95     const Dimension& GetMaxWidth() const
96     {
97         return maxWidth_;
98     }
99 
GetMinWidth()100     const Dimension& GetMinWidth() const
101     {
102         return minWidth_;
103     }
104 
GetMinHeight()105     const Dimension& GetMinHeight() const
106     {
107         return minHeight_;
108     }
109 
GetBackgroundColor()110     const Color& GetBackgroundColor() const
111     {
112         return backgroundColor_;
113     }
114 
GetTextStyle()115     const TextStyle& GetTextStyle() const
116     {
117         return textStyle_;
118     }
119 
GetRadius()120     const Radius& GetRadius() const
121     {
122         return radius_;
123     }
124 
GetBottom()125     const Dimension& GetBottom() const
126     {
127         return bottom_;
128     }
129 
GetMinFontSize()130     const Dimension& GetMinFontSize() const
131     {
132         return minFontSize_;
133     }
134 
GetTextMaxLines()135     uint32_t GetTextMaxLines() const
136     {
137         return textMaxLines_;
138     }
139 
GetMarging()140     const Edge& GetMarging() const
141     {
142         return marging_;
143     }
144 
145 protected:
146     ToastTheme() = default;
147 
148 private:
149     Edge padding_;
150     Dimension maxWidth_;
151     Dimension minWidth_;
152     Dimension minHeight_;
153     Color backgroundColor_;
154     TextStyle textStyle_;
155     Radius radius_;
156     Dimension bottom_;
157     Dimension minFontSize_;
158     uint32_t textMaxLines_ = 1;
159     Edge marging_;
160 };
161 
162 } // namespace OHOS::Ace
163 
164 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOAST_TOAST_THEME_H
165