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