1 /*
2  * Copyright (c) 2023 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_APP_BAR_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_APP_BAR_THEME_H
18 
19 #include <optional>
20 
21 #include "base/geometry/dimension.h"
22 #include "base/geometry/ng/rect_t.h"
23 #include "base/memory/ace_type.h"
24 #include "base/memory/referenced.h"
25 #include "base/utils/utils.h"
26 #include "core/components/common/properties/color.h"
27 #include "core/components/theme/theme.h"
28 #include "core/components/theme/theme_attributes.h"
29 #include "core/components/theme/theme_constants.h"
30 #include "core/components/theme/theme_style.h"
31 
32 namespace OHOS::Ace::NG {
33 
34 class AppBarTheme : public virtual Theme {
35     DECLARE_ACE_TYPE(AppBarTheme, Theme);
36 
37 public:
38     ~AppBarTheme() = default;
39 
40     class Builder {
41     public:
42         Builder() = default;
43         ~Builder() = default;
Build(const RefPtr<ThemeConstants> & themeConstants)44         RefPtr<AppBarTheme> Build(const RefPtr<ThemeConstants>& themeConstants)
45         {
46             RefPtr<AppBarTheme> theme = AceType::Claim(new AppBarTheme());
47             if (!themeConstants) {
48                 LOGE("Build AppBarTheme error, themeConstants is null!");
49                 return theme;
50             }
51             ParsePattern(themeConstants, theme);
52             return theme;
53         }
54     };
55 
GetAppBarHeight()56     const Dimension& GetAppBarHeight() const
57     {
58         return appBarHeight_;
59     }
60 
GetIconSize()61     const Dimension& GetIconSize() const
62     {
63         return iconSize_;
64     }
65 
GetIconCornerRadius()66     const Dimension& GetIconCornerRadius() const
67     {
68         return iconCornerRadius_;
69     }
70 
GetFontSize()71     const Dimension& GetFontSize() const
72     {
73         return appBarFontSize_;
74     }
75 
GetTextColor()76     const Color& GetTextColor() const
77     {
78         return textColorPrimary_;
79     }
80 
GetBgColor()81     const Color& GetBgColor() const
82     {
83         return appBarBgColor_;
84     }
85 
GetBundleName()86     const std::string& GetBundleName() const
87     {
88         return bundleName_;
89     }
90 
GetAbilityName()91     const std::string& GetAbilityName() const
92     {
93         return abilityName_;
94     }
95 
GetStageAbilityName()96     const std::string& GetStageAbilityName() const
97     {
98         return stageAbilityName_;
99     }
100 
GetAppBarRect()101     std::optional<RectF> GetAppBarRect() const
102     {
103         return std::nullopt;
104     }
105 
GetIconColorLight()106     const Color& GetIconColorLight() const
107     {
108         return iconColorLight_;
109     }
110 
GetIconColorDark()111     const Color& GetIconColorDark() const
112     {
113         return iconColorDark_;
114     }
115 
GetDividerColor()116     const Color& GetDividerColor() const
117     {
118         return dividerColor_;
119     }
120 
GetBorderColor()121     const Color& GetBorderColor() const
122     {
123         return borderColor_;
124     }
125 
GetHoverColor()126     const Color& GetHoverColor() const
127     {
128         return hoverColor_;
129     }
130 
GetClickEffectColorLight()131     const Color& GetClickEffectColorLight() const
132     {
133         return clickEffectColorLight_;
134     }
135 
GetClickEffectColorDark()136     const Color& GetClickEffectColorDark() const
137     {
138         return clickEffectColorDark_;
139     }
140 
GetFocusedOutlineColorLight()141     const Color& GetFocusedOutlineColorLight() const
142     {
143         return focusedOutlineColorLight_;
144     }
145 
GetFocusedOutlineColorDark()146     const Color& GetFocusedOutlineColorDark() const
147     {
148         return focusedOutlineColorDark_;
149     }
150 
GetBlurColorLight()151     const Color& GetBlurColorLight() const
152     {
153         return blurColorLight_;
154     }
155 
GetBlurColorDark()156     const Color& GetBlurColorDark() const
157     {
158         return blurColorDark_;
159     }
160 
GetDividerColorLight()161     const Color& GetDividerColorLight() const
162     {
163         return dividerColorLight_;
164     }
165 
GetDividerColorDark()166     const Color& GetDividerColorDark() const
167     {
168         return dividerColorDark_;
169     }
170 
GetBorderColorLight()171     const Color& GetBorderColorLight() const
172     {
173         return borderColorLight_;
174     }
175 
GetBorderColorDark()176     const Color& GetBorderColorDark() const
177     {
178         return borderColorDark_;
179     }
180 
GetBentRadius()181     const Dimension& GetBentRadius() const
182     {
183         return bentRadius_;
184     }
185 
GetRightAngle()186     const Dimension& GetRightAngle() const
187     {
188         return rightAngle_;
189     }
190 
GetMenuBarWidth()191     const Dimension& GetMenuBarWidth() const
192     {
193         return menuBarWidth_;
194     }
195 
GetMenuBarHeight()196     const Dimension& GetMenuBarHeight() const
197     {
198         return menuBarHeight_;
199     }
200 
GetNewIconSize()201     const Dimension& GetNewIconSize() const
202     {
203         return newIconSize_;
204     }
205 
GetDividerWidth()206     const Dimension& GetDividerWidth() const
207     {
208         return dividerWidth_;
209     }
210 
GetDividerHeight()211     const Dimension& GetDividerHeight() const
212     {
213         return dividerHeight_;
214     }
215 
GetBorderWidth()216     const Dimension& GetBorderWidth() const
217     {
218         return borderWidth_;
219     }
220 
GetButtonWidth()221     const Dimension& GetButtonWidth() const
222     {
223         return buttonWidth_;
224     }
225 
GetButtonHeight()226     const Dimension& GetButtonHeight() const
227     {
228         return buttonHeight_;
229     }
230 
GetFocusedOutlineWidth()231     const Dimension& GetFocusedOutlineWidth() const
232     {
233         return focusedOutlineWidth_;
234     }
235 
GetMenuBarTopMargin()236     const Dimension& GetMenuBarTopMargin() const
237     {
238         return menuBarTopMargin_;
239     }
240 
GetMenuBarLeftMargin()241     const Dimension& GetMenuBarLeftMargin() const
242     {
243         return menuBarLeftMargin_;
244     }
245 
GetMenuBarRightMargin()246     const Dimension& GetMenuBarRightMargin() const
247     {
248         return menuBarRightMargin_;
249     }
250 
GetIconOutsideMargin()251     const Dimension& GetIconOutsideMargin() const
252     {
253         return iconOutsideMargin_;
254     }
255 
GetIconInsideMargin()256     const Dimension& GetIconInsideMargin() const
257     {
258         return iconInsideMargin_;
259     }
260 
GetIconVerticalMargin()261     const Dimension& GetIconVerticalMargin() const
262     {
263         return iconVerticalMargin_;
264     }
265 
GetBlurRadius()266     Dimension GetBlurRadius() const
267     {
268         return blurRadius_;
269     }
270 
271 private:
272     AppBarTheme() = default;
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<AppBarTheme> & theme)273     static void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<AppBarTheme>& theme)
274     {
275         RefPtr<ThemeStyle> appBarPattern = themeConstants->GetPatternByName(THEME_PATTERN_APP_BAR);
276         if (!appBarPattern) {
277             LOGE("Pattern of app bar is null, please check!");
278             return;
279         }
280         initColorParameters(theme, appBarPattern);
281         theme->appBarHeight_ = appBarPattern->GetAttr<Dimension>("app_bar_height", 0.0_vp);
282         theme->iconSize_ = appBarPattern->GetAttr<Dimension>("icon_size", 0.0_vp);
283         theme->iconCornerRadius_ = appBarPattern->GetAttr<Dimension>("icon_corner_radius", 0.0_vp);
284         theme->appBarFontSize_ = appBarPattern->GetAttr<Dimension>("app_bar_font_size", 0.0_vp);
285         theme->bundleName_ = appBarPattern->GetAttr<std::string>("app_bar_bundle_name", "com.ohos.hag.famanager");
286         theme->abilityName_ = appBarPattern->GetAttr<std::string>("app_bar_ability_name", "FaPanelAbility");
287         theme->stageAbilityName_ = appBarPattern->GetAttr<std::string>("app_bar_stage_ability_name", "PanelAbility");
288         theme->bentRadius_ = appBarPattern->GetAttr<Dimension>("bent_radius", 18.0_vp);
289         theme->rightAngle_ = appBarPattern->GetAttr<Dimension>("right_angle", 0.0_vp);
290         theme->menuBarWidth_ = appBarPattern->GetAttr<Dimension>("menu_bar_width", 80.0_vp);
291         theme->menuBarHeight_ = appBarPattern->GetAttr<Dimension>("menu_bar_height", 36.0_vp);
292         theme->newIconSize_ = appBarPattern->GetAttr<Dimension>("new_icon_size", 20.0_vp);
293         theme->dividerWidth_ = appBarPattern->GetAttr<Dimension>("divider_width", 1.0_px);
294         theme->dividerHeight_ = appBarPattern->GetAttr<Dimension>("divider_height", 16.0_vp);
295         theme->borderWidth_ = appBarPattern->GetAttr<Dimension>("border_width", 1.0_px);
296         theme->buttonWidth_ = appBarPattern->GetAttr<Dimension>("button_width", 40.0_vp);
297         theme->buttonHeight_ = appBarPattern->GetAttr<Dimension>("button_height", 36.0_vp);
298         theme->focusedOutlineWidth_ = appBarPattern->GetAttr<Dimension>("focused_outline_width", 2.0_vp);
299         theme->menuBarTopMargin_ = appBarPattern->GetAttr<Dimension>("menu_bar_top_margin", 10.0_vp);
300         theme->menuBarLeftMargin_ = appBarPattern->GetAttr<Dimension>("menu_bar_left_margin", 12.0_vp);
301         theme->menuBarRightMargin_ = appBarPattern->GetAttr<Dimension>("menu_bar_right_margin", 8.0_vp);
302         theme->iconOutsideMargin_ = appBarPattern->GetAttr<Dimension>("icon_outside_margin", 12.0_vp);
303         theme->iconInsideMargin_ = appBarPattern->GetAttr<Dimension>("icon_inside_margin", 8.0_vp);
304         theme->iconVerticalMargin_ = appBarPattern->GetAttr<Dimension>("icon_vertical_margin", 8.0_vp);
305         theme->blurRadius_ = appBarPattern->GetAttr<Dimension>("blur_radius", 5.0_vp);
306     }
307 
initColorParameters(const RefPtr<AppBarTheme> & theme,const RefPtr<ThemeStyle> & appBarPattern)308     static void initColorParameters(const RefPtr<AppBarTheme>& theme, const RefPtr<ThemeStyle>& appBarPattern)
309     {
310         theme->textColorPrimary_ = appBarPattern->GetAttr<Color>("text_color_primary", Color());
311         theme->appBarBgColor_ = appBarPattern->GetAttr<Color>("app_bar_bg_color", Color());
312         theme->iconColorLight_ = appBarPattern->GetAttr<Color>("icon_color_light", Color(0xff182431));
313         theme->iconColorDark_ = appBarPattern->GetAttr<Color>("icon_color_dark", Color(0xdbffffff));
314         theme->dividerColor_ = appBarPattern->GetAttr<Color>("divider_color", Color());
315         theme->borderColor_ = appBarPattern->GetAttr<Color>("border_color", Color());
316         theme->hoverColor_ = appBarPattern->GetAttr<Color>("hover_color", Color());
317         theme->clickEffectColorLight_ = appBarPattern->GetAttr<Color>("click_effect_color_light", Color(0x19182431));
318         theme->clickEffectColorDark_ = appBarPattern->GetAttr<Color>("click_effect_color_Dark", Color(0x26ffffff));
319         theme->focusedOutlineColorLight_ = appBarPattern->GetAttr<Color>("focused_outline_color_light",
320             Color(0xff007dff));
321         theme->focusedOutlineColorDark_ = appBarPattern->GetAttr<Color>("focused_outline_color_dark",
322             Color(0xff3f97e9));
323         theme->blurColorLight_ = appBarPattern->GetAttr<Color>("blur_color_light", Color(0x99FFFFFF));
324         theme->blurColorDark_ = appBarPattern->GetAttr<Color>("blur_color_dark", Color(0x33000000));
325         theme->dividerColorLight_ = appBarPattern->GetAttr<Color>("divider_color_light", Color(0x33000000));
326         theme->dividerColorDark_ = appBarPattern->GetAttr<Color>("divider_color_dark", Color(0x33FFFFFF));
327         theme->borderColorLight_ = appBarPattern->GetAttr<Color>("border_color_light", Color(0x33182431));
328         theme->borderColorDark_ = appBarPattern->GetAttr<Color>("border_color_dark", Color(0x4DFFFFFF));
329     }
330 
331     Dimension appBarHeight_;
332     Dimension iconSize_;
333     Dimension iconCornerRadius_;
334     Dimension appBarFontSize_;
335     Color textColorPrimary_;
336     Color appBarBgColor_;
337     std::string bundleName_;
338     std::string abilityName_;
339     std::string stageAbilityName_;
340 
341     Color iconColorLight_;
342     Color iconColorDark_;
343     Color dividerColor_;
344     Color borderColor_;
345     Color hoverColor_;
346     Color clickEffectColorLight_;
347     Color clickEffectColorDark_;
348     Color focusedOutlineColorLight_;
349     Color focusedOutlineColorDark_;
350     Color blurColorLight_;
351     Color blurColorDark_;
352     Color dividerColorLight_;
353     Color dividerColorDark_;
354     Color borderColorLight_;
355     Color borderColorDark_;
356     Dimension bentRadius_;
357     Dimension rightAngle_;
358     Dimension menuBarWidth_;
359     Dimension menuBarHeight_;
360     Dimension newIconSize_;
361     Dimension dividerWidth_;
362     Dimension dividerHeight_;
363     Dimension borderWidth_;
364     Dimension buttonWidth_;
365     Dimension buttonHeight_;
366     Dimension focusedOutlineWidth_;
367     Dimension menuBarTopMargin_;
368     Dimension menuBarLeftMargin_;
369     Dimension menuBarRightMargin_;
370     Dimension iconOutsideMargin_;
371     Dimension iconInsideMargin_;
372     Dimension iconVerticalMargin_;
373     Dimension blurRadius_;
374 };
375 } // namespace OHOS::Ace::NG
376 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_APP_BAR_THEME_H