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_BUTTON_BUTTON_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_BUTTON_THEME_H 18 19 #include "core/components/common/properties/color.h" 20 #include "core/components/common/properties/text_style.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 * ButtonTheme defines color and styles of ButtonComponent. ButtonTheme should be built 29 * using ButtonTheme::Builder. 30 */ 31 class ButtonTheme : public virtual Theme { 32 DECLARE_ACE_TYPE(ButtonTheme, Theme); 33 34 public: 35 class Builder { 36 public: 37 Builder() = default; 38 ~Builder() = default; 39 Build(const RefPtr<ThemeConstants> & themeConstants)40 RefPtr<ButtonTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 41 { 42 RefPtr<ButtonTheme> theme = AceType::Claim(new ButtonTheme()); 43 if (!themeConstants) { 44 return theme; 45 } 46 theme->radius_ = themeConstants->GetDimension(THEME_BUTTON_RADIUS); 47 theme->bgColor_ = themeConstants->GetColor(THEME_BUTTON_BACKGROUND_COLOR); 48 theme->bgFocusColor_ = themeConstants->GetColor(THEME_BUTTON_FOCUS_COLOR); 49 theme->clickedColor_ = themeConstants->GetColor(THEME_BUTTON_CLICKED_COLOR); 50 theme->disabledColor_ = themeConstants->GetColor(THEME_BUTTON_DISABLED_COLOR); 51 theme->bgDisabledAlpha_ = themeConstants->GetDouble(THEME_BUTTON_DISABLED_ALPHA); 52 theme->hoverColor_ = themeConstants->GetColor(THEME_BUTTON_HOVER_COLOR); 53 theme->borderColor_ = themeConstants->GetColor(THEME_BUTTON_BORDER_COLOR); 54 theme->borderWidth_ = themeConstants->GetDimension(THEME_BUTTON_BORDER_WIDTH); 55 theme->textFocusColor_ = themeConstants->GetColor(THEME_BUTTON_TEXT_FOCUS_COLOR); 56 theme->textDisabledColor_ = themeConstants->GetColor(THEME_BUTTON_TEXT_DISABLED_COLOR); 57 theme->textWaitingColor_ = themeConstants->GetColor(THEME_BUTTON_TEXT_WAITING_COLOR); 58 theme->textStyle_.SetTextColor(themeConstants->GetColor(THEME_BUTTON_TEXT_COLOR)); 59 theme->textStyle_.SetFontSize(themeConstants->GetDimension(THEME_BUTTON_TEXT_FONTSIZE)); 60 theme->textStyle_.SetFontWeight(FontWeight(themeConstants->GetInt(THEME_BUTTON_TEXT_FONTWEIGHT))); 61 theme->minWidth_ = themeConstants->GetDimension(THEME_BUTTON_MIN_WIDTH); 62 theme->height_ = themeConstants->GetDimension(THEME_BUTTON_HEIGHT); 63 theme->downloadHeight_ = themeConstants->GetDimension(THEME_BUTTON_DOWNLOAD_HEIGHT); 64 theme->padding_ = Edge(themeConstants->GetDimension(THEME_BUTTON_PADDING_HORIZONTAL).Value(), 65 themeConstants->GetDimension(THEME_BUTTON_PADDING_VERTICAL).Value(), 66 themeConstants->GetDimension(THEME_BUTTON_PADDING_HORIZONTAL).Value(), 67 themeConstants->GetDimension(THEME_BUTTON_PADDING_VERTICAL).Value(), 68 themeConstants->GetDimension(THEME_BUTTON_PADDING_VERTICAL).Unit()); 69 theme->minFontSize_ = themeConstants->GetDimension(THEME_BUTTON_TEXT_FONTSIZE_MIN); 70 int32_t maxlines = themeConstants->GetInt(THEME_BUTTON_TEXT_MAX_LINES); 71 theme->textMaxLines_ = maxlines < 0 ? theme->textMaxLines_ : static_cast<uint32_t>(maxlines); 72 theme->minCircleButtonDiameter_ = themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MIN_DIAMETER); 73 theme->minCircleButtonIcon_ = themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MIN_ICON_SIZE); 74 theme->minCircleButtonPadding_ = Edge(themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MIN_PADDING)); 75 theme->maxCircleButtonDiameter_ = themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MAX_DIAMETER); 76 theme->maxCircleButtonIcon_ = themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MAX_ICON_SIZE); 77 theme->maxCircleButtonPadding_ = Edge(themeConstants->GetDimension(THEME_BUTTON_CIRCLE_MAX_PADDING)); 78 theme->progressFocusColor_ = themeConstants->GetColor(THEME_BUTTON_PROGRESS_FOCUS_COLOR); 79 theme->downloadBorderColor_ = themeConstants->GetColor(THEME_BUTTON_DOWNLOAD_BORDER_COLOR); 80 theme->normalTextColor_ = themeConstants->GetColor(THEME_BUTTON_NORMAL_TEXT_COLOR); 81 theme->downloadBackgroundColor_ = themeConstants->GetColor(THEME_BUTTON_DOWNLOAD_BG_COLOR); 82 theme->downloadTextColor_ = themeConstants->GetColor(THEME_BUTTON_DOWNLOAD_TEXT_COLOR); 83 theme->downloadFontSize_ = themeConstants->GetDimension(THEME_BUTTON_DOWNLOAD_TEXT_FONTSIZE); 84 theme->progressColor_ = themeConstants->GetColor(THEME_BUTTON_PROGRESS_COLOR); 85 theme->progressDiameter_ = themeConstants->GetDimension(THEME_BUTTON_PROGRESS_DIAMETER); 86 theme->downloadProgressColor_ = themeConstants->GetColor(THEME_BUTTON_DOWNLOAD_PROGRESS_COLOR); 87 theme->innerPadding_ = themeConstants->GetDimension(THEME_BUTTON_INNER_PADDING); 88 ParsePattern(themeConstants->GetThemeStyle(), theme); 89 return theme; 90 } 91 92 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<ButtonTheme> & theme)93 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<ButtonTheme>& theme) const 94 { 95 if (!themeStyle) { 96 return; 97 } 98 auto buttonPattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_BUTTON, nullptr); 99 if (!buttonPattern) { 100 LOGW("find pattern of button fail"); 101 return; 102 } 103 theme->bgColor_ = buttonPattern->GetAttr<Color>(PATTERN_BG_COLOR, Color()); 104 theme->clickedColor_ = buttonPattern->GetAttr<Color>("bg_color_clicked_blend", Color()); 105 theme->disabledColor_ = theme->bgColor_ 106 .BlendOpacity(buttonPattern->GetAttr<double>(PATTERN_BG_COLOR_DISABLED_ALPHA, 0.0)); 107 theme->hoverColor_ = buttonPattern->GetAttr<Color>("bg_color_hovered_blend", Color()); 108 theme->borderColor_ = buttonPattern->GetAttr<Color>("border_color", Color()); 109 theme->borderWidth_ = buttonPattern->GetAttr<Dimension>("border_width", 0.0_vp); 110 theme->textStyle_.SetTextColor(buttonPattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color())); 111 theme->textDisabledColor_ = buttonPattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color()) 112 .BlendOpacity(buttonPattern->GetAttr<double>("text_color_disabled_alpha", 0.0)); 113 theme->textWaitingColor_ = buttonPattern->GetAttr<Color>("waiting_button_text_color", Color()); 114 theme->normalTextColor_ = buttonPattern->GetAttr<Color>("normal_text_color", Color()); 115 theme->downloadBackgroundColor_ = buttonPattern->GetAttr<Color>("download_button_bg_color", Color()) 116 .BlendOpacity(buttonPattern->GetAttr<double>("download_button_bg_color_alpha", 0.0)); 117 theme->downloadBorderColor_ = buttonPattern->GetAttr<Color>("download_button_border_color", Color()) 118 .BlendOpacity(buttonPattern->GetAttr<double>("download_button_border_color_alpha", 0.0)); 119 theme->downloadProgressColor_ = buttonPattern->GetAttr<Color>("download_button_process_color", Color()) 120 .BlendOpacity(buttonPattern->GetAttr<double>("download_button_process_color_alpha", 0.0)); 121 theme->downloadTextColor_ = buttonPattern->GetAttr<Color>("download_button_text_color", Color()); 122 theme->progressColor_ = buttonPattern->GetAttr<Color>("process_button_text_color", Color()); 123 } 124 }; 125 126 ~ButtonTheme() override = default; 127 GetRadius()128 const Dimension& GetRadius() const 129 { 130 return radius_; 131 } 132 GetBgColor()133 const Color& GetBgColor() const 134 { 135 return bgColor_; 136 } 137 GetBgFocusColor()138 const Color& GetBgFocusColor() const 139 { 140 return bgFocusColor_; 141 } 142 GetClickedColor()143 const Color& GetClickedColor() const 144 { 145 return clickedColor_; 146 } 147 GetDisabledColor()148 const Color& GetDisabledColor() const 149 { 150 return disabledColor_; 151 } 152 GetHoverColor()153 const Color& GetHoverColor() const 154 { 155 return hoverColor_; 156 } 157 GetBorderColor()158 const Color& GetBorderColor() const 159 { 160 return borderColor_; 161 } 162 GetBorderWidth()163 const Dimension& GetBorderWidth() const 164 { 165 return borderWidth_; 166 } 167 GetBgDisabledAlpha()168 double GetBgDisabledAlpha() const 169 { 170 return bgDisabledAlpha_; 171 } 172 GetTextFocusColor()173 const Color& GetTextFocusColor() const 174 { 175 return textFocusColor_; 176 } 177 GetTextDisabledColor()178 const Color& GetTextDisabledColor() const 179 { 180 return textDisabledColor_; 181 } 182 GetNormalTextColor()183 const Color& GetNormalTextColor() const 184 { 185 return normalTextColor_; 186 } 187 GetDownloadBackgroundColor()188 const Color& GetDownloadBackgroundColor() const 189 { 190 return downloadBackgroundColor_; 191 } 192 GetDownloadTextColor()193 const Color& GetDownloadTextColor() const 194 { 195 return downloadTextColor_; 196 } 197 GetTextWaitingColor()198 const Color& GetTextWaitingColor() const 199 { 200 return textWaitingColor_; 201 } 202 GetTextStyle()203 const TextStyle& GetTextStyle() const 204 { 205 return textStyle_; 206 } 207 GetMinWidth()208 const Dimension& GetMinWidth() const 209 { 210 return minWidth_; 211 } 212 GetHeight()213 const Dimension& GetHeight() const 214 { 215 return height_; 216 } 217 GetDownloadHeight()218 const Dimension& GetDownloadHeight() const 219 { 220 return downloadHeight_; 221 } 222 GetPadding()223 const Edge& GetPadding() const 224 { 225 return padding_; 226 } 227 GetMinFontSize()228 const Dimension& GetMinFontSize() const 229 { 230 return minFontSize_; 231 } 232 GetDownloadFontSize()233 const Dimension& GetDownloadFontSize() const 234 { 235 return downloadFontSize_; 236 } 237 GetMaxFontSize()238 const Dimension& GetMaxFontSize() const 239 { 240 return textStyle_.GetFontSize(); 241 } 242 GetTextMaxLines()243 uint32_t GetTextMaxLines() const 244 { 245 return textMaxLines_; 246 } 247 GetMinCircleButtonDiameter()248 const Dimension& GetMinCircleButtonDiameter() const 249 { 250 return minCircleButtonDiameter_; 251 } 252 GetMinCircleButtonIcon()253 const Dimension& GetMinCircleButtonIcon() const 254 { 255 return minCircleButtonIcon_; 256 } 257 GetMinCircleButtonPadding()258 const Edge& GetMinCircleButtonPadding() const 259 { 260 return minCircleButtonPadding_; 261 } 262 GetMaxCircleButtonDiameter()263 const Dimension& GetMaxCircleButtonDiameter() const 264 { 265 return maxCircleButtonDiameter_; 266 } 267 GetMaxCircleButtonIcon()268 const Dimension& GetMaxCircleButtonIcon() const 269 { 270 return maxCircleButtonIcon_; 271 } 272 GetMaxCircleButtonPadding()273 const Edge& GetMaxCircleButtonPadding() const 274 { 275 return maxCircleButtonPadding_; 276 } 277 GetProgressFocusColor()278 const Color& GetProgressFocusColor() const 279 { 280 return progressFocusColor_; 281 } 282 GetDownloadBorderColor()283 const Color& GetDownloadBorderColor() const 284 { 285 return downloadBorderColor_; 286 } 287 GetProgressColor()288 const Color& GetProgressColor() const 289 { 290 return progressColor_; 291 } 292 GetProgressDiameter()293 const Dimension& GetProgressDiameter() const 294 { 295 return progressDiameter_; 296 } 297 GetDownloadProgressColor()298 const Color& GetDownloadProgressColor() const 299 { 300 return downloadProgressColor_; 301 } 302 GetInnerPadding()303 const Dimension& GetInnerPadding() const 304 { 305 return innerPadding_; 306 } 307 308 protected: 309 ButtonTheme() = default; 310 311 private: 312 Color bgColor_; 313 Color bgFocusColor_; 314 Color clickedColor_; 315 Color disabledColor_; 316 Color hoverColor_; 317 Color borderColor_; 318 Color textFocusColor_; 319 Color textDisabledColor_; 320 Color textWaitingColor_; 321 Color progressColor_; 322 Color progressFocusColor_; 323 Color normalTextColor_; 324 Color downloadBackgroundColor_; 325 Color downloadTextColor_; 326 Color downloadBorderColor_; 327 Color downloadProgressColor_; 328 TextStyle textStyle_; 329 Edge padding_; 330 Edge minCircleButtonPadding_; 331 Edge maxCircleButtonPadding_; 332 333 Dimension radius_; 334 Dimension minWidth_; 335 Dimension height_; 336 Dimension progressDiameter_; 337 Dimension innerPadding_; 338 Dimension minFontSize_; 339 Dimension downloadFontSize_; 340 Dimension minCircleButtonDiameter_; 341 Dimension minCircleButtonIcon_; 342 Dimension maxCircleButtonDiameter_; 343 Dimension maxCircleButtonIcon_; 344 Dimension borderWidth_; 345 Dimension downloadHeight_; 346 347 double bgDisabledAlpha_ = 1.0; 348 uint32_t textMaxLines_ = 1; 349 }; 350 351 } // namespace OHOS::Ace 352 353 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_BUTTON_THEME_H 354