• 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/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 
GetButtonTextInsideMargin()181     const Dimension& GetButtonTextInsideMargin() const
182     {
183         return buttonTextInsideMargin_;
184     }
185 
GetButtonSpacing()186     const Dimension& GetButtonSpacing() const
187     {
188         return buttonSpacing;
189     }
190 
GetLittlePadding()191     const Dimension& GetLittlePadding() const
192     {
193         return littlePadding_;
194     }
195 
GetFocusPaintWidth()196     const Dimension& GetFocusPaintWidth() const
197     {
198         return focusPaintWidth_;
199     }
200 
GetButtonMiniMumWidth()201     const Dimension& GetButtonMiniMumWidth() const
202     {
203         return buttonMiniMumWidth;
204     }
205 
GetBubbleMiniMumHeight()206     const Dimension& GetBubbleMiniMumHeight() const
207     {
208         return bubbleMiniMumHeight_;
209     }
210 
GetArrowHeight()211     const Dimension& GetArrowHeight() const
212     {
213         return arrowHeight_;
214     }
215 
GetPopupAnimationOffset()216     float GetPopupAnimationOffset() const
217     {
218         return popupAnimationOffset_;
219     }
220 
GetShowDuration()221     int32_t GetShowDuration() const
222     {
223         return popupAnimationShowDuration_;
224     }
225 
GetCloseDuration()226     int32_t GetCloseDuration() const
227     {
228         return popupAnimationCloseDuration_;
229     }
GetHoverAnimationDuration()230     int32_t GetHoverAnimationDuration() const
231     {
232         return hoverAnimationDuration_;
233     }
GetHoverToPressAnimationDuration()234     int32_t GetHoverToPressAnimationDuration() const
235     {
236         return hoverToPressAnimationDuration_;
237     }
238 
GetOpacityStart()239     float GetOpacityStart() const
240     {
241         return opacityStart_;
242     }
243 
GetOpacityEnd()244     float GetOpacityEnd() const
245     {
246         return opacityEnd_;
247     }
248 
GetHoverOpacity()249     float GetHoverOpacity() const
250     {
251         return opacityHover_;
252     }
253 
GetPressOpacity()254     float GetPressOpacity() const
255     {
256         return opacityPress_;
257     }
258 
GetPopupDoubleBorderEnable()259     int32_t GetPopupDoubleBorderEnable() const
260     {
261         return popupDoubleBorderEnable_;
262     }
263 
GetPopupOuterBorderColor()264     Color GetPopupOuterBorderColor() const
265     {
266         return popupOuterBorderColor_;
267     }
268 
GetPopupInnerBorderColor()269     Color GetPopupInnerBorderColor() const
270     {
271         return popupInnerBorderColor_;
272     }
273 
GetButtonFontColor()274     Color GetButtonFontColor() const
275     {
276         return buttonFontColor_;
277     }
278 
GetFontPrimaryColor()279     Color GetFontPrimaryColor() const
280     {
281         return fontPrimaryColor_;
282     }
283 
GetFontSecondaryColor()284     Color GetFontSecondaryColor() const
285     {
286         return fontSecondaryColor_;
287     }
288 
289 protected:
290     PopupTheme() = default;
291 
292 private:
293     Edge padding_;
294     Color maskColor_;
295     Color backgroundColor_;
296     Color buttonHoverColor_ = Color(0x0cffffff);
297     Color buttonBackgroundColor_ = Color::TRANSPARENT;
298     Color buttonPressColor_ = Color(0x1affffff);
299     Color focusColor_ = Color::WHITE;
300     int32_t popupDoubleBorderEnable_ = 0;
301     Color popupOuterBorderColor_ = Color::TRANSPARENT;
302     Color popupInnerBorderColor_ = Color::TRANSPARENT;
303 
304     TextStyle textStyle_;
305     Radius radius_;
306     uint32_t showTime_ = 0;
307     uint32_t hideTime_ = 0;
308     Dimension targetSpace_ = TARGET_SPACE;
309     Dimension fontSize_;
310     Dimension buttonFontSize_ = 14.0_fp;
311     Color fontColor_;
312     Dimension bubbleSpacing_ = 8.0_vp;
313     Dimension buttonTextInsideMargin_ = 8.0_vp;
314     Dimension buttonSpacing = 4.0_vp;
315     Dimension littlePadding_ = 4.0_vp;
316     Dimension arrowHeight_ = 8.0_vp;
317     Dimension focusPaintWidth_ = 2.0_vp;
318     Dimension buttonMiniMumWidth = 72.0_vp;
319     Dimension bubbleMiniMumHeight_ = 48.0_vp;
320     float popupAnimationOffset_ = 8.0f;
321     int32_t popupAnimationShowDuration_ = 250;
322     int32_t popupAnimationCloseDuration_ = 100;
323     int32_t hoverAnimationDuration_ = 250;
324     int32_t hoverToPressAnimationDuration_ = 100;
325     float opacityStart_ = 0.0f;
326     float opacityEnd_ = 1.0f;
327     float opacityHover_ = 0.05f;
328     float opacityPress_ = 0.1f;
329     Color buttonFontColor_;
330     Color fontPrimaryColor_;
331     Color fontSecondaryColor_;
332 };
333 
334 } // namespace OHOS::Ace
335 
336 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_POPUP_POPUP_THEME_H
337