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/common/container.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components/common/properties/edge.h"
22 #include "core/components/common/properties/text_style.h"
23 #include "core/components/theme/theme.h"
24 #include "core/components/theme/theme_constants.h"
25 #include "core/components/theme/theme_constants_defines.h"
26 
27 namespace OHOS::Ace {
28 
29 /**
30  * ToastTheme defines color and styles of Toast. ToastTheme should be built
31  * using ToastTheme::Builder.
32  */
33 class ToastTheme : public virtual Theme {
34     DECLARE_ACE_TYPE(ToastTheme, Theme);
35 
36 public:
37     class Builder {
38     public:
39         Builder() = default;
40         ~Builder() = default;
41 
Build(const RefPtr<ThemeConstants> & themeConstants)42         RefPtr<ToastTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
43         {
44             RefPtr<ToastTheme> theme = AceType::Claim(new ToastTheme());
45             if (!themeConstants) {
46                 return theme;
47             }
48             ParsePattern(themeConstants, theme);
49             return theme;
50         }
51     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<ToastTheme> & theme)52         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<ToastTheme>& theme) const
53         {
54             RefPtr<ThemeStyle> toastPattern = themeConstants->GetPatternByName(THEME_PATTERN_TOAST);
55             if (!toastPattern) {
56                 return;
57             }
58 
59             theme->minWidth_ = toastPattern->GetAttr<Dimension>("toast_content_min_width", 0.0_vp);
60             theme->minHeight_ = toastPattern->GetAttr<Dimension>("toast_content_min_height", 0.0_vp);
61             theme->bottom_ = toastPattern->GetAttr<Dimension>("toast_bottom", 0.0_vp);
62             theme->minFontSize_ = toastPattern->GetAttr<Dimension>("toast_text_min_font_size", 0.0_vp);
63             auto textMaxLines = static_cast<int32_t>(toastPattern->GetAttr<double>("toast_text_max_lines", 0.0));
64             theme->textMaxLines_ = textMaxLines < 0 ? theme->textMaxLines_ : static_cast<uint32_t>(textMaxLines);
65             theme->backgroundColor_ = toastPattern->GetAttr<Color>(PATTERN_BG_COLOR, Color());
66             theme->blurStyleTextColor_ = toastPattern->GetAttr<Color>(PATTERN_TEXT_COLOR_BLUR, Color());
67 
68             if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
69                 theme->padding_ = Edge(toastPattern->GetAttr<Dimension>("toast_padding_level8", 0.0_vp).Value(),
70                     toastPattern->GetAttr<Dimension>("toast_padding_level4", 0.0_vp).Value(),
71                     toastPattern->GetAttr<Dimension>("toast_padding_level8", 0.0_vp).Value(),
72                     toastPattern->GetAttr<Dimension>("toast_padding_level4", 0.0_vp).Value(),
73                     toastPattern->GetAttr<Dimension>("toast_padding_level4", 0.0_vp).Unit());
74                 theme->marging_ = Edge(toastPattern->GetAttr<Dimension>("toast_margin_left", 0.0_vp).Value(), 0.0,
75                     toastPattern->GetAttr<Dimension>("toast_margin_right", 0.0_vp).Value(), 0.0,
76                     toastPattern->GetAttr<Dimension>("toast_margin_right", 0.0_vp).Unit());
77                 theme->maxWidth_ = toastPattern->GetAttr<Dimension>("toast_content_limit_max_width", 400.0_vp);
78                 theme->textStyle_.SetTextColor(toastPattern->GetAttr<Color>("toast_font_primary", Color()));
79                 theme->textStyle_.SetFontSize(toastPattern->GetAttr<Dimension>("toast_Body_M", 0.0_vp));
80                 theme->textStyle_.SetFontWeight(
81                     FontWeight(static_cast<int32_t>(toastPattern->GetAttr<double>("toast_font_weight_regular", 0.0))));
82                 theme->radius_ = Radius(toastPattern->GetAttr<Dimension>("toast_border_radius_level9", 24.0_vp),
83                     toastPattern->GetAttr<Dimension>("toast_border_radius_level9", 24.0_vp));
84             } else {
85                 theme->padding_ = Edge(toastPattern->GetAttr<Dimension>("toast_padding_horizontal", 0.0_vp).Value(),
86                     toastPattern->GetAttr<Dimension>("toast_padding_vertical", 0.0_vp).Value(),
87                     toastPattern->GetAttr<Dimension>("toast_padding_horizontal", 0.0_vp).Value(),
88                     toastPattern->GetAttr<Dimension>("toast_padding_vertical", 0.0_vp).Value(),
89                     toastPattern->GetAttr<Dimension>("toast_padding_vertical", 0.0_vp).Unit());
90                 theme->marging_ = Edge(toastPattern->GetAttr<Dimension>("toast_margin", 0.0_vp).Value(), 0.0,
91                     toastPattern->GetAttr<Dimension>("toast_margin", 0.0_vp).Value(), 0.0,
92                     toastPattern->GetAttr<Dimension>("toast_margin", 0.0_vp).Unit());
93                 theme->maxWidth_ = toastPattern->GetAttr<Dimension>("toast_content_max_width", 0.0_vp);
94                 theme->textStyle_.SetTextColor(toastPattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color()));
95                 theme->textStyle_.SetFontSize(toastPattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_vp));
96                 theme->textStyle_.SetFontWeight(
97                     FontWeight(static_cast<int32_t>(toastPattern->GetAttr<double>("toast_text_font_weight", 0.0))));
98                 theme->radius_ = Radius(toastPattern->GetAttr<Dimension>("toast_border_radius", 24.0_vp),
99                     toastPattern->GetAttr<Dimension>("toast_border_radius", 24.0_vp));
100             }
101         }
102     };
103 
104     ~ToastTheme() override = default;
105 
GetPadding()106     const Edge& GetPadding() const
107     {
108         return padding_;
109     }
110 
GetMaxWidth()111     const Dimension& GetMaxWidth() const
112     {
113         return maxWidth_;
114     }
115 
GetMinWidth()116     const Dimension& GetMinWidth() const
117     {
118         return minWidth_;
119     }
120 
GetMinHeight()121     const Dimension& GetMinHeight() const
122     {
123         return minHeight_;
124     }
125 
GetBackgroundColor()126     const Color& GetBackgroundColor() const
127     {
128         return backgroundColor_;
129     }
130 
GetTextStyle()131     const TextStyle& GetTextStyle() const
132     {
133         return textStyle_;
134     }
135 
GetRadius()136     const Radius& GetRadius() const
137     {
138         return radius_;
139     }
140 
GetBottom()141     const Dimension& GetBottom() const
142     {
143         return bottom_;
144     }
145 
GetMinFontSize()146     const Dimension& GetMinFontSize() const
147     {
148         return minFontSize_;
149     }
150 
GetTextMaxLines()151     uint32_t GetTextMaxLines() const
152     {
153         return textMaxLines_;
154     }
155 
GetMarging()156     const Edge& GetMarging() const
157     {
158         return marging_;
159     }
160 
GetBlurStyleTextColor()161     const Color& GetBlurStyleTextColor() const
162     {
163         return blurStyleTextColor_;
164     }
165 
166 protected:
167     ToastTheme() = default;
168 
169 private:
170     Edge padding_;
171     Dimension maxWidth_;
172     Dimension minWidth_;
173     Dimension minHeight_;
174     Color backgroundColor_;
175     TextStyle textStyle_;
176     Radius radius_;
177     Dimension bottom_;
178     Dimension minFontSize_;
179     uint32_t textMaxLines_ = 1;
180     Edge marging_;
181     Color blurStyleTextColor_;
182 };
183 
184 } // namespace OHOS::Ace
185 
186 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOAST_TOAST_THEME_H
187