• 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             ParsePattern(themeConstants, theme);
48             return theme;
49         }
50     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<ToastTheme> & theme)51         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<ToastTheme>& theme) const
52         {
53             RefPtr<ThemeStyle> toastPattern = themeConstants->GetPatternByName(THEME_PATTERN_TOAST);
54             if (!toastPattern) {
55                 return;
56             }
57             theme->padding_ = Edge(toastPattern->GetAttr<Dimension>("toast_padding_horizontal", 0.0_vp).Value(),
58                 toastPattern->GetAttr<Dimension>("toast_padding_vertical", 0.0_vp).Value(),
59                 toastPattern->GetAttr<Dimension>("toast_padding_horizontal", 0.0_vp).Value(),
60                 toastPattern->GetAttr<Dimension>("toast_padding_vertical", 0.0_vp).Value(),
61                 toastPattern->GetAttr<Dimension>("toast_padding_vertical", 0.0_vp).Unit());
62             theme->maxWidth_ = toastPattern->GetAttr<Dimension>("toast_content_max_width", 0.0_vp);
63             theme->minWidth_ = toastPattern->GetAttr<Dimension>("toast_content_min_width", 0.0_vp);
64             theme->minHeight_ = toastPattern->GetAttr<Dimension>("toast_content_min_height", 0.0_vp);
65             theme->textStyle_.SetFontWeight(
66                 FontWeight(static_cast<int32_t>(toastPattern->GetAttr<double>("toast_text_font_weight", 0.0))));
67             theme->bottom_ = toastPattern->GetAttr<Dimension>("toast_bottom", 0.0_vp);
68             theme->marging_ = Edge(toastPattern->GetAttr<Dimension>("toast_margin", 0.0_vp).Value(), 0.0,
69                 toastPattern->GetAttr<Dimension>("toast_margin", 0.0_vp).Value(), 0.0,
70                 toastPattern->GetAttr<Dimension>("toast_margin", 0.0_vp).Unit());
71             theme->minFontSize_ = toastPattern->GetAttr<Dimension>("toast_text_min_font_size", 0.0_vp);
72             auto textMaxLines = static_cast<int32_t>(toastPattern->GetAttr<double>("toast_text_max_lines", 0.0));
73             theme->textMaxLines_ = textMaxLines < 0 ? theme->textMaxLines_ : static_cast<uint32_t>(textMaxLines);
74             theme->textStyle_.SetFontSize(toastPattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_vp));
75             theme->textStyle_.SetTextColor(toastPattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color()));
76             theme->backgroundColor_ = toastPattern->GetAttr<Color>(PATTERN_BG_COLOR, Color());
77             theme->radius_ = Radius(toastPattern->GetAttr<Dimension>("toast_border_radius", 24.0_vp),
78                 toastPattern->GetAttr<Dimension>("toast_border_radius", 24.0_vp));
79         }
80     };
81 
82     ~ToastTheme() override = default;
83 
GetPadding()84     const Edge& GetPadding() const
85     {
86         return padding_;
87     }
88 
GetMaxWidth()89     const Dimension& GetMaxWidth() const
90     {
91         return maxWidth_;
92     }
93 
GetMinWidth()94     const Dimension& GetMinWidth() const
95     {
96         return minWidth_;
97     }
98 
GetMinHeight()99     const Dimension& GetMinHeight() const
100     {
101         return minHeight_;
102     }
103 
GetBackgroundColor()104     const Color& GetBackgroundColor() const
105     {
106         return backgroundColor_;
107     }
108 
GetTextStyle()109     const TextStyle& GetTextStyle() const
110     {
111         return textStyle_;
112     }
113 
GetRadius()114     const Radius& GetRadius() const
115     {
116         return radius_;
117     }
118 
GetBottom()119     const Dimension& GetBottom() const
120     {
121         return bottom_;
122     }
123 
GetMinFontSize()124     const Dimension& GetMinFontSize() const
125     {
126         return minFontSize_;
127     }
128 
GetTextMaxLines()129     uint32_t GetTextMaxLines() const
130     {
131         return textMaxLines_;
132     }
133 
GetMarging()134     const Edge& GetMarging() const
135     {
136         return marging_;
137     }
138 
139 protected:
140     ToastTheme() = default;
141 
142 private:
143     Edge padding_;
144     Dimension maxWidth_;
145     Dimension minWidth_;
146     Dimension minHeight_;
147     Color backgroundColor_;
148     TextStyle textStyle_;
149     Radius radius_;
150     Dimension bottom_;
151     Dimension minFontSize_;
152     uint32_t textMaxLines_ = 1;
153     Edge marging_;
154 };
155 
156 } // namespace OHOS::Ace
157 
158 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOAST_TOAST_THEME_H
159