• 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_TOGGLE_TOGGLE_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOGGLE_TOGGLE_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  * ToggleTheme defines color and styles of ToggleComponent. ToggleTheme should be built
29  * using ToggleTheme::Builder.
30  */
31 class ToggleTheme : public virtual Theme {
32     DECLARE_ACE_TYPE(ToggleTheme, Theme);
33 
34 public:
35     class Builder {
36     public:
37         Builder() = default;
38         ~Builder() = default;
39 
Build(const RefPtr<ThemeConstants> & themeConstants)40         RefPtr<ToggleTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
41         {
42             RefPtr<ToggleTheme> theme = AceType::MakeRefPtr<ToggleTheme>();
43             if (!themeConstants) {
44                 return theme;
45             }
46             ParsePattern(themeConstants, theme);
47             return theme;
48         }
49 
50     protected:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<ToggleTheme> & theme)51         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<ToggleTheme>& theme) const
52         {
53             RefPtr<ThemeStyle> togglePattern = themeConstants->GetPatternByName(THEME_PATTERN_TOGGLE);
54             if (!togglePattern) {
55                 return;
56             }
57             theme->textStyle_.SetFontSize(togglePattern->GetAttr<Dimension>("toggle_text_font_size", 0.0_fp));
58             theme->textStyle_.SetFontWeight(
59                 FontWeight(static_cast<int32_t>(togglePattern->GetAttr<double>("toggle_text_font_weight", 0.0))));
60             theme->height_ = togglePattern->GetAttr<Dimension>("toggle_height", 0.0_vp);
61             theme->padding_ = Edge(togglePattern->GetAttr<Dimension>("toggle_padding_horizontal", 0.0_vp).Value(),
62                 togglePattern->GetAttr<Dimension>("toggle_padding_vertical", 0.0_vp).Value(),
63                 togglePattern->GetAttr<Dimension>("toggle_padding_horizontal", 0.0_vp).Value(),
64                 togglePattern->GetAttr<Dimension>("toggle_padding_vertical", 0.0_vp).Value(),
65                 togglePattern->GetAttr<Dimension>("toggle_padding_vertical", 0.0_vp).Unit());
66             theme->disabledAlpha_ = togglePattern->GetAttr<double>("toggle_disable_alpha", 0.0);
67             theme->backgroundColor_ = togglePattern->GetAttr<Color>(PATTERN_BG_COLOR, Color());
68             theme->checkedColor_ = togglePattern->GetAttr<Color>("bg_color_checked", Color())
69                 .BlendOpacity(togglePattern->GetAttr<double>("bg_color_checked_alpha", 0.0));
70             theme->textStyle_.SetTextColor(togglePattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color()));
71             theme->pressedBlendColor_ = togglePattern->GetAttr<Color>("bg_color_pressed_blend", Color());
72             theme->textMargin_ = togglePattern->GetAttr<Dimension>("text_margin", Dimension());
73             theme->buttonMargin_ = togglePattern->GetAttr<Dimension>("button_margin", Dimension());
74             theme->buttonHeight_ = togglePattern->GetAttr<Dimension>("button_height", Dimension());
75             theme->buttonRadius_ = togglePattern->GetAttr<Dimension>("button_radius", Dimension());
76             theme->textFontSize_ = togglePattern->GetAttr<Dimension>("text_font_size", Dimension());
77             theme->textColor_ = togglePattern->GetAttr<Color>("text_color", Color());
78             theme->shadowNormal_ = static_cast<uint32_t>(
79                 togglePattern->GetAttr<double>("shadow_default_status_button", SHADOW_NONE));
80             theme->shadowFocus_ = static_cast<uint32_t>(
81                 togglePattern->GetAttr<double>("shadow_focus_status_button", SHADOW_NONE));
82             theme->scaleHoverOrFocus_ = togglePattern->GetAttr<double>("scale_focus_status_button", 1.0);
83             theme->borderWidth_ = togglePattern->GetAttr<Dimension>("border_width_status_button", Dimension());
84             theme->borderColorChecked_ = togglePattern->GetAttr<Color>("border_color_status_button_checked", Color());
85             theme->borderColorUnchecked_ = togglePattern->GetAttr<Color>(
86                 "border_color_status_button_unchecked", Color());
87             theme->bgColorFocusChecked_ = togglePattern->GetAttr<Color>(
88                 "bg_color_focus_status_button_checked", Color())
89                 .BlendOpacity(togglePattern->GetAttr<double>("bg_color_checked_focus_alpha", 0.0));
90             theme->bgColorFocusUnchecked_ = togglePattern->GetAttr<Color>(
91                 "bg_color_focus_status_button_unchecked", Color());
92             theme->textColorFocus_ = togglePattern->GetAttr<Color>("status_button_text_color_focus", Color());
93         }
94     };
95 
96     ~ToggleTheme() override = default;
97 
GetBackgroundColor()98     const Color& GetBackgroundColor() const
99     {
100         return backgroundColor_;
101     }
102 
GetCheckedColor()103     const Color& GetCheckedColor() const
104     {
105         return checkedColor_;
106     }
107 
GetTextStyle()108     const TextStyle& GetTextStyle() const
109     {
110         return textStyle_;
111     }
112 
GetHeight()113     const Dimension& GetHeight() const
114     {
115         return height_;
116     }
117 
GetPadding()118     const Edge& GetPadding() const
119     {
120         return padding_;
121     }
122 
GetPressedBlendColor()123     const Color& GetPressedBlendColor() const
124     {
125         return pressedBlendColor_;
126     }
127 
GetDisabledAlpha()128     double GetDisabledAlpha() const
129     {
130         return disabledAlpha_;
131     }
132 
GetTextMargin()133     const Dimension& GetTextMargin() const
134     {
135         return textMargin_;
136     }
137 
GetButtonMargin()138     const Dimension& GetButtonMargin() const
139     {
140         return buttonMargin_;
141     }
142 
GetButtonHeight()143     const Dimension& GetButtonHeight() const
144     {
145         return buttonHeight_;
146     }
147 
GetButtonRadius()148     const Dimension& GetButtonRadius() const
149     {
150         return buttonRadius_;
151     }
152 
GetTextFontSize()153     const Dimension& GetTextFontSize() const
154     {
155         return textFontSize_;
156     }
157 
GetBorderWidth()158     const Dimension& GetBorderWidth() const
159     {
160         return borderWidth_;
161     }
162 
GetTextColor()163     const Color& GetTextColor() const
164     {
165         return textColor_;
166     }
167 
GetTextColorFocus()168     const Color& GetTextColorFocus() const
169     {
170         return textColorFocus_;
171     }
172 
GetBorderColorChecked()173     const Color& GetBorderColorChecked() const
174     {
175         return borderColorChecked_;
176     }
177 
GetBorderColorUnchecked()178     const Color& GetBorderColorUnchecked() const
179     {
180         return borderColorUnchecked_;
181     }
182 
GetBackgroundColorFocusChecked()183     const Color& GetBackgroundColorFocusChecked() const
184     {
185         return bgColorFocusChecked_;
186     }
187 
GetBackgroundColorFocusUnchecked()188     const Color& GetBackgroundColorFocusUnchecked() const
189     {
190         return bgColorFocusUnchecked_;
191     }
192 
GetShadowNormal()193     uint32_t GetShadowNormal() const
194     {
195         return shadowNormal_;
196     }
197 
GetShadowFocus()198     uint32_t GetShadowFocus() const
199     {
200         return shadowFocus_;
201     }
202 
GetScaleHoverOrFocus()203     float GetScaleHoverOrFocus() const
204     {
205         return scaleHoverOrFocus_;
206     }
207 
208 protected:
209     ToggleTheme() = default;
210     Color backgroundColor_;
211     Color checkedColor_;
212 
213 private:
214     Color pressedBlendColor_;
215     TextStyle textStyle_;
216     Dimension height_;
217     Edge padding_;
218     double disabledAlpha_ { 1.0 };
219     Dimension textMargin_;
220     Dimension buttonMargin_;
221     Dimension buttonHeight_;
222     Dimension buttonRadius_;
223     Dimension textFontSize_;
224     Dimension borderWidth_;
225     Color textColor_;
226     Color borderColorUnchecked_;
227     Color borderColorChecked_;
228     Color bgColorFocusChecked_;
229     Color bgColorFocusUnchecked_;
230     Color textColorFocus_;
231     uint32_t shadowNormal_;
232     uint32_t shadowFocus_;
233     float scaleHoverOrFocus_;
234     static constexpr double SHADOW_NONE = 6.0;
235 };
236 
237 } // namespace OHOS::Ace
238 
239 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOGGLE_TOGGLE_THEME_H
240