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_PIECE_PIECE_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PIECE_PIECE_THEME_H 18 19 #include "base/geometry/dimension.h" 20 #include "core/components/common/properties/color.h" 21 #include "core/components/theme/theme.h" 22 #include "core/components/theme/theme_constants.h" 23 #include "core/components/theme/theme_constants_defines.h" 24 25 namespace OHOS::Ace { 26 27 /** 28 * PieceTheme defines default style of DOMPiece,using PieceTheme::Builder to build DOMPiece. 29 */ 30 class PieceTheme : public virtual Theme { 31 DECLARE_ACE_TYPE(PieceTheme, Theme); 32 33 public: 34 class Builder final { 35 public: 36 Builder() = default; 37 ~Builder() = default; 38 Build(const RefPtr<ThemeConstants> & themeConstants)39 RefPtr<PieceTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 40 { 41 RefPtr<PieceTheme> theme = AceType::Claim(new PieceTheme()); 42 if (!themeConstants) { 43 return theme; 44 } 45 theme->height_ = themeConstants->GetDimension(THEME_PIECE_HEIGHT); 46 theme->textStyle_.SetTextOverflow(TextOverflow::ELLIPSIS); 47 auto maxlines = themeConstants->GetInt(THEME_PIECE_TEXT_LINES); 48 theme->textStyle_.SetMaxLines(maxlines < 0 ? UINT32_MAX : maxlines); 49 theme->textStyle_.SetTextColor(themeConstants->GetColor(THEME_PIECE_TEXT_COLOR)); 50 theme->textStyle_.SetFontSize(themeConstants->GetDimension(THEME_PIECE_FONT_SIZE)); 51 theme->textStyle_.SetFontWeight(FontWeight(themeConstants->GetInt(THEME_PIECE_FONT_WEIGHT))); 52 theme->hoverColor_ = themeConstants->GetColor(THEME_PIECE_HOVER_COLOR); 53 theme->backgroundColor_ = themeConstants->GetColor(THEME_PIECE_BACKGROUND_COLOR); 54 theme->paddingHorizontal_ = themeConstants->GetDimension(THEME_PIECE_PADDING_HORIZONTAL); 55 theme->paddingVertical_ = themeConstants->GetDimension(THEME_PIECE_PADDING_VERTICAL); 56 theme->iconResource_ = themeConstants->GetResourceId(THEME_PIECE_ICON_SOURCE); 57 theme->iconSize_ = themeConstants->GetDimension(THEME_PIECE_ICON_SIZE); 58 theme->interval_ = themeConstants->GetDimension(THEME_PIECE_INTERVAL); 59 ParsePattern(themeConstants->GetThemeStyle(), theme); 60 return theme; 61 } 62 63 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<PieceTheme> & theme)64 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<PieceTheme>& theme) const 65 { 66 if (!themeStyle || !theme) { 67 return; 68 } 69 auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_PIECE, nullptr); 70 if (!pattern) { 71 LOGW("find pattern of piece fail"); 72 return; 73 } 74 theme->textStyle_.SetTextColor(pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color())); 75 theme->textStyle_.SetFontSize(pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_fp)); 76 theme->backgroundColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR, Color()) 77 .BlendOpacity(pattern->GetAttr<double>(PATTERN_BG_COLOR_ALPHA, 0.0)); 78 theme->hoverColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_HOVERED, Color()); 79 } 80 }; 81 82 ~PieceTheme() override = default; GetHoverColor()83 const Color& GetHoverColor() const 84 { 85 return hoverColor_; 86 } GetBackgroundColor()87 const Color& GetBackgroundColor() const 88 { 89 return backgroundColor_; 90 } GetHeight()91 const Dimension& GetHeight() const 92 { 93 return height_; 94 } GetPaddingVertical()95 const Dimension& GetPaddingVertical() const 96 { 97 return paddingVertical_; 98 } GetPaddingHorizontal()99 const Dimension& GetPaddingHorizontal() const 100 { 101 return paddingHorizontal_; 102 } GetInterval()103 const Dimension& GetInterval() const 104 { 105 return interval_; 106 } GetIconResource()107 InternalResource::ResourceId GetIconResource() const 108 { 109 return iconResource_; 110 } GetTextStyle()111 const TextStyle& GetTextStyle() const 112 { 113 return textStyle_; 114 } GetIconSize()115 const Dimension& GetIconSize() const 116 { 117 return iconSize_; 118 } 119 120 protected: 121 PieceTheme() = default; 122 123 private: 124 Color hoverColor_; 125 Color backgroundColor_; 126 Dimension height_; 127 Dimension interval_; 128 Dimension iconSize_; 129 Dimension paddingVertical_; 130 Dimension paddingHorizontal_; 131 TextStyle textStyle_; 132 InternalResource::ResourceId iconResource_ = InternalResource::ResourceId::NO_ID; 133 }; 134 135 } // namespace OHOS::Ace 136 137 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PIECE_PIECE_THEME_H 138