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_DIALOG_DIALOG_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DIALOG_DIALOG_THEME_H 18 19 #include "base/utils/system_properties.h" 20 #include "core/components/common/properties/color.h" 21 #include "core/components/common/properties/edge.h" 22 #include "core/components/common/properties/radius.h" 23 #include "core/components/common/properties/text_style.h" 24 #include "core/components/theme/theme.h" 25 #include "core/components/theme/theme_constants.h" 26 #include "core/components/theme/theme_constants_defines.h" 27 28 namespace OHOS::Ace { 29 30 /** 31 * DialogTheme defines color and styles of DialogComponent. DialogTheme should be built 32 * using DialogTheme::Builder. 33 */ 34 class DialogTheme : public virtual Theme { 35 DECLARE_ACE_TYPE(DialogTheme, Theme); 36 37 public: 38 class Builder { 39 public: 40 Builder() = default; 41 ~Builder() = default; 42 Build(const RefPtr<ThemeConstants> & themeConstants)43 RefPtr<DialogTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 44 { 45 RefPtr<DialogTheme> theme = AceType::Claim(new DialogTheme()); 46 if (!themeConstants) { 47 return theme; 48 } 49 // init theme from global data 50 theme->backgroundColor_ = themeConstants->GetColor(THEME_DIALOG_BACKGROUND_COLOR); 51 theme->titleTextStyle_.SetTextColor(themeConstants->GetColor(THEME_DIALOG_TITLE_TEXT_COLOR)); 52 theme->titleTextStyle_.SetFontSize(themeConstants->GetDimension(THEME_DIALOG_TITLE_TEXT_FONTSIZE)); 53 theme->titleTextStyle_.SetFontWeight( 54 FontWeight(themeConstants->GetInt(THEME_DIALOG_TITLE_TEXT_FONTWEIGHT))); 55 theme->titleMinFontSize_ = themeConstants->GetDimension(THEME_DIALOG_TITLE_TEXT_FONTSIZE_MIN); 56 theme->contentMinFontSize_ = themeConstants->GetDimension(THEME_DIALOG_CONTENT_TEXT_FONTSIZE_MIN); 57 auto titleMaxLines = themeConstants->GetInt(THEME_DIALOG_TITLE_TEXT_MAX_LINES); 58 theme->titleMaxLines_ = titleMaxLines < 0 ? theme->titleMaxLines_ : static_cast<uint32_t>(titleMaxLines); 59 theme->contentTextStyle_.SetTextColor(themeConstants->GetColor(THEME_DIALOG_CONTENT_TEXT_COLOR)); 60 theme->contentTextStyle_.SetFontSize(themeConstants->GetDimension(THEME_DIALOG_CONTENT_TEXT_FONTSIZE)); 61 theme->defaultPadding_ = Edge(themeConstants->GetDimension(THEME_DIALOG_PADDING_LEFT), 62 themeConstants->GetDimension(THEME_DIALOG_PADDING_TOP), 63 themeConstants->GetDimension(THEME_DIALOG_PADDING_RIGHT), 64 themeConstants->GetDimension(THEME_DIALOG_PADDING_BOTTOM)); 65 theme->adjustPadding_ = Edge(themeConstants->GetDimension(THEME_DIALOG_PADDING_LEFT_ADJUST), 66 themeConstants->GetDimension(THEME_DIALOG_PADDING_TOP_ADJUST), 67 themeConstants->GetDimension(THEME_DIALOG_PADDING_RIGHT_ADJUST), 68 themeConstants->GetDimension(THEME_DIALOG_PADDING_BOTTOM_ADJUST)); 69 theme->titleDefaultPadding_ = Edge(themeConstants->GetDimension(THEME_DIALOG_PADDING_LEFT), 70 themeConstants->GetDimension(THEME_DIALOG_TITLE_PADDING_VERTICAL), 71 themeConstants->GetDimension(THEME_DIALOG_PADDING_RIGHT), 72 themeConstants->GetDimension(THEME_DIALOG_TITLE_PADDING_VERTICAL)); 73 theme->titleAdjustPadding_ = Edge(themeConstants->GetDimension(THEME_DIALOG_PADDING_LEFT_ADJUST), 74 themeConstants->GetDimension(THEME_DIALOG_TITLE_PADDING_VERTICAL), 75 themeConstants->GetDimension(THEME_DIALOG_PADDING_RIGHT_ADJUST), 76 themeConstants->GetDimension(THEME_DIALOG_PADDING_BOTTOM_ADJUST)); 77 theme->contentDefaultPadding_ = Edge(themeConstants->GetDimension(THEME_DIALOG_PADDING_LEFT), 78 themeConstants->GetDimension(THEME_DIALOG_PADDING_BOTTOM), 79 themeConstants->GetDimension(THEME_DIALOG_PADDING_RIGHT), 80 themeConstants->GetDimension(THEME_DIALOG_PADDING_BOTTOM)); 81 theme->contentAdjustPadding_ = Edge(themeConstants->GetDimension(THEME_DIALOG_PADDING_LEFT_ADJUST), 82 themeConstants->GetDimension(THEME_DIALOG_TITLE_PADDING_VERTICAL), 83 themeConstants->GetDimension(THEME_DIALOG_PADDING_RIGHT_ADJUST), 84 themeConstants->GetDimension(THEME_DIALOG_PADDING_BOTTOM_ADJUST)); 85 theme->actionsPadding_ = Edge(themeConstants->GetDimension(THEME_DIALOG_PADDING_ACTIONS_LEFT), 86 themeConstants->GetDimension(THEME_DIALOG_PADDING_ACTIONS_TOP), 87 themeConstants->GetDimension(THEME_DIALOG_PADDING_ACTIONS_RIGHT), 88 themeConstants->GetDimension(THEME_DIALOG_PADDING_ACTIONS_BOTTOM)); 89 theme->buttonPaddingLeft_ = 90 Edge(themeConstants->GetDimension(THEME_DIALOG_PADDING_MIN).Value(), 0.0, 0.0, 0.0, DimensionUnit::VP); 91 theme->buttonPaddingRight_ = 92 Edge(0.0, 0.0, themeConstants->GetDimension(THEME_DIALOG_PADDING_MIN).Value(), 0.0, DimensionUnit::VP); 93 theme->buttonPaddingCenter_ = Edge(0.0, themeConstants->GetDimension(THEME_DIALOG_PADDING_MID).Value(), 0.0, 94 themeConstants->GetDimension(THEME_DIALOG_PADDING_MID).Value(), DimensionUnit::VP); 95 theme->buttonSpacingHorizontal_ = themeConstants->GetDimension(THEME_DIALOG_BUTTON_SPACING_HORIZONTAL); 96 theme->buttonSpacingVertical_ = themeConstants->GetDimension(THEME_DIALOG_BUTTON_SPACING_VERTICAL); 97 theme->buttonBackgroundColor_ = themeConstants->GetColor(THEME_DIALOG_BUTTON_BG_COLOR); 98 theme->buttonClickedColor_ = themeConstants->GetColor(THEME_DIALOG_BUTTON_CLICKED_COLOR); 99 theme->frameStart_ = themeConstants->GetDouble(THEME_DIALOG_FRAME_START); 100 theme->frameEnd_ = themeConstants->GetDouble(THEME_DIALOG_FRAME_END); 101 theme->scaleStart_ = themeConstants->GetDouble(THEME_DIALOG_SCALE_START); 102 theme->scaleEnd_ = themeConstants->GetDouble(THEME_DIALOG_SCALE_END); 103 theme->opacityStart_ = themeConstants->GetDouble(THEME_DIALOG_OPACITY_START); 104 theme->opacityEnd_ = themeConstants->GetDouble(THEME_DIALOG_OPACITY_END); 105 theme->maskColorStart_ = themeConstants->GetColor(THEME_DIALOG_MASK_COLOR_START); 106 theme->maskColorEnd_ = themeConstants->GetColor(THEME_DIALOG_MASK_COLOR_END); 107 theme->animationDurationIn_ = themeConstants->GetInt(THEME_DIALOG_ANIMATION_DURATION_IN); 108 theme->animationDurationOut_ = themeConstants->GetInt(THEME_DIALOG_ANIMATION_DURATION_OUT); 109 theme->translateValue_ = Dimension(themeConstants->GetDouble(THEME_DIALOG_TRANSLATE), DimensionUnit::PX); 110 theme->dividerColor_ = themeConstants->GetColor(THEME_DIALOG_DIVIDER_COLOR); 111 theme->dividerWidth_ = themeConstants->GetDimension(THEME_DIALOG_DIVIDER_WIDTH); 112 theme->dividerHeight_ = themeConstants->GetDimension(THEME_DIALOG_DIVIDER_HEIGHT); 113 theme->dividerPadding_ = Edge(themeConstants->GetDimension(THEME_DIALOG_DIVIDER_PADDING_HORIZON), 114 themeConstants->GetDimension(THEME_DIALOG_DIVIDER_PADDING_VERTICAL), 115 themeConstants->GetDimension(THEME_DIALOG_DIVIDER_PADDING_HORIZON), 116 themeConstants->GetDimension(THEME_DIALOG_DIVIDER_PADDING_VERTICAL)); 117 theme->marginBottom_ = themeConstants->GetDimension(THEME_OHOS_DIMENS_DIALOG_BOTTOM); 118 theme->marginLeft_ = themeConstants->GetDimension(THEME_OHOS_DIMENS_DIALOG_START); 119 theme->marginRight_ = themeConstants->GetDimension(THEME_OHOS_DIMENS_DIALOG_END); 120 ParsePattern(themeConstants->GetThemeStyle(), theme); 121 return theme; 122 } 123 ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<DialogTheme> & theme)124 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<DialogTheme>& theme) const 125 { 126 if (!themeStyle) { 127 LOGI("progress theme style is null"); 128 return; 129 } 130 auto dialogPattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_DIALOG, nullptr); 131 if (!dialogPattern) { 132 return; 133 } 134 theme->backgroundColor_ = dialogPattern->GetAttr<Color>(PATTERN_BG_COLOR, Color(0xd9ffffff)); 135 theme->titleTextStyle_.SetTextColor(dialogPattern->GetAttr<Color>("title_text_color", Color::BLACK)); 136 theme->titleTextStyle_.SetFontSize(dialogPattern->GetAttr<Dimension>("title_text_font_size", 20.0_fp)); 137 theme->contentTextStyle_.SetTextColor(dialogPattern->GetAttr<Color>("content_text_color", Color::BLACK)); 138 theme->contentTextStyle_.SetFontSize(dialogPattern->GetAttr<Dimension>("content_text_font_size", 16.0_fp)); 139 theme->buttonBackgroundColor_ = dialogPattern->GetAttr<Color>("button_bg_color", Color::BLACK); 140 theme->radius_ = Radius(dialogPattern->GetAttr<Dimension>("radius", 24.0_vp)); 141 if (SystemProperties::GetDeviceType() != DeviceType::CAR) { 142 return; 143 } 144 auto defaultPadding = dialogPattern->GetAttr<Dimension>(DIALOG_CONTENT_TOP_PADDING, 24.0_vp); 145 auto titlePadding = dialogPattern->GetAttr<Dimension>(DIALOG_TITLE_TOP_PADDING, 0.0_vp); 146 auto actionsTopPadding = dialogPattern->GetAttr<Dimension>(DIALOG_ACTIONS_TOP_PADDING, 0.0_vp); 147 theme->titleAdjustPadding_ = Edge(defaultPadding, titlePadding, defaultPadding, titlePadding); 148 theme->titleDefaultPadding_ = Edge(defaultPadding, titlePadding, defaultPadding, titlePadding); 149 theme->defaultPadding_ = Edge(defaultPadding, defaultPadding, defaultPadding, defaultPadding); 150 theme->adjustPadding_ = Edge(defaultPadding, defaultPadding, defaultPadding, 0.0_vp); 151 theme->contentDefaultPadding_ = Edge(defaultPadding, 0.0_vp, defaultPadding, defaultPadding); 152 theme->contentAdjustPadding_ = Edge(defaultPadding, 0.0_vp, defaultPadding, 0.0_vp); 153 theme->actionsPadding_ = Edge(defaultPadding, actionsTopPadding, defaultPadding, actionsTopPadding); 154 theme->buttonHeight_ = dialogPattern->GetAttr<Dimension>(DIALOG_BUTTON_HEIGHT, 0.0_vp); 155 theme->titleMaxLines_ = static_cast<uint32_t>(dialogPattern->GetAttr<int32_t>(DIALOG_TITLE_MAX_LINES, 2)); 156 theme->buttonSpacingHorizontal_ = actionsTopPadding; 157 theme->commonButtonTextColor_ = 158 dialogPattern->GetAttr<Color>(DIALOG_COMMON_BUTTON_TEXT_COLOR, Color::WHITE); 159 theme->buttonMinTextSize_ = dialogPattern->GetAttr<Dimension>(DIALOG_MIN_BUTTON_TEXT_SIZE, 10.0_vp); 160 theme->minButtonWidth_ = dialogPattern->GetAttr<Dimension>(DIALOG_MIN_BUTTON_WIDTH, 104.0_vp); 161 theme->maxButtonWidth_ = dialogPattern->GetAttr<Dimension>(DIALOG_MAX_BUTTON_WIDTH, 260.0_vp); 162 theme->maskColorEnd_ = dialogPattern->GetAttr<Color>(DIALOG_MASK_COLOR_END, Color::WHITE); 163 // pattern config 164 theme->titleTextStyle_.SetFontSize( 165 dialogPattern->GetAttr<Dimension>("title_text_font_size", 20.0_vp)); 166 theme->titleMinFontSize_ = dialogPattern->GetAttr<Dimension>("title_text_font_size_min", 20.0_vp); 167 theme->commonButtonBgColor_ = dialogPattern->GetAttr<Color>("common_button_bg_color", Color::GRAY); 168 theme->emphasizeButtonBgColor_ = dialogPattern->GetAttr<Color>("first_button_bg_color", Color::BLACK); 169 theme->emphasizeButtonTextColor_ = 170 dialogPattern->GetAttr<Color>("first_button_text_color", Color::WHITE); 171 theme->buttonTextSize_ = dialogPattern->GetAttr<Dimension>("button_text_font_size", 16.0_vp); 172 theme->buttonClickedColor_ = dialogPattern->GetAttr<Color>("button_bg_color_clicked", Color::BLACK); 173 theme->contentTextStyle_.SetFontSize(themeStyle->GetAttr<Dimension>("content_text_font_size", 16.0_vp)); 174 theme->contentMinFontSize_ = themeStyle->GetAttr<Dimension>("content_text_font_size_min", 16.0_vp); 175 } 176 }; 177 178 ~DialogTheme() override = default; 179 GetRadius()180 const Radius& GetRadius() const 181 { 182 return radius_; 183 } 184 GetBackgroundColor()185 const Color& GetBackgroundColor() const 186 { 187 return backgroundColor_; 188 } 189 GetCommonButtonBgColor()190 const Color& GetCommonButtonBgColor() const 191 { 192 return commonButtonBgColor_; 193 } 194 GetEmphasizeButtonBgColor()195 const Color& GetEmphasizeButtonBgColor() const 196 { 197 return emphasizeButtonBgColor_; 198 } 199 GetTitleTextStyle()200 const TextStyle& GetTitleTextStyle() const 201 { 202 return titleTextStyle_; 203 } 204 GetTitleMinFontSize()205 const Dimension& GetTitleMinFontSize() const 206 { 207 return titleMinFontSize_; 208 } 209 GetContentMinFontSize()210 const Dimension& GetContentMinFontSize() const 211 { 212 return contentMinFontSize_; 213 } 214 GetTitleMaxLines()215 uint32_t GetTitleMaxLines() const 216 { 217 return titleMaxLines_; 218 } 219 GetContentTextStyle()220 const TextStyle& GetContentTextStyle() const 221 { 222 return contentTextStyle_; 223 } 224 GetDefaultPadding()225 const Edge& GetDefaultPadding() const 226 { 227 return defaultPadding_; 228 } 229 GetAdjustPadding()230 const Edge& GetAdjustPadding() const 231 { 232 return adjustPadding_; 233 } 234 GetTitleDefaultPadding()235 const Edge& GetTitleDefaultPadding() const 236 { 237 return titleDefaultPadding_; 238 } 239 GetTitleAdjustPadding()240 const Edge& GetTitleAdjustPadding() const 241 { 242 return titleAdjustPadding_; 243 } 244 GetContentDefaultPadding()245 const Edge& GetContentDefaultPadding() const 246 { 247 return contentDefaultPadding_; 248 } 249 GetContentAdjustPadding()250 const Edge& GetContentAdjustPadding() const 251 { 252 return contentAdjustPadding_; 253 } 254 GetActionsPadding()255 const Edge& GetActionsPadding() const 256 { 257 return actionsPadding_; 258 } 259 GetButtonPaddingLeft()260 const Edge& GetButtonPaddingLeft() const 261 { 262 return buttonPaddingLeft_; 263 } 264 GetButtonPaddingRight()265 const Edge& GetButtonPaddingRight() const 266 { 267 return buttonPaddingRight_; 268 } 269 GetButtonPaddingCenter()270 const Edge& GetButtonPaddingCenter() const 271 { 272 return buttonPaddingCenter_; 273 } 274 GetButtonSpacingHorizontal()275 const Dimension& GetButtonSpacingHorizontal() const 276 { 277 return buttonSpacingHorizontal_; 278 } 279 GetButtonSpacingVertical()280 const Dimension& GetButtonSpacingVertical() const 281 { 282 return buttonSpacingVertical_; 283 } 284 GetButtonBackgroundColor()285 const Color& GetButtonBackgroundColor() const 286 { 287 return buttonBackgroundColor_; 288 } 289 GetButtonClickedColor()290 const Color& GetButtonClickedColor() const 291 { 292 return buttonClickedColor_; 293 } 294 GetFrameStart()295 double GetFrameStart() const 296 { 297 return frameStart_; 298 } 299 GetFrameEnd()300 double GetFrameEnd() const 301 { 302 return frameEnd_; 303 } 304 GetScaleStart()305 double GetScaleStart() const 306 { 307 return scaleStart_; 308 } 309 GetScaleEnd()310 double GetScaleEnd() const 311 { 312 return scaleEnd_; 313 } 314 GetOpacityStart()315 double GetOpacityStart() const 316 { 317 return opacityStart_; 318 } 319 GetOpacityEnd()320 double GetOpacityEnd() const 321 { 322 return opacityEnd_; 323 } 324 GetTranslateValue()325 const Dimension& GetTranslateValue() const 326 { 327 return translateValue_; 328 } 329 GetMaskColorStart()330 const Color& GetMaskColorStart() const 331 { 332 return maskColorStart_; 333 } 334 GetMaskColorEnd()335 const Color& GetMaskColorEnd() const 336 { 337 return maskColorEnd_; 338 } 339 GetCommonButtonTextColor()340 const Color& GetCommonButtonTextColor() const 341 { 342 return commonButtonTextColor_; 343 } 344 GetEmphasizeButtonTextColor()345 const Color& GetEmphasizeButtonTextColor() const 346 { 347 return emphasizeButtonTextColor_; 348 } 349 GetOpacityAnimationDurIn()350 int32_t GetOpacityAnimationDurIn() const 351 { 352 return opacityAnimationDurIn_; 353 } 354 GetAnimationDurationIn()355 int32_t GetAnimationDurationIn() const 356 { 357 return animationDurationIn_; 358 } 359 GetAnimationDurationOut()360 int32_t GetAnimationDurationOut() const 361 { 362 return animationDurationOut_; 363 } 364 GetDividerColor()365 const Color& GetDividerColor() 366 { 367 return dividerColor_; 368 } 369 GetDividerWidth()370 const Dimension& GetDividerWidth() 371 { 372 return dividerWidth_; 373 } 374 GetDividerHeight()375 const Dimension& GetDividerHeight() 376 { 377 return dividerHeight_; 378 } 379 GetDividerPadding()380 const Edge& GetDividerPadding() 381 { 382 return dividerPadding_; 383 } 384 GetMarginBottom()385 const Dimension& GetMarginBottom() const 386 { 387 return marginBottom_; 388 } 389 GetMarginLeft()390 const Dimension& GetMarginLeft() const 391 { 392 return marginLeft_; 393 } 394 GetMarginRight()395 const Dimension& GetMarginRight() const 396 { 397 return marginRight_; 398 } 399 GetButtonHeight()400 const Dimension& GetButtonHeight() const 401 { 402 return buttonHeight_; 403 } 404 GetButtonTextSize()405 const Dimension& GetButtonTextSize() const 406 { 407 return buttonTextSize_; 408 } 409 GetMinButtonTextSize()410 const Dimension& GetMinButtonTextSize() const 411 { 412 return buttonMinTextSize_; 413 } 414 415 protected: 416 DialogTheme() = default; 417 418 private: 419 Radius radius_; 420 Color backgroundColor_; 421 TextStyle titleTextStyle_; 422 TextStyle contentTextStyle_; 423 Dimension titleMinFontSize_; 424 Dimension contentMinFontSize_; 425 uint32_t titleMaxLines_ = 1; 426 Edge defaultPadding_; 427 Edge adjustPadding_; 428 Edge titleDefaultPadding_; 429 Edge titleAdjustPadding_; 430 Edge contentDefaultPadding_; 431 Edge contentAdjustPadding_; 432 Edge actionsPadding_; 433 Edge buttonPaddingLeft_; 434 Edge buttonPaddingRight_; 435 Edge buttonPaddingCenter_; 436 Dimension buttonSpacingHorizontal_; 437 Dimension buttonSpacingVertical_; 438 Color buttonBackgroundColor_; 439 Color buttonClickedColor_; 440 Color emphasizeButtonTextColor_; 441 Dimension translateValue_; 442 double frameStart_ = 0.0; 443 double frameEnd_ = 1.0; 444 double scaleStart_ = 0.0; 445 double scaleEnd_ = 1.0; 446 double opacityStart_ = 0.0; 447 double opacityEnd_ = 1.0; 448 int32_t animationDurationIn_ = 250; 449 int32_t opacityAnimationDurIn_ = 150; 450 int32_t animationDurationOut_ = 250; 451 Color maskColorStart_; 452 Color maskColorEnd_; 453 Color dividerColor_; 454 Color commonButtonBgColor_; 455 Color commonButtonTextColor_; 456 Color emphasizeButtonBgColor_; 457 Dimension dividerWidth_; 458 Dimension dividerHeight_; 459 Edge dividerPadding_; 460 Dimension marginLeft_; 461 Dimension marginRight_; 462 Dimension marginBottom_; 463 Dimension buttonHeight_; 464 Dimension buttonTextSize_; 465 Dimension buttonMinTextSize_; 466 Dimension minButtonWidth_; 467 Dimension maxButtonWidth_; 468 }; 469 470 } // namespace OHOS::Ace 471 472 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DIALOG_DIALOG_THEME_H 473