1 /*
2  * Copyright (c) 2021-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_V2_PATTERN_LOCK_PATTERN_LOCK_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_PATTERN_LOCK_PATTERN_LOCK_THEME_H
18 #include "base/utils/utils.h"
19 #include "core/common/container.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components/theme/theme.h"
22 #include "core/components/theme/theme_constants.h"
23 #include "core/components/theme/theme_constants_defines.h"
24 #include "core/components/theme/theme_manager.h"
25 namespace OHOS::Ace::V2 {
26 class PatternLockTheme : public virtual Theme {
27     DECLARE_ACE_TYPE(PatternLockTheme, Theme);
28 
29 public:
30     class Builder {
31     public:
32         Builder() = default;
33         ~Builder() = default;
34 
35         static constexpr Dimension DEFAULT_SIDE_LENGTH = 288.0_vp;
36         static constexpr Dimension DEFAULT_CIRCLE_RADIUS = 6.0_vp;
37         static constexpr Dimension DEFAULT_PATH_STROKE_WIDTH = 12.0_vp;
38         static constexpr Dimension DEFAULT_ACTIVE_CIRCLE_RADIUS = 7.0_vp;
39         static constexpr Dimension DEFAULT_BACKGROUND_CIRCLE_RADIUS = 11.0_vp;
40         static constexpr Dimension DEFAULT_LIGHT_RING_CIRCLE_RADIUS_START = 6.0_vp;
41         static constexpr Dimension DEFAULT_LIGHT_RING_CIRCLE_RADIUS_END = 37.5_vp;
42         static constexpr Dimension DEFAULT_HOVER_CIRCLE_RADIUS = 11.0_vp;
43         static constexpr Dimension HOTSPOT_CIRCLE_RADIUS = 48.0_vp;
44         static constexpr Dimension FOCUS_PADDING_RADIUS = 2.0_vp;
45         static constexpr Dimension FOCUS_PAINT_WIDTH = 2.0_vp;
46 
47         static constexpr Dimension DEFAULT_SIDE_LENGTH_API9 = 300.0_vp;
48         static constexpr Dimension DEFAULT_CIRCLE_RADIUS_API9 = 14.0_vp;
49         static constexpr Dimension DEFAULT_PATH_STROKE_WIDTH_API9 = 34.0_vp;
50         static constexpr Dimension DEFAULT_ACTIVE_CIRCLE_RADIUS_API9 = 16.0_vp;
51         static constexpr Dimension DEFAULT_BACKGROUND_CIRCLE_RADIUS_API9 = 26.0_vp;
52 
Build(const RefPtr<ThemeConstants> & themeConstants)53         RefPtr<PatternLockTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
54         {
55             RefPtr<PatternLockTheme> theme = AceType::Claim(new PatternLockTheme());
56             if (!themeConstants) {
57                 return theme;
58             }
59             theme->wrongColor_ = Color::RED;
60             theme->correctColor_ = Color::BLUE;
61             theme->hoverColor_ = Color::BLACK;
62             theme->focusColor_ = Color::BLACK;
63             theme->lightRingRadiusStart_ = DEFAULT_LIGHT_RING_CIRCLE_RADIUS_START;
64             theme->lightRingRadiusEnd_ = DEFAULT_LIGHT_RING_CIRCLE_RADIUS_END;
65             theme->hoverRadius_ = DEFAULT_HOVER_CIRCLE_RADIUS;
66             theme->hotSpotCircleRadius_ = HOTSPOT_CIRCLE_RADIUS;
67             theme->focusPaddingRadius_ = FOCUS_PADDING_RADIUS;
68             theme->focusPaintWidth_ = FOCUS_PAINT_WIDTH;
69             if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TEN)) {
70                 theme->regularColor_ = Color::BLACK;
71                 theme->activeColor_ = Color::BLACK;
72                 theme->selectedColor_ = Color::BLACK;
73                 theme->pathColor_ = Color::BLUE;
74                 theme->sideLength_ = DEFAULT_SIDE_LENGTH_API9;
75                 theme->circleRadius_ = DEFAULT_CIRCLE_RADIUS_API9;
76                 theme->pathStrokeWidth_ = DEFAULT_PATH_STROKE_WIDTH_API9;
77                 theme->activeCircleRadius_ = DEFAULT_ACTIVE_CIRCLE_RADIUS_API9;
78                 theme->backgroundCircleRadius_ = DEFAULT_BACKGROUND_CIRCLE_RADIUS_API9;
79             } else {
80                 theme->sideLength_ = DEFAULT_SIDE_LENGTH;
81                 theme->circleRadius_ = DEFAULT_CIRCLE_RADIUS;
82                 theme->pathStrokeWidth_ = DEFAULT_PATH_STROKE_WIDTH;
83                 theme->activeCircleRadius_ = DEFAULT_ACTIVE_CIRCLE_RADIUS;
84                 theme->backgroundCircleRadius_ = DEFAULT_BACKGROUND_CIRCLE_RADIUS;
85                 ParsePattern(themeConstants, theme);
86             }
87             return theme;
88         }
89 
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<PatternLockTheme> & theme)90         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<PatternLockTheme>& theme) const
91         {
92             if (theme == nullptr) {
93                 return;
94             }
95             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_PATTERN_LOCK);
96             if (pattern) {
97                 theme->regularColor_ = pattern->GetAttr<Color>("regular_color", Color::BLACK);
98                 theme->activeColor_ = pattern->GetAttr<Color>("selected_color", Color::BLACK);
99                 theme->selectedColor_ = pattern->GetAttr<Color>("selected_color", Color::BLACK);
100                 theme->wrongColor_ = pattern->GetAttr<Color>("wrong_color", Color::BLACK);
101                 theme->correctColor_ = pattern->GetAttr<Color>("correct_color", Color::BLACK);
102                 theme->pathColor_ = pattern->GetAttr<Color>("path_color", Color::BLACK);
103                 theme->hoverColor_ = pattern->GetAttr<Color>("hover_color", Color::BLACK);
104                 theme->focusColor_ = pattern->GetAttr<Color>("focus_color", Color::BLACK);
105                 theme->passPointTxt_ = pattern->GetAttr<std::string>("pass_point", "");
106             }
107         }
108     };
109     ~PatternLockTheme() override = default;
GetRegularColor()110     const Color& GetRegularColor() const
111     {
112         return regularColor_;
113     }
GetSelectedColor()114     const Color& GetSelectedColor() const
115     {
116         return selectedColor_;
117     }
GetActiveColor()118     const Color& GetActiveColor() const
119     {
120         return activeColor_;
121     }
GetPathColor()122     const Color& GetPathColor() const
123     {
124         return pathColor_;
125     }
126 
GetWrongColor()127     const Color& GetWrongColor() const
128     {
129         return wrongColor_;
130     }
131 
GetCorrectColor()132     const Color& GetCorrectColor() const
133     {
134         return correctColor_;
135     }
136 
GetHoverColor()137     const Color& GetHoverColor() const
138     {
139         return hoverColor_;
140     }
141 
GetFocusColor()142     const Color& GetFocusColor() const
143     {
144         return focusColor_;
145     }
146 
GetSideLength()147     Dimension GetSideLength() const
148     {
149         return sideLength_;
150     }
151 
GetCircleRadius()152     Dimension GetCircleRadius() const
153     {
154         return circleRadius_;
155     }
156 
GetPathStrokeWidth()157     Dimension GetPathStrokeWidth() const
158     {
159         return pathStrokeWidth_;
160     }
161 
GetActiveCircleRadiusScale()162     float GetActiveCircleRadiusScale() const
163     {
164         if (!NearZero(circleRadius_.Value())) {
165             return activeCircleRadius_.Value() / circleRadius_.Value();
166         }
167         return 1;
168     }
169 
GetBackgroundRadiusScale()170     float GetBackgroundRadiusScale() const
171     {
172         if (!NearZero(circleRadius_.Value())) {
173             return backgroundCircleRadius_.Value() / circleRadius_.Value();
174         }
175         return 1;
176     }
177 
GetLightRingCircleRadiusStartScale()178     float GetLightRingCircleRadiusStartScale() const
179     {
180         if (!NearZero(circleRadius_.Value())) {
181             return lightRingRadiusStart_.Value() / circleRadius_.Value();
182         }
183         return 1;
184     }
185 
GetLightRingCircleRadiusEndScale()186     float GetLightRingCircleRadiusEndScale() const
187     {
188         if (!NearZero(circleRadius_.Value())) {
189             return lightRingRadiusEnd_.Value() / circleRadius_.Value();
190         }
191         return 1;
192     }
193 
GetHoverRadiusScale()194     float GetHoverRadiusScale() const
195     {
196         if (!NearZero(circleRadius_.Value())) {
197             return hoverRadius_.Value() / circleRadius_.Value();
198         }
199         return 1;
200     }
201 
GetHotSpotCircleRadius()202     Dimension GetHotSpotCircleRadius() const
203     {
204         return hotSpotCircleRadius_;
205     }
206 
GetFocusPaddingRadius()207     Dimension GetFocusPaddingRadius() const
208     {
209         return focusPaddingRadius_;
210     }
211 
GetFocusPaintWidth()212     Dimension GetFocusPaintWidth() const
213     {
214         return focusPaintWidth_;
215     }
216 
GetPassPointTxt()217     std::string GetPassPointTxt() const
218     {
219         return passPointTxt_;
220     }
221 protected:
222     PatternLockTheme() = default;
223 
224 private:
225     Color regularColor_;
226     Color selectedColor_;
227     Color activeColor_;
228     Color pathColor_;
229     Color wrongColor_;
230     Color correctColor_;
231     Color hoverColor_;
232     Color focusColor_;
233     Dimension sideLength_;
234     Dimension circleRadius_;
235     Dimension pathStrokeWidth_;
236     Dimension activeCircleRadius_;
237     Dimension backgroundCircleRadius_;
238     Dimension lightRingRadiusStart_;
239     Dimension lightRingRadiusEnd_;
240     Dimension hoverRadius_;
241     Dimension hotSpotCircleRadius_;
242     Dimension focusPaddingRadius_;
243     Dimension focusPaintWidth_;
244     std::string passPointTxt_;
245 };
246 } // namespace OHOS::Ace::V2
247 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_PATTERN_LOCK_PATTERN_LOCK_THEME_H
248