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