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/components/theme/theme.h" 21 #include "core/components/theme/theme_constants.h" 22 #include "core/components/theme/theme_constants_defines.h" 23 24 namespace OHOS::Ace { 25 26 class CheckableTheme : public virtual Theme { 27 DECLARE_ACE_TYPE(CheckableTheme, Theme); 28 29 public: 30 ~CheckableTheme() override = default; 31 GetPointColor()32 const Color& GetPointColor() const 33 { 34 return pointColor_; 35 } GetActiveColor()36 const Color& GetActiveColor() const 37 { 38 return activeColor_; 39 } GetInactiveColor()40 const Color& GetInactiveColor() const 41 { 42 return inactiveColor_; 43 } GetFocusColor()44 const Color& GetFocusColor() const 45 { 46 return focusColor_; 47 } GetWidth()48 const Dimension& GetWidth() const 49 { 50 return width_; 51 } GetHeight()52 const Dimension& GetHeight() const 53 { 54 return height_; 55 } GetHotZoneHorizontalPadding()56 const Dimension& GetHotZoneHorizontalPadding() const 57 { 58 return hotZoneHorizontalPadding_; 59 } GetHotZoneVerticalPadding()60 const Dimension& GetHotZoneVerticalPadding() const 61 { 62 return hotZoneVerticalPadding_; 63 } GetAspectRatio()64 double GetAspectRatio() const 65 { 66 return aspectRatio_; 67 } GetDefaultWidth()68 const Dimension& GetDefaultWidth() const 69 { 70 return defaultWidth_; 71 } GetDefaultHeight()72 const Dimension& GetDefaultHeight() const 73 { 74 return defaultHeight_; 75 } GetRadioInnerSizeRatio()76 double GetRadioInnerSizeRatio() const 77 { 78 return radioInnerSizeRatio_; 79 } GetNeedFocus()80 bool GetNeedFocus() const 81 { 82 return needFocus_; 83 } IsBackgroundSolid()84 bool IsBackgroundSolid() const 85 { 86 return backgroundSolid_; 87 } GetBorderWidth()88 const Dimension& GetBorderWidth() const 89 { 90 return borderWidth_; 91 } 92 SetHoverColor(const Color & hoverColor)93 void SetHoverColor(const Color& hoverColor) 94 { 95 hoverColor_ = hoverColor; 96 } 97 GetHoverColor()98 const Color& GetHoverColor() const 99 { 100 return hoverColor_; 101 } 102 SetClickEffectColor(const Color & clickEffectColor)103 void SetClickEffectColor(const Color& clickEffectColor) 104 { 105 clickEffectColor_ = clickEffectColor; 106 } 107 GetClickEffectColor()108 const Color& GetClickEffectColor() const 109 { 110 return clickEffectColor_; 111 } 112 GetInactivePointColor()113 const Color& GetInactivePointColor() const 114 { 115 return inactivePointColor_; 116 } 117 GetShadowColor()118 const Color& GetShadowColor() const 119 { 120 return shadowColor_; 121 } 122 GetShadowWidth()123 const Dimension& GetShadowWidth() const 124 { 125 return shadowWidth_; 126 } 127 GetHoverRadius()128 const Dimension& GetHoverRadius() const 129 { 130 return hoverRadius_; 131 } 132 GetFocusRadius()133 const Dimension& GetFocusRadius() const 134 { 135 return focusRadius_; 136 } 137 GetFocusPaintPadding()138 const Dimension& GetFocusPaintPadding() const 139 { 140 return focusPaintPadding_; 141 } 142 GetHoverDuration()143 double GetHoverDuration() const 144 { 145 return hoverDuration_; 146 } 147 GetHoverToTouchDuration()148 double GetHoverToTouchDuration() const 149 { 150 return hoverToTouchDuration_; 151 } 152 GetTouchDuration()153 double GetTouchDuration() const 154 { 155 return touchDuration_; 156 } 157 158 protected: 159 CheckableTheme() = default; 160 161 Color pointColor_; 162 Color activeColor_; 163 Color inactiveColor_; 164 Color inactivePointColor_; 165 Color focusColor_; 166 Color hoverColor_; 167 Color clickEffectColor_; 168 Color shadowColor_; 169 Dimension width_; 170 Dimension height_; 171 Dimension hotZoneHorizontalPadding_; 172 Dimension hotZoneVerticalPadding_; 173 Dimension defaultWidth_; 174 Dimension defaultHeight_; 175 Dimension borderWidth_; 176 Dimension shadowWidth_; 177 Dimension hoverRadius_; 178 Dimension focusRadius_; 179 Dimension focusPaintPadding_; 180 double hoverDuration_ = 0.0f; 181 double hoverToTouchDuration_ = 0.0f; 182 double touchDuration_ = 0.0f; 183 double aspectRatio_ = 1.0; 184 double radioInnerSizeRatio_ = 0.5; 185 bool needFocus_ = true; 186 bool backgroundSolid_ = true; 187 const float ratio_ = 1.8f; 188 }; 189 190 class CheckboxTheme : public CheckableTheme { 191 DECLARE_ACE_TYPE(CheckboxTheme, CheckableTheme); 192 193 public: 194 class Builder { 195 public: 196 Builder() = default; 197 ~Builder() = default; 198 Build(const RefPtr<ThemeConstants> & themeConstants)199 RefPtr<CheckboxTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 200 { 201 RefPtr<CheckboxTheme> theme = AceType::Claim(new CheckboxTheme()); 202 if (!themeConstants) { 203 LOGI("Build AppTheme error, themeConstants is null!"); 204 return theme; 205 } 206 theme->width_ = themeConstants->GetDimension(THEME_CHECKBOX_SIZE); 207 theme->height_ = theme->width_; 208 theme->hotZoneHorizontalPadding_ = themeConstants->GetDimension(THEME_CHECKBOX_HOTZONE_PADDING); 209 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_; 210 theme->pointColor_ = themeConstants->GetColor(THEME_CHECKBOX_POINT_COLOR); 211 theme->activeColor_ = themeConstants->GetColor(THEME_CHECKBOX_ACTIVE_COLOR); 212 theme->inactiveColor_ = themeConstants->GetColor(THEME_CHECKBOX_INACTIVE_COLOR); 213 theme->focusColor_ = themeConstants->GetColor(THEME_CHECKBOX_FOCUS_COLOR); 214 theme->defaultWidth_ = themeConstants->GetDimension(THEME_CHECKBOX_DEFAULT_SIZE); 215 theme->defaultHeight_ = theme->defaultWidth_; 216 theme->needFocus_ = themeConstants->GetInt(THEME_CHECKBOX_NEED_FOCUS); 217 theme->backgroundSolid_ = themeConstants->GetInt(THEME_CHECKBOX_INACTIVE_BACKGROUND_SOLID); 218 theme->borderWidth_ = themeConstants->GetDimension(THEME_CHECKBOX_BORDER_WIDTH); 219 theme->borderRadius_ = themeConstants->GetDimension(THEME_CHECKBOX_BORDER_RADIUS); 220 theme->checkStroke_ = themeConstants->GetDimension(THEME_CHECKBOX_STROKE_WIDTH); 221 theme->hoverColor_ = themeConstants->GetColor(THEME_CHECKBOX_HOVER_COLOR); 222 theme->inactivePointColor_ = themeConstants->GetColor(THEME_CHECKBOX_INACTIVE_POINT_COLOR); 223 theme->hoverRadius_ = themeConstants->GetDimension(THEME_CHECKBOX_HOVER_RADIUS); 224 theme->shadowColor_ = themeConstants->GetColor(THEME_CHECKBOX_SHADOW_COLOR); 225 theme->shadowWidth_ = themeConstants->GetDimension(THEME_CHECKBOX_SHADOW_WIDTH); 226 ParsePattern(themeConstants->GetThemeStyle(), theme); 227 return theme; 228 } 229 230 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<CheckboxTheme> & theme)231 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<CheckboxTheme>& theme) const 232 { 233 if (!themeStyle) { 234 return; 235 } 236 auto checkboxPattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_CHECKBOX, nullptr); 237 if (!checkboxPattern) { 238 LOGE("Pattern of checkbox is null, please check!"); 239 return; 240 } 241 theme->pointColor_ = checkboxPattern->GetAttr<Color>("fg_color_checked", Color::RED); 242 theme->activeColor_ = checkboxPattern->GetAttr<Color>("bg_color_checked", Color::RED); 243 theme->inactiveColor_ = checkboxPattern->GetAttr<Color>("bg_border_color_unchecked", Color::RED); 244 theme->focusColor_ = checkboxPattern->GetAttr<Color>("focus_border_color", Color::RED); 245 theme->borderRadius_ = checkboxPattern->GetAttr<Dimension>("bg_border_radius", 0.0_vp); 246 theme->hoverColor_ = checkboxPattern->GetAttr<Color>("hover_border_color", Color::RED); 247 theme->clickEffectColor_ = checkboxPattern->GetAttr<Color>("click_effect_color", Color::RED); 248 theme->inactivePointColor_ = checkboxPattern->GetAttr<Color>("bg_color_unchecked", Color::RED); 249 theme->hoverRadius_ = checkboxPattern->GetAttr<Dimension>("hover_border_radius", 0.0_vp); 250 theme->focusRadius_ = checkboxPattern->GetAttr<Dimension>("focus_border_radius", 0.0_vp); 251 theme->focusPaintPadding_ = checkboxPattern->GetAttr<Dimension>("focus_paint_padding", 0.0_vp); 252 theme->hoverDuration_ = checkboxPattern->GetAttr<double>("hover_animation_duration", 0.0); 253 theme->hoverToTouchDuration_ = checkboxPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0); 254 theme->touchDuration_ = checkboxPattern->GetAttr<double>("touch_animation_duration", 0.0); 255 theme->colorAnimationDuration_ = checkboxPattern->GetAttr<double>("color_animation_duration", 0.0); 256 257 if (SystemProperties::GetDeviceType() != DeviceType::CAR) { 258 return; 259 } 260 // width/height/borderRadius not exist in theme 261 theme->width_ = checkboxPattern->GetAttr<Dimension>("width", 26.0_vp); 262 theme->height_ = theme->width_; 263 theme->borderRadius_ = checkboxPattern->GetAttr<Dimension>("hover_border_radius", 4.0_vp); 264 theme->hotZoneHorizontalPadding_ = 265 checkboxPattern->GetAttr<Dimension>("hotzone_padding_horizontal", 11.0_vp); 266 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_; 267 } 268 }; 269 GetBorderRadius()270 const Dimension& GetBorderRadius() const 271 { 272 return borderRadius_; 273 } 274 GetCheckStroke()275 const Dimension& GetCheckStroke() const 276 { 277 return checkStroke_; 278 } 279 GetColorAnimationDuration()280 double GetColorAnimationDuration() const 281 { 282 return colorAnimationDuration_; 283 } 284 285 private: 286 Dimension borderRadius_; 287 Dimension checkStroke_; 288 double colorAnimationDuration_ = 0.0; 289 }; 290 291 class SwitchTheme : public CheckableTheme { 292 DECLARE_ACE_TYPE(SwitchTheme, CheckableTheme); 293 294 public: 295 class Builder { 296 public: 297 Builder() = default; 298 ~Builder() = default; 299 Build(const RefPtr<ThemeConstants> & themeConstants)300 RefPtr<SwitchTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 301 { 302 RefPtr<SwitchTheme> theme = AceType::Claim(new SwitchTheme()); 303 if (!themeConstants) { 304 LOGE("Build AppTheme error, themeConstants is null!"); 305 return theme; 306 } 307 theme->width_ = themeConstants->GetDimension(THEME_SWITCH_WIDTH); 308 theme->height_ = themeConstants->GetDimension(THEME_SWITCH_HEIGHT); 309 theme->hotZoneHorizontalPadding_ = themeConstants->GetDimension(THEME_SWITCH_HOTZONE_HORIZONTAL_PADDING); 310 theme->hotZoneVerticalPadding_ = themeConstants->GetDimension(THEME_SWITCH_HOTZONE_VERTICAL_PADDING); 311 theme->aspectRatio_ = themeConstants->GetDouble(THEME_SWITCH_ASPECT_RATIO); 312 theme->backgroundSolid_ = themeConstants->GetInt(THEME_SWITCH_INACTIVE_BACKGROUND_SOLID); 313 theme->pointColor_ = themeConstants->GetColor(THEME_SWITCH_POINT_COLOR); 314 theme->activeColor_ = themeConstants->GetColor(THEME_SWITCH_ACTIVE_COLOR); 315 theme->inactiveColor_ = themeConstants->GetColor(THEME_SWITCH_INACTIVE_COLOR); 316 theme->focusColor_ = themeConstants->GetColor(THEME_SWITCH_FOCUS_COLOR); 317 theme->defaultWidth_ = themeConstants->GetDimension(THEME_SWITCH_DEFAULT_WIDTH); 318 theme->defaultHeight_ = themeConstants->GetDimension(THEME_SWITCH_DEFAULT_HEIGHT); 319 theme->needFocus_ = themeConstants->GetInt(THEME_SWITCH_NEED_FOCUS); 320 theme->borderWidth_ = themeConstants->GetDimension(THEME_SWITCH_BORDER_WIDTH); 321 theme->hoverColor_ = themeConstants->GetColor(THEME_SWITCH_HOVER_COLOR); 322 theme->hoverRadius_ = themeConstants->GetDimension(THEME_SWITCH_HOVER_RADIUS); 323 theme->inactivePointColor_ = themeConstants->GetColor(THEME_SWITCH_INACTIVE_POINT_COLOR); 324 theme->shadowColor_ = themeConstants->GetColor(THEME_SWITCH_SHADOW_COLOR); 325 theme->shadowWidth_ = themeConstants->GetDimension(THEME_SWITCH_SHADOW_WIDTH); 326 ParsePattern(themeConstants->GetThemeStyle(), theme); 327 return theme; 328 } 329 330 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<SwitchTheme> & theme)331 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<SwitchTheme>& theme) const 332 { 333 if (!themeStyle) { 334 return; 335 } 336 auto switchPattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_SWITCH, nullptr); 337 if (!switchPattern) { 338 LOGE("Pattern of switch is null, please check!"); 339 return; 340 } 341 theme->pointColor_ = switchPattern->GetAttr<Color>("fg_color_checked", Color::RED); 342 theme->activeColor_ = switchPattern->GetAttr<Color>("bg_color_checked", Color::RED); 343 theme->inactiveColor_ = switchPattern->GetAttr<Color>("bg_color_unchecked", Color::RED); 344 theme->focusColor_ = switchPattern->GetAttr<Color>("focus_border_color", Color::RED); 345 theme->hoverColor_ = switchPattern->GetAttr<Color>("hover_border_color", Color::RED); 346 theme->hoverRadius_ = switchPattern->GetAttr<Dimension>("hover_border_radius", 0.0_vp); 347 theme->inactivePointColor_ = switchPattern->GetAttr<Color>("fg_color_unchecked", Color::RED); 348 theme->clickEffectColor_ = switchPattern->GetAttr<Color>("click_effect_color", Color::RED); 349 theme->focusPaintPadding_ = switchPattern->GetAttr<Dimension>("focus_paint_padding", 0.0_vp); 350 theme->hoverDuration_ = switchPattern->GetAttr<double>("hover_animation_duration", 0.0); 351 theme->hoverToTouchDuration_ = switchPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0); 352 theme->touchDuration_ = switchPattern->GetAttr<double>("touch_animation_duration", 0.0); 353 theme->colorAnimationDuration_ = switchPattern->GetAttr<double>("color_animation_duration", 0.0); 354 355 if (SystemProperties::GetDeviceType() != DeviceType::CAR) { 356 return; 357 } 358 theme->width_ = switchPattern->GetAttr<Dimension>(SWITCH_WIDTH, 40.0_vp); 359 theme->height_ = switchPattern->GetAttr<Dimension>(SWITCH_HEIGHT, 26.0_vp); 360 theme->shadowWidth_ = switchPattern->GetAttr<Dimension>(SWITCH_SHADOW_WIDTH, 2.0_vp); 361 theme->hotZoneHorizontalPadding_ = switchPattern->GetAttr<Dimension>(SWITCH_HORIZONTAL_PADDING, 4.0_vp); 362 theme->hotZoneVerticalPadding_ = switchPattern->GetAttr<Dimension>(SWITCH_VERTICAL_PADDING, 13.0_vp); 363 } 364 }; 365 GetRatio()366 float GetRatio() const 367 { 368 return ratio_; 369 } 370 GetColorAnimationDuration()371 double GetColorAnimationDuration() const 372 { 373 return colorAnimationDuration_; 374 } 375 376 private: 377 double colorAnimationDuration_ = 0.0; 378 }; 379 380 class RadioTheme : public CheckableTheme { 381 DECLARE_ACE_TYPE(RadioTheme, CheckableTheme); 382 383 public: 384 class Builder { 385 public: 386 Builder() = default; 387 ~Builder() = default; 388 Build(const RefPtr<ThemeConstants> & themeConstants)389 RefPtr<RadioTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 390 { 391 RefPtr<RadioTheme> theme = AceType::Claim(new RadioTheme()); 392 if (!themeConstants) { 393 return theme; 394 } 395 theme->width_ = themeConstants->GetDimension(THEME_RADIO_SIZE); 396 theme->height_ = theme->width_; 397 theme->hotZoneHorizontalPadding_ = themeConstants->GetDimension(THEME_RADIO_HOTZONE_PADDING); 398 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_; 399 theme->pointColor_ = themeConstants->GetColor(THEME_RADIO_POINT_COLOR); 400 theme->activeColor_ = themeConstants->GetColor(THEME_RADIO_ACTIVE_COLOR); 401 theme->inactiveColor_ = themeConstants->GetColor(THEME_RADIO_INACTIVE_COLOR); 402 theme->focusColor_ = themeConstants->GetColor(THEME_RADIO_FOCUS_COLOR); 403 theme->defaultWidth_ = themeConstants->GetDimension(THEME_RADIO_DEFAULT_SIZE); 404 theme->defaultHeight_ = theme->defaultWidth_; 405 theme->radioInnerSizeRatio_ = themeConstants->GetDouble(THEME_RADIO_INNER_SIZE_RATIO); 406 theme->needFocus_ = themeConstants->GetInt(THEME_RADIO_NEED_FOCUS); 407 theme->backgroundSolid_ = themeConstants->GetInt(THEME_RADIO_INACTIVE_BACKGROUND_SOLID); 408 theme->borderWidth_ = themeConstants->GetDimension(THEME_RADIO_BORDER_WIDTH); 409 theme->hoverColor_ = themeConstants->GetColor(THEME_RADIO_HOVER_COLOR); 410 theme->inactivePointColor_ = themeConstants->GetColor(THEME_RADIO_INACTIVE_POINT_COLOR); 411 theme->shadowColor_ = themeConstants->GetColor(THEME_RADIO_SHADOW_COLOR); 412 theme->shadowWidth_ = themeConstants->GetDimension(THEME_RADIO_SHADOW_WIDTH); 413 ParsePattern(themeConstants->GetThemeStyle(), theme); 414 return theme; 415 } 416 417 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<RadioTheme> & theme)418 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<RadioTheme>& theme) const 419 { 420 if (!themeStyle) { 421 return; 422 } 423 auto radioPattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_RADIO, nullptr); 424 if (!radioPattern) { 425 LOGW("find pattern of radio fail"); 426 return; 427 } 428 theme->pointColor_ = radioPattern->GetAttr<Color>("fg_color_checked", Color::RED); 429 theme->activeColor_ = radioPattern->GetAttr<Color>("bg_color_checked", Color::RED); 430 theme->inactiveColor_ = radioPattern->GetAttr<Color>("bg_color_unchecked", Color::RED); 431 theme->inactivePointColor_ = radioPattern->GetAttr<Color>("fg_color_unchecked", Color::RED); 432 theme->focusColor_ = radioPattern->GetAttr<Color>("focus_border_color", Color::RED); 433 theme->hoverColor_ = radioPattern->GetAttr<Color>("hover_border_color", Color::RED); 434 theme->clickEffectColor_ = radioPattern->GetAttr<Color>("click_effect_color", Color::RED); 435 theme->focusPaintPadding_ = radioPattern->GetAttr<Dimension>("focus_paint_padding", 0.0_vp); 436 theme->hoverDuration_ = radioPattern->GetAttr<double>("hover_animation_duration", 0.0); 437 theme->hoverToTouchDuration_ = radioPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0); 438 theme->touchDuration_ = radioPattern->GetAttr<double>("touch_animation_duration", 0.0); 439 if (SystemProperties::GetDeviceType() != DeviceType::CAR) { 440 return; 441 } 442 theme->width_ = radioPattern->GetAttr<Dimension>(RADIO_WIDTH, 26.0_vp); 443 theme->height_ = theme->width_; 444 theme->hotZoneHorizontalPadding_ = radioPattern->GetAttr<Dimension>(RADIO_PADDING, 11.0_vp); 445 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_; 446 } 447 }; 448 }; 449 450 } // namespace OHOS::Ace 451 452 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_CHECKABLE_THEME_H 453