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::Claim(new ToggleTheme());
43             if (!themeConstants) {
44                 return theme;
45             }
46             ParsePattern(themeConstants, theme);
47             return theme;
48         }
49 
50     private:
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         }
79     };
80 
81     ~ToggleTheme() override = default;
82 
GetBackgroundColor()83     const Color& GetBackgroundColor() const
84     {
85         return backgroundColor_;
86     }
87 
GetCheckedColor()88     const Color& GetCheckedColor() const
89     {
90         return checkedColor_;
91     }
92 
GetTextStyle()93     const TextStyle& GetTextStyle() const
94     {
95         return textStyle_;
96     }
97 
GetHeight()98     const Dimension& GetHeight() const
99     {
100         return height_;
101     }
102 
GetPadding()103     const Edge& GetPadding() const
104     {
105         return padding_;
106     }
107 
GetPressedBlendColor()108     const Color& GetPressedBlendColor() const
109     {
110         return pressedBlendColor_;
111     }
112 
GetDisabledAlpha()113     double GetDisabledAlpha() const
114     {
115         return disabledAlpha_;
116     }
117 
GetTextMargin()118     const Dimension& GetTextMargin() const
119     {
120         return textMargin_;
121     }
122 
GetButtonMargin()123     const Dimension& GetButtonMargin() const
124     {
125         return buttonMargin_;
126     }
127 
GetButtonHeight()128     const Dimension& GetButtonHeight() const
129     {
130         return buttonHeight_;
131     }
132 
GetButtonRadius()133     const Dimension& GetButtonRadius() const
134     {
135         return buttonRadius_;
136     }
137 
GetTextFontSize()138     const Dimension& GetTextFontSize() const
139     {
140         return textFontSize_;
141     }
142 
GetTextColor()143     const Color& GetTextColor() const
144     {
145         return textColor_;
146     }
147 
148 protected:
149     ToggleTheme() = default;
150 
151 private:
152     Color backgroundColor_;
153     Color checkedColor_;
154     Color pressedBlendColor_;
155     TextStyle textStyle_;
156     Dimension height_;
157     Edge padding_;
158     double disabledAlpha_ { 1.0 };
159     Dimension textMargin_;
160     Dimension buttonMargin_;
161     Dimension buttonHeight_;
162     Dimension buttonRadius_;
163     Dimension textFontSize_;
164     Color textColor_;
165 };
166 
167 } // namespace OHOS::Ace
168 
169 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOGGLE_TOGGLE_THEME_H
170