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