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 double DEFAULT_OPACITY = 0.95; 33 } // namespace 34 35 /** 36 * PopupTheme defines color and styles of PopupComponent. PopupTheme should be built 37 * using PopupTheme::Builder. 38 */ 39 class PopupTheme : public virtual Theme { 40 DECLARE_ACE_TYPE(PopupTheme, Theme); 41 42 public: 43 class Builder { 44 public: 45 Builder() = default; 46 ~Builder() = default; 47 Build(const RefPtr<ThemeConstants> & themeConstants)48 RefPtr<PopupTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 49 { 50 RefPtr<PopupTheme> theme = AceType::Claim(new PopupTheme()); 51 if (!themeConstants) { 52 return theme; 53 } 54 // init theme from global data 55 theme->padding_ = Edge(themeConstants->GetDimension(THEME_POPUP_PADDING_HORIZONTAL), 56 themeConstants->GetDimension(THEME_POPUP_PADDING_VERTICAL), 57 themeConstants->GetDimension(THEME_POPUP_PADDING_HORIZONTAL), 58 themeConstants->GetDimension(THEME_POPUP_PADDING_VERTICAL)); 59 theme->maskColor_ = themeConstants->GetColor(THEME_POPUP_MASK_COLOR); 60 theme->backgroundColor_ = 61 themeConstants->GetColor(THEME_POPUP_BACKGROUND_COLOR).ChangeOpacity(DEFAULT_OPACITY); 62 theme->textStyle_.SetTextColor(themeConstants->GetColor(THEME_POPUP_TEXT_COLOR)); 63 theme->textStyle_.SetFontSize(themeConstants->GetDimension(THEME_POPUP_TEXT_FONTSIZE)); 64 theme->radius_ = Radius( 65 themeConstants->GetDimension(THEME_POPUP_RADIUS), themeConstants->GetDimension(THEME_POPUP_RADIUS)); 66 ParsePattern(themeConstants->GetThemeStyle(), theme); 67 theme->showTime_ = SHOW_TIME; 68 theme->hideTime_ = HIDE_TIME; 69 theme->targetSpace_ = TARGET_SPACE; 70 return theme; 71 } 72 73 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<PopupTheme> & theme)74 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<PopupTheme>& theme) const 75 { 76 if (!themeStyle || !theme) { 77 return; 78 } 79 auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_POPUP, nullptr); 80 if (!pattern) { 81 LOGW("find pattern of popup fail"); 82 return; 83 } 84 theme->backgroundColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR, theme->backgroundColor_); 85 theme->fontSize_ = pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 14.0_fp); 86 theme->buttonFontSize_ = pattern->GetAttr<Dimension>(POPUP_BUTTON_TEXT_FONT_SIZE, 14.0_fp); 87 theme->fontColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::WHITE); 88 theme->buttonHoverColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_HOVERED, Color()); 89 theme->buttonPressColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_PRESSED, Color()); 90 theme->focusColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_FOCUSED, Color()); 91 theme->radius_ = Radius(pattern->GetAttr<Dimension>(POPUP_BORDER_RADIUS, 24.0_vp), 92 pattern->GetAttr<Dimension>(POPUP_BORDER_RADIUS, 24.0_vp)); 93 theme->padding_ = Edge(pattern->GetAttr<Dimension>(POPUP_HORIZONTAL_PADDING, 16.0_vp), 94 pattern->GetAttr<Dimension>(POPUP_VERTICAL_PADDING, 12.0_vp), 95 pattern->GetAttr<Dimension>(POPUP_HORIZONTAL_PADDING, 16.0_vp), 96 pattern->GetAttr<Dimension>(POPUP_VERTICAL_PADDING, 12.0_vp)); 97 } 98 }; 99 100 ~PopupTheme() override = default; 101 GetPadding()102 const Edge& GetPadding() const 103 { 104 return padding_; 105 } 106 GetMaskColor()107 const Color& GetMaskColor() const 108 { 109 return maskColor_; 110 } 111 GetBackgroundColor()112 const Color& GetBackgroundColor() const 113 { 114 return backgroundColor_; 115 } 116 GetButtonHoverColor()117 const Color& GetButtonHoverColor() const 118 { 119 return buttonHoverColor_; 120 } 121 GetButtonBackgroundColor()122 const Color& GetButtonBackgroundColor() const 123 { 124 return buttonBackgroundColor_; 125 } 126 GetButtonPressColor()127 const Color& GetButtonPressColor() const 128 { 129 return buttonPressColor_; 130 } 131 GetFocusColor()132 const Color& GetFocusColor() const 133 { 134 return focusColor_; 135 } 136 GetTextStyle()137 const TextStyle& GetTextStyle() const 138 { 139 return textStyle_; 140 } 141 GetFontSize()142 const Dimension& GetFontSize() const 143 { 144 return fontSize_; 145 } 146 GetButtonFontSize()147 const Dimension& GetButtonFontSize() const 148 { 149 return buttonFontSize_; 150 } 151 GetFontColor()152 const Color& GetFontColor() const 153 { 154 return fontColor_; 155 } 156 GetRadius()157 const Radius& GetRadius() const 158 { 159 return radius_; 160 } 161 GetShowTime()162 uint32_t GetShowTime() const 163 { 164 return showTime_; 165 } 166 GetHideTime()167 uint32_t GetHideTime() const 168 { 169 return hideTime_; 170 } 171 GetTargetSpace()172 const Dimension& GetTargetSpace() const 173 { 174 return targetSpace_; 175 } 176 GetBubbleSpacing()177 const Dimension& GetBubbleSpacing() const 178 { 179 return bubbleSpacing_; 180 } 181 GetButtonTextInsideMargin()182 const Dimension& GetButtonTextInsideMargin() const 183 { 184 return buttonTextInsideMargin_; 185 } 186 GetButtonSpacing()187 const Dimension& GetButtonSpacing() const 188 { 189 return buttonSpacing; 190 } 191 GetLittlePadding()192 const Dimension& GetLittlePadding() const 193 { 194 return littlePadding_; 195 } 196 GetFocusPaintWidth()197 const Dimension& GetFocusPaintWidth() const 198 { 199 return focusPaintWidth_; 200 } 201 GetButtonMiniMumWidth()202 const Dimension& GetButtonMiniMumWidth() const 203 { 204 return buttonMiniMumWidth; 205 } 206 GetBubbleMiniMumHeight()207 const Dimension& GetBubbleMiniMumHeight() const 208 { 209 return bubbleMiniMumHeight_; 210 } 211 GetArrowHeight()212 const Dimension& GetArrowHeight() const 213 { 214 return arrowHeight_; 215 } 216 GetPopupAnimationOffset()217 float GetPopupAnimationOffset() const 218 { 219 return popupAnimationOffset_; 220 } 221 GetShowDuration()222 int32_t GetShowDuration() const 223 { 224 return popupAnimationShowDuration_; 225 } 226 GetCloseDuration()227 int32_t GetCloseDuration() const 228 { 229 return popupAnimationCloseDuration_; 230 } GetHoverAnimationDuration()231 int32_t GetHoverAnimationDuration() const 232 { 233 return hoverAnimationDuration_; 234 } GetHoverToPressAnimationDuration()235 int32_t GetHoverToPressAnimationDuration() const 236 { 237 return hoverToPressAnimationDuration_; 238 } 239 GetOpacityStart()240 float GetOpacityStart() const 241 { 242 return opacityStart_; 243 } 244 GetOpacityEnd()245 float GetOpacityEnd() const 246 { 247 return opacityEnd_; 248 } 249 GetHoverOpacity()250 float GetHoverOpacity() const 251 { 252 return opacityHover_; 253 } 254 GetPressOpacity()255 float GetPressOpacity() const 256 { 257 return opacityPress_; 258 } 259 260 protected: 261 PopupTheme() = default; 262 263 private: 264 Edge padding_; 265 Color maskColor_; 266 Color backgroundColor_; 267 Color buttonHoverColor_ = Color(0x0cffffff); 268 Color buttonBackgroundColor_ = Color::TRANSPARENT; 269 Color buttonPressColor_ = Color(0x1affffff); 270 Color focusColor_ = Color::WHITE; 271 272 TextStyle textStyle_; 273 Radius radius_; 274 uint32_t showTime_ = 0; 275 uint32_t hideTime_ = 0; 276 Dimension targetSpace_ = TARGET_SPACE; 277 Dimension fontSize_; 278 Dimension buttonFontSize_ = 14.0_fp; 279 Color fontColor_; 280 Dimension bubbleSpacing_ = 8.0_vp; 281 Dimension buttonTextInsideMargin_ = 8.0_vp; 282 Dimension buttonSpacing = 4.0_vp; 283 Dimension littlePadding_ = 4.0_vp; 284 Dimension arrowHeight_ = 8.0_vp; 285 Dimension focusPaintWidth_ = 2.0_vp; 286 Dimension buttonMiniMumWidth = 72.0_vp; 287 Dimension bubbleMiniMumHeight_ = 48.0_vp; 288 float popupAnimationOffset_ = 8.0f; 289 int32_t popupAnimationShowDuration_ = 250; 290 int32_t popupAnimationCloseDuration_ = 100; 291 int32_t hoverAnimationDuration_ = 250; 292 int32_t hoverToPressAnimationDuration_ = 100; 293 float opacityStart_ = 0.0f; 294 float opacityEnd_ = 1.0f; 295 float opacityHover_ = 0.05f; 296 float opacityPress_ = 0.1f; 297 }; 298 299 } // namespace OHOS::Ace 300 301 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_POPUP_POPUP_THEME_H 302