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 constexpr Color DEFAULT_UNCHECK_BORDER_COLOR = Color(0x4B181819); 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 } GetUnCheckBorderColor()238 const Color& GetUnCheckBorderColor() const 239 { 240 return uncheckBorderColor_; 241 } 242 protected: 243 CheckableTheme() = default; 244 245 Color pointColor_; 246 Color activeColor_; 247 Color inactiveColor_; 248 Color inactivePointColor_; 249 Color focusColor_; 250 Color hoverColor_; 251 Color clickEffectColor_; 252 Color shadowColor_; 253 Color focusBoardColor_; 254 Color borderFocusedColor_; 255 Color focusedBGColorUnselected_; 256 Color focusLineColor_; 257 Color focusedRingUnchecked_; 258 Color focusedBgUnchecked_; 259 Color focusedBgColor_; 260 Dimension sizeFocusBg_; 261 Dimension sizeHoverBg_; 262 Dimension width_; 263 Dimension height_; 264 Dimension hotZoneHorizontalPadding_; 265 Dimension hotZoneVerticalPadding_; 266 Dimension defaultWidth_; 267 Dimension defaultHeight_; 268 Dimension borderWidth_; 269 Dimension shadowWidth_; 270 Dimension hoverRadius_; 271 Dimension defaultPaddingSize_; 272 Dimension defaultRoundPaddingSize_; 273 Dimension focusRadius_; 274 Dimension focusPaintPadding_; 275 Dimension focusBoardSize_; 276 Dimension roundFocusBoardSize_; 277 double hoverDuration_ = 0.0f; 278 double hoverToTouchDuration_ = 0.0f; 279 double touchDuration_ = 0.0f; 280 double aspectRatio_ = 1.0; 281 double radioInnerSizeRatio_ = 0.5; 282 bool needFocus_ = true; 283 bool backgroundSolid_ = true; 284 const float ratio_ = 1.8f; 285 InternalResource::ResourceId tickResourceId_ = InternalResource::ResourceId::RADIO_TICK_SVG; 286 InternalResource::ResourceId dotResourceId_ = InternalResource::ResourceId::RADIO_DOT_SVG; 287 Dimension hotZoneHorizontalSize_; 288 Dimension hotZoneVerticalSize_; 289 Color uncheckBorderColor_; 290 }; 291 292 class CheckboxTheme : public CheckableTheme { 293 DECLARE_ACE_TYPE(CheckboxTheme, CheckableTheme); 294 295 public: 296 class Builder { 297 public: 298 Builder() = default; 299 ~Builder() = default; 300 Build(const RefPtr<ThemeConstants> & themeConstants)301 RefPtr<CheckboxTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 302 { 303 RefPtr<CheckboxTheme> theme = AceType::MakeRefPtr<CheckboxTheme>(); 304 if (!themeConstants) { 305 LOGI("Build AppTheme error, themeConstants is null!"); 306 return theme; 307 } 308 ParsePattern(themeConstants, theme); 309 return theme; 310 } 311 312 protected: 313 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<CheckboxTheme>& theme) const; 314 315 void SetCheckboxSize(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<CheckboxTheme>& theme) const; 316 317 void SetCheckboxFocus(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<CheckboxTheme>& theme) const; 318 }; 319 GetHoverPaddingSize()320 const Dimension& GetHoverPaddingSize() const 321 { 322 return hoverPaddingSize_; 323 } 324 GetBorderRadius()325 const Dimension& GetBorderRadius() const 326 { 327 return borderRadius_; 328 } 329 GetWhiteBorderRadius()330 const Dimension& GetWhiteBorderRadius() const 331 { 332 return whiteBorderRadius_; 333 } 334 GetCheckStroke()335 const Dimension& GetCheckStroke() const 336 { 337 return checkStroke_; 338 } 339 GetColorAnimationDuration()340 double GetColorAnimationDuration() const 341 { 342 return colorAnimationDuration_; 343 } 344 IsCircleDial()345 bool IsCircleDial() const 346 { 347 return showCircleDial_; 348 } 349 350 private: 351 Dimension borderRadius_; 352 Dimension whiteBorderRadius_; 353 Dimension checkStroke_; 354 Dimension hoverPaddingSize_; 355 double colorAnimationDuration_ = 0.0; 356 bool showCircleDial_ = false; 357 }; 358 359 class SwitchTheme : public CheckableTheme { 360 DECLARE_ACE_TYPE(SwitchTheme, CheckableTheme); 361 362 public: 363 class Builder { 364 public: 365 Builder() = default; 366 ~Builder() = default; 367 Build(const RefPtr<ThemeConstants> & themeConstants)368 RefPtr<SwitchTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 369 { 370 RefPtr<SwitchTheme> theme = AceType::MakeRefPtr<SwitchTheme>(); 371 if (!themeConstants) { 372 LOGE("Build AppTheme error, themeConstants is null!"); 373 return theme; 374 } 375 ParsePattern(themeConstants, theme); 376 ParseSubStylePattern(themeConstants, theme); 377 return theme; 378 } 379 380 protected: 381 void ParseSubStylePattern(const RefPtr<ThemeConstants>& themeConstants, 382 const RefPtr<SwitchTheme>& theme) const; 383 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<SwitchTheme>& theme) const; 384 }; 385 GetPointColorUnselectedFocus()386 const Color& GetPointColorUnselectedFocus() const 387 { 388 return focusedPointColorUnselected_; 389 } 390 GetPointColorUnchecked()391 const Color& GetPointColorUnchecked() const 392 { 393 return pointColorUnchecked_; 394 } 395 GetFocusBoardWidth()396 const Dimension& GetFocusBoardWidth() const 397 { 398 return focusBoardWidth_; 399 } 400 GetFocusBoardHeight()401 const Dimension& GetFocusBoardHeight() const 402 { 403 return focusBoardHeight_; 404 } 405 GetFocusBoardRadius()406 const Dimension& GetFocusBoardRadius() const 407 { 408 return focusBoardRadius_; 409 } 410 GetRatio()411 float GetRatio() const 412 { 413 return ratio_; 414 } 415 GetColorAnimationDuration()416 double GetColorAnimationDuration() const 417 { 418 return colorAnimationDuration_; 419 } 420 GetPointAnimationDuration()421 double GetPointAnimationDuration() const 422 { 423 return pointAnimationDuration_; 424 } 425 GetInteractiveHoverColor()426 const Color& GetInteractiveHoverColor() const 427 { 428 return interactiveHoverColor_; 429 } 430 GetInteractivePressedColor()431 const Color& GetInteractivePressedColor() const 432 { 433 return interactivePressedColor_; 434 } 435 GetSwitchUseDiffPointColor()436 bool GetSwitchUseDiffPointColor() const 437 { 438 return isUseDiffPointColor_; 439 } 440 GetSwitchFocuPadding()441 const Dimension& GetSwitchFocuPadding() const 442 { 443 return focusPadding_; 444 } 445 GetSwitchOnText()446 const std::string& GetSwitchOnText() const 447 { 448 return switchOnText_; 449 } 450 GetSwitchOffText()451 const std::string& GetSwitchOffText() const 452 { 453 return switchOffText_; 454 } 455 456 private: 457 double colorAnimationDuration_ = 0.0; 458 double pointAnimationDuration_ = 0.0; 459 Color interactiveHoverColor_; 460 Color interactivePressedColor_; 461 bool isUseDiffPointColor_ = false; 462 Color focusedPointColorUnselected_; 463 Color pointColorUnchecked_; 464 Dimension focusBoardWidth_; 465 Dimension focusBoardHeight_; 466 Dimension focusBoardRadius_; 467 Dimension focusPadding_; 468 std::string switchOnText_; 469 std::string switchOffText_; 470 }; 471 472 class RadioTheme : public CheckableTheme { 473 DECLARE_ACE_TYPE(RadioTheme, CheckableTheme); 474 475 public: 476 class Builder { 477 public: 478 Builder() = default; 479 ~Builder() = default; 480 Build(const RefPtr<ThemeConstants> & themeConstants)481 RefPtr<RadioTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 482 { 483 RefPtr<RadioTheme> theme = AceType::MakeRefPtr<RadioTheme>(); 484 if (!themeConstants) { 485 return theme; 486 } 487 ParsePattern(themeConstants, theme); 488 ParseNewPattern(themeConstants, theme); 489 return theme; 490 } 491 492 protected: ParseUncheckBorderColor(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RadioTheme> & theme)493 void ParseUncheckBorderColor( 494 const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RadioTheme>& theme) const 495 { 496 RefPtr<ThemeStyle> radioPattern = themeConstants->GetPatternByName(THEME_PATTERN_RADIO); 497 if (!radioPattern) { 498 LOGW("find pattern of radio fail"); 499 return; 500 } 501 502 theme->uncheckBorderColor_ = 503 radioPattern->GetAttr<Color>("radio_uncheck_border_color", DEFAULT_UNCHECK_BORDER_COLOR); 504 } 505 ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RadioTheme> & theme)506 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RadioTheme>& theme) const 507 { 508 RefPtr<ThemeStyle> radioPattern = themeConstants->GetPatternByName(THEME_PATTERN_RADIO); 509 if (!radioPattern) { 510 LOGW("find pattern of radio fail"); 511 return; 512 } 513 ParseUncheckBorderColor(themeConstants, theme); 514 theme->width_ = radioPattern->GetAttr<Dimension>("radio_size", 0.0_vp); 515 theme->height_ = theme->width_; 516 theme->hotZoneHorizontalPadding_ = radioPattern->GetAttr<Dimension>("radio_hotzone_padding", 0.0_vp); 517 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_; 518 theme->defaultWidth_ = radioPattern->GetAttr<Dimension>("radio_default_size", 0.0_vp); 519 theme->defaultHeight_ = theme->defaultWidth_; 520 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) { 521 theme->width_ = radioPattern->GetAttr<Dimension>("radio_size_api_twelve", 24.0_vp); 522 theme->height_ = theme->width_; 523 theme->defaultPaddingSize_ = radioPattern->GetAttr<Dimension>("radio_default_padding_size", 2.0_vp); 524 theme->sizeHoverBg_ = radioPattern->GetAttr<Dimension>("radio_hover_bg_size", 2.0_vp); 525 theme->defaultWidth_ = radioPattern->GetAttr<Dimension>("radio_default_size_api_twelve", 24.0_vp); 526 theme->defaultHeight_ = theme->defaultWidth_; 527 theme->showCircleDial_ = static_cast<bool>(radioPattern->GetAttr<double>("radio_circle_dial", 0.0)); 528 } 529 theme->radioInnerSizeRatio_ = radioPattern->GetAttr<double>("radio_inner_size_ratio", 0.0); 530 theme->needFocus_ = static_cast<bool>(radioPattern->GetAttr<double>("radio_need_focus", 0.0)); 531 theme->backgroundSolid_ = 532 static_cast<bool>(radioPattern->GetAttr<double>("radio_inactive_background_solid", 0.0)); 533 theme->borderWidth_ = radioPattern->GetAttr<Dimension>("radio_border_width", 0.0_vp); 534 theme->shadowColor_ = radioPattern->GetAttr<Color>("radio_shadow_color", Color()); 535 theme->shadowWidth_ = radioPattern->GetAttr<Dimension>("radio_shadow_width", 0.0_vp); 536 theme->pointColor_ = radioPattern->GetAttr<Color>("fg_color_checked", Color::RED); 537 theme->activeColor_ = radioPattern->GetAttr<Color>("bg_color_checked", Color::RED); 538 theme->inactiveColor_ = radioPattern->GetAttr<Color>("bg_color_unchecked", Color::RED); 539 theme->inactivePointColor_ = radioPattern->GetAttr<Color>("fg_color_unchecked", Color::RED); 540 theme->focusColor_ = radioPattern->GetAttr<Color>("bg_focus_outline_color", Color::RED); 541 theme->hoverColor_ = radioPattern->GetAttr<Color>("hover_border_color", Color::RED); 542 theme->clickEffectColor_ = radioPattern->GetAttr<Color>("click_effect_color", Color::RED); 543 theme->focusPaintPadding_ = radioPattern->GetAttr<Dimension>("focus_paint_padding", 0.0_vp); 544 theme->hoverDuration_ = radioPattern->GetAttr<double>("hover_animation_duration", 0.0); 545 theme->hoverToTouchDuration_ = radioPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0); 546 theme->touchDuration_ = radioPattern->GetAttr<double>("touch_animation_duration", 0.0); 547 } 548 SetRadioSize(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RadioTheme> & theme)549 void SetRadioSize(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RadioTheme>& theme) const 550 { 551 RefPtr<ThemeStyle> radioPattern = themeConstants->GetPatternByName(THEME_PATTERN_RADIO); 552 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) { 553 theme->width_ = radioPattern->GetAttr<Dimension>("radio_size_api_twelve", 24.0_vp); 554 theme->height_ = theme->width_; 555 } 556 } ParseNewPattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RadioTheme> & theme)557 void ParseNewPattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RadioTheme>& theme) const 558 { 559 RefPtr<ThemeStyle> radioPattern = themeConstants->GetPatternByName(THEME_PATTERN_RADIO); 560 if (!radioPattern) { 561 LOGW("find pattern of radio fail"); 562 return; 563 } 564 theme->focusedRingUnchecked_ = radioPattern->GetAttr<Color>("focused_ring_unchecked", Color::TRANSPARENT); 565 theme->focusedBgUnchecked_ = radioPattern->GetAttr<Color>("focused_bg_unchecked", Color::TRANSPARENT); 566 theme->focusedBgColor_ = radioPattern->GetAttr<Color>("color_focused_bg", Color::RED); 567 theme->sizeFocusBg_ = radioPattern->GetAttr<Dimension>("size_focused_bg", 0.0_vp); 568 } 569 }; 570 IsCircleDial()571 bool IsCircleDial() const 572 { 573 return showCircleDial_; 574 } 575 576 private: 577 bool showCircleDial_ = false; 578 }; 579 580 } // namespace OHOS::Ace 581 582 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_CHECKABLE_THEME_H 583