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