• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/common/container.h"
21 #include "core/components/common/properties/color.h"
22 #include "core/components/common/properties/edge.h"
23 #include "core/components/common/properties/text_style.h"
24 #include "core/components/theme/theme.h"
25 #include "core/components/theme/theme_constants.h"
26 #include "core/components/theme/theme_constants_defines.h"
27 #include "core/components/common/properties/decoration.h"
28 
29 namespace OHOS::Ace {
30 namespace {
31 constexpr uint32_t SHOW_TIME = 250; // unit is ms.
32 constexpr uint32_t HIDE_TIME = 250; // unit is ms.
33 constexpr Dimension TARGET_SPACE = 8.0_vp;
34 constexpr Dimension BORDER_RADIUS_POPUP = 20.0_vp;
35 constexpr double DEFAULT_OPACITY = 0.95;
36 } // namespace
37 
38 /**
39  * PopupTheme defines color and styles of PopupComponent. PopupTheme should be built
40  * using PopupTheme::Builder.
41  */
42 class PopupTheme : public virtual Theme {
43     DECLARE_ACE_TYPE(PopupTheme, Theme);
44 
45 public:
46     class Builder {
47     public:
48         Builder() = default;
49         ~Builder() = default;
50 
Build(const RefPtr<ThemeConstants> & themeConstants)51         RefPtr<PopupTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
52         {
53             RefPtr<PopupTheme> theme = AceType::Claim(new PopupTheme());
54             if (!themeConstants) {
55                 return theme;
56             }
57             ParsePattern(themeConstants, theme);
58             theme->showTime_ = SHOW_TIME;
59             theme->hideTime_ = HIDE_TIME;
60             return theme;
61         }
62 
63     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<PopupTheme> & theme)64         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<PopupTheme>& theme) const
65         {
66             if (!theme) {
67                 return;
68             }
69             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_POPUP);
70             if (!pattern) {
71                 LOGW("find pattern of popup fail");
72                 return;
73             }
74             theme->maskColor_ = pattern->GetAttr<Color>("popup_mask_color", Color());
75             theme->textStyle_.SetTextColor(pattern->GetAttr<Color>("popup_text_color", Color()));
76             theme->textStyle_.SetFontSize(pattern->GetAttr<Dimension>("popup_text_font_size", 0.0_vp));
77             theme->backgroundColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR, theme->backgroundColor_);
78             theme->fontSize_ = pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 14.0_fp);
79             theme->buttonFontSize_ = pattern->GetAttr<Dimension>(POPUP_BUTTON_TEXT_FONT_SIZE, 14.0_fp);
80             theme->fontColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::WHITE);
81             theme->buttonHoverColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_HOVERED, Color());
82             theme->buttonPressColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_PRESSED, Color());
83             theme->focusColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_FOCUSED, Color());
84             auto popupBorderRadius = pattern->GetAttr<Dimension>(POPUP_BORDER_RADIUS, BORDER_RADIUS_POPUP);
85             theme->radius_ = Radius(popupBorderRadius);
86             theme->padding_ = Edge(pattern->GetAttr<Dimension>(POPUP_HORIZONTAL_PADDING, 16.0_vp),
87                 pattern->GetAttr<Dimension>(POPUP_VERTICAL_PADDING, 12.0_vp),
88                 pattern->GetAttr<Dimension>(POPUP_HORIZONTAL_PADDING, 16.0_vp),
89                 pattern->GetAttr<Dimension>(POPUP_VERTICAL_PADDING, 12.0_vp));
90             auto popupDoubleBorderEnable = pattern->GetAttr<std::string>("popup_double_border_enable", "0");
91             theme->popupDoubleBorderEnable_ = StringUtils::StringToInt(popupDoubleBorderEnable);
92             theme->popupOuterBorderWidth_ = pattern->GetAttr<Dimension>("popup_outer_border_width", 0.8_vp);
93             theme->popupOuterBorderColor_ = pattern->GetAttr<Color>("popup_outer_border_color", Color::TRANSPARENT);
94             theme->popupInnerBorderWidth_ = pattern->GetAttr<Dimension>("popup_inner_border_width", 0.8_vp);
95             theme->popupInnerBorderColor_ = pattern->GetAttr<Color>("popup_inner_border_color", Color::TRANSPARENT);
96             theme->buttonFontColor_ = pattern->GetAttr<Color>("text_primary_activated_color", Color::WHITE);
97             theme->fontPrimaryColor_ = pattern->GetAttr<Color>("text_primary_color", Color::WHITE);
98             theme->fontSecondaryColor_ = pattern->GetAttr<Color>("text_secondary_color", Color::WHITE);
99             auto tipsDoubleBorderEnable = pattern->GetAttr<std::string>("tips_double_border_enable", "1");
100             theme->tipsDoubleBorderEnable_ = StringUtils::StringToInt(tipsDoubleBorderEnable);
101             theme->tipsOuterBorderWidth_ = pattern->GetAttr<Dimension>("tips_outer_border_width", 1.0_vp);
102             theme->tipsOuterBorderColor_ = pattern->GetAttr<Color>("tips_outer_border_color", Color::TRANSPARENT);
103             theme->tipsInnerBorderWidth_ = pattern->GetAttr<Dimension>("tips_inner_border_width", 1.0_vp);
104             theme->tipsInnerBorderColor_ = pattern->GetAttr<Color>("tips_inner_border_color", Color::TRANSPARENT);
105             theme->popupShadowStyle_ = static_cast<ShadowStyle>(
106                 pattern->GetAttr<int>("popup_default_shadow_style", static_cast<int>(ShadowStyle::OuterDefaultMD)));
107             theme->popupBackgroundBlurStyle_ = pattern->GetAttr<int>(
108                 "popup_background_blur_style", static_cast<int>(BlurStyle::COMPONENT_ULTRA_THICK));
109             ParseAdditionalStylePattern(pattern, theme);
110         }
ParseAdditionalStylePattern(const RefPtr<ThemeStyle> & pattern,const RefPtr<PopupTheme> & theme)111         void ParseAdditionalStylePattern(
112             const RefPtr<ThemeStyle>& pattern, const RefPtr<PopupTheme>& theme) const
113         {
114             theme->targetSpace_ = pattern->GetAttr<Dimension>("popup_target_space", TARGET_SPACE);
115             theme->defaultBGColor_ = pattern->GetAttr<Color>("popup_default_bg_color", Color::TRANSPARENT);
116             theme->borderColor_ = pattern->GetAttr<Color>("popup_border_color", Color::BLACK);
117             theme->borderWidth_ = pattern->GetAttr<Dimension>("popup_border_width", 0.0_vp);
118             theme->minHeight_ = pattern->GetAttr<Dimension>("popup_min_height", 0.0_vp);
119             theme->popupMaxColumns_ = static_cast<uint32_t>(pattern->GetAttr<double>("popup_max_columns", 0));
120             theme->bgThemeColorMode_ = static_cast<uint32_t>(pattern->GetAttr<double>("popup_bg_theme_color_mode", 0));
121             theme->bubbleMiniMumHeight_ =
122                 pattern->GetAttr<Dimension>("bubble_min_mum_height", theme->bubbleMiniMumHeight_);
123         }
124     };
125 
126     ~PopupTheme() override = default;
127 
GetPadding()128     const Edge& GetPadding() const
129     {
130         return padding_;
131     }
132 
GetMaskColor()133     const Color& GetMaskColor() const
134     {
135         return maskColor_;
136     }
137 
GetBackgroundColor()138     const Color& GetBackgroundColor() const
139     {
140         return backgroundColor_;
141     }
142 
GetButtonHoverColor()143     const Color& GetButtonHoverColor() const
144     {
145         return buttonHoverColor_;
146     }
147 
GetButtonBackgroundColor()148     const Color& GetButtonBackgroundColor() const
149     {
150         return buttonBackgroundColor_;
151     }
152 
GetButtonPressColor()153     const Color& GetButtonPressColor() const
154     {
155         return buttonPressColor_;
156     }
157 
GetFocusColor()158     const Color& GetFocusColor() const
159     {
160         return focusColor_;
161     }
162 
GetTextStyle()163     const TextStyle& GetTextStyle() const
164     {
165         return textStyle_;
166     }
167 
GetFontSize()168     const Dimension& GetFontSize() const
169     {
170         return fontSize_;
171     }
172 
GetButtonFontSize()173     const Dimension& GetButtonFontSize() const
174     {
175         return buttonFontSize_;
176     }
177 
GetFontColor()178     const Color& GetFontColor() const
179     {
180         return fontColor_;
181     }
182 
GetRadius()183     const Radius& GetRadius() const
184     {
185         return radius_;
186     }
187 
GetShowTime()188     uint32_t GetShowTime() const
189     {
190         return showTime_;
191     }
192 
GetHideTime()193     uint32_t GetHideTime() const
194     {
195         return hideTime_;
196     }
197 
GetTargetSpace()198     const Dimension& GetTargetSpace() const
199     {
200         return targetSpace_;
201     }
202 
GetBubbleSpacing()203     const Dimension& GetBubbleSpacing() const
204     {
205         return bubbleSpacing_;
206     }
207 
GetAgingTextLeftPadding()208     const Dimension& GetAgingTextLeftPadding() const
209     {
210         return ageTextLeftPadding_;
211     }
212 
GetAgingTextRightPadding()213     const Dimension& GetAgingTextRightPadding() const
214     {
215         return ageTextRightPadding_;
216     }
217 
GetAgingButtonTextLeftPadding()218     const Dimension& GetAgingButtonTextLeftPadding() const
219     {
220         return ageButtonTextLeftPadding_;
221     }
222 
GetAgingButtonTextRightPadding()223     const Dimension& GetAgingButtonTextRightPadding() const
224     {
225         return ageButtonTextRightPadding_;
226     }
227 
GetAgingButtonLeftPadding()228     const Dimension& GetAgingButtonLeftPadding() const
229     {
230         return ageButtonLeftPadding_;
231     }
232 
GetAgingButtonRightPadding()233     const Dimension& GetAgingButtonRightPadding() const
234     {
235         return ageButtonRightPadding_;
236     }
237 
GetButtonTextInsideMargin()238     const Dimension& GetButtonTextInsideMargin() const
239     {
240         return buttonTextInsideMargin_;
241     }
242 
GetButtonSpacing()243     const Dimension& GetButtonSpacing() const
244     {
245         return buttonSpacing;
246     }
247 
GetLittlePadding()248     const Dimension& GetLittlePadding() const
249     {
250         return littlePadding_;
251     }
252 
GetFocusPaintWidth()253     const Dimension& GetFocusPaintWidth() const
254     {
255         return focusPaintWidth_;
256     }
257 
GetButtonMiniMumWidth()258     const Dimension& GetButtonMiniMumWidth() const
259     {
260         return buttonMiniMumWidth;
261     }
262 
GetBubbleMiniMumHeight()263     const Dimension& GetBubbleMiniMumHeight() const
264     {
265         return bubbleMiniMumHeight_;
266     }
267 
GetArrowHeight()268     const Dimension& GetArrowHeight() const
269     {
270         return arrowHeight_;
271     }
272 
GetPopupAnimationOffset()273     float GetPopupAnimationOffset() const
274     {
275         return popupAnimationOffset_;
276     }
277 
GetShowDuration()278     int32_t GetShowDuration() const
279     {
280         return popupAnimationShowDuration_;
281     }
282 
GetCloseDuration()283     int32_t GetCloseDuration() const
284     {
285         return popupAnimationCloseDuration_;
286     }
GetHoverAnimationDuration()287     int32_t GetHoverAnimationDuration() const
288     {
289         return hoverAnimationDuration_;
290     }
GetHoverToPressAnimationDuration()291     int32_t GetHoverToPressAnimationDuration() const
292     {
293         return hoverToPressAnimationDuration_;
294     }
295 
GetOpacityStart()296     float GetOpacityStart() const
297     {
298         return opacityStart_;
299     }
300 
GetOpacityEnd()301     float GetOpacityEnd() const
302     {
303         return opacityEnd_;
304     }
305 
GetHoverOpacity()306     float GetHoverOpacity() const
307     {
308         return opacityHover_;
309     }
310 
GetPressOpacity()311     float GetPressOpacity() const
312     {
313         return opacityPress_;
314     }
315 
GetPopupDoubleBorderEnable()316     int32_t GetPopupDoubleBorderEnable() const
317     {
318         return popupDoubleBorderEnable_;
319     }
320 
GetPopupOuterBorderWidth()321     Dimension GetPopupOuterBorderWidth() const
322     {
323         return popupOuterBorderWidth_;
324     }
325 
GetPopupOuterBorderColor()326     Color GetPopupOuterBorderColor() const
327     {
328         return popupOuterBorderColor_;
329     }
330 
GetPopupInnerBorderWidth()331     Dimension GetPopupInnerBorderWidth() const
332     {
333         return popupInnerBorderWidth_;
334     }
335 
GetPopupInnerBorderColor()336     Color GetPopupInnerBorderColor() const
337     {
338         return popupInnerBorderColor_;
339     }
340 
GetTipsDoubleBorderEnable()341     int32_t GetTipsDoubleBorderEnable() const
342     {
343         return tipsDoubleBorderEnable_;
344     }
345 
GetTipsOuterBorderWidth()346     Dimension GetTipsOuterBorderWidth() const
347     {
348         return tipsOuterBorderWidth_;
349     }
350 
GetTipsOuterBorderColor()351     Color GetTipsOuterBorderColor() const
352     {
353         return tipsOuterBorderColor_;
354     }
355 
GetTipsInnerBorderWidth()356     Dimension GetTipsInnerBorderWidth() const
357     {
358         return tipsInnerBorderWidth_;
359     }
360 
GetTipsInnerBorderColor()361     Color GetTipsInnerBorderColor() const
362     {
363         return tipsInnerBorderColor_;
364     }
365 
GetButtonFontColor()366     Color GetButtonFontColor() const
367     {
368         return buttonFontColor_;
369     }
370 
GetFontPrimaryColor()371     Color GetFontPrimaryColor() const
372     {
373         return fontPrimaryColor_;
374     }
375 
GetFontSecondaryColor()376     Color GetFontSecondaryColor() const
377     {
378         return fontSecondaryColor_;
379     }
380 
GetPopupShadowStyle()381     ShadowStyle GetPopupShadowStyle() const
382     {
383         return popupShadowStyle_;
384     }
385 
GetPopupBackgroundBlurStyle()386     int GetPopupBackgroundBlurStyle() const
387     {
388         return popupBackgroundBlurStyle_;
389     }
390 
GetDefaultBGColor()391     const Color& GetDefaultBGColor() const
392     {
393         return defaultBGColor_;
394     }
395 
GetBorderColor()396     const Color& GetBorderColor() const
397     {
398         return borderColor_;
399     }
400 
GetBorderWidth()401     const Dimension& GetBorderWidth() const
402     {
403         return borderWidth_;
404     }
405 
GetMinHeight()406     const Dimension& GetMinHeight() const
407     {
408         return minHeight_;
409     }
410 
GetMaxColumns()411     uint32_t GetMaxColumns() const
412     {
413         return popupMaxColumns_;
414     }
415 
GetBgThemeColorMode()416     uint32_t GetBgThemeColorMode() const
417     {
418         return bgThemeColorMode_;
419     }
420 
421 protected:
422     PopupTheme() = default;
423 
424 private:
425     Edge padding_;
426     Color maskColor_;
427     Color backgroundColor_;
428     Color buttonHoverColor_ = Color(0x0cffffff);
429     Color buttonBackgroundColor_ = Color::TRANSPARENT;
430     Color buttonPressColor_ = Color(0x1affffff);
431     Color focusColor_ = Color::WHITE;
432     int32_t popupDoubleBorderEnable_ = 0;
433     Dimension popupOuterBorderWidth_ = 0.8_vp;
434     Color popupOuterBorderColor_ = Color::TRANSPARENT;
435     Dimension popupInnerBorderWidth_ = 0.8_vp;
436     Color popupInnerBorderColor_ = Color::TRANSPARENT;
437     int32_t tipsDoubleBorderEnable_ = 0;
438     Dimension tipsOuterBorderWidth_ = 1.0_vp;
439     Color tipsOuterBorderColor_ = Color::TRANSPARENT;
440     Dimension tipsInnerBorderWidth_ = 1.0_vp;
441     Color tipsInnerBorderColor_ = Color::TRANSPARENT;
442 
443     TextStyle textStyle_;
444     Radius radius_;
445     uint32_t showTime_ = 0;
446     uint32_t hideTime_ = 0;
447     Dimension targetSpace_ = TARGET_SPACE;
448     Dimension fontSize_;
449     Dimension buttonFontSize_ = 14.0_fp;
450     Color fontColor_;
451     Dimension bubbleSpacing_ = 8.0_vp;
452     Dimension ageTextLeftPadding_ = 12.0_vp;
453     Dimension ageTextRightPadding_ = 12.0_vp;
454     Dimension ageButtonTextLeftPadding_ = 12.0_vp;
455     Dimension ageButtonTextRightPadding_ = 16.0_vp;
456     Dimension ageButtonLeftPadding_ = 0.0_vp;
457     Dimension ageButtonRightPadding_ = 0.0_vp;
458     Dimension buttonTextInsideMargin_ = 8.0_vp;
459     Dimension buttonSpacing = 4.0_vp;
460     Dimension littlePadding_ = 4.0_vp;
461     Dimension arrowHeight_ = 8.0_vp;
462     Dimension focusPaintWidth_ = 2.0_vp;
463     Dimension buttonMiniMumWidth = 72.0_vp;
464     Dimension bubbleMiniMumHeight_ = 48.0_vp;
465     float popupAnimationOffset_ = 8.0f;
466     int32_t popupAnimationShowDuration_ = 250;
467     int32_t popupAnimationCloseDuration_ = 100;
468     int32_t hoverAnimationDuration_ = 250;
469     int32_t hoverToPressAnimationDuration_ = 100;
470     float opacityStart_ = 0.0f;
471     float opacityEnd_ = 1.0f;
472     float opacityHover_ = 0.05f;
473     float opacityPress_ = 0.1f;
474     Color buttonFontColor_;
475     Color fontPrimaryColor_;
476     Color fontSecondaryColor_;
477     ShadowStyle popupShadowStyle_ = ShadowStyle::OuterDefaultMD;
478     int popupBackgroundBlurStyle_ = static_cast<int>(BlurStyle::COMPONENT_ULTRA_THICK);
479     Color defaultBGColor_;
480     Color borderColor_;
481     Dimension borderWidth_ = 0.0_vp;
482     Dimension minHeight_ = 0.0_vp;
483     uint32_t popupMaxColumns_ = 0;
484     uint32_t bgThemeColorMode_ = 0;
485 };
486 
487 } // namespace OHOS::Ace
488 
489 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_POPUP_POPUP_THEME_H
490