• 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::Claim(new ToggleTheme());
43             if (!themeConstants) {
44                 return theme;
45             }
46             theme->backgroundColor_ = themeConstants->GetColor(THEME_TOGGLE_BACKGROUND_COLOR);
47             theme->checkedColor_ = themeConstants->GetColor(THEME_TOGGLE_CHECKED_COLOR);
48             theme->textStyle_.SetTextColor(themeConstants->GetColor(THEME_TOGGLE_TEXT_COLOR));
49             theme->textStyle_.SetFontSize(themeConstants->GetDimension(THEME_TOGGLE_TEXT_FONTSIZE));
50             theme->textStyle_.SetFontWeight(FontWeight(themeConstants->GetInt(THEME_TOGGLE_TEXT_FONTWEIGHT)));
51             theme->height_ = themeConstants->GetDimension(THEME_TOGGLE_HEIGHT);
52             theme->padding_ = Edge(themeConstants->GetDimension(THEME_TOGGLE_PADDING_HORIZONTAL).Value(),
53                 themeConstants->GetDimension(THEME_TOGGLE_PADDING_VERTICAL).Value(),
54                 themeConstants->GetDimension(THEME_TOGGLE_PADDING_HORIZONTAL).Value(),
55                 themeConstants->GetDimension(THEME_TOGGLE_PADDING_VERTICAL).Value(),
56                 themeConstants->GetDimension(THEME_TOGGLE_PADDING_VERTICAL).Unit());
57             theme->disabledAlpha_ = themeConstants->GetDouble(THEME_TOGGLE_DISABLED_ALPHA);
58             theme->pressedBlendColor_ = themeConstants->GetColor(THEME_TOGGLE_PRESSED_BLEND_COLOR);
59             ParsePattern(themeConstants->GetThemeStyle(), theme);
60             return theme;
61         }
62 
63     private:
ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<ToggleTheme> & theme)64         void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<ToggleTheme>& theme) const
65         {
66             if (!themeStyle) {
67                 return;
68             }
69             auto togglePattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_TOGGLE, nullptr);
70             if (!togglePattern) {
71                 return;
72             }
73             theme->backgroundColor_ = togglePattern->GetAttr<Color>(PATTERN_BG_COLOR, Color());
74             theme->checkedColor_ = togglePattern->GetAttr<Color>("bg_color_checked", Color())
75                 .BlendOpacity(togglePattern->GetAttr<double>("bg_color_checked_alpha", 0.0));
76             theme->textStyle_.SetTextColor(togglePattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color()));
77             theme->pressedBlendColor_ = togglePattern->GetAttr<Color>("bg_color_pressed_blend", Color());
78             theme->textMargin_ = togglePattern->GetAttr<Dimension>("text_margin", Dimension());
79             theme->buttonMargin_ = togglePattern->GetAttr<Dimension>("button_margin", Dimension());
80             theme->buttonHeight_ = togglePattern->GetAttr<Dimension>("button_height", Dimension());
81             theme->buttonRadius_ = togglePattern->GetAttr<Dimension>("button_radius", Dimension());
82             theme->textFontSize_ = togglePattern->GetAttr<Dimension>("text_font_size", Dimension());
83             theme->textColor_ = togglePattern->GetAttr<Color>("text_color", Color());
84         }
85     };
86 
87     ~ToggleTheme() override = default;
88 
GetBackgroundColor()89     const Color& GetBackgroundColor() const
90     {
91         return backgroundColor_;
92     }
93 
GetCheckedColor()94     const Color& GetCheckedColor() const
95     {
96         return checkedColor_;
97     }
98 
GetTextStyle()99     const TextStyle& GetTextStyle() const
100     {
101         return textStyle_;
102     }
103 
GetHeight()104     const Dimension& GetHeight() const
105     {
106         return height_;
107     }
108 
GetPadding()109     const Edge& GetPadding() const
110     {
111         return padding_;
112     }
113 
GetPressedBlendColor()114     const Color& GetPressedBlendColor() const
115     {
116         return pressedBlendColor_;
117     }
118 
GetDisabledAlpha()119     double GetDisabledAlpha() const
120     {
121         return disabledAlpha_;
122     }
123 
GetTextMargin()124     const Dimension& GetTextMargin() const
125     {
126         return textMargin_;
127     }
128 
GetButtonMargin()129     const Dimension& GetButtonMargin() const
130     {
131         return buttonMargin_;
132     }
133 
GetButtonHeight()134     const Dimension& GetButtonHeight() const
135     {
136         return buttonHeight_;
137     }
138 
GetButtonRadius()139     const Dimension& GetButtonRadius() const
140     {
141         return buttonRadius_;
142     }
143 
GetTextFontSize()144     const Dimension& GetTextFontSize() const
145     {
146         return textFontSize_;
147     }
148 
GetTextColor()149     const Color& GetTextColor() const
150     {
151         return textColor_;
152     }
153 
154 
155 protected:
156     ToggleTheme() = default;
157 
158 private:
159     Color backgroundColor_;
160     Color checkedColor_;
161     Color pressedBlendColor_;
162     TextStyle textStyle_;
163     Dimension height_;
164     Edge padding_;
165     double disabledAlpha_ { 1.0 };
166     Dimension textMargin_;
167     Dimension buttonMargin_;
168     Dimension buttonHeight_;
169     Dimension buttonRadius_;
170     Dimension textFontSize_;
171     Color textColor_;
172 };
173 
174 } // namespace OHOS::Ace
175 
176 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOGGLE_TOGGLE_THEME_H
177