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_SELECT_SELECT_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SELECT_SELECT_THEME_H 18 19 #include "base/geometry/dimension.h" 20 #include "core/components/common/layout/constants.h" 21 #include "core/components/common/properties/color.h" 22 #include "core/components/common/properties/text_style.h" 23 #include "core/components/theme/theme.h" 24 #include "core/components/theme/theme_constants.h" 25 #include "core/components/theme/theme_constants_defines.h" 26 27 namespace OHOS::Ace { 28 29 /** 30 * SelectTheme defines color and styles of SelectComponent. SelectTheme should be build 31 * using SelectTheme::Builder. 32 */ 33 class SelectTheme final : public virtual Theme { 34 DECLARE_ACE_TYPE(SelectTheme, Theme); 35 36 public: 37 class Builder final { 38 public: 39 Builder() = default; 40 ~Builder() = default; 41 Build(const RefPtr<ThemeConstants> & themeConstants)42 RefPtr<SelectTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 43 { 44 RefPtr<SelectTheme> theme = AceType::Claim(new SelectTheme()); 45 if (!themeConstants) { 46 return theme; 47 } 48 theme->disabledColor_ = themeConstants->GetColor(THEME_SELECT_DISABLED_COLOR); 49 theme->clickedColor_ = themeConstants->GetColor(THEME_SELECT_CLICKED_COLOR); 50 theme->selectedColor_ = themeConstants->GetColor(THEME_SELECT_SELECTED_COLOR); 51 theme->fontFamily_ = "sans-serif"; 52 theme->fontSize_ = themeConstants->GetDimension(THEME_SELECT_FONT_SIZE); 53 theme->fontColor_ = themeConstants->GetColor(THEME_SELECT_FONT_COLOR); 54 theme->fontWeight_ = FontWeight::NORMAL; 55 theme->textDecoration_ = TextDecoration::NONE; 56 auto optionSize = themeConstants->GetInt(THEME_SELECT_OPTION_SHOW_COUNT); 57 theme->optionSize_ = optionSize < 0 ? theme->optionSize_ : static_cast<size_t>(optionSize); 58 theme->rrectSize_ = themeConstants->GetDimension(THEME_SELECT_ITSELF_RRECT_SIZE); 59 theme->popupBorderWidth_ = themeConstants->GetDimension(THEME_SELECT_POPUP_BORDER_WIDTH); 60 theme->popupShadowWidth_ = themeConstants->GetDimension(THEME_SELECT_POPUP_SHADOW_WIDTH); 61 theme->popupRRectSize_ = themeConstants->GetDimension(THEME_SELECT_POPUP_RRECT_SIZE); 62 theme->popupMinWidth_ = themeConstants->GetDimension(THEME_SELECT_POPUP_MIN_WIDTH); 63 theme->normalPadding_ = themeConstants->GetDimension(THEME_SELECT_NORMAL_PADDING); 64 theme->iconSize_ = themeConstants->GetDimension(THEME_SELECT_ITSELF_ICON_SIZE); 65 theme->isTV_ = themeConstants->GetInt(THEME_SELECT_IS_TV); 66 theme->horizontalSpacing_ = themeConstants->GetDimension(THEME_SELECT_POPUP_SPACING_HORIZONTAL); 67 theme->verticalSpacing_ = themeConstants->GetDimension(THEME_SELECT_POPUP_SPACING_VERTICAL); 68 theme->contentSpacing_ = themeConstants->GetDimension(THEME_SELECT_POPUP_SPACING_CONTENT); 69 70 theme->selectShowTime_ = 250; // unit is ms. 71 theme->selectHideTime_ = 250; // unit is ms. 72 theme->menuShowTime_ = 250; // unit is ms. 73 theme->menuHideTime_ = 250; // unit is ms. 74 theme->titleLeftPadding_ = Dimension(16.0, DimensionUnit::VP); 75 theme->titleTopPadding_ = Dimension(8.0, DimensionUnit::VP); 76 theme->titleRightPadding_ = Dimension(8.0, DimensionUnit::VP); 77 theme->titleBottomPadding_ = Dimension(16.0, DimensionUnit::VP); 78 theme->titleStyle_.SetFontSize(themeConstants->GetDimension(THEME_OHOS_TEXT_SIZE_HEADLINE7)); 79 std::vector<std::string> families; 80 families.emplace_back("sans-serif"); 81 theme->titleStyle_.SetFontFamilies(families); 82 theme->titleStyle_.SetFontWeight(FontWeight::W500); 83 theme->titleStyle_.SetTextColor(themeConstants->GetColor(THEME_OHOS_COLOR_TEXT_PRIMARY)); 84 theme->titleStyle_.SetTextDecoration(TextDecoration::NONE); 85 // this is the const padding needed by emui 86 theme->optionPadding_ = Edge(16.0, 15.0, 16.0, 15.0, DimensionUnit::VP); 87 theme->optionInterval_ = theme->isTV_ ? Dimension(6.0, DimensionUnit::VP) : 0.0_vp; 88 theme->tvFocusTextColor_ = Color(0xE6000000); 89 theme->tvNormalBackColor_ = Color(0x33FFFFFF); 90 theme->tvBackColor_ = (theme->isTV_ ? Color(0x99000000) : Color::TRANSPARENT); 91 // disabled color 92 theme->normalDisableColor_ = themeConstants->GetColor(THEME_SELECT_OPTION_DISABLE_COLOR); 93 theme->focusedDisableColor_ = themeConstants->GetColor(THEME_SELECT_OPTION_FOCUSED_DISABLE_COLOR); 94 theme->normalTextDisableColor_ = themeConstants->GetColor(THEME_SELECT_OPTION_DISABLE_TEXT_COLOR); 95 theme->focusedTextDisableColor_ = themeConstants->GetColor(THEME_SELECT_OPTION_FOCUSED_DISABLE_TEXT_COLOR); 96 Parse(themeConstants->GetThemeStyle(), theme); 97 return theme; 98 } 99 Parse(const RefPtr<ThemeStyle> & style,const RefPtr<SelectTheme> & theme)100 void Parse(const RefPtr<ThemeStyle>& style, const RefPtr<SelectTheme>& theme) const 101 { 102 if (!style || !theme) { 103 return; 104 } 105 theme->fontColor_ = style->GetAttr<Color>(THEME_ATTR_COLOR_SPINNER_ICON, theme->fontColor_); 106 theme->clickedColor_ = style->GetAttr<Color>(THEME_ATTR_COLOR_CLICK_EFFECT, theme->clickedColor_); 107 theme->selectedColor_ = style->GetAttr<Color>(THEME_ATTR_COLOR_CONTROL_ACTIVE, theme->selectedColor_); 108 theme->selectedColorText_ = style->GetAttr<Color>( 109 THEME_ATTR_COLOR_TEXT_PRIMARY_ACTIVATED, theme->selectedColorText_); 110 theme->hoverColor_ = style->GetAttr<Color>(THEME_ATTR_COLOR_HOVER, theme->hoverColor_); 111 theme->backgroundColor_ = style->GetAttr<Color>(THEME_ATTR_COLOR_DIALOG_BG, theme->backgroundColor_); 112 theme->lineColor_ = style->GetAttr<Color>(THEME_ATTR_COLOR_LIST_DIVIDER, theme->lineColor_); 113 } 114 }; 115 116 ~SelectTheme() override = default; 117 clone()118 RefPtr<SelectTheme> clone() 119 { 120 RefPtr<SelectTheme> theme = AceType::Claim(new SelectTheme()); 121 theme->disabledColor_ = disabledColor_; 122 theme->clickedColor_ = clickedColor_; 123 theme->selectedColor_ = selectedColor_; 124 theme->fontSize_ = fontSize_; 125 theme->fontFamily_ = fontFamily_; 126 theme->fontColor_ = fontColor_; 127 theme->fontWeight_ = fontWeight_; 128 theme->textDecoration_ = textDecoration_; 129 theme->rrectSize_ = rrectSize_; 130 theme->iconSize_ = iconSize_; 131 theme->normalPadding_ = normalPadding_; 132 theme->optionSize_ = optionSize_; 133 theme->popupRRectSize_ = popupRRectSize_; 134 theme->popupMinWidth_ = popupMinWidth_; 135 theme->popupShadowWidth_ = popupShadowWidth_; 136 theme->popupBorderWidth_ = popupBorderWidth_; 137 theme->titleLeftPadding_ = titleLeftPadding_; 138 theme->titleTopPadding_ = titleTopPadding_; 139 theme->titleRightPadding_ = titleRightPadding_; 140 theme->titleBottomPadding_ = titleBottomPadding_; 141 theme->titleStyle_ = titleStyle_; 142 theme->isTV_ = isTV_; 143 theme->horizontalSpacing_ = horizontalSpacing_; 144 theme->verticalSpacing_ = verticalSpacing_; 145 theme->contentSpacing_ = contentSpacing_; 146 theme->menuHideTime_ = menuHideTime_; 147 theme->menuShowTime_ = menuShowTime_; 148 theme->selectShowTime_ = selectShowTime_; 149 theme->selectHideTime_ = selectHideTime_; 150 theme->optionPadding_ = optionPadding_; 151 theme->optionInterval_ = optionInterval_; 152 theme->tvFocusTextColor_ = tvFocusTextColor_; 153 theme->tvNormalBackColor_ = tvNormalBackColor_; 154 theme->tvBackColor_ = tvBackColor_; 155 theme->focusedDisableColor_ = focusedDisableColor_; 156 theme->normalDisableColor_ = normalDisableColor_; 157 theme->focusedTextDisableColor_ = focusedTextDisableColor_; 158 theme->normalTextDisableColor_ = normalTextDisableColor_; 159 theme->backgroundColor_ = backgroundColor_; 160 theme->hoverColor_ = hoverColor_; 161 theme->selectedColorText_ = selectedColorText_; 162 theme->lineColor_ = lineColor_; 163 return theme; 164 } 165 GetSelectedColorText()166 const Color& GetSelectedColorText() const 167 { 168 return selectedColorText_; 169 } 170 GetHoverColor()171 const Color& GetHoverColor() const 172 { 173 return hoverColor_; 174 } 175 GetBackgroundColor()176 const Color& GetBackgroundColor() const 177 { 178 return backgroundColor_; 179 } 180 GetDisabledColor()181 const Color& GetDisabledColor() const 182 { 183 return disabledColor_; 184 } SetDisabledColor(const Color & value)185 void SetDisabledColor(const Color& value) 186 { 187 disabledColor_ = value; 188 } 189 GetClickedColor()190 const Color& GetClickedColor() const 191 { 192 return clickedColor_; 193 } SetClickedColor(const Color & value)194 void SetClickedColor(const Color& value) 195 { 196 clickedColor_ = value; 197 } 198 GetSelectedColor()199 const Color& GetSelectedColor() const 200 { 201 return selectedColor_; 202 } SetSelectedColor(const Color & value)203 void SetSelectedColor(const Color& value) 204 { 205 selectedColor_ = value; 206 } 207 GetFontSize()208 const Dimension& GetFontSize() const 209 { 210 return fontSize_; 211 } SetFontSize(const Dimension & value)212 void SetFontSize(const Dimension& value) 213 { 214 fontSize_ = value; 215 } 216 GetFontColor()217 const Color& GetFontColor() const 218 { 219 return fontColor_; 220 } SetFontColor(const Color & value)221 void SetFontColor(const Color& value) 222 { 223 fontColor_ = value; 224 } 225 GetFontFamily()226 const std::string& GetFontFamily() const 227 { 228 return fontFamily_; 229 } SetFontFamily(const std::string & value)230 void SetFontFamily(const std::string& value) 231 { 232 fontFamily_ = value; 233 } 234 GetFontWeight()235 FontWeight GetFontWeight() const 236 { 237 return fontWeight_; 238 } SetFontWeight(FontWeight value)239 void SetFontWeight(FontWeight value) 240 { 241 fontWeight_ = value; 242 } 243 GetTextDecoration()244 TextDecoration GetTextDecoration() const 245 { 246 return textDecoration_; 247 } SetTextDecoration(TextDecoration value)248 void SetTextDecoration(TextDecoration value) 249 { 250 textDecoration_ = value; 251 } 252 GetOptionSize()253 std::size_t GetOptionSize() const 254 { 255 return optionSize_; 256 } SetOptionSize(std::size_t value)257 void SetOptionSize(std::size_t value) 258 { 259 optionSize_ = value; 260 } 261 GetRRectSize()262 const Dimension& GetRRectSize() const 263 { 264 return rrectSize_; 265 } SetRRectSize(const Dimension & value)266 void SetRRectSize(const Dimension& value) 267 { 268 rrectSize_ = value; 269 } 270 GetPopupRRectSize()271 const Dimension& GetPopupRRectSize() const 272 { 273 return popupRRectSize_; 274 } SetPopupRRectSize(const Dimension & value)275 void SetPopupRRectSize(const Dimension& value) 276 { 277 popupRRectSize_ = value; 278 } 279 GetPopupBorderWidth()280 const Dimension& GetPopupBorderWidth() const 281 { 282 return popupBorderWidth_; 283 } SetPopupBorderWidth(const Dimension & value)284 void SetPopupBorderWidth(const Dimension& value) 285 { 286 popupBorderWidth_ = value; 287 } 288 GetPopupShadowWidth()289 const Dimension& GetPopupShadowWidth() const 290 { 291 return popupShadowWidth_; 292 } SetPopupShadowWidth(const Dimension & value)293 void SetPopupShadowWidth(const Dimension& value) 294 { 295 popupShadowWidth_ = value; 296 } 297 GetPopupMinWidth()298 const Dimension& GetPopupMinWidth() const 299 { 300 return popupMinWidth_; 301 } SetPopupMinWidth(const Dimension & value)302 void SetPopupMinWidth(const Dimension& value) 303 { 304 popupMinWidth_ = value; 305 } 306 GetNormalPadding()307 const Dimension& GetNormalPadding() const 308 { 309 return normalPadding_; 310 } SetNormalPadding(const Dimension & value)311 void SetNormalPadding(const Dimension& value) 312 { 313 normalPadding_ = value; 314 } 315 GetIconSize()316 const Dimension& GetIconSize() const 317 { 318 return iconSize_; 319 } SetIconSize(const Dimension & value)320 void SetIconSize(const Dimension& value) 321 { 322 iconSize_ = value; 323 } 324 GetTitleLeftPadding()325 const Dimension& GetTitleLeftPadding() const 326 { 327 return titleLeftPadding_; 328 } SetTitleLeftPadding(const Dimension & value)329 void SetTitleLeftPadding(const Dimension& value) 330 { 331 titleLeftPadding_ = value; 332 } 333 GetTitleTopPadding()334 const Dimension& GetTitleTopPadding() const 335 { 336 return titleTopPadding_; 337 } SetTitleTopPadding(const Dimension & value)338 void SetTitleTopPadding(const Dimension& value) 339 { 340 titleTopPadding_ = value; 341 } 342 GetTitleRightPadding()343 const Dimension& GetTitleRightPadding() const 344 { 345 return titleRightPadding_; 346 } SetTitleRightPadding(const Dimension & value)347 void SetTitleRightPadding(const Dimension& value) 348 { 349 titleRightPadding_ = value; 350 } 351 GetTitleBottomPadding()352 const Dimension& GetTitleBottomPadding() const 353 { 354 return titleBottomPadding_; 355 } SetTitleBottomPadding(const Dimension & value)356 void SetTitleBottomPadding(const Dimension& value) 357 { 358 titleBottomPadding_ = value; 359 } 360 GetTitleStyle()361 const TextStyle& GetTitleStyle() 362 { 363 return titleStyle_; 364 } SetTitleStyle(const TextStyle & value)365 void SetTitleStyle(const TextStyle& value) 366 { 367 titleStyle_ = value; 368 } 369 IsTV()370 bool IsTV() const 371 { 372 return isTV_; 373 } SetIsTV(bool isTV)374 void SetIsTV(bool isTV) 375 { 376 isTV_ = isTV; 377 } 378 GetHorizontalSpacing()379 const Dimension& GetHorizontalSpacing() const 380 { 381 return horizontalSpacing_; 382 } SetHorizontalSpacing(const Dimension & horizontalSpacing)383 void SetHorizontalSpacing(const Dimension& horizontalSpacing) 384 { 385 horizontalSpacing_ = horizontalSpacing; 386 } 387 GetVerticalSpacing()388 const Dimension& GetVerticalSpacing() const 389 { 390 return verticalSpacing_; 391 } SetVerticalSpacing(const Dimension & verticalSpacing)392 void SetVerticalSpacing(const Dimension& verticalSpacing) 393 { 394 verticalSpacing_ = verticalSpacing; 395 } 396 GetContentSpacing()397 const Dimension& GetContentSpacing() const 398 { 399 return contentSpacing_; 400 } SetContentSpacing(const Dimension & contentSpacing)401 void SetContentSpacing(const Dimension& contentSpacing) 402 { 403 contentSpacing_ = contentSpacing; 404 } 405 GetOptionPadding()406 const Edge& GetOptionPadding() const 407 { 408 return optionPadding_; 409 } SetOptionPadding(const Edge & value)410 void SetOptionPadding(const Edge& value) 411 { 412 optionPadding_ = value; 413 } 414 GetShowTime(bool isMenu)415 uint32_t GetShowTime(bool isMenu) const 416 { 417 if (isMenu) { 418 return menuShowTime_; 419 } else { 420 return selectShowTime_; 421 } 422 } 423 GetHideTime(bool isMenu)424 uint32_t GetHideTime(bool isMenu) const 425 { 426 if (isMenu) { 427 return menuHideTime_; 428 } else { 429 return selectHideTime_; 430 } 431 } 432 433 SelectTheme() = default; 434 IsAllowScale()435 bool IsAllowScale() const 436 { 437 return allowScale_; 438 } 439 SetAllowScale(bool allowScale)440 void SetAllowScale(bool allowScale) 441 { 442 allowScale_ = allowScale; 443 } 444 GetOptionInterval()445 const Dimension& GetOptionInterval() const 446 { 447 return optionInterval_; 448 } 449 GetTvFocusTextColor()450 const Color& GetTvFocusTextColor() const 451 { 452 return tvFocusTextColor_; 453 } 454 GetTvNormalBackColor()455 const Color& GetTvNormalBackColor() const 456 { 457 return tvNormalBackColor_; 458 } 459 GetTvBackColor()460 const Color& GetTvBackColor() const 461 { 462 return tvBackColor_; 463 } 464 GetFocusedDisableColor()465 const Color& GetFocusedDisableColor() const 466 { 467 return focusedDisableColor_; 468 } 469 GetNormalDisableColor()470 const Color& GetNormalDisableColor() const 471 { 472 return normalDisableColor_; 473 } 474 GetFocusedTextDisableColor()475 const Color& GetFocusedTextDisableColor() const 476 { 477 return focusedTextDisableColor_; 478 } 479 GetNormalTextDisableColor()480 const Color& GetNormalTextDisableColor() const 481 { 482 return normalTextDisableColor_; 483 } 484 GetLineColor()485 const Color& GetLineColor() const 486 { 487 return lineColor_; 488 } 489 490 private: 491 Color disabledColor_; 492 Color clickedColor_; 493 Color selectedColor_; 494 495 Color backgroundColor_ = Color::WHITE; 496 Color hoverColor_ = Color(0x0c000000); 497 Color selectedColorText_ = Color(0xff0a59f7); 498 Color lineColor_ = Color(0x33000000); 499 500 bool allowScale_ = true; 501 Dimension fontSize_; 502 Color fontColor_; 503 std::string fontFamily_; 504 FontWeight fontWeight_ { FontWeight::NORMAL }; 505 TextDecoration textDecoration_ { TextDecoration::NONE }; 506 507 std::size_t optionSize_ { 0 }; 508 Dimension rrectSize_; 509 Dimension iconSize_; 510 Dimension normalPadding_; 511 512 Dimension popupRRectSize_; 513 Dimension popupBorderWidth_; 514 Dimension popupShadowWidth_; 515 Dimension popupMinWidth_; 516 517 Dimension titleLeftPadding_; 518 Dimension titleTopPadding_; 519 Dimension titleRightPadding_; 520 Dimension titleBottomPadding_; 521 Dimension horizontalSpacing_; 522 Dimension verticalSpacing_; 523 Dimension contentSpacing_; 524 Dimension optionInterval_; 525 526 Color tvFocusTextColor_; 527 Color tvNormalBackColor_; 528 Color tvBackColor_; 529 530 Color focusedDisableColor_; 531 Color normalDisableColor_; 532 Color focusedTextDisableColor_; 533 Color normalTextDisableColor_; 534 535 TextStyle titleStyle_; 536 bool isTV_ = false; 537 uint32_t menuShowTime_ = 0; 538 uint32_t selectShowTime_ = 0; 539 uint32_t menuHideTime_ = 0; 540 uint32_t selectHideTime_ = 0; 541 542 Edge optionPadding_; 543 }; 544 545 } // namespace OHOS::Ace 546 547 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SELECT_SELECT_THEME_H 548