1 /* 2 * Copyright (c) 2021-2022 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_POPUP_POPUP_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_POPUP_POPUP_THEME_H 18 19 #include "base/geometry/dimension.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 namespace { 29 constexpr uint32_t SHOW_TIME = 250; // unit is ms. 30 constexpr uint32_t HIDE_TIME = 250; // unit is ms. 31 constexpr Dimension TARGET_SPACE = 8.0_vp; 32 constexpr Dimension BORDER_RADIUS_POPUP = 20.0_vp; 33 constexpr double DEFAULT_OPACITY = 0.95; 34 } // namespace 35 36 /** 37 * PopupTheme defines color and styles of PopupComponent. PopupTheme should be built 38 * using PopupTheme::Builder. 39 */ 40 class PopupTheme : public virtual Theme { 41 DECLARE_ACE_TYPE(PopupTheme, Theme); 42 43 public: 44 class Builder { 45 public: 46 Builder() = default; 47 ~Builder() = default; 48 Build(const RefPtr<ThemeConstants> & themeConstants)49 RefPtr<PopupTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 50 { 51 RefPtr<PopupTheme> theme = AceType::Claim(new PopupTheme()); 52 if (!themeConstants) { 53 return theme; 54 } 55 ParsePattern(themeConstants, theme); 56 theme->showTime_ = SHOW_TIME; 57 theme->hideTime_ = HIDE_TIME; 58 theme->targetSpace_ = TARGET_SPACE; 59 return theme; 60 } 61 62 private: ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<PopupTheme> & theme)63 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<PopupTheme>& theme) const 64 { 65 if (!theme) { 66 return; 67 } 68 RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_POPUP); 69 if (!pattern) { 70 LOGW("find pattern of popup fail"); 71 return; 72 } 73 theme->maskColor_ = pattern->GetAttr<Color>("popup_mask_color", Color()); 74 theme->textStyle_.SetTextColor(pattern->GetAttr<Color>("popup_text_color", Color())); 75 theme->textStyle_.SetFontSize(pattern->GetAttr<Dimension>("popup_text_font_size", 0.0_vp)); 76 theme->backgroundColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR, theme->backgroundColor_); 77 theme->fontSize_ = pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 14.0_fp); 78 theme->buttonFontSize_ = pattern->GetAttr<Dimension>(POPUP_BUTTON_TEXT_FONT_SIZE, 14.0_fp); 79 theme->fontColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::WHITE); 80 theme->buttonHoverColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_HOVERED, Color()); 81 theme->buttonPressColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_PRESSED, Color()); 82 theme->focusColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_FOCUSED, Color()); 83 84 theme->radius_ = Radius(BORDER_RADIUS_POPUP, BORDER_RADIUS_POPUP); 85 theme->padding_ = Edge(pattern->GetAttr<Dimension>(POPUP_HORIZONTAL_PADDING, 16.0_vp), 86 pattern->GetAttr<Dimension>(POPUP_VERTICAL_PADDING, 12.0_vp), 87 pattern->GetAttr<Dimension>(POPUP_HORIZONTAL_PADDING, 16.0_vp), 88 pattern->GetAttr<Dimension>(POPUP_VERTICAL_PADDING, 12.0_vp)); 89 auto popupDoubleBorderEnable = pattern->GetAttr<std::string>("popup_double_border_enable", "0"); 90 theme->popupDoubleBorderEnable_ = StringUtils::StringToInt(popupDoubleBorderEnable); 91 theme->popupOuterBorderColor_ = pattern->GetAttr<Color>("popup_outer_border_color", Color::TRANSPARENT); 92 theme->popupInnerBorderColor_ = pattern->GetAttr<Color>("popup_inner_border_color", Color::TRANSPARENT); 93 theme->buttonFontColor_ = pattern->GetAttr<Color>("text_primary_activated_color", Color::WHITE); 94 theme->fontPrimaryColor_ = pattern->GetAttr<Color>("text_primary_color", Color::WHITE); 95 theme->fontSecondaryColor_ = pattern->GetAttr<Color>("text_secondary_color", Color::WHITE); 96 } 97 }; 98 99 ~PopupTheme() override = default; 100 GetPadding()101 const Edge& GetPadding() const 102 { 103 return padding_; 104 } 105 GetMaskColor()106 const Color& GetMaskColor() const 107 { 108 return maskColor_; 109 } 110 GetBackgroundColor()111 const Color& GetBackgroundColor() const 112 { 113 return backgroundColor_; 114 } 115 GetButtonHoverColor()116 const Color& GetButtonHoverColor() const 117 { 118 return buttonHoverColor_; 119 } 120 GetButtonBackgroundColor()121 const Color& GetButtonBackgroundColor() const 122 { 123 return buttonBackgroundColor_; 124 } 125 GetButtonPressColor()126 const Color& GetButtonPressColor() const 127 { 128 return buttonPressColor_; 129 } 130 GetFocusColor()131 const Color& GetFocusColor() const 132 { 133 return focusColor_; 134 } 135 GetTextStyle()136 const TextStyle& GetTextStyle() const 137 { 138 return textStyle_; 139 } 140 GetFontSize()141 const Dimension& GetFontSize() const 142 { 143 return fontSize_; 144 } 145 GetButtonFontSize()146 const Dimension& GetButtonFontSize() const 147 { 148 return buttonFontSize_; 149 } 150 GetFontColor()151 const Color& GetFontColor() const 152 { 153 return fontColor_; 154 } 155 GetRadius()156 const Radius& GetRadius() const 157 { 158 return radius_; 159 } 160 GetShowTime()161 uint32_t GetShowTime() const 162 { 163 return showTime_; 164 } 165 GetHideTime()166 uint32_t GetHideTime() const 167 { 168 return hideTime_; 169 } 170 GetTargetSpace()171 const Dimension& GetTargetSpace() const 172 { 173 return targetSpace_; 174 } 175 GetBubbleSpacing()176 const Dimension& GetBubbleSpacing() const 177 { 178 return bubbleSpacing_; 179 } 180 GetAgingTextLeftPadding()181 const Dimension& GetAgingTextLeftPadding() const 182 { 183 return ageTextLeftPadding_; 184 } 185 GetAgingTextRightPadding()186 const Dimension& GetAgingTextRightPadding() const 187 { 188 return ageTextRightPadding_; 189 } 190 GetAgingButtonTextLeftPadding()191 const Dimension& GetAgingButtonTextLeftPadding() const 192 { 193 return ageButtonTextLeftPadding_; 194 } 195 GetAgingButtonTextRightPadding()196 const Dimension& GetAgingButtonTextRightPadding() const 197 { 198 return ageButtonTextRightPadding_; 199 } 200 GetAgingButtonLeftPadding()201 const Dimension& GetAgingButtonLeftPadding() const 202 { 203 return ageButtonLeftPadding_; 204 } 205 GetAgingButtonRightPadding()206 const Dimension& GetAgingButtonRightPadding() const 207 { 208 return ageButtonRightPadding_; 209 } 210 GetButtonTextInsideMargin()211 const Dimension& GetButtonTextInsideMargin() const 212 { 213 return buttonTextInsideMargin_; 214 } 215 GetButtonSpacing()216 const Dimension& GetButtonSpacing() const 217 { 218 return buttonSpacing; 219 } 220 GetLittlePadding()221 const Dimension& GetLittlePadding() const 222 { 223 return littlePadding_; 224 } 225 GetFocusPaintWidth()226 const Dimension& GetFocusPaintWidth() const 227 { 228 return focusPaintWidth_; 229 } 230 GetButtonMiniMumWidth()231 const Dimension& GetButtonMiniMumWidth() const 232 { 233 return buttonMiniMumWidth; 234 } 235 GetBubbleMiniMumHeight()236 const Dimension& GetBubbleMiniMumHeight() const 237 { 238 return bubbleMiniMumHeight_; 239 } 240 GetArrowHeight()241 const Dimension& GetArrowHeight() const 242 { 243 return arrowHeight_; 244 } 245 GetPopupAnimationOffset()246 float GetPopupAnimationOffset() const 247 { 248 return popupAnimationOffset_; 249 } 250 GetShowDuration()251 int32_t GetShowDuration() const 252 { 253 return popupAnimationShowDuration_; 254 } 255 GetCloseDuration()256 int32_t GetCloseDuration() const 257 { 258 return popupAnimationCloseDuration_; 259 } GetHoverAnimationDuration()260 int32_t GetHoverAnimationDuration() const 261 { 262 return hoverAnimationDuration_; 263 } GetHoverToPressAnimationDuration()264 int32_t GetHoverToPressAnimationDuration() const 265 { 266 return hoverToPressAnimationDuration_; 267 } 268 GetOpacityStart()269 float GetOpacityStart() const 270 { 271 return opacityStart_; 272 } 273 GetOpacityEnd()274 float GetOpacityEnd() const 275 { 276 return opacityEnd_; 277 } 278 GetHoverOpacity()279 float GetHoverOpacity() const 280 { 281 return opacityHover_; 282 } 283 GetPressOpacity()284 float GetPressOpacity() const 285 { 286 return opacityPress_; 287 } 288 GetPopupDoubleBorderEnable()289 int32_t GetPopupDoubleBorderEnable() const 290 { 291 return popupDoubleBorderEnable_; 292 } 293 GetPopupOuterBorderColor()294 Color GetPopupOuterBorderColor() const 295 { 296 return popupOuterBorderColor_; 297 } 298 GetPopupInnerBorderColor()299 Color GetPopupInnerBorderColor() const 300 { 301 return popupInnerBorderColor_; 302 } 303 GetButtonFontColor()304 Color GetButtonFontColor() const 305 { 306 return buttonFontColor_; 307 } 308 GetFontPrimaryColor()309 Color GetFontPrimaryColor() const 310 { 311 return fontPrimaryColor_; 312 } 313 GetFontSecondaryColor()314 Color GetFontSecondaryColor() const 315 { 316 return fontSecondaryColor_; 317 } 318 319 protected: 320 PopupTheme() = default; 321 322 private: 323 Edge padding_; 324 Color maskColor_; 325 Color backgroundColor_; 326 Color buttonHoverColor_ = Color(0x0cffffff); 327 Color buttonBackgroundColor_ = Color::TRANSPARENT; 328 Color buttonPressColor_ = Color(0x1affffff); 329 Color focusColor_ = Color::WHITE; 330 int32_t popupDoubleBorderEnable_ = 0; 331 Color popupOuterBorderColor_ = Color::TRANSPARENT; 332 Color popupInnerBorderColor_ = Color::TRANSPARENT; 333 334 TextStyle textStyle_; 335 Radius radius_; 336 uint32_t showTime_ = 0; 337 uint32_t hideTime_ = 0; 338 Dimension targetSpace_ = TARGET_SPACE; 339 Dimension fontSize_; 340 Dimension buttonFontSize_ = 14.0_fp; 341 Color fontColor_; 342 Dimension bubbleSpacing_ = 8.0_vp; 343 Dimension ageTextLeftPadding_ = 12.0_vp; 344 Dimension ageTextRightPadding_ = 12.0_vp; 345 Dimension ageButtonTextLeftPadding_ = 12.0_vp; 346 Dimension ageButtonTextRightPadding_ = 16.0_vp; 347 Dimension ageButtonLeftPadding_ = 0.0_vp; 348 Dimension ageButtonRightPadding_ = 0.0_vp; 349 Dimension buttonTextInsideMargin_ = 8.0_vp; 350 Dimension buttonSpacing = 4.0_vp; 351 Dimension littlePadding_ = 4.0_vp; 352 Dimension arrowHeight_ = 8.0_vp; 353 Dimension focusPaintWidth_ = 2.0_vp; 354 Dimension buttonMiniMumWidth = 72.0_vp; 355 Dimension bubbleMiniMumHeight_ = 48.0_vp; 356 float popupAnimationOffset_ = 8.0f; 357 int32_t popupAnimationShowDuration_ = 250; 358 int32_t popupAnimationCloseDuration_ = 100; 359 int32_t hoverAnimationDuration_ = 250; 360 int32_t hoverToPressAnimationDuration_ = 100; 361 float opacityStart_ = 0.0f; 362 float opacityEnd_ = 1.0f; 363 float opacityHover_ = 0.05f; 364 float opacityPress_ = 0.1f; 365 Color buttonFontColor_; 366 Color fontPrimaryColor_; 367 Color fontSecondaryColor_; 368 }; 369 370 } // namespace OHOS::Ace 371 372 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_POPUP_POPUP_THEME_H 373