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_PROGRESS_PROGRESS_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PROGRESS_PROGRESS_THEME_H 18 19 #include "base/geometry/dimension.h" 20 #include "core/common/container.h" 21 #include "core/components/common/properties/color.h" 22 #include "core/components/theme/theme.h" 23 #include "core/components/theme/theme_constants.h" 24 #include "core/components/theme/theme_constants_defines.h" 25 26 namespace OHOS::Ace { 27 28 /** 29 * ProgressTheme defines color and styles of ProgressComponent. ProgressTheme should be built 30 * using ProgressTheme::Builder. 31 */ 32 class ProgressTheme : public virtual Theme { 33 DECLARE_ACE_TYPE(ProgressTheme, Theme); 34 35 public: 36 class Builder { 37 public: 38 Builder() = default; 39 ~Builder() = default; 40 Build(const RefPtr<ThemeConstants> & themeConstants)41 RefPtr<ProgressTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 42 { 43 RefPtr<ProgressTheme> theme = AceType::Claim(new ProgressTheme()); 44 if (!themeConstants) { 45 return theme; 46 } 47 // Read style from system. 48 ParsePattern(themeConstants->GetThemeStyle(), theme); 49 return theme; 50 } 51 ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<ProgressTheme> & theme)52 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<ProgressTheme>& theme) const 53 { 54 if (!themeStyle) { 55 return; 56 } 57 auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>("progress_pattern", nullptr); 58 if (!pattern) { 59 return; 60 } 61 const double defaultCachedAlpha = 0.4; 62 const double defaultLoadBGAlpha = 0.6; 63 const double defaultRingBackgroundOpacity = 0.03; 64 Color defaultColor = Color::FromRGBO(18, 24, 31, 1.0); 65 theme->trackThickness_ = pattern->GetAttr<Dimension>("progress_thickness", 0.0_vp); 66 theme->trackWidth_ = pattern->GetAttr<Dimension>("progress_default_width", 0.0_vp); 67 theme->ringThickness_ = pattern->GetAttr<Dimension>("progress_ring_thickness", 0.0_vp); 68 theme->ringDiameter_ = pattern->GetAttr<Dimension>("progress_default_diameter", 0.0_vp); 69 theme->loadingDiameter_ = pattern->GetAttr<Dimension>("progress_default_diameter", 0.0_vp); 70 theme->scaleNumber_ = static_cast<int32_t>(pattern->GetAttr<double>("progress_scale_number", 0.0)); 71 theme->scaleWidth_ = pattern->GetAttr<Dimension>("progress_scale_width", 0.0_vp); 72 theme->scaleLength_ = pattern->GetAttr<Dimension>("progress_stroke_width", 0.0_vp); 73 theme->scaleRingDiameter_ = pattern->GetAttr<Dimension>("progress_scale_default_diameter", 0.0_vp); 74 theme->moonDiameter_ = pattern->GetAttr<Dimension>("progress_moon_diameter", 0.0_vp); 75 theme->moveRatio_ = pattern->GetAttr<double>("progress_loading_move_ratio", 0.0); 76 theme->ringRadius_ = pattern->GetAttr<Dimension>("progress_loading_ring_radius", 0.0_vp); 77 theme->orbitRadius_ = pattern->GetAttr<Dimension>("progress_loading_orbit_radius", 0.0_vp); 78 theme->cometTailLen_ = pattern->GetAttr<double>("progress_loading_comet_tail_len", 0.0); 79 theme->bubbleRadius_ = pattern->GetAttr<Dimension>("progress_bubble_radius", 0.0_vp); 80 theme->bubbleDiameter_ = pattern->GetAttr<Dimension>("progress_bubble_diameter", 0.0_vp); 81 theme->progressHeight_ = pattern->GetAttr<Dimension>("progress_button_download_height", 0.0_vp); 82 theme->trackBgColor_ = pattern->GetAttr<Color>("bg_color", Color::RED); 83 theme->trackSelectedColor_ = pattern->GetAttr<Color>("fg_color", Color::RED); 84 theme->trackCachedColor_ = theme->trackSelectedColor_ 85 .BlendOpacity(pattern->GetAttr<double>("fg_color_cached_alpha", defaultCachedAlpha)); 86 theme->progressColor_ = pattern->GetAttr<Color>("fg_progress_color", Color::RED); 87 theme->loadingColor_ = pattern->GetAttr<Color>("fg_progress_color", Color::RED); 88 theme->moonFrontColor_ = pattern->GetAttr<Color>("moon_progress_fg_color", Color::RED) 89 .BlendOpacity(pattern->GetAttr<double>("moon_progress_fg_color_alpha", 1.0)); 90 theme->moonTrackBackgroundColor_ = pattern->GetAttr<Color>("moon_progress_bg_color", Color::RED) 91 .BlendOpacity(pattern->GetAttr<double>("moon_progress_bg_color_alpha", 1.0)) 92 .BlendOpacity(pattern->GetAttr<double>("moon_progress_bg_color_alpha_ex", 1.0)); 93 theme->borderColor_ = pattern->GetAttr<Color>("progress_border_color", Color::RED) 94 .BlendOpacity(pattern->GetAttr<double>("progress_border_color_alpha", 1.0)); 95 theme->maskColor_ = pattern->GetAttr<Color>("progress_mask_color", Color::RED); 96 theme->borderWidth_ = pattern->GetAttr<Dimension>("progress_border_width", 1.0_vp); 97 98 theme->textColor_ = pattern->GetAttr<Color>("progress_text_color", defaultColor); 99 theme->textSize_ = pattern->GetAttr<Dimension>("progress_text_size", 12.0_fp); 100 theme->capsuleSelectColor_ = pattern->GetAttr<Color>("capsule_progress_select_color", Color::RED); 101 theme->selectColorAlpha_ = pattern->GetAttr<double>("capsule_progress_default_alpha", 1.0); 102 theme->capsuleSelectColor_ = theme->capsuleSelectColor_.BlendOpacity(theme->selectColorAlpha_); 103 theme->progressDisable_ = pattern->GetAttr<double>("progress_disabled_alpha", 1.0); 104 theme->clickEffect_ = pattern->GetAttr<Color>("progress_click_effect", Color::RED); 105 theme->capsuleBgColor_ = pattern->GetAttr<Color>("capsule_progress_bg_color", Color::RED) 106 .BlendOpacity(pattern->GetAttr<double>("capsule_progress_bg_alpha", 1.0)); 107 theme->ringProgressEndSideColor_ = 108 pattern->GetAttr<Color>("ring_progress_fg_color_end", theme->trackSelectedColor_); 109 theme->ringProgressBeginSideColor_ = 110 pattern->GetAttr<Color>("ring_progress_fg_color_begin", theme->trackSelectedColor_); 111 theme->ringProgressBackgroundColor_ = 112 pattern->GetAttr<Color>("ring_progress_bg_color", Color::GRAY); 113 114 if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TEN)) { 115 theme->capsuleBgColor_ = theme->trackBgColor_; 116 theme->capsuleSelectColor_ = theme->trackSelectedColor_; 117 theme->ringProgressBackgroundColor_ = theme->trackBgColor_; 118 theme->ringProgressBeginSideColor_ = theme->trackSelectedColor_; 119 theme->ringProgressEndSideColor_ = theme->trackSelectedColor_; 120 } else if (Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN)) { 121 theme->capsuleSelectColor_ = 122 pattern->GetAttr<Color>("progress_select_color", Color::RED).BlendOpacity(theme->selectColorAlpha_); 123 theme->borderColor_ = theme->capsuleSelectColor_; 124 theme->ringProgressBackgroundColor_ = theme->trackBgColor_.ChangeOpacity(defaultRingBackgroundOpacity); 125 theme->loadingColor_ = theme->loadingColor_.BlendOpacity( 126 pattern->GetAttr<double>("loading_progress_bg_color_alpha", defaultLoadBGAlpha)); 127 } 128 } 129 }; 130 131 ~ProgressTheme() override = default; 132 GetTrackThickness()133 const Dimension& GetTrackThickness() const 134 { 135 return trackThickness_; 136 } 137 GetTrackWidth()138 const Dimension& GetTrackWidth() const 139 { 140 return trackWidth_; 141 } 142 GetRingThickness()143 const Dimension& GetRingThickness() const 144 { 145 return ringThickness_; 146 } 147 GetRingDiameter()148 const Dimension& GetRingDiameter() const 149 { 150 return ringDiameter_; 151 } 152 GetTrackBgColor()153 const Color& GetTrackBgColor() const 154 { 155 return trackBgColor_; 156 } 157 GetTrackSelectedColor()158 const Color& GetTrackSelectedColor() const 159 { 160 return trackSelectedColor_; 161 } 162 GetTrackCachedColor()163 Color GetTrackCachedColor() const 164 { 165 return trackCachedColor_; 166 } 167 GetLoadingDiameter()168 const Dimension& GetLoadingDiameter() const 169 { 170 return loadingDiameter_; 171 } 172 GetLoadingColor()173 const Color& GetLoadingColor() const 174 { 175 return loadingColor_; 176 } 177 GetScaleWidth()178 const Dimension& GetScaleWidth() const 179 { 180 return scaleWidth_; 181 } 182 GetScaleNumber()183 int32_t GetScaleNumber() const 184 { 185 return scaleNumber_; 186 } 187 GetScaleLength()188 const Dimension& GetScaleLength() const 189 { 190 return scaleLength_; 191 } 192 GetProgressColor()193 const Color& GetProgressColor() const 194 { 195 return progressColor_; 196 } 197 GetMoveRatio()198 double GetMoveRatio() const 199 { 200 return moveRatio_; 201 } 202 GetRingRadius()203 const Dimension& GetRingRadius() const 204 { 205 return ringRadius_; 206 } 207 GetOrbitRadius()208 const Dimension& GetOrbitRadius() const 209 { 210 return orbitRadius_; 211 } 212 GetCometTailLen()213 double GetCometTailLen() const 214 { 215 return cometTailLen_; 216 } 217 GetScaleRingDiameter()218 const Dimension& GetScaleRingDiameter() const 219 { 220 return scaleRingDiameter_; 221 } 222 GetMoonDiameter()223 const Dimension& GetMoonDiameter() const 224 { 225 return moonDiameter_; 226 } 227 GetMoonBackgroundColor()228 const Color& GetMoonBackgroundColor() const 229 { 230 return moonTrackBackgroundColor_; 231 } 232 GetMoonFrontColor()233 const Color& GetMoonFrontColor() const 234 { 235 return moonFrontColor_; 236 } 237 GetBubbleDiameter()238 const Dimension& GetBubbleDiameter() const 239 { 240 return bubbleDiameter_; 241 } 242 GetBubbleRadius()243 const Dimension& GetBubbleRadius() const 244 { 245 return bubbleRadius_; 246 } 247 GetBorderColor()248 const Color& GetBorderColor() const 249 { 250 return borderColor_; 251 } 252 GetBorderWidth()253 const Dimension& GetBorderWidth() const 254 { 255 return borderWidth_; 256 } 257 GetMaskColor()258 const Color& GetMaskColor() const 259 { 260 return maskColor_; 261 } 262 GetTextColor()263 const Color& GetTextColor() const 264 { 265 return textColor_; 266 } 267 GetTextSize()268 const Dimension& GetTextSize() const 269 { 270 return textSize_; 271 } 272 GetProgressHeight()273 const Dimension& GetProgressHeight() const 274 { 275 return progressHeight_; 276 } 277 GetCapsuleSelectColor()278 const Color& GetCapsuleSelectColor() const 279 { 280 return capsuleSelectColor_; 281 } 282 GetProgressDisable()283 const float& GetProgressDisable() const 284 { 285 return progressDisable_; 286 } 287 GetClickEffect()288 const Color& GetClickEffect() const 289 { 290 return clickEffect_; 291 } 292 GetSelectColorAlpha()293 const float& GetSelectColorAlpha() const 294 { 295 return selectColorAlpha_; 296 } 297 GetTextMargin()298 const Dimension& GetTextMargin() const 299 { 300 return textMargin_; 301 } 302 GetCapsuleBgColor()303 const Color& GetCapsuleBgColor() const 304 { 305 return capsuleBgColor_; 306 } 307 GetRingProgressEndSideColor()308 const Color& GetRingProgressEndSideColor() const 309 { 310 return ringProgressEndSideColor_; 311 } 312 GetRingProgressBeginSideColor()313 const Color& GetRingProgressBeginSideColor() const 314 { 315 return ringProgressBeginSideColor_; 316 } 317 GetRingProgressBgColor()318 const Color& GetRingProgressBgColor() const 319 { 320 return ringProgressBackgroundColor_; 321 } 322 323 protected: 324 ProgressTheme() = default; 325 326 private: 327 Dimension trackThickness_; 328 Dimension trackWidth_; 329 Color trackBgColor_; 330 Color trackSelectedColor_; 331 Color trackCachedColor_; 332 333 Dimension ringThickness_; 334 Dimension ringDiameter_; 335 Dimension bubbleDiameter_; 336 Dimension bubbleRadius_; 337 338 Dimension loadingDiameter_; 339 Color loadingColor_; 340 341 Dimension scaleWidth_; 342 int32_t scaleNumber_ = 0; 343 Dimension scaleLength_; 344 Dimension scaleRingDiameter_; 345 346 Dimension moonDiameter_; 347 Color moonTrackBackgroundColor_; 348 Color moonFrontColor_; 349 350 // For loading progress in cycle type. 351 Color progressColor_; 352 double moveRatio_ = 0.0; 353 Dimension ringRadius_; 354 Dimension orbitRadius_; 355 double cometTailLen_ = 0.0; 356 357 Color borderColor_; 358 Dimension borderWidth_; 359 Color maskColor_; 360 361 // For capsule progress. 362 Color textColor_; 363 Dimension textSize_; 364 Dimension progressHeight_; 365 Color capsuleSelectColor_; 366 float progressDisable_ = 0.4; 367 Color clickEffect_; 368 float selectColorAlpha_ = 1.0f; 369 const Dimension textMargin_ = 8.0_vp; 370 Color capsuleBgColor_; 371 372 // For ring progress. 373 Color ringProgressEndSideColor_; 374 Color ringProgressBeginSideColor_; 375 Color ringProgressBackgroundColor_; 376 }; 377 378 } // namespace OHOS::Ace 379 380 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PROGRESS_PROGRESS_THEME_H 381