• 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_SLIDER_SLIDER_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SLIDER_SLIDER_THEME_H
18 
19 #include "base/geometry/dimension.h"
20 #include "core/components/common/properties/color.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  * SliderTheme defines color and styles of SliderComponent. SliderTheme should be built
29  * using SliderTheme::Builder.
30  */
31 class SliderTheme : public virtual Theme {
32     DECLARE_ACE_TYPE(SliderTheme, Theme);
33 
34 public:
35     class Builder {
36     public:
37         Builder() = default;
38         ~Builder() = default;
39 
40         static constexpr Color BLOCK_COLOR_PRESSED = Color(0x19182431);
41         static constexpr Color BLOCK_OUTER_EDGE_COLOR = Color(0x0A000000);
42         static constexpr Color BLOCK_SHADOW_COLOR = Color(0x26000000);
43         static constexpr Dimension BUBBLE_TO_CIRCLE_CENTER_DISTANCE = 20.0_vp;
44         static constexpr Dimension MEASURE_CONTENT_DEFAULT_WIDTH = 40.0_vp;
45         static constexpr Dimension OUTSET_HOT_BLOCK_SHADOW_WIDTH = 4.0_vp;
46         static constexpr Dimension INSET_HOT_BLOCK_SHADOW_WIDTH = 6.0_vp;
47         static constexpr Dimension FOCUS_SIDE_DISTANCE = 2.0_vp;
48 
Build(const RefPtr<ThemeConstants> & themeConstants)49         RefPtr<SliderTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
50         {
51             RefPtr<SliderTheme> theme = AceType::Claim(new SliderTheme());
52             if (!themeConstants) {
53                 return theme;
54             }
55             // init theme from global data
56             theme->outsetBlockSize_ = themeConstants->GetDimension(THEME_SLIDER_OUTSET_BLOCK_SIZE);
57             theme->outsetBlockHotSize_ = themeConstants->GetDimension(THEME_SLIDER_OUTSET_BLOCK_HOT_REGION_SIZE);
58             theme->blockColor_ = themeConstants->GetColor(THEME_SLIDER_BLOCK_COLOR);
59             theme->outsetTrackThickness_ = themeConstants->GetDimension(THEME_SLIDER_OUTSET_TRACK_THICKNESS);
60             theme->insetTrackThickness_ = themeConstants->GetDimension(THEME_SLIDER_INSET_TRACK_THICKNESS);
61             theme->trackSelectedColor_ = themeConstants->GetColor(THEME_SLIDER_TRACK_SELECTED);
62             theme->trackBgColor_ = themeConstants->GetColor(THEME_SLIDER_TRACK_BG);
63             theme->insetBlockSize_ = themeConstants->GetDimension(THEME_SLIDER_INSET_BLOCK_SIZE);
64             theme->insetBlockHotSize_ = themeConstants->GetDimension(THEME_SLIDER_INSET_BLOCK_HOT_REGION_SIZE);
65             theme->markerSize_ = themeConstants->GetDimension(THEME_SLIDER_MARKER_SIZE);
66             theme->markerColor_ = themeConstants->GetColor(THEME_SLIDER_MARKER_COLOR);
67             theme->tipColor_ = themeConstants->GetColor(THEME_SLIDER_TIP_COLOR);
68             theme->tipTextColor_ = themeConstants->GetColor(THEME_SLIDER_TIP_TEXT_COLOR);
69             theme->tipFontSize_ = themeConstants->GetDimension(THEME_SLIDER_TIP_FONT_SIZE);
70             theme->tipTextPadding_ = themeConstants->GetDimension(THEME_SLIDER_TIP_TEXT_PADDING_SIZE);
71             theme->blockHoverColor_ = themeConstants->GetColor(THEME_SLIDER_BLOCK_HOVER_COLOR);
72             theme->blockShadowColor_ = BLOCK_SHADOW_COLOR;
73             ParsePattern(themeConstants->GetThemeStyle(), theme);
74             return theme;
75         }
76 
ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<SliderTheme> & theme)77         void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<SliderTheme>& theme) const
78         {
79             if (!themeStyle) {
80                 LOGI("progress theme style is null");
81                 return;
82             }
83             auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_SLIDER, nullptr);
84             if (pattern) {
85                 const double defaultMarkColorAplpa = 0.1;
86                 theme->trackBgColor_ = pattern->GetAttr<Color>("track_bg_color", Color::RED);
87                 theme->trackSelectedColor_ = pattern->GetAttr<Color>("track_color_selected", Color::RED);
88                 theme->markerColor_ = pattern->GetAttr<Color>("marker_color", Color::RED)
89                     .BlendOpacity(pattern->GetAttr<double>("marker_color_alpha", defaultMarkColorAplpa));
90                 theme->tipTextColor_ = pattern->GetAttr<Color>("tip_text_color", Color::RED);
91                 theme->tipColor_ = pattern->GetAttr<Color>("tip_color", Color::RED);
92                 theme->blockHoverColor_ = pattern->GetAttr<Color>("block_color_hovered", Color::RED);
93                 theme->blockPressedColor_ = pattern->GetAttr<Color>("block_color_pressed", BLOCK_COLOR_PRESSED);
94                 theme->blockOuterEdgeColor_ =
95                     pattern->GetAttr<Color>("block_outer_edge_color", BLOCK_OUTER_EDGE_COLOR);
96                 theme->bubbleToCircleCenterDistance_ =
97                     pattern->GetAttr<Dimension>("bubble_to_circle_center_distance", BUBBLE_TO_CIRCLE_CENTER_DISTANCE);
98                 theme->measureContentDefaultWidth_ =
99                     pattern->GetAttr<Dimension>("measure_content_default_width", MEASURE_CONTENT_DEFAULT_WIDTH);
100                 theme->outsetHotBlockShadowWidth_ =
101                     pattern->GetAttr<Dimension>("outset_hot_block_shadow_width", OUTSET_HOT_BLOCK_SHADOW_WIDTH);
102                 theme->insetHotBlockShadowWidth_ =
103                     pattern->GetAttr<Dimension>("inset_hot_block_shadow_width", INSET_HOT_BLOCK_SHADOW_WIDTH);
104                 theme->focusSideDistance_ =
105                     pattern->GetAttr<Dimension>("focus_side_distance", FOCUS_SIDE_DISTANCE);
106                 theme->layoutMaxLength_ = pattern->GetAttr<Dimension>("slider_max_length", .0_vp);
107                 theme->hoverAnimationDuration_ = pattern->GetAttr<double>("hover_animation_duration", 0.0);
108                 theme->pressAnimationDuration_ = pattern->GetAttr<double>("press_animation_duration", 0.0);
109                 theme->moveAnimationDuration_ = pattern->GetAttr<double>("move_animation_duration", 0.0);
110             } else {
111                 LOGW("find pattern of slider fail");
112             }
113         }
114     };
115 
116     ~SliderTheme() override = default;
117 
GetOutsetBlockSize()118     Dimension GetOutsetBlockSize() const
119     {
120         return outsetBlockSize_;
121     }
122 
GetOutsetBlockHotSize()123     Dimension GetOutsetBlockHotSize() const
124     {
125         return outsetBlockHotSize_;
126     }
127 
GetInsetBlockSize()128     Dimension GetInsetBlockSize() const
129     {
130         return insetBlockSize_;
131     }
132 
GetInsetBlockHotSize()133     Dimension GetInsetBlockHotSize() const
134     {
135         return insetBlockHotSize_;
136     }
137 
GetBlockHoverColor()138     Color GetBlockHoverColor() const
139     {
140         return blockHoverColor_;
141     }
142 
GetBlockColor()143     Color GetBlockColor() const
144     {
145         return blockColor_;
146     }
147 
GetInsetTrackThickness()148     Dimension GetInsetTrackThickness() const
149     {
150         return insetTrackThickness_;
151     }
152 
GetOutsetTrackThickness()153     Dimension GetOutsetTrackThickness() const
154     {
155         return outsetTrackThickness_;
156     }
157 
GetMarkerSize()158     Dimension GetMarkerSize() const
159     {
160         return markerSize_;
161     }
162 
GetTipFontSize()163     Dimension GetTipFontSize() const
164     {
165         return tipFontSize_;
166     }
167 
GetTipTextPadding()168     Dimension GetTipTextPadding() const
169     {
170         return tipTextPadding_;
171     }
172 
GetBubbleToCircleCenterDistance()173     Dimension GetBubbleToCircleCenterDistance() const
174     {
175         return bubbleToCircleCenterDistance_;
176     }
177 
GetMeasureContentDefaultWidth()178     Dimension GetMeasureContentDefaultWidth() const
179     {
180         return measureContentDefaultWidth_;
181     }
182 
GetOutsetHotBlockShadowWidth()183     Dimension GetOutsetHotBlockShadowWidth() const
184     {
185         return outsetHotBlockShadowWidth_;
186     }
187 
GetInsetHotBlockShadowWidth()188     Dimension GetInsetHotBlockShadowWidth() const
189     {
190         return insetHotBlockShadowWidth_;
191     }
192 
GetBlockPressedColor()193     Color GetBlockPressedColor() const
194     {
195         return blockPressedColor_;
196     }
197 
GetBlockOuterEdgeColor()198     Color GetBlockOuterEdgeColor() const
199     {
200         return blockOuterEdgeColor_;
201     }
202 
GetTipColor()203     Color GetTipColor() const
204     {
205         return tipColor_;
206     }
207 
GetTipTextColor()208     Color GetTipTextColor() const
209     {
210         return tipTextColor_;
211     }
212 
GetMarkerColor()213     Color GetMarkerColor() const
214     {
215         return markerColor_;
216     }
217 
GetTrackBgColor()218     Color GetTrackBgColor() const
219     {
220         return trackBgColor_;
221     }
222 
GetTrackSelectedColor()223     Color GetTrackSelectedColor() const
224     {
225         return trackSelectedColor_;
226     }
227 
GetBlockShadowColor()228     Color GetBlockShadowColor() const
229     {
230         return blockShadowColor_;
231     }
232 
GetFocusSideDistance()233     Dimension GetFocusSideDistance() const
234     {
235         return focusSideDistance_;
236     }
237 
GetLayoutMaxLength()238     Dimension GetLayoutMaxLength() const
239     {
240         return layoutMaxLength_;
241     }
242 
GetHoverAnimationDuration()243     double GetHoverAnimationDuration() const
244     {
245         return hoverAnimationDuration_;
246     }
247 
GetPressAnimationDuration()248     double GetPressAnimationDuration() const
249     {
250         return pressAnimationDuration_;
251     }
252 
GetMoveAnimationDuration()253     double GetMoveAnimationDuration() const
254     {
255         return moveAnimationDuration_;
256     }
257 
258 protected:
259     SliderTheme() = default;
260 
261 private:
262     // outset slider mode
263     Dimension outsetBlockSize_;
264     Dimension outsetBlockHotSize_;
265     Dimension outsetTrackThickness_;
266     Dimension outsetHotBlockShadowWidth_;
267 
268     // inset slide mode
269     Dimension insetBlockSize_;
270     Dimension insetBlockHotSize_;
271     Dimension insetTrackThickness_;
272     Dimension insetHotBlockShadowWidth_;
273 
274     // common
275     Dimension markerSize_;
276     Dimension tipFontSize_;
277     Dimension tipTextPadding_;
278     Dimension bubbleToCircleCenterDistance_;
279     Dimension measureContentDefaultWidth_;
280     Color blockColor_;
281     Color blockHoverColor_;
282     Color blockPressedColor_;
283     Color blockOuterEdgeColor_;
284     Color tipColor_;
285     Color tipTextColor_;
286     Color markerColor_;
287     Color trackBgColor_;
288     Color trackSelectedColor_;
289     Color blockShadowColor_;
290 
291     // others
292     Dimension focusSideDistance_;
293     Dimension layoutMaxLength_;
294     double hoverAnimationDuration_ = 0.0;
295     double pressAnimationDuration_ = 0.0;
296     double moveAnimationDuration_ = 0.0;
297 };
298 
299 } // namespace OHOS::Ace
300 
301 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SLIDER_SLIDER_THEME_H
302