1 /* 2 * Copyright (c) 2021-2022 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_CHECKABLE_CHECKABLE_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_CHECKABLE_THEME_H 18 19 #include "base/utils/system_properties.h" 20 #include "core/common/container.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 class CheckableTheme : public virtual Theme { 28 DECLARE_ACE_TYPE(CheckableTheme, Theme); 29 30 public: 31 ~CheckableTheme() override = default; 32 GetPointColor()33 const Color& GetPointColor() const 34 { 35 return pointColor_; 36 } GetActiveColor()37 const Color& GetActiveColor() const 38 { 39 return activeColor_; 40 } GetInactiveColor()41 const Color& GetInactiveColor() const 42 { 43 return inactiveColor_; 44 } GetFocusColor()45 const Color& GetFocusColor() const 46 { 47 return focusColor_; 48 } GetFocusBoardColor()49 const Color& GetFocusBoardColor() const 50 { 51 return focusBoardColor_; 52 } GetBorderFocusedColor()53 const Color& GetBorderFocusedColor() const 54 { 55 return borderFocusedColor_; 56 } GetFocusedBGColorUnselected()57 const Color& GetFocusedBGColorUnselected() const 58 { 59 return focusedBGColorUnselected_; 60 } GetWidth()61 const Dimension& GetWidth() const 62 { 63 return width_; 64 } GetHeight()65 const Dimension& GetHeight() const 66 { 67 return height_; 68 } GetHotZoneHorizontalPadding()69 const Dimension& GetHotZoneHorizontalPadding() const 70 { 71 return hotZoneHorizontalPadding_; 72 } GetHotZoneVerticalPadding()73 const Dimension& GetHotZoneVerticalPadding() const 74 { 75 return hotZoneVerticalPadding_; 76 } GetAspectRatio()77 double GetAspectRatio() const 78 { 79 return aspectRatio_; 80 } GetDefaultWidth()81 const Dimension& GetDefaultWidth() const 82 { 83 return defaultWidth_; 84 } GetDefaultHeight()85 const Dimension& GetDefaultHeight() const 86 { 87 return defaultHeight_; 88 } GetRadioInnerSizeRatio()89 double GetRadioInnerSizeRatio() const 90 { 91 return radioInnerSizeRatio_; 92 } GetNeedFocus()93 bool GetNeedFocus() const 94 { 95 return needFocus_; 96 } IsBackgroundSolid()97 bool IsBackgroundSolid() const 98 { 99 return backgroundSolid_; 100 } GetBorderWidth()101 const Dimension& GetBorderWidth() const 102 { 103 return borderWidth_; 104 } 105 SetHoverColor(const Color & hoverColor)106 void SetHoverColor(const Color& hoverColor) 107 { 108 hoverColor_ = hoverColor; 109 } 110 GetHoverColor()111 const Color& GetHoverColor() const 112 { 113 return hoverColor_; 114 } 115 SetClickEffectColor(const Color & clickEffectColor)116 void SetClickEffectColor(const Color& clickEffectColor) 117 { 118 clickEffectColor_ = clickEffectColor; 119 } 120 GetClickEffectColor()121 const Color& GetClickEffectColor() const 122 { 123 return clickEffectColor_; 124 } 125 GetInactivePointColor()126 const Color& GetInactivePointColor() const 127 { 128 return inactivePointColor_; 129 } 130 GetShadowColor()131 const Color& GetShadowColor() const 132 { 133 return shadowColor_; 134 } 135 GetShadowWidth()136 const Dimension& GetShadowWidth() const 137 { 138 return shadowWidth_; 139 } 140 GetHoverRadius()141 const Dimension& GetHoverRadius() const 142 { 143 return hoverRadius_; 144 } 145 GetDefaultPaddingSize()146 const Dimension& GetDefaultPaddingSize() const 147 { 148 return defaultPaddingSize_; 149 } 150 GetFocusRadius()151 const Dimension& GetFocusRadius() const 152 { 153 return focusRadius_; 154 } 155 GetFocusPaintPadding()156 const Dimension& GetFocusPaintPadding() const 157 { 158 return focusPaintPadding_; 159 } 160 GetFocusBoardSize()161 const Dimension& GetFocusBoardSize() const 162 { 163 return focusBoardSize_; 164 } 165 GetRoundFocusBoardSize()166 const Dimension& GetRoundFocusBoardSize() const 167 { 168 return roundFocusBoardSize_; 169 } 170 GetDefaultRoundPaddingSize()171 const Dimension& GetDefaultRoundPaddingSize() const 172 { 173 return defaultRoundPaddingSize_; 174 } 175 GetHoverDuration()176 double GetHoverDuration() const 177 { 178 return hoverDuration_; 179 } 180 GetHoverToTouchDuration()181 double GetHoverToTouchDuration() const 182 { 183 return hoverToTouchDuration_; 184 } 185 GetTouchDuration()186 double GetTouchDuration() const 187 { 188 return touchDuration_; 189 } GetTickResourceId()190 const InternalResource::ResourceId& GetTickResourceId() const 191 { 192 return tickResourceId_; 193 } GetDotResourceId()194 const InternalResource::ResourceId& GetDotResourceId() const 195 { 196 return dotResourceId_; 197 } 198 GetFocusLineColor()199 const Color& GetFocusLineColor() const 200 { 201 return focusLineColor_; 202 } 203 GetFocusedBgColor()204 const Color& GetFocusedBgColor() const 205 { 206 return focusedBgColor_; 207 } 208 GetSizeFocusBg()209 const Dimension& GetSizeFocusBg() const 210 { 211 return sizeFocusBg_; 212 } 213 GetSizeHoverBg()214 const Dimension& GetSizeHoverBg() const 215 { 216 return sizeHoverBg_; 217 } 218 GetFocusedRingUnchecked()219 const Color& GetFocusedRingUnchecked() const 220 { 221 return focusedRingUnchecked_; 222 } 223 GetFocusedBgUnchecked()224 const Color& GetFocusedBgUnchecked() const 225 { 226 return focusedBgUnchecked_; 227 } 228 GetHotZoneHorizontalSize()229 const Dimension& GetHotZoneHorizontalSize() const 230 { 231 return hotZoneHorizontalSize_; 232 } 233 GetHotZoneVerticalSize()234 const Dimension& GetHotZoneVerticalSize() const 235 { 236 return hotZoneVerticalSize_; 237 } 238 239 protected: 240 CheckableTheme() = default; 241 242 Color pointColor_; 243 Color activeColor_; 244 Color inactiveColor_; 245 Color inactivePointColor_; 246 Color focusColor_; 247 Color hoverColor_; 248 Color clickEffectColor_; 249 Color shadowColor_; 250 Color focusBoardColor_; 251 Color borderFocusedColor_; 252 Color focusedBGColorUnselected_; 253 Color focusLineColor_; 254 Color focusedRingUnchecked_; 255 Color focusedBgUnchecked_; 256 Color focusedBgColor_; 257 Dimension sizeFocusBg_; 258 Dimension sizeHoverBg_; 259 Dimension width_; 260 Dimension height_; 261 Dimension hotZoneHorizontalPadding_; 262 Dimension hotZoneVerticalPadding_; 263 Dimension defaultWidth_; 264 Dimension defaultHeight_; 265 Dimension borderWidth_; 266 Dimension shadowWidth_; 267 Dimension hoverRadius_; 268 Dimension defaultPaddingSize_; 269 Dimension defaultRoundPaddingSize_; 270 Dimension focusRadius_; 271 Dimension focusPaintPadding_; 272 Dimension focusBoardSize_; 273 Dimension roundFocusBoardSize_; 274 double hoverDuration_ = 0.0f; 275 double hoverToTouchDuration_ = 0.0f; 276 double touchDuration_ = 0.0f; 277 double aspectRatio_ = 1.0; 278 double radioInnerSizeRatio_ = 0.5; 279 bool needFocus_ = true; 280 bool backgroundSolid_ = true; 281 const float ratio_ = 1.8f; 282 InternalResource::ResourceId tickResourceId_ = InternalResource::ResourceId::RADIO_TICK_SVG; 283 InternalResource::ResourceId dotResourceId_ = InternalResource::ResourceId::RADIO_DOT_SVG; 284 Dimension hotZoneHorizontalSize_; 285 Dimension hotZoneVerticalSize_; 286 }; 287 288 class CheckboxTheme : public CheckableTheme { 289 DECLARE_ACE_TYPE(CheckboxTheme, CheckableTheme); 290 291 public: 292 class Builder { 293 public: 294 Builder() = default; 295 ~Builder() = default; 296 Build(const RefPtr<ThemeConstants> & themeConstants)297 RefPtr<CheckboxTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 298 { 299 RefPtr<CheckboxTheme> theme = AceType::Claim(new CheckboxTheme()); 300 if (!themeConstants) { 301 LOGI("Build AppTheme error, themeConstants is null!"); 302 return theme; 303 } 304 ParsePattern(themeConstants, theme); 305 return theme; 306 } 307 308 protected: 309 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<CheckboxTheme>& theme) const; 310 311 void SetCheckboxSize(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<CheckboxTheme>& theme) const; 312 313 void SetCheckboxFocus(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<CheckboxTheme>& theme) const; 314 }; 315 GetHoverPaddingSize()316 const Dimension& GetHoverPaddingSize() const 317 { 318 return hoverPaddingSize_; 319 } 320 GetBorderRadius()321 const Dimension& GetBorderRadius() const 322 { 323 return borderRadius_; 324 } 325 GetWhiteBorderRadius()326 const Dimension& GetWhiteBorderRadius() const 327 { 328 return whiteBorderRadius_; 329 } 330 GetCheckStroke()331 const Dimension& GetCheckStroke() const 332 { 333 return checkStroke_; 334 } 335 GetColorAnimationDuration()336 double GetColorAnimationDuration() const 337 { 338 return colorAnimationDuration_; 339 } 340 IsCircleDial()341 bool IsCircleDial() const 342 { 343 return showCircleDial_; 344 } 345 346 private: 347 Dimension borderRadius_; 348 Dimension whiteBorderRadius_; 349 Dimension checkStroke_; 350 Dimension hoverPaddingSize_; 351 double colorAnimationDuration_ = 0.0; 352 bool showCircleDial_ = false; 353 }; 354 355 class SwitchTheme : public CheckableTheme { 356 DECLARE_ACE_TYPE(SwitchTheme, CheckableTheme); 357 358 public: 359 class Builder { 360 public: 361 Builder() = default; 362 ~Builder() = default; 363 Build(const RefPtr<ThemeConstants> & themeConstants)364 RefPtr<SwitchTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 365 { 366 RefPtr<SwitchTheme> theme = AceType::Claim(new SwitchTheme()); 367 if (!themeConstants) { 368 LOGE("Build AppTheme error, themeConstants is null!"); 369 return theme; 370 } 371 ParsePattern(themeConstants, theme); 372 ParseSubStylePattern(themeConstants, theme); 373 return theme; 374 } 375 376 protected: 377 void ParseSubStylePattern(const RefPtr<ThemeConstants>& themeConstants, 378 const RefPtr<SwitchTheme>& theme) const; 379 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<SwitchTheme>& theme) const; 380 }; 381 GetPointColorUnselectedFocus()382 const Color& GetPointColorUnselectedFocus() const 383 { 384 return focusedPointColorUnselected_; 385 } 386 GetPointColorUnchecked()387 const Color& GetPointColorUnchecked() const 388 { 389 return pointColorUnchecked_; 390 } 391 GetFocusBoardWidth()392 const Dimension& GetFocusBoardWidth() const 393 { 394 return focusBoardWidth_; 395 } 396 GetFocusBoardHeight()397 const Dimension& GetFocusBoardHeight() const 398 { 399 return focusBoardHeight_; 400 } 401 GetFocusBoardRadius()402 const Dimension& GetFocusBoardRadius() const 403 { 404 return focusBoardRadius_; 405 } 406 GetRatio()407 float GetRatio() const 408 { 409 return ratio_; 410 } 411 GetColorAnimationDuration()412 double GetColorAnimationDuration() const 413 { 414 return colorAnimationDuration_; 415 } 416 GetPointAnimationDuration()417 double GetPointAnimationDuration() const 418 { 419 return pointAnimationDuration_; 420 } 421 GetInteractiveHoverColor()422 const Color& GetInteractiveHoverColor() const 423 { 424 return interactiveHoverColor_; 425 } 426 GetInteractivePressedColor()427 const Color& GetInteractivePressedColor() const 428 { 429 return interactivePressedColor_; 430 } 431 GetSwitchUseDiffPointColor()432 bool GetSwitchUseDiffPointColor() const 433 { 434 return isUseDiffPointColor_; 435 } 436 GetSwitchFocuPadding()437 const Dimension& GetSwitchFocuPadding() const 438 { 439 return focusPadding_; 440 } 441 442 private: 443 double colorAnimationDuration_ = 0.0; 444 double pointAnimationDuration_ = 0.0; 445 Color interactiveHoverColor_; 446 Color interactivePressedColor_; 447 bool isUseDiffPointColor_ = false; 448 Color focusedPointColorUnselected_; 449 Color pointColorUnchecked_; 450 Dimension focusBoardWidth_; 451 Dimension focusBoardHeight_; 452 Dimension focusBoardRadius_; 453 Dimension focusPadding_; 454 }; 455 456 class RadioTheme : public CheckableTheme { 457 DECLARE_ACE_TYPE(RadioTheme, CheckableTheme); 458 459 public: 460 class Builder { 461 public: 462 Builder() = default; 463 ~Builder() = default; 464 Build(const RefPtr<ThemeConstants> & themeConstants)465 RefPtr<RadioTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 466 { 467 RefPtr<RadioTheme> theme = AceType::Claim(new RadioTheme()); 468 if (!themeConstants) { 469 return theme; 470 } 471 ParsePattern(themeConstants, theme); 472 ParseNewPattern(themeConstants, theme); 473 return theme; 474 } 475 476 protected: ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RadioTheme> & theme)477 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RadioTheme>& theme) const 478 { 479 RefPtr<ThemeStyle> radioPattern = themeConstants->GetPatternByName(THEME_PATTERN_RADIO); 480 if (!radioPattern) { 481 LOGW("find pattern of radio fail"); 482 return; 483 } 484 theme->width_ = radioPattern->GetAttr<Dimension>("radio_size", 0.0_vp); 485 theme->height_ = theme->width_; 486 theme->hotZoneHorizontalPadding_ = radioPattern->GetAttr<Dimension>("radio_hotzone_padding", 0.0_vp); 487 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_; 488 theme->defaultWidth_ = radioPattern->GetAttr<Dimension>("radio_default_size", 0.0_vp); 489 theme->defaultHeight_ = theme->defaultWidth_; 490 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) { 491 theme->width_ = radioPattern->GetAttr<Dimension>("radio_size_api_twelve", 24.0_vp); 492 theme->height_ = theme->width_; 493 theme->defaultPaddingSize_ = radioPattern->GetAttr<Dimension>("radio_default_padding_size", 2.0_vp); 494 theme->sizeHoverBg_ = radioPattern->GetAttr<Dimension>("radio_hover_bg_size", 2.0_vp); 495 theme->defaultWidth_ = radioPattern->GetAttr<Dimension>("radio_default_size_api_twelve", 24.0_vp); 496 theme->defaultHeight_ = theme->defaultWidth_; 497 theme->showCircleDial_ = static_cast<bool>(radioPattern->GetAttr<double>("radio_circle_dial", 0.0)); 498 } 499 theme->radioInnerSizeRatio_ = radioPattern->GetAttr<double>("radio_inner_size_ratio", 0.0); 500 theme->needFocus_ = static_cast<bool>(radioPattern->GetAttr<double>("radio_need_focus", 0.0)); 501 theme->backgroundSolid_ = 502 static_cast<bool>(radioPattern->GetAttr<double>("radio_inactive_background_solid", 0.0)); 503 theme->borderWidth_ = radioPattern->GetAttr<Dimension>("radio_border_width", 0.0_vp); 504 theme->shadowColor_ = radioPattern->GetAttr<Color>("radio_shadow_color", Color()); 505 theme->shadowWidth_ = radioPattern->GetAttr<Dimension>("radio_shadow_width", 0.0_vp); 506 theme->pointColor_ = radioPattern->GetAttr<Color>("fg_color_checked", Color::RED); 507 theme->activeColor_ = radioPattern->GetAttr<Color>("bg_color_checked", Color::RED); 508 theme->inactiveColor_ = radioPattern->GetAttr<Color>("bg_color_unchecked", Color::RED); 509 theme->inactivePointColor_ = radioPattern->GetAttr<Color>("fg_color_unchecked", Color::RED); 510 theme->focusColor_ = radioPattern->GetAttr<Color>("bg_focus_outline_color", Color::RED); 511 theme->hoverColor_ = radioPattern->GetAttr<Color>("hover_border_color", Color::RED); 512 theme->clickEffectColor_ = radioPattern->GetAttr<Color>("click_effect_color", Color::RED); 513 theme->focusPaintPadding_ = radioPattern->GetAttr<Dimension>("focus_paint_padding", 0.0_vp); 514 theme->hoverDuration_ = radioPattern->GetAttr<double>("hover_animation_duration", 0.0); 515 theme->hoverToTouchDuration_ = radioPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0); 516 theme->touchDuration_ = radioPattern->GetAttr<double>("touch_animation_duration", 0.0); 517 if (SystemProperties::GetDeviceType() != DeviceType::CAR) { 518 return; 519 } 520 theme->width_ = radioPattern->GetAttr<Dimension>(RADIO_WIDTH, 26.0_vp); 521 theme->height_ = theme->width_; 522 theme->hotZoneHorizontalPadding_ = radioPattern->GetAttr<Dimension>(RADIO_PADDING, 11.0_vp); 523 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_; 524 SetRadioSize(themeConstants, theme); 525 } 526 SetRadioSize(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RadioTheme> & theme)527 void SetRadioSize(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RadioTheme>& theme) const 528 { 529 RefPtr<ThemeStyle> radioPattern = themeConstants->GetPatternByName(THEME_PATTERN_RADIO); 530 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) { 531 theme->width_ = radioPattern->GetAttr<Dimension>("radio_size_api_twelve", 24.0_vp); 532 theme->height_ = theme->width_; 533 } 534 } ParseNewPattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RadioTheme> & theme)535 void ParseNewPattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RadioTheme>& theme) const 536 { 537 RefPtr<ThemeStyle> radioPattern = themeConstants->GetPatternByName(THEME_PATTERN_RADIO); 538 if (!radioPattern) { 539 LOGW("find pattern of radio fail"); 540 return; 541 } 542 theme->focusedRingUnchecked_ = radioPattern->GetAttr<Color>("focused_ring_unchecked", Color::TRANSPARENT); 543 theme->focusedBgUnchecked_ = radioPattern->GetAttr<Color>("focused_bg_unchecked", Color::TRANSPARENT); 544 theme->focusedBgColor_ = radioPattern->GetAttr<Color>("color_focused_bg", Color::RED); 545 theme->sizeFocusBg_ = radioPattern->GetAttr<Dimension>("size_focused_bg", 0.0_vp); 546 } 547 }; 548 IsCircleDial()549 bool IsCircleDial() const 550 { 551 return showCircleDial_; 552 } 553 554 private: 555 bool showCircleDial_ = false; 556 }; 557 558 } // namespace OHOS::Ace 559 560 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_CHECKABLE_THEME_H 561