• 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_CHECKABLE_CHECKABLE_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_CHECKABLE_THEME_H
18 
19 #include "base/utils/system_properties.h"
20 #include "core/components/theme/theme.h"
21 #include "core/components/theme/theme_constants.h"
22 #include "core/components/theme/theme_constants_defines.h"
23 
24 namespace OHOS::Ace {
25 
26 class CheckableTheme : public virtual Theme {
27     DECLARE_ACE_TYPE(CheckableTheme, Theme);
28 
29 public:
30     ~CheckableTheme() override = default;
31 
GetPointColor()32     const Color& GetPointColor() const
33     {
34         return pointColor_;
35     }
GetActiveColor()36     const Color& GetActiveColor() const
37     {
38         return activeColor_;
39     }
GetInactiveColor()40     const Color& GetInactiveColor() const
41     {
42         return inactiveColor_;
43     }
GetFocusColor()44     const Color& GetFocusColor() const
45     {
46         return focusColor_;
47     }
GetWidth()48     const Dimension& GetWidth() const
49     {
50         return width_;
51     }
GetHeight()52     const Dimension& GetHeight() const
53     {
54         return height_;
55     }
GetHotZoneHorizontalPadding()56     const Dimension& GetHotZoneHorizontalPadding() const
57     {
58         return hotZoneHorizontalPadding_;
59     }
GetHotZoneVerticalPadding()60     const Dimension& GetHotZoneVerticalPadding() const
61     {
62         return hotZoneVerticalPadding_;
63     }
GetAspectRatio()64     double GetAspectRatio() const
65     {
66         return aspectRatio_;
67     }
GetDefaultWidth()68     const Dimension& GetDefaultWidth() const
69     {
70         return defaultWidth_;
71     }
GetDefaultHeight()72     const Dimension& GetDefaultHeight() const
73     {
74         return defaultHeight_;
75     }
GetRadioInnerSizeRatio()76     double GetRadioInnerSizeRatio() const
77     {
78         return radioInnerSizeRatio_;
79     }
GetNeedFocus()80     bool GetNeedFocus() const
81     {
82         return needFocus_;
83     }
IsBackgroundSolid()84     bool IsBackgroundSolid() const
85     {
86         return backgroundSolid_;
87     }
GetBorderWidth()88     const Dimension& GetBorderWidth() const
89     {
90         return borderWidth_;
91     }
92 
SetHoverColor(const Color & hoverColor)93     void SetHoverColor(const Color& hoverColor)
94     {
95         hoverColor_ = hoverColor;
96     }
97 
GetHoverColor()98     const Color& GetHoverColor() const
99     {
100         return hoverColor_;
101     }
102 
SetClickEffectColor(const Color & clickEffectColor)103     void SetClickEffectColor(const Color& clickEffectColor)
104     {
105         clickEffectColor_ = clickEffectColor;
106     }
107 
GetClickEffectColor()108     const Color& GetClickEffectColor() const
109     {
110         return clickEffectColor_;
111     }
112 
GetInactivePointColor()113     const Color& GetInactivePointColor() const
114     {
115         return inactivePointColor_;
116     }
117 
GetShadowColor()118     const Color& GetShadowColor() const
119     {
120         return shadowColor_;
121     }
122 
GetShadowWidth()123     const Dimension& GetShadowWidth() const
124     {
125         return shadowWidth_;
126     }
127 
GetHoverRadius()128     const Dimension& GetHoverRadius() const
129     {
130         return hoverRadius_;
131     }
132 
GetFocusRadius()133     const Dimension& GetFocusRadius() const
134     {
135         return focusRadius_;
136     }
137 
GetFocusPaintPadding()138     const Dimension& GetFocusPaintPadding() const
139     {
140         return focusPaintPadding_;
141     }
142 
GetHoverDuration()143     double GetHoverDuration() const
144     {
145         return hoverDuration_;
146     }
147 
GetHoverToTouchDuration()148     double GetHoverToTouchDuration() const
149     {
150         return hoverToTouchDuration_;
151     }
152 
GetTouchDuration()153     double GetTouchDuration() const
154     {
155         return touchDuration_;
156     }
157 
158 protected:
159     CheckableTheme() = default;
160 
161     Color pointColor_;
162     Color activeColor_;
163     Color inactiveColor_;
164     Color inactivePointColor_;
165     Color focusColor_;
166     Color hoverColor_;
167     Color clickEffectColor_;
168     Color shadowColor_;
169     Dimension width_;
170     Dimension height_;
171     Dimension hotZoneHorizontalPadding_;
172     Dimension hotZoneVerticalPadding_;
173     Dimension defaultWidth_;
174     Dimension defaultHeight_;
175     Dimension borderWidth_;
176     Dimension shadowWidth_;
177     Dimension hoverRadius_;
178     Dimension focusRadius_;
179     Dimension focusPaintPadding_;
180     double hoverDuration_ = 0.0f;
181     double hoverToTouchDuration_ = 0.0f;
182     double touchDuration_ = 0.0f;
183     double aspectRatio_ = 1.0;
184     double radioInnerSizeRatio_ = 0.5;
185     bool needFocus_ = true;
186     bool backgroundSolid_ = true;
187     const float ratio_ = 1.8f;
188 };
189 
190 class CheckboxTheme : public CheckableTheme {
191     DECLARE_ACE_TYPE(CheckboxTheme, CheckableTheme);
192 
193 public:
194     class Builder {
195     public:
196         Builder() = default;
197         ~Builder() = default;
198 
Build(const RefPtr<ThemeConstants> & themeConstants)199         RefPtr<CheckboxTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
200         {
201             RefPtr<CheckboxTheme> theme = AceType::Claim(new CheckboxTheme());
202             if (!themeConstants) {
203                 LOGI("Build AppTheme error, themeConstants is null!");
204                 return theme;
205             }
206             ParsePattern(themeConstants, theme);
207             return theme;
208         }
209 
210     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<CheckboxTheme> & theme)211         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<CheckboxTheme>& theme) const
212         {
213             RefPtr<ThemeStyle> checkboxPattern = themeConstants->GetPatternByName(THEME_PATTERN_CHECKBOX);
214             if (!checkboxPattern) {
215                 LOGE("Pattern of checkbox is null, please check!");
216                 return;
217             }
218             theme->width_ = checkboxPattern->GetAttr<Dimension>("checkbox_size", 0.0_vp);
219             theme->height_ = theme->width_;
220             theme->hotZoneHorizontalPadding_ = checkboxPattern->GetAttr<Dimension>("checkbox_hotzone_padding", 0.0_vp);
221             theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_;
222             theme->defaultWidth_ = checkboxPattern->GetAttr<Dimension>("checkbox_default_size", 0.0_vp);
223             theme->defaultHeight_ = theme->defaultWidth_;
224             theme->needFocus_ = static_cast<bool>(checkboxPattern->GetAttr<double>("checkbox_need_focus", 0.0));
225             theme->backgroundSolid_ =
226                 static_cast<bool>(checkboxPattern->GetAttr<double>("checkbox_inactive_background_solid", 0.0));
227             theme->borderWidth_ = checkboxPattern->GetAttr<Dimension>("checkbox_border_width", 0.0_vp);
228             theme->checkStroke_ = checkboxPattern->GetAttr<Dimension>("checkbox_stroke_width", 0.0_vp);
229             theme->shadowColor_ = checkboxPattern->GetAttr<Color>("checkbox_shadow_color", Color());
230             theme->shadowWidth_ = checkboxPattern->GetAttr<Dimension>("checkbox_shadow_width", 0.0_vp);
231             theme->pointColor_ = checkboxPattern->GetAttr<Color>("fg_color_checked", Color::RED);
232             theme->activeColor_ = checkboxPattern->GetAttr<Color>("bg_color_checked", Color::RED);
233             theme->inactiveColor_ = checkboxPattern->GetAttr<Color>("bg_border_color_unchecked", Color::RED);
234             theme->focusColor_ = checkboxPattern->GetAttr<Color>("focus_border_color", Color::RED);
235             theme->borderRadius_ = checkboxPattern->GetAttr<Dimension>("bg_border_radius", 0.0_vp);
236             theme->hoverColor_ = checkboxPattern->GetAttr<Color>("hover_border_color", Color::RED);
237             theme->clickEffectColor_ = checkboxPattern->GetAttr<Color>("click_effect_color", Color::RED);
238             theme->inactivePointColor_ = checkboxPattern->GetAttr<Color>("bg_color_unchecked", Color::RED);
239             theme->hoverRadius_ = checkboxPattern->GetAttr<Dimension>("hover_border_radius", 0.0_vp);
240             theme->focusRadius_ = checkboxPattern->GetAttr<Dimension>("focus_border_radius", 0.0_vp);
241             theme->focusPaintPadding_ = checkboxPattern->GetAttr<Dimension>("focus_paint_padding", 0.0_vp);
242             theme->hoverDuration_ = checkboxPattern->GetAttr<double>("hover_animation_duration", 0.0);
243             theme->hoverToTouchDuration_ = checkboxPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0);
244             theme->touchDuration_ = checkboxPattern->GetAttr<double>("touch_animation_duration", 0.0);
245             theme->colorAnimationDuration_ = checkboxPattern->GetAttr<double>("color_animation_duration", 0.0);
246 
247             if (SystemProperties::GetDeviceType() != DeviceType::CAR) {
248                 return;
249             }
250             // width/height/borderRadius not exist in theme
251             theme->width_ = checkboxPattern->GetAttr<Dimension>("width", 26.0_vp);
252             theme->height_ = theme->width_;
253             theme->borderRadius_ = checkboxPattern->GetAttr<Dimension>("hover_border_radius", 4.0_vp);
254             theme->hotZoneHorizontalPadding_ =
255                 checkboxPattern->GetAttr<Dimension>("hotzone_padding_horizontal", 11.0_vp);
256             theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_;
257         }
258     };
259 
GetBorderRadius()260     const Dimension& GetBorderRadius() const
261     {
262         return borderRadius_;
263     }
264 
GetCheckStroke()265     const Dimension& GetCheckStroke() const
266     {
267         return checkStroke_;
268     }
269 
GetColorAnimationDuration()270     double GetColorAnimationDuration() const
271     {
272         return colorAnimationDuration_;
273     }
274 
275 private:
276     Dimension borderRadius_;
277     Dimension checkStroke_;
278     double colorAnimationDuration_ = 0.0;
279 };
280 
281 class SwitchTheme : public CheckableTheme {
282     DECLARE_ACE_TYPE(SwitchTheme, CheckableTheme);
283 
284 public:
285     class Builder {
286     public:
287         Builder() = default;
288         ~Builder() = default;
289 
Build(const RefPtr<ThemeConstants> & themeConstants)290         RefPtr<SwitchTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
291         {
292             RefPtr<SwitchTheme> theme = AceType::Claim(new SwitchTheme());
293             if (!themeConstants) {
294                 LOGE("Build AppTheme error, themeConstants is null!");
295                 return theme;
296             }
297             ParsePattern(themeConstants, theme);
298             return theme;
299         }
300 
301     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<SwitchTheme> & theme)302         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<SwitchTheme>& theme) const
303         {
304             RefPtr<ThemeStyle> switchPattern = themeConstants->GetPatternByName(THEME_PATTERN_SWITCH);
305             if (!switchPattern) {
306                 LOGE("Pattern of switch is null, please check!");
307                 return;
308             }
309             theme->width_ = switchPattern->GetAttr<Dimension>("switch_pattern_width", 0.0_vp);
310             theme->height_ = switchPattern->GetAttr<Dimension>("switch_pattern_height", 0.0_vp);
311             theme->hotZoneHorizontalPadding_ =
312                 switchPattern->GetAttr<Dimension>("switch_hotzone_horizontal_padding", 0.0_vp);
313             theme->hotZoneVerticalPadding_ =
314                 switchPattern->GetAttr<Dimension>("switch_hotzone_vertical_padding", 0.0_vp);
315             theme->aspectRatio_ = switchPattern->GetAttr<double>("switch_aspect_ratio", 0.0);
316             theme->backgroundSolid_ =
317                 static_cast<bool>(switchPattern->GetAttr<double>("switch_inactive_background_solid", 0.0));
318             theme->defaultWidth_ = switchPattern->GetAttr<Dimension>("switch_default_width", 0.0_vp);
319             theme->defaultHeight_ = switchPattern->GetAttr<Dimension>("switch_default_height", 0.0_vp);
320             theme->needFocus_ = static_cast<bool>(switchPattern->GetAttr<double>("switch_need_focus", 0.0));
321             theme->borderWidth_ = switchPattern->GetAttr<Dimension>("switch_border_width", 0.0_vp);
322             theme->shadowColor_ = switchPattern->GetAttr<Color>("switch_shadow_color", Color());
323             theme->shadowWidth_ = switchPattern->GetAttr<Dimension>("switch_pattern_shadow_width", 0.0_vp);
324             theme->pointColor_ = switchPattern->GetAttr<Color>("fg_color_checked", Color::RED);
325             theme->activeColor_ = switchPattern->GetAttr<Color>("bg_color_checked", Color::RED);
326             theme->inactiveColor_ = switchPattern->GetAttr<Color>("bg_color_unchecked", Color::RED);
327             theme->focusColor_ = switchPattern->GetAttr<Color>("focus_border_color", Color::RED);
328             theme->hoverColor_ = switchPattern->GetAttr<Color>("hover_border_color", Color::RED);
329             theme->hoverRadius_ = switchPattern->GetAttr<Dimension>("hover_border_radius", 0.0_vp);
330             theme->inactivePointColor_ = switchPattern->GetAttr<Color>("fg_color_unchecked", Color::RED);
331             theme->clickEffectColor_ = switchPattern->GetAttr<Color>("click_effect_color", Color::RED);
332             theme->focusPaintPadding_ = switchPattern->GetAttr<Dimension>("focus_paint_padding", 0.0_vp);
333             theme->hoverDuration_ = switchPattern->GetAttr<double>("hover_animation_duration", 0.0);
334             theme->hoverToTouchDuration_ = switchPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0);
335             theme->touchDuration_ = switchPattern->GetAttr<double>("touch_animation_duration", 0.0);
336             theme->colorAnimationDuration_ = switchPattern->GetAttr<double>("color_animation_duration", 0.0);
337             theme->pointAnimationDuration_ = switchPattern->GetAttr<double>("point_animation_duration", 0.0);
338 
339             if (SystemProperties::GetDeviceType() != DeviceType::CAR) {
340                 return;
341             }
342             theme->width_ = switchPattern->GetAttr<Dimension>(SWITCH_WIDTH, 40.0_vp);
343             theme->height_ = switchPattern->GetAttr<Dimension>(SWITCH_HEIGHT, 26.0_vp);
344             theme->shadowWidth_ = switchPattern->GetAttr<Dimension>(SWITCH_SHADOW_WIDTH, 2.0_vp);
345             theme->hotZoneHorizontalPadding_ = switchPattern->GetAttr<Dimension>(SWITCH_HORIZONTAL_PADDING, 4.0_vp);
346             theme->hotZoneVerticalPadding_ = switchPattern->GetAttr<Dimension>(SWITCH_VERTICAL_PADDING, 13.0_vp);
347         }
348     };
349 
GetRatio()350     float GetRatio() const
351     {
352         return ratio_;
353     }
354 
GetColorAnimationDuration()355     double GetColorAnimationDuration() const
356     {
357         return colorAnimationDuration_;
358     }
359 
GetPointAnimationDuration()360     double GetPointAnimationDuration() const
361     {
362         return pointAnimationDuration_;
363     }
364 
365 private:
366     double colorAnimationDuration_ = 0.0;
367     double pointAnimationDuration_ = 0.0;
368 };
369 
370 class RadioTheme : public CheckableTheme {
371     DECLARE_ACE_TYPE(RadioTheme, CheckableTheme);
372 
373 public:
374     class Builder {
375     public:
376         Builder() = default;
377         ~Builder() = default;
378 
Build(const RefPtr<ThemeConstants> & themeConstants)379         RefPtr<RadioTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
380         {
381             RefPtr<RadioTheme> theme = AceType::Claim(new RadioTheme());
382             if (!themeConstants) {
383                 return theme;
384             }
385             ParsePattern(themeConstants, theme);
386             return theme;
387         }
388 
389     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RadioTheme> & theme)390         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RadioTheme>& theme) const
391         {
392             RefPtr<ThemeStyle> radioPattern = themeConstants->GetPatternByName(THEME_PATTERN_RADIO);
393             if (!radioPattern) {
394                 LOGW("find pattern of radio fail");
395                 return;
396             }
397             theme->width_ = radioPattern->GetAttr<Dimension>("radio_size", 0.0_vp);
398             theme->height_ = theme->width_;
399             theme->hotZoneHorizontalPadding_ = radioPattern->GetAttr<Dimension>("radio_hotzone_padding", 0.0_vp);
400             theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_;
401             theme->defaultWidth_ = radioPattern->GetAttr<Dimension>("radio_default_size", 0.0_vp);
402             theme->defaultHeight_ = theme->defaultWidth_;
403             theme->radioInnerSizeRatio_ = radioPattern->GetAttr<double>("radio_inner_size_ratio", 0.0);
404             theme->needFocus_ = static_cast<bool>(radioPattern->GetAttr<double>("radio_need_focus", 0.0));
405             theme->backgroundSolid_ =
406                 static_cast<bool>(radioPattern->GetAttr<double>("radio_inactive_background_solid", 0.0));
407             theme->borderWidth_ = radioPattern->GetAttr<Dimension>("radio_border_width", 0.0_vp);
408             theme->shadowColor_ = radioPattern->GetAttr<Color>("radio_shadow_color", Color());
409             theme->shadowWidth_ = radioPattern->GetAttr<Dimension>("radio_shadow_width", 0.0_vp);
410             theme->pointColor_ = radioPattern->GetAttr<Color>("fg_color_checked", Color::RED);
411             theme->activeColor_ = radioPattern->GetAttr<Color>("bg_color_checked", Color::RED);
412             theme->inactiveColor_ = radioPattern->GetAttr<Color>("bg_color_unchecked", Color::RED);
413             theme->inactivePointColor_ = radioPattern->GetAttr<Color>("fg_color_unchecked", Color::RED);
414             theme->focusColor_ = radioPattern->GetAttr<Color>("focus_border_color", Color::RED);
415             theme->hoverColor_ = radioPattern->GetAttr<Color>("hover_border_color", Color::RED);
416             theme->clickEffectColor_ = radioPattern->GetAttr<Color>("click_effect_color", Color::RED);
417             theme->focusPaintPadding_ = radioPattern->GetAttr<Dimension>("focus_paint_padding", 0.0_vp);
418             theme->hoverDuration_ = radioPattern->GetAttr<double>("hover_animation_duration", 0.0);
419             theme->hoverToTouchDuration_ = radioPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0);
420             theme->touchDuration_ = radioPattern->GetAttr<double>("touch_animation_duration", 0.0);
421             if (SystemProperties::GetDeviceType() != DeviceType::CAR) {
422                 return;
423             }
424             theme->width_ = radioPattern->GetAttr<Dimension>(RADIO_WIDTH, 26.0_vp);
425             theme->height_ = theme->width_;
426             theme->hotZoneHorizontalPadding_ = radioPattern->GetAttr<Dimension>(RADIO_PADDING, 11.0_vp);
427             theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_;
428         }
429     };
430 };
431 
432 } // namespace OHOS::Ace
433 
434 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_CHECKABLE_THEME_H
435