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_TOAST_TOAST_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOAST_TOAST_THEME_H 18 19 #include "core/common/container.h" 20 #include "core/components/common/properties/color.h" 21 #include "core/components/common/properties/edge.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 * ToastTheme defines color and styles of Toast. ToastTheme should be built 31 * using ToastTheme::Builder. 32 */ 33 class ToastTheme : public virtual Theme { 34 DECLARE_ACE_TYPE(ToastTheme, Theme); 35 36 public: 37 class Builder { 38 public: 39 Builder() = default; 40 ~Builder() = default; 41 Build(const RefPtr<ThemeConstants> & themeConstants)42 RefPtr<ToastTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 43 { 44 RefPtr<ToastTheme> theme = AceType::MakeRefPtr<ToastTheme>(); 45 if (!themeConstants) { 46 return theme; 47 } 48 ParsePattern(themeConstants, theme); 49 return theme; 50 } 51 private: ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<ToastTheme> & theme)52 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<ToastTheme>& theme) const 53 { 54 RefPtr<ThemeStyle> toastPattern = themeConstants->GetPatternByName(THEME_PATTERN_TOAST); 55 if (!toastPattern) { 56 return; 57 } 58 59 theme->minWidth_ = toastPattern->GetAttr<Dimension>("toast_content_min_width", 0.0_vp); 60 theme->minHeight_ = toastPattern->GetAttr<Dimension>("toast_content_min_height", 0.0_vp); 61 theme->top_ = toastPattern->GetAttr<Dimension>("toast_top", 0.0_vp); 62 theme->bottom_ = toastPattern->GetAttr<Dimension>("toast_bottom", 0.0_vp); 63 theme->minFontSize_ = toastPattern->GetAttr<Dimension>("toast_text_min_font_size", 0.0_vp); 64 auto textMaxLines = static_cast<int32_t>(toastPattern->GetAttr<double>("toast_text_max_lines", 0.0)); 65 theme->textMaxLines_ = textMaxLines < 0 ? theme->textMaxLines_ : static_cast<uint32_t>(textMaxLines); 66 theme->backgroundColor_ = toastPattern->GetAttr<Color>(PATTERN_BG_COLOR, Color()); 67 theme->blurStyleTextColor_ = toastPattern->GetAttr<Color>(PATTERN_TEXT_COLOR_BLUR, Color()); 68 theme->toastAlign_ = static_cast<int32_t>(toastPattern->GetAttr<double>("toast_align", -1.0)); 69 theme->multiLineTextAlign_ = static_cast<TextAlign>(toastPattern->GetAttr<double>("toast_text_align", 0.0)); 70 constexpr double toastLimitHeightRatio = 0.65; 71 theme->toastLimitHeightRatio_ = 72 toastPattern->GetAttr<double>("toast_limit_height_ratio", toastLimitHeightRatio); 73 theme->bgThemeColorMode_ = 74 static_cast<uint32_t>(toastPattern->GetAttr<double>("toast_bg_theme_color_mode", 0)); 75 76 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) { 77 theme->padding_ = Edge(toastPattern->GetAttr<Dimension>("toast_padding_level8", 0.0_vp).Value(), 78 toastPattern->GetAttr<Dimension>("toast_padding_level4", 0.0_vp).Value(), 79 toastPattern->GetAttr<Dimension>("toast_padding_level8", 0.0_vp).Value(), 80 toastPattern->GetAttr<Dimension>("toast_padding_level4", 0.0_vp).Value(), 81 toastPattern->GetAttr<Dimension>("toast_padding_level4", 0.0_vp).Unit()); 82 theme->marging_ = Edge(toastPattern->GetAttr<Dimension>("toast_margin_left", 0.0_vp).Value(), 0.0, 83 toastPattern->GetAttr<Dimension>("toast_margin_right", 0.0_vp).Value(), 0.0, 84 toastPattern->GetAttr<Dimension>("toast_margin_right", 0.0_vp).Unit()); 85 theme->maxWidth_ = toastPattern->GetAttr<Dimension>("toast_content_limit_max_width", 400.0_vp); 86 theme->textStyle_.SetTextColor(toastPattern->GetAttr<Color>("toast_font_primary", Color())); 87 theme->textStyle_.SetFontSize(toastPattern->GetAttr<Dimension>("toast_Body_M", 0.0_vp)); 88 theme->textStyle_.SetFontWeight( 89 FontWeight(static_cast<int32_t>(toastPattern->GetAttr<double>("toast_font_weight_regular", 0.0)))); 90 theme->radius_ = Radius(toastPattern->GetAttr<Dimension>("toast_border_radius_level9", 24.0_vp), 91 toastPattern->GetAttr<Dimension>("toast_border_radius_level9", 24.0_vp)); 92 } else { 93 theme->padding_ = Edge(toastPattern->GetAttr<Dimension>("toast_padding_horizontal", 0.0_vp).Value(), 94 toastPattern->GetAttr<Dimension>("toast_padding_vertical", 0.0_vp).Value(), 95 toastPattern->GetAttr<Dimension>("toast_padding_horizontal", 0.0_vp).Value(), 96 toastPattern->GetAttr<Dimension>("toast_padding_vertical", 0.0_vp).Value(), 97 toastPattern->GetAttr<Dimension>("toast_padding_vertical", 0.0_vp).Unit()); 98 theme->marging_ = Edge(toastPattern->GetAttr<Dimension>("toast_margin", 0.0_vp).Value(), 0.0, 99 toastPattern->GetAttr<Dimension>("toast_margin", 0.0_vp).Value(), 0.0, 100 toastPattern->GetAttr<Dimension>("toast_margin", 0.0_vp).Unit()); 101 theme->maxWidth_ = toastPattern->GetAttr<Dimension>("toast_content_max_width", 0.0_vp); 102 theme->textStyle_.SetTextColor(toastPattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color())); 103 theme->textStyle_.SetFontSize(toastPattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_vp)); 104 theme->textStyle_.SetFontWeight( 105 FontWeight(static_cast<int32_t>(toastPattern->GetAttr<double>("toast_text_font_weight", 0.0)))); 106 theme->radius_ = Radius(toastPattern->GetAttr<Dimension>("toast_border_radius", 24.0_vp), 107 toastPattern->GetAttr<Dimension>("toast_border_radius", 24.0_vp)); 108 } 109 theme->toastDoubleBorderEnable_ = toastPattern->GetAttr<int>("toast_double_border_enable", 0); 110 theme->toastOuterBorderColor_ = 111 toastPattern->GetAttr<Color>("toast_outer_border_color", Color::TRANSPARENT); 112 theme->toastOuterBorderWidth_ = toastPattern->GetAttr<double>("toast_outer_border_width", 0.0f); 113 theme->toastInnerBorderColor_ = 114 toastPattern->GetAttr<Color>("toast_inner_border_color", Color::TRANSPARENT); 115 theme->toastInnerBorderWidth_ = toastPattern->GetAttr<double>("toast_inner_border_width", 0.0f); 116 theme->toastShadowStyle_ = static_cast<ShadowStyle>(toastPattern->GetAttr<int>( 117 "toast_default_shadow_style", static_cast<int>(ShadowStyle::OuterDefaultMD))); 118 theme->toastBackgroundBlurStyle_ = toastPattern->GetAttr<int>( 119 "toast_background_blur_style", static_cast<int>(BlurStyle::COMPONENT_ULTRA_THICK)); 120 theme->defaultBGColor_ = toastPattern->GetAttr<Color>("toast_default_bg_color", Color::TRANSPARENT); 121 } 122 }; 123 124 ~ToastTheme() override = default; 125 GetPadding()126 const Edge& GetPadding() const 127 { 128 return padding_; 129 } 130 GetMaxWidth()131 const Dimension& GetMaxWidth() const 132 { 133 return maxWidth_; 134 } 135 GetMinWidth()136 const Dimension& GetMinWidth() const 137 { 138 return minWidth_; 139 } 140 GetMinHeight()141 const Dimension& GetMinHeight() const 142 { 143 return minHeight_; 144 } 145 GetBackgroundColor()146 const Color& GetBackgroundColor() const 147 { 148 return backgroundColor_; 149 } 150 GetTextStyle()151 const TextStyle& GetTextStyle() const 152 { 153 return textStyle_; 154 } 155 GetRadius()156 const Radius& GetRadius() const 157 { 158 return radius_; 159 } 160 GetTop()161 const Dimension& GetTop() const 162 { 163 return top_; 164 } 165 GetBottom()166 const Dimension& GetBottom() const 167 { 168 return bottom_; 169 } 170 GetMinFontSize()171 const Dimension& GetMinFontSize() const 172 { 173 return minFontSize_; 174 } 175 GetTextMaxLines()176 uint32_t GetTextMaxLines() const 177 { 178 return textMaxLines_; 179 } 180 GetMarging()181 const Edge& GetMarging() const 182 { 183 return marging_; 184 } 185 GetBlurStyleTextColor()186 const Color& GetBlurStyleTextColor() const 187 { 188 return blurStyleTextColor_; 189 } 190 GetToastDoubleBorderEnable()191 int32_t GetToastDoubleBorderEnable() const 192 { 193 return toastDoubleBorderEnable_; 194 } 195 GetToastOuterBorderWidth()196 double GetToastOuterBorderWidth() const 197 { 198 return toastOuterBorderWidth_; 199 } 200 GetToastOuterBorderColor()201 Color GetToastOuterBorderColor() const 202 { 203 return toastOuterBorderColor_; 204 } 205 GetToastInnerBorderWidth()206 double GetToastInnerBorderWidth() const 207 { 208 return toastInnerBorderWidth_; 209 } 210 GetToastInnerBorderColor()211 Color GetToastInnerBorderColor() const 212 { 213 return toastInnerBorderColor_; 214 } 215 GetToastBackgroundBlurStyle()216 int GetToastBackgroundBlurStyle() const 217 { 218 return toastBackgroundBlurStyle_; 219 } 220 GetBgThemeColorMode()221 uint32_t GetBgThemeColorMode() const 222 { 223 return bgThemeColorMode_; 224 } 225 GetAlign()226 int32_t GetAlign() const 227 { 228 return toastAlign_; 229 } 230 GetMultiLineTextAlign()231 TextAlign GetMultiLineTextAlign() const 232 { 233 return multiLineTextAlign_; 234 } 235 GetToastLimitHeightRatio()236 double GetToastLimitHeightRatio() const 237 { 238 return toastLimitHeightRatio_; 239 } 240 GetToastShadowStyle()241 ShadowStyle GetToastShadowStyle() const 242 { 243 return toastShadowStyle_; 244 } 245 GetDefaultBGColor()246 Color GetDefaultBGColor() const 247 { 248 return defaultBGColor_; 249 } 250 251 protected: 252 ToastTheme() = default; 253 254 private: 255 Edge padding_; 256 Dimension maxWidth_; 257 Dimension minWidth_; 258 Dimension minHeight_; 259 Color backgroundColor_; 260 TextStyle textStyle_; 261 Radius radius_; 262 Dimension top_; 263 Dimension bottom_; 264 Dimension minFontSize_; 265 uint32_t textMaxLines_ = 1; 266 Edge marging_; 267 Color blurStyleTextColor_; 268 int32_t toastDoubleBorderEnable_ = 0; 269 double toastOuterBorderWidth_ = 0.0f; 270 Color toastOuterBorderColor_ = Color::TRANSPARENT; 271 double toastInnerBorderWidth_ = 0.0f; 272 Color toastInnerBorderColor_ = Color::TRANSPARENT; 273 uint32_t bgThemeColorMode_ = 0; 274 TextAlign multiLineTextAlign_ = TextAlign::START; 275 int32_t toastAlign_ = 0; 276 double toastLimitHeightRatio_ = 0.65; 277 ShadowStyle toastShadowStyle_ = ShadowStyle::OuterDefaultMD; 278 int toastBackgroundBlurStyle_ = static_cast<int>(BlurStyle::COMPONENT_ULTRA_THICK); 279 Color defaultBGColor_ = Color::TRANSPARENT; 280 }; 281 282 } // namespace OHOS::Ace 283 284 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TOAST_TOAST_THEME_H 285