1 /*
2 * Copyright (c) 2024 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 #include "core/components/checkable/checkable_theme.h"
17
18 namespace OHOS::Ace {
SetCheckboxFocus(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<CheckboxTheme> & theme) const19 void CheckboxTheme::Builder::SetCheckboxFocus(const RefPtr<ThemeConstants>& themeConstants,
20 const RefPtr<CheckboxTheme>& theme) const
21 {
22 RefPtr<ThemeStyle> checkboxPattern = themeConstants->GetPatternByName(THEME_PATTERN_CHECKBOX);
23 if (!checkboxPattern) {
24 LOGE("Pattern of checkbox is null, please check!");
25 return;
26 }
27 theme->focusBoardColor_ = checkboxPattern->GetAttr<Color>("color_focused_bg", Color(0xffff0000));
28 theme->focusBoardSize_ = checkboxPattern->GetAttr<Dimension>("size_focused_bg", 2.0_vp);
29 theme->borderFocusedColor_ = checkboxPattern->GetAttr<Color>("focused_border_color", Color(0xffff0000));
30 theme->focusedBGColorUnselected_ =
31 checkboxPattern->GetAttr<Color>("focused_bg_color_unselected", Color(0xffff0000));
32 theme->roundFocusBoardSize_ = checkboxPattern->GetAttr<Dimension>("round_size_focused_bg", 2.0_vp);
33 theme->whiteBorderRadius_ = checkboxPattern->GetAttr<Dimension>("focus_white_border_radius", 0.0_vp);
34 theme->focusLineColor_ = checkboxPattern->GetAttr<Color>("checkbox_focus_color_sys", Color(0xff007dff));
35 theme->defaultRoundPaddingSize_ =
36 checkboxPattern->GetAttr<Dimension>("checkbox_default_round_padding_size", 2.0_vp);
37 }
38
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<CheckboxTheme> & theme) const39 void CheckboxTheme::Builder::ParsePattern(const RefPtr<ThemeConstants>& themeConstants,
40 const RefPtr<CheckboxTheme>& theme) const
41 {
42 RefPtr<ThemeStyle> checkboxPattern = themeConstants->GetPatternByName(THEME_PATTERN_CHECKBOX);
43 if (!checkboxPattern) {
44 LOGE("Pattern of checkbox is null, please check!");
45 return;
46 }
47 theme->width_ = checkboxPattern->GetAttr<Dimension>("checkbox_size", 0.0_vp);
48 theme->height_ = theme->width_;
49 theme->hotZoneHorizontalPadding_ = checkboxPattern->GetAttr<Dimension>("checkbox_hotzone_padding", 0.0_vp);
50 theme->defaultWidth_ = checkboxPattern->GetAttr<Dimension>("checkbox_default_size", 0.0_vp);
51 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
52 theme->defaultPaddingSize_ =
53 checkboxPattern->GetAttr<Dimension>("checkbox_default_padding_size", 2.0_vp);
54 theme->defaultWidth_ = checkboxPattern->GetAttr<Dimension>("checkbox_default_size_twelve", 24.0_vp);
55 theme->hoverPaddingSize_ = checkboxPattern->GetAttr<Dimension>("checkbox_hover_padding_size", 2.0_vp);
56 theme->showCircleDial_ = static_cast<bool>(checkboxPattern->GetAttr<double>("radio_circle_dial", 0.0));
57 }
58 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_;
59 theme->defaultHeight_ = theme->defaultWidth_;
60 theme->needFocus_ = static_cast<bool>(checkboxPattern->GetAttr<double>("checkbox_need_focus", 0.0));
61 theme->backgroundSolid_ =
62 static_cast<bool>(checkboxPattern->GetAttr<double>("checkbox_inactive_background_solid", 0.0));
63 theme->borderWidth_ = checkboxPattern->GetAttr<Dimension>("checkbox_border_width", 0.0_vp);
64 theme->checkStroke_ = checkboxPattern->GetAttr<Dimension>("checkbox_stroke_width", 0.0_vp);
65 theme->shadowColor_ = checkboxPattern->GetAttr<Color>("checkbox_shadow_color", Color());
66 theme->shadowWidth_ = checkboxPattern->GetAttr<Dimension>("checkbox_shadow_width", 0.0_vp);
67 theme->pointColor_ = checkboxPattern->GetAttr<Color>("fg_color_checked", Color::RED);
68 theme->activeColor_ = checkboxPattern->GetAttr<Color>("bg_color_checked", Color::RED);
69 theme->inactiveColor_ = checkboxPattern->GetAttr<Color>("bg_border_color_unchecked", Color::RED);
70 theme->focusColor_ = checkboxPattern->GetAttr<Color>("focus_border_color", Color::RED);
71 theme->borderRadius_ = checkboxPattern->GetAttr<Dimension>("bg_border_radius", 0.0_vp);
72 theme->hoverColor_ = checkboxPattern->GetAttr<Color>("hover_border_color", Color::RED);
73 theme->clickEffectColor_ = checkboxPattern->GetAttr<Color>("click_effect_color", Color::RED);
74 theme->inactivePointColor_ = checkboxPattern->GetAttr<Color>("bg_color_unchecked", Color::RED);
75 theme->hoverRadius_ = checkboxPattern->GetAttr<Dimension>("hover_border_radius", 0.0_vp);
76 theme->focusRadius_ = checkboxPattern->GetAttr<Dimension>("focus_border_radius", 0.0_vp);
77 theme->focusPaintPadding_ = checkboxPattern->GetAttr<Dimension>("focus_paint_padding", 0.0_vp);
78 theme->hoverDuration_ = checkboxPattern->GetAttr<double>("hover_animation_duration", 0.0);
79 theme->hoverToTouchDuration_ = checkboxPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0);
80 theme->touchDuration_ = checkboxPattern->GetAttr<double>("touch_animation_duration", 0.0);
81 theme->colorAnimationDuration_ = checkboxPattern->GetAttr<double>("color_animation_duration", 0.0);
82 SetCheckboxFocus(themeConstants, theme);
83
84 if (SystemProperties::GetDeviceType() != DeviceType::CAR) {
85 return;
86 }
87 SetCheckboxSize(themeConstants, theme);
88 }
89
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<SwitchTheme> & theme) const90 void SwitchTheme::Builder::ParsePattern(const RefPtr<ThemeConstants>& themeConstants,
91 const RefPtr<SwitchTheme>& theme) const
92 {
93 RefPtr<ThemeStyle> switchPattern = themeConstants->GetPatternByName(THEME_PATTERN_SWITCH);
94 if (!switchPattern) {
95 LOGE("Pattern of switch is null, please check!");
96 return;
97 }
98 theme->width_ = switchPattern->GetAttr<Dimension>("switch_pattern_width", 0.0_vp);
99 theme->height_ = switchPattern->GetAttr<Dimension>("switch_pattern_height", 0.0_vp);
100 theme->hotZoneHorizontalPadding_ =
101 switchPattern->GetAttr<Dimension>("switch_hotzone_horizontal_padding", 0.0_vp);
102 theme->hotZoneVerticalPadding_ =
103 switchPattern->GetAttr<Dimension>("switch_hotzone_vertical_padding", 0.0_vp);
104 theme->hotZoneHorizontalSize_ =
105 switchPattern->GetAttr<Dimension>("switch_hotzone_horizontal_size", 6.0_vp);
106 theme->hotZoneVerticalSize_ =
107 switchPattern->GetAttr<Dimension>("switch_hotzone_vertical_size", 14.0_vp);
108 theme->aspectRatio_ = switchPattern->GetAttr<double>("switch_aspect_ratio", 0.0);
109 theme->backgroundSolid_ =
110 static_cast<bool>(switchPattern->GetAttr<double>("switch_inactive_background_solid", 0.0));
111 theme->defaultWidth_ = switchPattern->GetAttr<Dimension>("switch_default_width", 0.0_vp);
112 theme->defaultHeight_ = switchPattern->GetAttr<Dimension>("switch_default_height", 0.0_vp);
113 theme->needFocus_ = static_cast<bool>(switchPattern->GetAttr<double>("switch_need_focus", 0.0));
114 theme->borderWidth_ = switchPattern->GetAttr<Dimension>("switch_border_width", 0.0_vp);
115 theme->shadowColor_ = switchPattern->GetAttr<Color>("switch_shadow_color", Color());
116 theme->shadowWidth_ = switchPattern->GetAttr<Dimension>("switch_pattern_shadow_width", 0.0_vp);
117 theme->pointColor_ = switchPattern->GetAttr<Color>("fg_color_checked", Color::RED);
118 theme->activeColor_ = switchPattern->GetAttr<Color>("bg_color_checked", Color::RED);
119 theme->inactiveColor_ = switchPattern->GetAttr<Color>("bg_color_unchecked", Color::RED);
120 theme->focusColor_ = switchPattern->GetAttr<Color>("focus_border_color", Color::RED);
121 theme->hoverColor_ = switchPattern->GetAttr<Color>("hover_border_color", Color::RED);
122 theme->hoverRadius_ = switchPattern->GetAttr<Dimension>("hover_border_radius", 0.0_vp);
123 theme->inactivePointColor_ = switchPattern->GetAttr<Color>("fg_color_unchecked", Color::RED);
124 theme->clickEffectColor_ = switchPattern->GetAttr<Color>("click_effect_color", Color::RED);
125 theme->focusPaintPadding_ = switchPattern->GetAttr<Dimension>("focus_paint_padding", 0.0_vp);
126 theme->hoverDuration_ = switchPattern->GetAttr<double>("hover_animation_duration", 0.0);
127 theme->hoverToTouchDuration_ = switchPattern->GetAttr<double>("hover_to_press_animation_duration", 0.0);
128 theme->touchDuration_ = switchPattern->GetAttr<double>("touch_animation_duration", 0.0);
129 theme->colorAnimationDuration_ = switchPattern->GetAttr<double>("color_animation_duration", 0.0);
130 theme->pointAnimationDuration_ = switchPattern->GetAttr<double>("point_animation_duration", 0.0);
131 theme->interactiveHoverColor_ = switchPattern->GetAttr<Color>("interactive_hover", Color::RED);
132 theme->interactivePressedColor_ = switchPattern->GetAttr<Color>("interactive_pressed", Color::RED);
133 if (SystemProperties::GetDeviceType() != DeviceType::CAR) {
134 return;
135 }
136 theme->width_ = switchPattern->GetAttr<Dimension>(SWITCH_WIDTH, 40.0_vp);
137 theme->height_ = switchPattern->GetAttr<Dimension>(SWITCH_HEIGHT, 26.0_vp);
138 theme->shadowWidth_ = switchPattern->GetAttr<Dimension>(SWITCH_SHADOW_WIDTH, 2.0_vp);
139 theme->hotZoneHorizontalPadding_ = switchPattern->GetAttr<Dimension>(SWITCH_HORIZONTAL_PADDING, 4.0_vp);
140 theme->hotZoneVerticalPadding_ = switchPattern->GetAttr<Dimension>(SWITCH_VERTICAL_PADDING, 13.0_vp);
141 }
142
ParseSubStylePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<SwitchTheme> & theme) const143 void SwitchTheme::Builder::ParseSubStylePattern(const RefPtr<ThemeConstants>& themeConstants,
144 const RefPtr<SwitchTheme>& theme) const
145 {
146 RefPtr<ThemeStyle> switchPattern = themeConstants->GetPatternByName(THEME_PATTERN_SWITCH);
147 if (!switchPattern) {
148 LOGE("Pattern of switch is null, please check!");
149 return;
150 }
151 theme->focusBoardWidth_ = switchPattern->GetAttr<Dimension>("switch_focus_board_width", 0.0_vp);
152 theme->focusBoardHeight_ = switchPattern->GetAttr<Dimension>("switch_focus_board_height", 0.0_vp);
153 theme->focusBoardRadius_ = switchPattern->GetAttr<Dimension>("switch_focus_board_radius", 0.0_vp);
154 theme->focusBoardColor_ = switchPattern->GetAttr<Color>("switch_focus_board_color", Color());
155 theme->focusedBGColorUnselected_ =
156 switchPattern->GetAttr<Color>("switch_focus_bgColor_unselected", Color());
157 theme->focusedPointColorUnselected_ =
158 switchPattern->GetAttr<Color>("switch_focus_pointColor_unselected", Color());
159 theme->pointColorUnchecked_ = switchPattern->GetAttr<Color>("switch_unchecked_pointColor", Color());
160 theme->isUseDiffPointColor_ =
161 static_cast<bool>(switchPattern->GetAttr<double>("switch_different_pointColor", 0.0));
162 theme->focusPadding_ = switchPattern->GetAttr<Dimension>("switch_focus_box_padding", 0.0_vp);
163 }
164
SetCheckboxSize(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<CheckboxTheme> & theme) const165 void CheckboxTheme::Builder::SetCheckboxSize(const RefPtr<ThemeConstants>& themeConstants,
166 const RefPtr<CheckboxTheme>& theme) const
167 {
168 RefPtr<ThemeStyle> checkboxPattern = themeConstants->GetPatternByName(THEME_PATTERN_CHECKBOX);
169 // width/height/borderRadius not exist in theme
170 theme->width_ = checkboxPattern->GetAttr<Dimension>("width", 26.0_vp);
171 theme->height_ = theme->width_;
172 theme->borderRadius_ = checkboxPattern->GetAttr<Dimension>("hover_border_radius", 4.0_vp);
173 theme->hotZoneHorizontalPadding_ =
174 checkboxPattern->GetAttr<Dimension>("hotzone_padding_horizontal", 11.0_vp);
175 theme->hotZoneVerticalPadding_ = theme->hotZoneHorizontalPadding_;
176 }
177
178 } // namespace OHOS::Ace
179