• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_NG_PATTERNS_MENU_MENU_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_THEME_H
18 
19 #include <cstdint>
20 
21 #include "core/components/theme/theme.h"
22 #include "frameworks/base/geometry/dimension.h"
23 
24 namespace OHOS::Ace::NG {
25 constexpr Dimension GRADIENT_HEIGHT = Dimension(50, DimensionUnit::VP);
26 
27 constexpr uint8_t GRADIENT_END_GRADIENT = 255;
28 constexpr uint32_t DEFAULT_BACKGROUND_COLOR = 0xFFFFFFF;
29 constexpr uint32_t MENU_MIN_GRID_COUNTS = 2;
30 constexpr uint32_t MENU_MAX_GRID_COUNTS = 6;
31 constexpr int32_t HOVER_IMAGE_OPACITY_CHANGE_DURATION = 150;
32 constexpr int32_t HOVER_IMAGE_DELAY_DURATION = 200;
33 constexpr int32_t HOVER_IMAGE_CUSTOM_PREVIEW_SCALE_DURATION = 650;
34 constexpr int32_t HOVER_IMAGE_PREVIEW_DISAPPEAR_DURATION = 450;
35 constexpr double OUTBORDER_RADIUS = 19.75; // Default value of outBorderRadius
36 constexpr float MENU_BIG_FONT_SIZE_SCALE = 1.75f;
37 constexpr float MENU_LARGE_FONT_SIZE_SCALE_ = 2.0f;
38 constexpr float MENU_MAX_FONT_SIZE_SCALE = 3.2f;
39 constexpr int32_t MENU_TEXT_MAX_LINES = std::numeric_limits<int32_t>::max();
40 constexpr int32_t SUB_MENU_SHOW_DELAY_DURATION = 300;
41 constexpr int32_t SUB_MENU_HIDE_DELAY_DURATION = 500;
42 
43 /**
44  * MenuTheme defines styles of menu item. MenuTheme should be built
45  * using MenuTheme::Builder.
46  */
47 class MenuTheme : public virtual Theme {
48     DECLARE_ACE_TYPE(MenuTheme, Theme);
49 
50 public:
51     class Builder {
52     public:
53         Builder() = default;
54         ~Builder() = default;
55 
Build(const RefPtr<ThemeConstants> & themeConstants)56         RefPtr<MenuTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
57         {
58             RefPtr<MenuTheme> theme = AceType::Claim(new MenuTheme());
59             if (!themeConstants) {
60                 return theme;
61             }
62             theme->symbolId_ = themeConstants->GetSymbolByName("sys.symbol.checkmark");
63             ParsePattern(themeConstants->GetThemeStyle(), theme);
64             return theme;
65         }
66 
67     private:
ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<MenuTheme> & theme)68         void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<MenuTheme>& theme) const
69         {
70             if (!themeStyle) {
71                 return;
72             }
73             auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_SELECT, nullptr);
74             if (!pattern) {
75                 LOGE("Pattern of menu is null, please check!");
76                 return;
77             }
78             theme->previewMenuMaskColor_ = pattern->GetAttr<Color>("preview_menu_mask_color", Color(0x33182431));
79             theme->bgBlurEffectEnable_ =
80                 StringUtils::StringToInt(pattern->GetAttr<std::string>("menu_bg_blur_effect_enable", "0"));
81             theme->bgEffectSaturation_ = pattern->GetAttr<double>("menu_blur_effect_saturation", 1.0);
82             theme->bgEffectBrightness_ = pattern->GetAttr<double>("menu_blur_effect_brightness", 1.0);
83             theme->bgEffectRadius_ = pattern->GetAttr<Dimension>("menu_blur_effect_radius", 0.0_vp);
84             theme->bgEffectColor_ = pattern->GetAttr<Color>("menu_blur_effect_color", Color::TRANSPARENT);
85             theme->doubleBorderEnable_ =
86                 StringUtils::StringToInt(pattern->GetAttr<std::string>("menu_double_border_enable", "0"));
87             theme->outerBorderWidth_ = pattern->GetAttr<double>("menu_outer_border_width", 1.0);
88             theme->outerBorderRadius_ = pattern->GetAttr<double>("menu_outer_border_radius", OUTBORDER_RADIUS);
89             theme->outerBorderColor_ = pattern->GetAttr<Color>("menu_outer_border_color", Color::TRANSPARENT);
90             theme->innerBorderWidth_ = pattern->GetAttr<double>("menu_inner_border_width", 1.0);
91             theme->innerBorderRadius_ = pattern->GetAttr<Dimension>("menu_inner_border_radius", 0.0_vp);
92             theme->innerBorderColor_ = pattern->GetAttr<Color>("menu_inner_border_color", Color::TRANSPARENT);
93             theme->filterAnimationDuration_ = 250;
94             theme->previewAnimationDuration_ = 300;
95             theme->hoverImageSwitchToPreviewOpacityDuration_ = HOVER_IMAGE_OPACITY_CHANGE_DURATION;
96             theme->hoverImageDelayDuration_ = HOVER_IMAGE_DELAY_DURATION;
97             theme->hoverImageCustomPreviewScaleDuration_ = HOVER_IMAGE_CUSTOM_PREVIEW_SCALE_DURATION;
98             theme->hoverImagePreviewDisappearDuration_ = HOVER_IMAGE_PREVIEW_DISAPPEAR_DURATION;
99             theme->previewBeforeAnimationScale_ = 0.95f;
100             theme->previewAfterAnimationScale_ = 1.1f;
101             theme->menuAnimationScale_ = 0.4f;
102             theme->menuDragAnimationScale_ = 0.95f;
103             theme->springMotionResponse_ = 0.416f;
104             theme->springMotionDampingFraction_ = 0.73f;
105             theme->contextMenuAppearDuration_ = 250;
106             theme->disappearDuration_ = 250;
107             theme->previewDisappearSpringMotionResponse_ = 0.304f;
108             theme->previewDisappearSpringMotionDampingFraction_ = 0.97f;
109             theme->filterRadius_ = Dimension(100.0f);
110             theme->previewBorderRadius_ = 16.0_vp;
111             theme->previewMenuScaleNumber_ = 0.95f;
112             std::string hasFilter = pattern->GetAttr<std::string>("menu_has_filter", "true");
113             theme->hasFilter_ = (hasFilter == "true");
114             theme->bigFontSizeScale_ = MENU_BIG_FONT_SIZE_SCALE;
115             theme->largeFontSizeScale_ = MENU_LARGE_FONT_SIZE_SCALE_;
116             theme->maxFontSizeScale_ = MENU_MAX_FONT_SIZE_SCALE;
117             theme->textMaxLines_ = MENU_TEXT_MAX_LINES;
118             theme->normalLayout_ = pattern->GetAttr<int>("menu_normal_layout", 1);
119             theme->normalPlacement_ = pattern->GetAttr<int>("menu_normal_placement", 1);
120             theme->hasBackBlur_ = pattern->GetAttr<int>("menu_back_blur", 1);
121             theme->enableDirectionalKeyFocus_ = pattern->GetAttr<int>("menu_focus_directional_key_enable", 0);
122             theme->menuShadowStyle_ = static_cast<ShadowStyle>(
123                 pattern->GetAttr<int>("menu_default_shadow_style", static_cast<int>(ShadowStyle::OuterDefaultMD)));
124             theme->menuBackGroundBlurStyle_ =
125                 pattern->GetAttr<int>("menu_background_blur_style", static_cast<int>(BlurStyle::COMPONENT_ULTRA_THICK));
126             theme->subMenuShowDelayDuration_ =
127                 pattern->GetAttr<int>("sub_menu_show_delay_duration", SUB_MENU_SHOW_DELAY_DURATION);
128             theme->subMenuHideDelayDuration_ =
129                 pattern->GetAttr<int>("sub_menu_hide_delay_duration", SUB_MENU_HIDE_DELAY_DURATION);
130             theme->menuHapticFeedback_ =
131                 pattern->GetAttr<std::string>("menu_haptic_feedback", "haptic.long_press_medium");
132             ParseWideScreenAttrs(theme, pattern);
133         }
134 
ParseWideScreenAttrs(const RefPtr<MenuTheme> & theme,const RefPtr<ThemeStyle> & pattern)135         void ParseWideScreenAttrs(const RefPtr<MenuTheme>& theme, const RefPtr<ThemeStyle>& pattern) const
136         {
137             theme->hasBackBlurColor_ = static_cast<bool>(pattern->GetAttr<double>("menu_back_blur_with_color", 0.0f));
138             theme->backBlurColor_ = pattern->GetAttr<Color>("menu_back_blur_color", Color::TRANSPARENT);
139             theme->borderWidth_ = pattern->GetAttr<Dimension>("menu_border_width", 0.0_vp);
140             theme->borderColor_ = pattern->GetAttr<Color>("menu_border_color", Color::BLACK);
141             theme->focusStyleType_ = pattern->GetAttr<double>("menu_focus_style_type", 0.0);
142         }
143     };
144 
145     ~MenuTheme() override = default;
146 
GetSymbolId()147     uint32_t GetSymbolId() const
148     {
149         return symbolId_;
150     }
151 
GetFilterAnimationDuration()152     int32_t GetFilterAnimationDuration() const
153     {
154         return filterAnimationDuration_;
155     }
156 
GetPreviewAnimationDuration()157     int32_t GetPreviewAnimationDuration() const
158     {
159         return previewAnimationDuration_;
160     }
161 
GetHoverImageSwitchToPreviewOpacityDuration()162     int32_t GetHoverImageSwitchToPreviewOpacityDuration() const
163     {
164         return hoverImageSwitchToPreviewOpacityDuration_;
165     }
166 
GetHoverImageDelayDuration()167     int32_t GetHoverImageDelayDuration() const
168     {
169         return hoverImageDelayDuration_;
170     }
171 
GetHoverImageCustomPreviewScaleDuration()172     int32_t GetHoverImageCustomPreviewScaleDuration() const
173     {
174         return hoverImageCustomPreviewScaleDuration_;
175     }
176 
GetHoverImagePreviewDisAppearDuration()177     int32_t GetHoverImagePreviewDisAppearDuration() const
178     {
179         return hoverImagePreviewDisappearDuration_;
180     }
181 
GetPreviewBeforeAnimationScale()182     float GetPreviewBeforeAnimationScale() const
183     {
184         return previewBeforeAnimationScale_;
185     }
186 
GetPreviewAfterAnimationScale()187     float GetPreviewAfterAnimationScale() const
188     {
189         return previewAfterAnimationScale_;
190     }
191 
GetMenuAnimationScale()192     float GetMenuAnimationScale() const
193     {
194         return menuAnimationScale_;
195     }
196 
GetMenuDragAnimationScale()197     float GetMenuDragAnimationScale() const
198     {
199         return menuDragAnimationScale_;
200     }
201 
GetSpringMotionResponse()202     float GetSpringMotionResponse() const
203     {
204         return springMotionResponse_;
205     }
206 
GetSpringMotionDampingFraction()207     float GetSpringMotionDampingFraction() const
208     {
209         return springMotionDampingFraction_;
210     }
211 
GetContextMenuAppearDuration()212     int32_t GetContextMenuAppearDuration() const
213     {
214         return contextMenuAppearDuration_;
215     }
216 
GetDisappearDuration()217     int32_t GetDisappearDuration() const
218     {
219         return disappearDuration_;
220     }
221 
GetPreviewDisappearSpringMotionResponse()222     float GetPreviewDisappearSpringMotionResponse() const
223     {
224         return previewDisappearSpringMotionResponse_;
225     }
226 
GetPreviewDisappearSpringMotionDampingFraction()227     float GetPreviewDisappearSpringMotionDampingFraction() const
228     {
229         return previewDisappearSpringMotionDampingFraction_;
230     }
231 
GetPreviewMenuScaleNumber()232     float GetPreviewMenuScaleNumber() const
233     {
234         return previewMenuScaleNumber_;
235     }
236 
GetFilterRadius()237     Dimension GetFilterRadius() const
238     {
239         return filterRadius_;
240     }
241 
GetPreviewBorderRadius()242     Dimension GetPreviewBorderRadius() const
243     {
244         return previewBorderRadius_;
245     }
246 
GetPreviewMenuMaskColor()247     Color GetPreviewMenuMaskColor() const
248     {
249         return previewMenuMaskColor_;
250     }
251 
GetBgBlurEffectEnable()252     int32_t GetBgBlurEffectEnable() const
253     {
254         return bgBlurEffectEnable_;
255     }
256 
GetBgEffectSaturation()257     double GetBgEffectSaturation() const
258     {
259         return bgEffectSaturation_;
260     }
261 
GetBgEffectBrightness()262     double GetBgEffectBrightness() const
263     {
264         return bgEffectBrightness_;
265     }
266 
GetBgEffectRadius()267     Dimension GetBgEffectRadius() const
268     {
269         return bgEffectRadius_;
270     }
271 
GetBgEffectColor()272     Color GetBgEffectColor() const
273     {
274         return bgEffectColor_;
275     }
276 
GetDoubleBorderEnable()277     int32_t GetDoubleBorderEnable() const
278     {
279         return doubleBorderEnable_;
280     }
281 
GetOuterBorderWidth()282     double GetOuterBorderWidth() const
283     {
284         return outerBorderWidth_;
285     }
286 
GetOuterBorderRadius()287     double GetOuterBorderRadius() const
288     {
289         return outerBorderRadius_;
290     }
291 
GetOuterBorderColor()292     Color GetOuterBorderColor() const
293     {
294         return outerBorderColor_;
295     }
296 
GetInnerBorderWidth()297     double GetInnerBorderWidth() const
298     {
299         return innerBorderWidth_;
300     }
301 
GetInnerBorderRadius()302     Dimension GetInnerBorderRadius() const
303     {
304         return innerBorderRadius_;
305     }
306 
GetInnerBorderColor()307     Color GetInnerBorderColor() const
308     {
309         return innerBorderColor_;
310     }
311 
GetHasFilter()312     bool GetHasFilter() const
313     {
314         return hasFilter_;
315     }
316 
GetBigFontSizeScale()317     float GetBigFontSizeScale() const
318     {
319         return bigFontSizeScale_;
320     }
321 
GetLargeFontSizeScale()322     float GetLargeFontSizeScale() const
323     {
324         return largeFontSizeScale_;
325     }
326 
GetMaxFontSizeScale()327     float GetMaxFontSizeScale() const
328     {
329         return maxFontSizeScale_;
330     }
331 
GetTextMaxLines()332     int32_t GetTextMaxLines() const
333     {
334         return textMaxLines_;
335     }
336 
GetNormalLayout()337     bool GetNormalLayout() const
338     {
339         return normalLayout_;
340     }
341 
GetNormalPlacement()342     bool GetNormalPlacement() const
343     {
344         return normalPlacement_;
345     }
346 
GetHasBackBlur()347     bool GetHasBackBlur() const
348     {
349         return hasBackBlur_;
350     }
351 
GetEnableDirectionalKeyFocus()352     bool GetEnableDirectionalKeyFocus() const
353     {
354         return enableDirectionalKeyFocus_;
355     }
356 
GetBorderColor()357     Color GetBorderColor() const
358     {
359         return borderColor_;
360     }
361 
HasBackBlurColor()362     bool HasBackBlurColor() const
363     {
364         return hasBackBlurColor_;
365     }
366 
GetBackBlurColor()367     Color GetBackBlurColor() const
368     {
369         return backBlurColor_;
370     }
371 
GetBorderWidth()372     Dimension GetBorderWidth() const
373     {
374         return borderWidth_;
375     }
376 
GetFocusStyleType()377     double GetFocusStyleType() const
378     {
379         return focusStyleType_;
380     }
381 
GetMenuShadowStyle()382     ShadowStyle GetMenuShadowStyle() const
383     {
384         return menuShadowStyle_;
385     }
386 
GetMenuBackgroundBlurStyle()387     int GetMenuBackgroundBlurStyle() const
388     {
389         return menuBackGroundBlurStyle_;
390     }
391 
GetSubMenuShowDelayDuration()392     int32_t GetSubMenuShowDelayDuration()
393     {
394         return subMenuShowDelayDuration_;
395     }
396 
GetSubMenuHideDelayDuration()397     int32_t GetSubMenuHideDelayDuration()
398     {
399         return subMenuHideDelayDuration_;
400     }
401 
GetMenuHapticFeedback()402     const std::string& GetMenuHapticFeedback() const
403     {
404         return menuHapticFeedback_;
405     }
406 
407 protected:
408     MenuTheme() = default;
409 
410 private:
411     int32_t filterAnimationDuration_ = 0;
412     int32_t previewAnimationDuration_ = 0;
413     int32_t hoverImageSwitchToPreviewOpacityDuration_ = 0;
414     int32_t hoverImageDelayDuration_ = 0;
415     int32_t hoverImageCustomPreviewScaleDuration_ = 0;
416     int32_t hoverImagePreviewDisappearDuration_ = 0;
417     int32_t subMenuShowDelayDuration_ = SUB_MENU_SHOW_DELAY_DURATION;
418     int32_t subMenuHideDelayDuration_ = SUB_MENU_HIDE_DELAY_DURATION;
419     float previewBeforeAnimationScale_ = 1.0f;
420     float previewAfterAnimationScale_ = 1.0f;
421     float menuAnimationScale_ = 1.0f;
422     float menuDragAnimationScale_ = 1.0f;
423     float springMotionResponse_ = 0.0f;
424     float springMotionDampingFraction_ = 0.0f;
425     int32_t contextMenuAppearDuration_ = 0;
426     int32_t disappearDuration_ = 0;
427     float previewDisappearSpringMotionResponse_ = 0.0f;
428     float previewDisappearSpringMotionDampingFraction_ = 0.0f;
429     float previewMenuScaleNumber_ = 0.0f;
430     Dimension filterRadius_;
431     Dimension previewBorderRadius_;
432     Color previewMenuMaskColor_;
433     int32_t bgBlurEffectEnable_ = 0;
434     double bgEffectSaturation_ = 1.0f;
435     double bgEffectBrightness_ = 1.0f;
436     Dimension bgEffectRadius_;
437     Color bgEffectColor_ = Color::TRANSPARENT;
438     int32_t doubleBorderEnable_ = 0;
439     double outerBorderWidth_ = 1.0f;
440     double outerBorderRadius_ = 19.75f;
441     Color outerBorderColor_ = Color::TRANSPARENT;
442     double innerBorderWidth_ = 1.0f;
443     Dimension innerBorderRadius_;
444     Color innerBorderColor_ = Color::TRANSPARENT;
445     uint32_t symbolId_;
446     bool hasFilter_ = true;
447     float bigFontSizeScale_ = 1.75f;
448     float largeFontSizeScale_ = 2.0f;
449     float maxFontSizeScale_ = 3.2f;
450     int32_t textMaxLines_ = std::numeric_limits<int32_t>::max();
451     bool normalLayout_ = true;
452     bool normalPlacement_ = true;
453     bool hasBackBlur_ = true;
454     bool enableDirectionalKeyFocus_ = false;
455     bool hasBackBlurColor_ = false;
456     Dimension borderWidth_;
457     Color backBlurColor_ = Color::TRANSPARENT;
458     Color borderColor_ = Color::TRANSPARENT;
459     double focusStyleType_ = 0.0;
460     ShadowStyle menuShadowStyle_ = ShadowStyle::OuterDefaultMD;
461     int menuBackGroundBlurStyle_ = static_cast<int>(BlurStyle::COMPONENT_ULTRA_THICK);
462     std::string menuHapticFeedback_;
463 };
464 
465 } // namespace OHOS::Ace::NG
466 
467 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_THEME_H
468