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_STEPPER_STEPPER_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_STEPPER_STEPPER_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 #include "core/components/theme/theme_manager.h"
25 
26 namespace OHOS::Ace {
27 namespace {
28 constexpr double DEFAULT_ALPHA_VALUE = 0.9;
29 constexpr Dimension STEPPER_FOCUSED_BORDER_WIDTH = 2.0_vp;
30 constexpr Dimension STEPPER_SCREEN_MARGIN = 4.0_vp;
31 } // namespace
32 /**
33  * StepperTheme defines color and styles of StepperComponent. StepperTheme should be built
34  * using StepperTheme::Builder.
35  */
36 class StepperTheme : public virtual Theme {
37     DECLARE_ACE_TYPE(StepperTheme, Theme);
38 
39 public:
40     class Builder {
41     public:
42         Builder() = default;
43         ~Builder() = default;
44 
Build(const RefPtr<ThemeConstants> & themeConstants)45         RefPtr<StepperTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
46         {
47             RefPtr<StepperTheme> theme = AceType::Claim(new StepperTheme());
48             if (!themeConstants) {
49                 return theme;
50             }
51 
52             auto themeStyle = themeConstants->GetThemeStyle();
53             if (themeStyle) {
54                 auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>("stepper_pattern", nullptr);
55                 if (pattern) {
56                     theme->textStyle_.SetTextColor(pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::RED));
57                     theme->textStyle_.SetFontSize(pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 16.0_vp));
58                     theme->radius_ = pattern->GetAttr<Dimension>("border_radius", 8.0_vp);
59                     theme->buttonPressedColor_ = pattern->GetAttr<Color>("button_bg_color_pressed", Color::RED);
60                     theme->mouseHoverColor_ = pattern->GetAttr<Color>("button_bg_color_hovered", Color::RED);
61                     theme->defaultPaddingStart_ = pattern->GetAttr<Dimension>("padding_left", 12.0_vp);
62                     theme->defaultPaddingEnd_ = pattern->GetAttr<Dimension>("padding_right", 12.0_vp);
63                     theme->arrowColor_ = pattern->GetAttr<Color>("arrorw_color", Color::RED);
64                     theme->progressColor_ = pattern->GetAttr<Color>("progress_color", Color::RED);
65                     theme->disabledColor_ = pattern->GetAttr<Color>("button_bg_color_disabled", Color::RED);
66                     theme->disabledAlpha_ = pattern->GetAttr<double>("button_bg_color_disabled_alpha", 0.0);
67                     theme->defaultAlpha_ =
68                         pattern->GetAttr<double>("attribute_alpha_content_primary", DEFAULT_ALPHA_VALUE);
69                     theme->focusColor_ = pattern->GetAttr<Color>(STEPPER_FOCUS_COLOR, Color::RED);
70                     theme->focusBorderWidth_ = STEPPER_FOCUSED_BORDER_WIDTH;
71                     theme->controlMargin_ = STEPPER_SCREEN_MARGIN;
72 
73                     theme->textStyle_.SetFontWeight(FontWeight(pattern->GetAttr<int>("stepper_text_fontweight",
74                         static_cast<int32_t>(FontWeight::W500))));
75                     theme->textStyle_.SetFontStyle(FontStyle::NORMAL);
76                     theme->textStyle_.SetTextDecoration(TextDecoration::NONE);
77                     std::vector<std::string> families;
78                     families.emplace_back("sans-serif");
79                     theme->textStyle_.SetFontFamilies(families);
80                     theme->minFontSize_ = pattern->GetAttr<Dimension>("text_fontsize_min", 9.0_fp);
81                     uint32_t maxlines = static_cast<uint32_t>(pattern->GetAttr<int>("text_max_lines", 2));
82                     theme->textMaxLines_ = maxlines < 0 ? theme->textMaxLines_ : maxlines;
83                     theme->progressDiameter_ = pattern->GetAttr<Dimension>("progress_diameter", 24.0_vp);
84                     theme->arrowWidth_ = pattern->GetAttr<Dimension>("arrow_width_diameter", 12.0_vp);
85                     theme->arrowHeight_ = pattern->GetAttr<Dimension>("arrow_height_diameter", 24.0_vp);
86                     theme->buttonPressedHeight_ = pattern->GetAttr<Dimension>("button_pressed_height", 40.0_vp);
87                     theme->controlHeight_ = pattern->GetAttr<Dimension>("control_height", 48.0_vp);
88                     theme->controlPadding_ = pattern->GetAttr<Dimension>("control_padding", 8.0_vp);
89                 }
90             }
91             return theme;
92         }
93     };
94 
95     ~StepperTheme() override = default;
96 
GetTextStyle()97     const TextStyle& GetTextStyle() const
98     {
99         return textStyle_;
100     }
101 
GetMinFontSize()102     const Dimension& GetMinFontSize() const
103     {
104         return minFontSize_;
105     }
106 
GetTextMaxLines()107     uint32_t GetTextMaxLines() const
108     {
109         return textMaxLines_;
110     }
111 
GetDefaultPaddingStart()112     const Dimension& GetDefaultPaddingStart() const
113     {
114         return defaultPaddingStart_;
115     }
116 
GetDefaultPaddingEnd()117     const Dimension& GetDefaultPaddingEnd() const
118     {
119         return defaultPaddingEnd_;
120     }
121 
GetProgressColor()122     const Color& GetProgressColor() const
123     {
124         return progressColor_;
125     }
126 
GetProgressDiameter()127     const Dimension& GetProgressDiameter() const
128     {
129         return progressDiameter_;
130     }
131 
GetArrowWidth()132     const Dimension& GetArrowWidth() const
133     {
134         return arrowWidth_;
135     }
136 
GetArrowHeight()137     const Dimension& GetArrowHeight() const
138     {
139         return arrowHeight_;
140     }
141 
GetArrowColor()142     const Color& GetArrowColor() const
143     {
144         return arrowColor_;
145     }
146 
GetDisabledColor()147     const Color& GetDisabledColor() const
148     {
149         return disabledColor_;
150     }
151 
GetRadius()152     const Dimension& GetRadius() const
153     {
154         return radius_;
155     }
156 
GetButtonPressedColor()157     const Color& GetButtonPressedColor() const
158     {
159         return buttonPressedColor_;
160     }
161 
GetButtonPressedHeight()162     const Dimension& GetButtonPressedHeight() const
163     {
164         return buttonPressedHeight_;
165     }
166 
GetControlHeight()167     const Dimension& GetControlHeight() const
168     {
169         return controlHeight_;
170     }
171 
GetControlMargin()172     const Dimension& GetControlMargin() const
173     {
174         return controlMargin_;
175     }
176 
GetControlPadding()177     const Dimension& GetControlPadding() const
178     {
179         return controlPadding_;
180     }
181 
GetFocusColor()182     const Color& GetFocusColor() const
183     {
184         return focusColor_;
185     }
186 
GetFocusBorderWidth()187     const Dimension& GetFocusBorderWidth() const
188     {
189         return focusBorderWidth_;
190     }
191 
GetMouseHoverColor()192     const Color& GetMouseHoverColor() const
193     {
194         return mouseHoverColor_;
195     }
196 
GetDisabledAlpha()197     double GetDisabledAlpha() const
198     {
199         return disabledAlpha_;
200     }
201 
GetDefaultAlpha()202     double GetDefaultAlpha() const
203     {
204         return defaultAlpha_;
205     }
206 
207 protected:
208     StepperTheme() = default;
209 
210 private:
211     TextStyle textStyle_;
212     Dimension minFontSize_;
213     uint32_t textMaxLines_ = 1;
214     Dimension defaultPaddingStart_;
215     Dimension defaultPaddingEnd_;
216     Color progressColor_;
217     Dimension progressDiameter_;
218     Dimension arrowWidth_;
219     Dimension arrowHeight_;
220     Color arrowColor_;
221     Color disabledColor_;
222     Dimension radius_;
223     Color buttonPressedColor_;
224     Dimension buttonPressedHeight_;
225     Dimension controlHeight_;
226     Dimension controlMargin_;
227     Dimension controlPadding_;
228     Color focusColor_;
229     Dimension focusBorderWidth_;
230     Color mouseHoverColor_;
231     double disabledAlpha_ = 0.4;
232     double defaultAlpha_ = 0.9;
233 };
234 
235 } // namespace OHOS::Ace
236 
237 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_STEPPER_STEPPER_THEME_H
238