• 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         static constexpr double DEFAULT_SLIDER_PPI = 775.0;
49         static constexpr int32_t SLIDER_TIP_DELAY_TIME = 2000;
50 #ifdef SUPPORT_DIGITAL_CROWN
51         static constexpr double CROWN_DISPLAY_CONTROL_RATIO = 2.1;
52 #endif
53 
Build(const RefPtr<ThemeConstants> & themeConstants)54         RefPtr<SliderTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
55         {
56             RefPtr<SliderTheme> theme = AceType::Claim(new SliderTheme());
57             if (!themeConstants) {
58                 return theme;
59             }
60             // init theme from global data
61             ParsePattern(themeConstants, theme);
62             return theme;
63         }
64 
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<SliderTheme> & theme)65         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<SliderTheme>& theme) const
66         {
67             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_SLIDER);
68             if (pattern) {
69                 const double defaultMarkColorAplpa = 0.1;
70                 theme->trackBgColor_ = pattern->GetAttr<Color>("track_bg_color", Color::RED);
71                 theme->trackSelectedColor_ = pattern->GetAttr<Color>("track_color_selected", Color::RED);
72                 theme->markerColor_ =
73                     pattern->GetAttr<Color>("marker_color", Color::RED)
74                         .BlendOpacity(pattern->GetAttr<double>("marker_color_alpha", defaultMarkColorAplpa));
75                 theme->tipTextColor_ = pattern->GetAttr<Color>("tip_text_color", Color::RED);
76                 theme->tipColor_ = pattern->GetAttr<Color>("tip_color", Color::RED);
77                 theme->blockHoverColor_ = pattern->GetAttr<Color>("block_color_hovered", Color::RED);
78                 theme->blockPressedColor_ = pattern->GetAttr<Color>("block_color_pressed", BLOCK_COLOR_PRESSED);
79                 theme->blockOuterEdgeColor_ = pattern->GetAttr<Color>("block_outer_edge_color", BLOCK_OUTER_EDGE_COLOR);
80                 theme->bubbleToCircleCenterDistance_ =
81                     pattern->GetAttr<Dimension>("bubble_to_circle_center_distance", BUBBLE_TO_CIRCLE_CENTER_DISTANCE);
82                 theme->measureContentDefaultWidth_ =
83                     pattern->GetAttr<Dimension>("measure_content_default_width", MEASURE_CONTENT_DEFAULT_WIDTH);
84                 theme->outsetHotBlockShadowWidth_ =
85                     pattern->GetAttr<Dimension>("outset_hot_block_shadow_width", OUTSET_HOT_BLOCK_SHADOW_WIDTH);
86                 theme->insetHotBlockShadowWidth_ =
87                     pattern->GetAttr<Dimension>("inset_hot_block_shadow_width", INSET_HOT_BLOCK_SHADOW_WIDTH);
88                 theme->focusSideDistance_ = pattern->GetAttr<Dimension>("focus_side_distance", FOCUS_SIDE_DISTANCE);
89                 theme->layoutMaxLength_ = pattern->GetAttr<Dimension>("slider_max_length", .0_vp);
90                 theme->hoverAnimationDuration_ = pattern->GetAttr<double>("hover_animation_duration", 0.0);
91                 theme->pressAnimationDuration_ = pattern->GetAttr<double>("press_animation_duration", 0.0);
92                 theme->moveAnimationDuration_ = pattern->GetAttr<double>("move_animation_duration", 0.0);
93                 theme->disabledAlpha_ = pattern->GetAttr<double>("slider_disable_alpha", 0.0);
94                 theme->sliderPPI_ = pattern->GetAttr<double>("slider_pixels_per_inch", DEFAULT_SLIDER_PPI);
95                 theme->outsetBlockSize_ = pattern->GetAttr<Dimension>("outset_block_size", 16.0_vp);
96                 theme->outsetBlockHotSize_ = pattern->GetAttr<Dimension>("outset_block_hot_region_size", 40.0_vp);
97                 theme->blockColor_ = pattern->GetAttr<Color>("block_color", Color(0xffffffff));
98                 theme->outsetTrackThickness_ = pattern->GetAttr<Dimension>("outset_track_thickness", 4.0_vp);
99                 theme->insetTrackThickness_ = pattern->GetAttr<Dimension>("inset_track_thickness", 20.0_vp);
100                 theme->insetBlockSize_ = pattern->GetAttr<Dimension>("inset_block_size", 12.0_vp);
101                 theme->insetBlockHotSize_ = pattern->GetAttr<Dimension>("inset_block_hot_region_size", 32.0_vp);
102                 theme->noneTrackThickness_ = pattern->GetAttr<Dimension>("none_track_thickness", 4.0_vp);
103                 theme->noneBlockHotSize_ = pattern->GetAttr<Dimension>("none_block_hot_region_size", 40.0_vp);
104                 theme->markerSize_ = pattern->GetAttr<Dimension>("marker_size", 4.0_vp);
105                 theme->tipFontSize_ = pattern->GetAttr<Dimension>("tip_font_size", 14.0_fp);
106                 theme->tipTextPadding_ = pattern->GetAttr<Dimension>("tip_text_padding_size", 8.0_vp);
107                 theme->blockShadowColor_ = pattern->GetAttr<Color>("block_shadow_color", BLOCK_SHADOW_COLOR);
108                 theme->showFocusFrame_ = static_cast<bool>(pattern->GetAttr<double>("show_focus_frame", 0.0));
109                 theme->focusedScaleValue_ = pattern->GetAttr<double>("focused_scale_value", 1.0);
110                 theme->outsetModeSelectedTrackColor_ =
111                     pattern->GetAttr<Color>("outset_mode_selected_track_color", Color(0xff007dff));
112                 theme->noneModeSelectedTrackColor_ =
113                     pattern->GetAttr<Color>("none_mode_selected_track_color", Color(0xff007dff));
114                 theme->measureContentOutsetWidth_ =
115                     pattern->GetAttr<Dimension>("measure_content_outset_width", MEASURE_CONTENT_DEFAULT_WIDTH);
116                 theme->selectedTxt_ = pattern->GetAttr<std::string>("slider_accessibility_selected", "");
117                 theme->unselectedTxt_ = pattern->GetAttr<std::string>("slider_accessibility_unselected", "");
118                 theme->unselectedDesc_ = pattern->GetAttr<std::string>("slider_accessibility_unselectedDesc", "");
119                 theme->disabledDesc_ = pattern->GetAttr<std::string>("slider_accessibility_disabledDesc", "");
120                 theme->tipDelayTime_ = pattern->GetAttr<int32_t>("slider_tip_delay_time", SLIDER_TIP_DELAY_TIME);
121 #ifdef SUPPORT_DIGITAL_CROWN
122                 theme->crownDisplayControlRatio_ =
123                     pattern->GetAttr<double>("crown_display_control_ratio", CROWN_DISPLAY_CONTROL_RATIO);
124 #endif
125             } else {
126                 LOGW("find pattern of slider fail");
127             }
128         }
129     };
130 
131     ~SliderTheme() override = default;
132 
GetOutsetBlockSize()133     Dimension GetOutsetBlockSize() const
134     {
135         return outsetBlockSize_;
136     }
137 
GetOutsetBlockHotSize()138     Dimension GetOutsetBlockHotSize() const
139     {
140         return outsetBlockHotSize_;
141     }
142 
GetInsetBlockSize()143     Dimension GetInsetBlockSize() const
144     {
145         return insetBlockSize_;
146     }
147 
GetInsetBlockHotSize()148     Dimension GetInsetBlockHotSize() const
149     {
150         return insetBlockHotSize_;
151     }
152 
GetBlockHoverColor()153     Color GetBlockHoverColor() const
154     {
155         return blockHoverColor_;
156     }
157 
GetBlockColor()158     Color GetBlockColor() const
159     {
160         return blockColor_;
161     }
162 
GetInsetTrackThickness()163     Dimension GetInsetTrackThickness() const
164     {
165         return insetTrackThickness_;
166     }
167 
GetOutsetTrackThickness()168     Dimension GetOutsetTrackThickness() const
169     {
170         return outsetTrackThickness_;
171     }
172 
GetNoneBlockHotSize()173     Dimension GetNoneBlockHotSize() const
174     {
175         return noneBlockHotSize_;
176     }
GetNoneTrackThickness()177     Dimension GetNoneTrackThickness() const
178     {
179         return noneTrackThickness_;
180     }
181 
GetMarkerSize()182     Dimension GetMarkerSize() const
183     {
184         return markerSize_;
185     }
186 
GetTipFontSize()187     Dimension GetTipFontSize() const
188     {
189         return tipFontSize_;
190     }
191 
GetTipTextPadding()192     Dimension GetTipTextPadding() const
193     {
194         return tipTextPadding_;
195     }
196 
GetBubbleToCircleCenterDistance()197     Dimension GetBubbleToCircleCenterDistance() const
198     {
199         return bubbleToCircleCenterDistance_;
200     }
201 
GetMeasureContentDefaultWidth()202     Dimension GetMeasureContentDefaultWidth() const
203     {
204         return measureContentDefaultWidth_;
205     }
206 
GetOutsetHotBlockShadowWidth()207     Dimension GetOutsetHotBlockShadowWidth() const
208     {
209         return outsetHotBlockShadowWidth_;
210     }
211 
GetInsetHotBlockShadowWidth()212     Dimension GetInsetHotBlockShadowWidth() const
213     {
214         return insetHotBlockShadowWidth_;
215     }
216 
GetBlockPressedColor()217     Color GetBlockPressedColor() const
218     {
219         return blockPressedColor_;
220     }
221 
GetBlockOuterEdgeColor()222     Color GetBlockOuterEdgeColor() const
223     {
224         return blockOuterEdgeColor_;
225     }
226 
GetTipColor()227     Color GetTipColor() const
228     {
229         return tipColor_;
230     }
231 
GetTipTextColor()232     Color GetTipTextColor() const
233     {
234         return tipTextColor_;
235     }
236 
GetMarkerColor()237     Color GetMarkerColor() const
238     {
239         return markerColor_;
240     }
241 
GetTrackBgColor()242     Color GetTrackBgColor() const
243     {
244         return trackBgColor_;
245     }
246 
GetTrackSelectedColor()247     Color GetTrackSelectedColor() const
248     {
249         return trackSelectedColor_;
250     }
251 
GetBlockShadowColor()252     Color GetBlockShadowColor() const
253     {
254         return blockShadowColor_;
255     }
256 
GetFocusSideDistance()257     Dimension GetFocusSideDistance() const
258     {
259         return focusSideDistance_;
260     }
261 
GetLayoutMaxLength()262     Dimension GetLayoutMaxLength() const
263     {
264         return layoutMaxLength_;
265     }
266 
GetHoverAnimationDuration()267     double GetHoverAnimationDuration() const
268     {
269         return hoverAnimationDuration_;
270     }
271 
GetPressAnimationDuration()272     double GetPressAnimationDuration() const
273     {
274         return pressAnimationDuration_;
275     }
276 
GetMoveAnimationDuration()277     double GetMoveAnimationDuration() const
278     {
279         return moveAnimationDuration_;
280     }
281 
GetDisabledAlpha()282     double GetDisabledAlpha() const
283     {
284         return disabledAlpha_;
285     }
286 
GetSliderPPI()287     double GetSliderPPI() const
288     {
289         return sliderPPI_;
290     }
291 
GetSelectedTxt()292     std::string GetSelectedTxt() const
293     {
294         return selectedTxt_;
295     }
GetUnselectedTxt()296     std::string GetUnselectedTxt() const
297     {
298         return unselectedTxt_;
299     }
GetUnselectedDesc()300     std::string GetUnselectedDesc() const
301     {
302         return unselectedDesc_;
303     }
GetDisabelDesc()304     std::string GetDisabelDesc() const
305     {
306         return disabledDesc_;
307     }
GetTipDelayTime()308     int32_t GetTipDelayTime() const
309     {
310         return tipDelayTime_;
311     }
312 
313 #ifdef SUPPORT_DIGITAL_CROWN
GetCrownDisplayControlRatio()314     double GetCrownDisplayControlRatio() const
315     {
316         return crownDisplayControlRatio_;
317     }
318 #endif
319 
ShowFocusFrame()320     bool ShowFocusFrame() const
321     {
322         return showFocusFrame_;
323     }
324 
GetFocusedScaleValue()325     double GetFocusedScaleValue() const
326     {
327         return focusedScaleValue_;
328     }
329 
GetMeasureContentOutsetWidth()330     Dimension GetMeasureContentOutsetWidth() const
331     {
332         return measureContentOutsetWidth_;
333     }
334 
GetOutsetModeSelectedTrackColor()335     const Color& GetOutsetModeSelectedTrackColor() const
336     {
337         return outsetModeSelectedTrackColor_;
338     }
339 
GetNoneModeSelectedTrackColor()340     const Color& GetNoneModeSelectedTrackColor() const
341     {
342         return noneModeSelectedTrackColor_;
343     }
344 
345 protected:
346     SliderTheme() = default;
347 
348 private:
349     // outset slider mode
350     Dimension outsetBlockSize_;
351     Dimension outsetBlockHotSize_;
352     Dimension outsetTrackThickness_;
353     Dimension outsetHotBlockShadowWidth_;
354 
355     // inset slide mode
356     Dimension insetBlockSize_;
357     Dimension insetBlockHotSize_;
358     Dimension insetTrackThickness_;
359     Dimension insetHotBlockShadowWidth_;
360 
361     // none slider mode
362     Dimension noneBlockHotSize_;
363     Dimension noneTrackThickness_;
364 
365     // common
366     Dimension markerSize_;
367     Dimension tipFontSize_;
368     Dimension tipTextPadding_;
369     Dimension bubbleToCircleCenterDistance_;
370     Dimension measureContentDefaultWidth_;
371     Color blockColor_;
372     Color blockHoverColor_;
373     Color blockPressedColor_;
374     Color blockOuterEdgeColor_;
375     Color tipColor_;
376     Color tipTextColor_;
377     Color markerColor_;
378     Color trackBgColor_;
379     Color trackSelectedColor_;
380     Color blockShadowColor_;
381 
382     // others
383     Dimension focusSideDistance_;
384     Dimension layoutMaxLength_;
385     double hoverAnimationDuration_ = 0.0;
386     double pressAnimationDuration_ = 0.0;
387     double moveAnimationDuration_ = 0.0;
388     double disabledAlpha_ = 1.0;
389     double sliderPPI_ = 0.0;
390     int32_t tipDelayTime_ = 0;
391 
392     // accessibility
393     std::string selectedTxt_ = "";
394     std::string unselectedTxt_ = "";
395     std::string unselectedDesc_ = "";
396     std::string disabledDesc_ = "";
397 #ifdef SUPPORT_DIGITAL_CROWN
398     double crownDisplayControlRatio_ = 1.0;
399 #endif
400     bool showFocusFrame_ = 0.0;
401     double focusedScaleValue_ = 1.0;
402     Dimension measureContentOutsetWidth_;
403     Color outsetModeSelectedTrackColor_;
404     Color noneModeSelectedTrackColor_;
405 };
406 
407 } // namespace OHOS::Ace
408 
409 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SLIDER_SLIDER_THEME_H
410