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_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 GetInactivePointColor()103 const Color& GetInactivePointColor() const 104 { 105 return inactivePointColor_; 106 } 107 GetShadowColor()108 const Color& GetShadowColor() const 109 { 110 return shadowColor_; 111 } 112 GetShadowWidth()113 const Dimension& GetShadowWidth() const 114 { 115 return shadowWidth_; 116 } 117 GetHoverRadius()118 const Dimension& GetHoverRadius() const 119 { 120 return hoverRadius_; 121 } 122 123 protected: 124 CheckableTheme() = default; 125 126 Color pointColor_; 127 Color activeColor_; 128 Color inactiveColor_; 129 Color inactivePointColor_; 130 Color focusColor_; 131 Color hoverColor_; 132 Color shadowColor_; 133 Dimension width_; 134 Dimension height_; 135 Dimension hotZoneHorizontalPadding_; 136 Dimension hotZoneVerticalPadding_; 137 Dimension defaultWidth_; 138 Dimension defaultHeight_; 139 Dimension borderWidth_; 140 Dimension shadowWidth_; 141 Dimension hoverRadius_; 142 double aspectRatio_ = 1.0; 143 double radioInnerSizeRatio_ = 0.5; 144 bool needFocus_ = true; 145 bool backgroundSolid_ = true; 146 }; 147 148 class CheckboxTheme : public CheckableTheme { 149 DECLARE_ACE_TYPE(CheckboxTheme, CheckableTheme); 150 151 public: 152 class Builder { 153 public: 154 Builder() = default; 155 ~Builder() = default; 156 Build(const RefPtr<ThemeConstants> & themeConstants)157 RefPtr<CheckboxTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 158 { 159 RefPtr<CheckboxTheme> theme = AceType::Claim(new CheckboxTheme()); 160 if (!themeConstants) { 161 LOGI("Build AppTheme error, themeConstants is null!"); 162 return theme; 163 } 164 theme->width_ = themeConstants->GetDimension(THEME_CHECKBOX_SIZE); 165 theme->height_ = theme->width_; 166 theme->hotZoneHorizontalPadding_ = themeConstants->GetDimension(THEME_CHECKBOX_HOTZONE_PADDING); 167 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_; 168 theme->pointColor_ = themeConstants->GetColor(THEME_CHECKBOX_POINT_COLOR); 169 theme->activeColor_ = themeConstants->GetColor(THEME_CHECKBOX_ACTIVE_COLOR); 170 theme->inactiveColor_ = themeConstants->GetColor(THEME_CHECKBOX_INACTIVE_COLOR); 171 theme->focusColor_ = themeConstants->GetColor(THEME_CHECKBOX_FOCUS_COLOR); 172 theme->defaultWidth_ = themeConstants->GetDimension(THEME_CHECKBOX_DEFAULT_SIZE); 173 theme->defaultHeight_ = theme->defaultWidth_; 174 theme->needFocus_ = themeConstants->GetInt(THEME_CHECKBOX_NEED_FOCUS); 175 theme->backgroundSolid_ = themeConstants->GetInt(THEME_CHECKBOX_INACTIVE_BACKGROUND_SOLID); 176 theme->borderWidth_ = themeConstants->GetDimension(THEME_CHECKBOX_BORDER_WIDTH); 177 theme->borderRadius_ = themeConstants->GetDimension(THEME_CHECKBOX_BORDER_RADIUS); 178 theme->checkStroke_ = themeConstants->GetDimension(THEME_CHECKBOX_STROKE_WIDTH); 179 theme->hoverColor_ = themeConstants->GetColor(THEME_CHECKBOX_HOVER_COLOR); 180 theme->inactivePointColor_ = themeConstants->GetColor(THEME_CHECKBOX_INACTIVE_POINT_COLOR); 181 theme->hoverRadius_ = themeConstants->GetDimension(THEME_CHECKBOX_HOVER_RADIUS); 182 theme->shadowColor_ = themeConstants->GetColor(THEME_CHECKBOX_SHADOW_COLOR); 183 theme->shadowWidth_ = themeConstants->GetDimension(THEME_CHECKBOX_SHADOW_WIDTH); 184 ParsePattern(themeConstants->GetThemeStyle(), theme); 185 return theme; 186 } 187 188 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<CheckboxTheme> & theme)189 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<CheckboxTheme>& theme) const 190 { 191 if (!themeStyle) { 192 return; 193 } 194 auto checkboxPattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_CHECKBOX, nullptr); 195 if (!checkboxPattern) { 196 LOGE("Pattern of checkbox is null, please check!"); 197 return; 198 } 199 theme->pointColor_ = checkboxPattern->GetAttr<Color>(FG_COLOR_CHECKED, Color::RED); 200 theme->activeColor_ = checkboxPattern->GetAttr<Color>(BG_COLOR_CHECKED, Color::RED); 201 theme->inactiveColor_ = checkboxPattern->GetAttr<Color>(BG_COLOR_UNCHECKED, Color::RED); 202 theme->focusColor_ = checkboxPattern->GetAttr<Color>(BG_COLOR_UNCHECKED, Color::RED); 203 theme->borderRadius_ = checkboxPattern->GetAttr<Dimension>(BORDER_RADIUS, 0.0_vp); 204 theme->hoverColor_ = checkboxPattern->GetAttr<Color>(HOVER_EFFECT_COLOR, Color::RED); 205 theme->inactivePointColor_ = checkboxPattern->GetAttr<Color>(FG_COLOR_UNCHECKED, Color::RED); 206 theme->hoverRadius_ = checkboxPattern->GetAttr<Dimension>(HOVER_EFFECT_RADIUS, 0.0_vp); 207 if (SystemProperties::GetDeviceType() != DeviceType::CAR) { 208 return; 209 } 210 theme->width_ = checkboxPattern->GetAttr<Dimension>(CHECKBOX_WIDTH, 26.0_vp); 211 theme->height_ = theme->width_; 212 theme->borderRadius_ = checkboxPattern->GetAttr<Dimension>(CHECKBOX_BORDER_RADIUS, 4.0_vp); 213 theme->hotZoneHorizontalPadding_ = 214 checkboxPattern->GetAttr<Dimension>(CHECKBOX_PADDING, 11.0_vp); 215 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_; 216 } 217 }; 218 GetBorderRadius()219 const Dimension& GetBorderRadius() const 220 { 221 return borderRadius_; 222 } 223 GetCheckStroke()224 const Dimension& GetCheckStroke() const 225 { 226 return checkStroke_; 227 } 228 229 private: 230 Dimension borderRadius_; 231 Dimension checkStroke_; 232 }; 233 234 class SwitchTheme : public CheckableTheme { 235 DECLARE_ACE_TYPE(SwitchTheme, CheckableTheme); 236 237 public: 238 class Builder { 239 public: 240 Builder() = default; 241 ~Builder() = default; 242 Build(const RefPtr<ThemeConstants> & themeConstants)243 RefPtr<SwitchTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 244 { 245 RefPtr<SwitchTheme> theme = AceType::Claim(new SwitchTheme()); 246 if (!themeConstants) { 247 LOGE("Build AppTheme error, themeConstants is null!"); 248 return theme; 249 } 250 theme->width_ = themeConstants->GetDimension(THEME_SWITCH_WIDTH); 251 theme->height_ = themeConstants->GetDimension(THEME_SWITCH_HEIGHT); 252 theme->hotZoneHorizontalPadding_ = themeConstants->GetDimension(THEME_SWITCH_HOTZONE_HORIZONTAL_PADDING); 253 theme->hotZoneVerticalPadding_ = themeConstants->GetDimension(THEME_SWITCH_HOTZONE_VERTICAL_PADDING); 254 theme->aspectRatio_ = themeConstants->GetDouble(THEME_SWITCH_ASPECT_RATIO); 255 theme->backgroundSolid_ = themeConstants->GetInt(THEME_SWITCH_INACTIVE_BACKGROUND_SOLID); 256 theme->pointColor_ = themeConstants->GetColor(THEME_SWITCH_POINT_COLOR); 257 theme->activeColor_ = themeConstants->GetColor(THEME_SWITCH_ACTIVE_COLOR); 258 theme->inactiveColor_ = themeConstants->GetColor(THEME_SWITCH_INACTIVE_COLOR); 259 theme->focusColor_ = themeConstants->GetColor(THEME_SWITCH_FOCUS_COLOR); 260 theme->defaultWidth_ = themeConstants->GetDimension(THEME_SWITCH_DEFAULT_WIDTH); 261 theme->defaultHeight_ = themeConstants->GetDimension(THEME_SWITCH_DEFAULT_HEIGHT); 262 theme->needFocus_ = themeConstants->GetInt(THEME_SWITCH_NEED_FOCUS); 263 theme->borderWidth_ = themeConstants->GetDimension(THEME_SWITCH_BORDER_WIDTH); 264 theme->hoverColor_ = themeConstants->GetColor(THEME_SWITCH_HOVER_COLOR); 265 theme->hoverRadius_ = themeConstants->GetDimension(THEME_SWITCH_HOVER_RADIUS); 266 theme->inactivePointColor_ = themeConstants->GetColor(THEME_SWITCH_INACTIVE_POINT_COLOR); 267 theme->shadowColor_ = themeConstants->GetColor(THEME_SWITCH_SHADOW_COLOR); 268 theme->shadowWidth_ = themeConstants->GetDimension(THEME_SWITCH_SHADOW_WIDTH); 269 ParsePattern(themeConstants->GetThemeStyle(), theme); 270 return theme; 271 } 272 273 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<SwitchTheme> & theme)274 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<SwitchTheme>& theme) const 275 { 276 if (!themeStyle) { 277 return; 278 } 279 auto switchPattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_SWITCH, nullptr); 280 if (!switchPattern) { 281 LOGE("Pattern of switch is null, please check!"); 282 return; 283 } 284 theme->pointColor_ = switchPattern->GetAttr<Color>(FG_COLOR_CHECKED, Color::RED); 285 theme->activeColor_ = switchPattern->GetAttr<Color>(BG_COLOR_CHECKED, Color::RED); 286 theme->inactiveColor_ = switchPattern->GetAttr<Color>(BG_COLOR_UNCHECKED, Color::RED); 287 theme->focusColor_ = switchPattern->GetAttr<Color>(BG_COLOR_UNCHECKED, Color::RED); 288 theme->hoverColor_ = switchPattern->GetAttr<Color>(HOVER_EFFECT_COLOR, Color::RED); 289 theme->hoverRadius_ = switchPattern->GetAttr<Dimension>(HOVER_EFFECT_RADIUS, 0.0_vp); 290 theme->inactivePointColor_ = switchPattern->GetAttr<Color>(FG_COLOR_UNCHECKED, Color::RED); 291 if (SystemProperties::GetDeviceType() != DeviceType::CAR) { 292 return; 293 } 294 theme->width_ = switchPattern->GetAttr<Dimension>(SWITCH_WIDTH, 40.0_vp); 295 theme->height_ = switchPattern->GetAttr<Dimension>(SWITCH_HEIGHT, 26.0_vp); 296 theme->shadowWidth_ = switchPattern->GetAttr<Dimension>(SWITCH_SHADOW_WIDTH, 2.0_vp); 297 theme->hotZoneHorizontalPadding_ = switchPattern->GetAttr<Dimension>(SWITCH_HORIZONTAL_PADDING, 4.0_vp); 298 theme->hotZoneVerticalPadding_ = switchPattern->GetAttr<Dimension>(SWITCH_VERTICAL_PADDING, 13.0_vp); 299 } 300 }; 301 }; 302 303 class RadioTheme : public CheckableTheme { 304 DECLARE_ACE_TYPE(RadioTheme, CheckableTheme); 305 306 public: 307 class Builder { 308 public: 309 Builder() = default; 310 ~Builder() = default; 311 Build(const RefPtr<ThemeConstants> & themeConstants)312 RefPtr<RadioTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 313 { 314 RefPtr<RadioTheme> theme = AceType::Claim(new RadioTheme()); 315 if (!themeConstants) { 316 return theme; 317 } 318 theme->width_ = themeConstants->GetDimension(THEME_RADIO_SIZE); 319 theme->height_ = theme->width_; 320 theme->hotZoneHorizontalPadding_ = themeConstants->GetDimension(THEME_RADIO_HOTZONE_PADDING); 321 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_; 322 theme->pointColor_ = themeConstants->GetColor(THEME_RADIO_POINT_COLOR); 323 theme->activeColor_ = themeConstants->GetColor(THEME_RADIO_ACTIVE_COLOR); 324 theme->inactiveColor_ = themeConstants->GetColor(THEME_RADIO_INACTIVE_COLOR); 325 theme->focusColor_ = themeConstants->GetColor(THEME_RADIO_FOCUS_COLOR); 326 theme->defaultWidth_ = themeConstants->GetDimension(THEME_RADIO_DEFAULT_SIZE); 327 theme->defaultHeight_ = theme->defaultWidth_; 328 theme->radioInnerSizeRatio_ = themeConstants->GetDouble(THEME_RADIO_INNER_SIZE_RATIO); 329 theme->needFocus_ = themeConstants->GetInt(THEME_RADIO_NEED_FOCUS); 330 theme->backgroundSolid_ = themeConstants->GetInt(THEME_RADIO_INACTIVE_BACKGROUND_SOLID); 331 theme->borderWidth_ = themeConstants->GetDimension(THEME_RADIO_BORDER_WIDTH); 332 theme->hoverColor_ = themeConstants->GetColor(THEME_RADIO_HOVER_COLOR); 333 theme->inactivePointColor_ = themeConstants->GetColor(THEME_RADIO_INACTIVE_POINT_COLOR); 334 theme->shadowColor_ = themeConstants->GetColor(THEME_RADIO_SHADOW_COLOR); 335 theme->shadowWidth_ = themeConstants->GetDimension(THEME_RADIO_SHADOW_WIDTH); 336 ParsePattern(themeConstants->GetThemeStyle(), theme); 337 return theme; 338 } 339 340 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<RadioTheme> & theme)341 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<RadioTheme>& theme) const 342 { 343 if (!themeStyle) { 344 return; 345 } 346 auto radioPattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_RADIO, nullptr); 347 if (!radioPattern) { 348 LOGE("Pattern of radio is null, please check!"); 349 return; 350 } 351 theme->pointColor_ = radioPattern->GetAttr<Color>(FG_COLOR_CHECKED, Color::RED); 352 theme->activeColor_ = radioPattern->GetAttr<Color>(BG_COLOR_CHECKED, Color::RED); 353 theme->inactiveColor_ = radioPattern->GetAttr<Color>(BG_COLOR_UNCHECKED, Color::RED); 354 theme->inactivePointColor_ = radioPattern->GetAttr<Color>(FG_COLOR_UNCHECKED, Color::RED); 355 theme->focusColor_ = radioPattern->GetAttr<Color>(BG_COLOR_UNCHECKED, Color::RED); 356 theme->hoverColor_ = radioPattern->GetAttr<Color>(HOVER_EFFECT_COLOR, Color::RED); 357 if (SystemProperties::GetDeviceType() != DeviceType::CAR) { 358 return; 359 } 360 theme->width_ = radioPattern->GetAttr<Dimension>(RADIO_WIDTH, 26.0_vp); 361 theme->height_ = theme->width_; 362 theme->hotZoneHorizontalPadding_ = radioPattern->GetAttr<Dimension>(RADIO_PADDING, 11.0_vp); 363 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_; 364 } 365 }; 366 }; 367 368 } // namespace OHOS::Ace 369 370 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_CHECKABLE_THEME_H 371