1 /*
2 * Copyright (c) 2021 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_BRIDGE_COMMON_DOM_DECLARATION_CLOCK_CLOCK_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DECLARATION_CLOCK_CLOCK_DECLARATION_H
18 
19 #include <float.h>
20 
21 #include "core/components/clock/clock_theme.h"
22 #include "core/components/declaration/common/declaration.h"
23 
24 namespace OHOS::Ace {
25 
26 struct ClockConfig final {
27     // ratio of digit-radius and half of side length of clock-face-image.
28     // digit-radius is used to calculate digit offset whose range is (0, 1].
29     // e.g., when size of clock-face-image is 200 x 200, digit "3" is [200 / 2 x 0.7 = 70] right of the center.
30     double digitRadiusRatio_ = 0.7;
31     // ratio of digit-size and side length of clock-face-image, which is used to decide font-size of digit.
32     // e.g., when size of clock-face-image is 200 x 200, font-size of digit is 200 x 0.08 = 16
33     // its range is (0, 1.0 / 7.0].
34     double digitSizeRatio_ = 0.08;
35 
36     // image sources and color for day
37     std::string clockFaceSrc_;
38     std::string hourHandSrc_;
39     std::string minuteHandSrc_;
40     std::string secondHandSrc_;
41     std::string digitColor_;
42 
43     // image sources and color for night
44     std::string clockFaceNightSrc_;
45     std::string hourHandNightSrc_;
46     std::string minuteHandNightSrc_;
47     std::string secondHandNightSrc_;
48     // "#00000000" represents transparent color
49     std::string digitColorNight_ = "#00000000";
50 };
51 
52 struct ClockAttribute : Attribute {
53     double hoursWest = DBL_MAX;
54     bool showDigit = true;
55 };
56 
57 struct ClockStyle : Style {
58     std::vector<std::string> fontFamilies;
59 };
60 
61 struct ClockEvent : Event {
62     EventMarker hourChangeEvent;
63 };
64 
65 class ClockDeclaration : public Declaration {
66     DECLARE_ACE_TYPE(ClockDeclaration, Declaration);
67 
68 public:
69     ClockDeclaration() = default;
70     ~ClockDeclaration() override = default;
71 
72     void InitializeStyle() override;
73     void SetClockConfig(const ClockConfig& clockConfig);
SetClockFaceSrc(const std::string & clockFaceSrc)74     void SetClockFaceSrc(const std::string& clockFaceSrc)
75     {
76         clockFaceSrc_ = ParseImageSrc(clockFaceSrc);
77     }
78 
GetClockFaceSrc()79     const std::string& GetClockFaceSrc() const
80     {
81         return clockFaceSrc_;
82     }
83 
SetClockFaceNightSrc(const std::string & clockFaceNightSrc)84     void SetClockFaceNightSrc(const std::string& clockFaceNightSrc)
85     {
86         clockFaceNightSrc_ = ParseImageSrc(clockFaceNightSrc);
87     }
88 
GetClockFaceNightSrc()89     const std::string& GetClockFaceNightSrc() const
90     {
91         return clockFaceNightSrc_;
92     }
93 
SetHourHandSrc(const std::string & hourHandSrc)94     void SetHourHandSrc(const std::string& hourHandSrc)
95     {
96         hourHandSrc_ = ParseImageSrc(hourHandSrc);
97     }
98 
GetHourHandSrc()99     const std::string& GetHourHandSrc() const
100     {
101         return hourHandSrc_;
102     }
103 
SetHourHandNightSrc(const std::string & hourHandNightSrc)104     void SetHourHandNightSrc(const std::string& hourHandNightSrc)
105     {
106         hourHandNightSrc_ = ParseImageSrc(hourHandNightSrc);
107     }
108 
GetHourHandNightSrc()109     const std::string& GetHourHandNightSrc() const
110     {
111         return hourHandNightSrc_;
112     }
113 
SetMinuteHandSrc(const std::string & minuteHandSrc)114     void SetMinuteHandSrc(const std::string& minuteHandSrc)
115     {
116         minuteHandSrc_ = ParseImageSrc(minuteHandSrc);
117     }
118 
GetMinuteHandSrc()119     const std::string& GetMinuteHandSrc() const
120     {
121         return minuteHandSrc_;
122     }
123 
SetMinuteHandNightSrc(const std::string & minuteHandNightSrc)124     void SetMinuteHandNightSrc(const std::string& minuteHandNightSrc)
125     {
126         minuteHandNightSrc_ = ParseImageSrc(minuteHandNightSrc);
127     }
128 
GetMinuteHandNightSrc()129     const std::string& GetMinuteHandNightSrc() const
130     {
131         return minuteHandNightSrc_;
132     }
133 
SetSecondHandSrc(const std::string & secondHandSrc)134     void SetSecondHandSrc(const std::string& secondHandSrc)
135     {
136         secondHandSrc_ = ParseImageSrc(secondHandSrc);
137     }
138 
GetSecondHandSrc()139     const std::string& GetSecondHandSrc() const
140     {
141         return secondHandSrc_;
142     }
143 
SetSecondHandNightSrc(const std::string & secondHandNightSrc)144     void SetSecondHandNightSrc(const std::string& secondHandNightSrc)
145     {
146         secondHandNightSrc_ = ParseImageSrc(secondHandNightSrc);
147     }
148 
GetSecondHandNightSrc()149     const std::string& GetSecondHandNightSrc() const
150     {
151         return secondHandNightSrc_;
152     }
153 
SetHoursWest(double hoursWest)154     void SetHoursWest(double hoursWest)
155     {
156         auto& attribute = MaybeResetAttribute<ClockAttribute>(AttributeTag::SPECIALIZED_ATTR);
157         attribute.hoursWest = hoursWest;
158     }
159 
GetHoursWest()160     double GetHoursWest() const
161     {
162         auto& attribute = static_cast<ClockAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
163         return attribute.hoursWest;
164     }
165 
SetDigitColor(const std::string & digitColor)166     void SetDigitColor(const std::string& digitColor)
167     {
168         digitColor_ = ParseColor(digitColor);
169     }
170 
GetDigitColor()171     const Color& GetDigitColor() const
172     {
173         return digitColor_;
174     }
175 
SetDigitColorNight(const std::string & digitColorNight)176     void SetDigitColorNight(const std::string& digitColorNight)
177     {
178         digitColorNight_ = ParseColor(digitColorNight);
179     }
180 
GetDigitColorNight()181     const Color& GetDigitColorNight() const
182     {
183         return digitColorNight_;
184     }
185 
SetDefaultSize(const Dimension & defaultSize)186     void SetDefaultSize(const Dimension& defaultSize)
187     {
188         defaultSize_ = defaultSize;
189     }
190 
GetDefaultSize()191     const Dimension& GetDefaultSize() const
192     {
193         return defaultSize_;
194     }
195 
SetDigitSizeRatio(double digitSizeRatio)196     void SetDigitSizeRatio(double digitSizeRatio)
197     {
198         digitSizeRatio_ = digitSizeRatio;
199     }
200 
GetDigitSizeRatio()201     double GetDigitSizeRatio() const
202     {
203         return digitSizeRatio_;
204     }
205 
SetDigitRadiusRatio(double digitRadiusRatio)206     void SetDigitRadiusRatio(double digitRadiusRatio)
207     {
208         digitRadiusRatio_ = digitRadiusRatio;
209     }
210 
GetDigitRadiusRatio()211     double GetDigitRadiusRatio() const
212     {
213         return digitRadiusRatio_;
214     }
215 
GetFontFamilies()216     const std::vector<std::string>& GetFontFamilies() const
217     {
218         auto& style = static_cast<ClockStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
219         return style.fontFamilies;
220     }
221 
SetFontFamilies(const std::vector<std::string> & fontFamilies)222     void SetFontFamilies(const std::vector<std::string>& fontFamilies)
223     {
224         auto& style = MaybeResetStyle<ClockStyle>(StyleTag::SPECIALIZED_STYLE);
225         style.fontFamilies = fontFamilies;
226     }
227 
GetShowDigit()228     bool GetShowDigit() const
229     {
230         auto& attribute = static_cast<ClockAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
231         return attribute.showDigit;
232     }
233 
SetShowDigit(bool showDigit)234     void SetShowDigit(bool showDigit)
235     {
236         auto& attribute = MaybeResetAttribute<ClockAttribute>(AttributeTag::SPECIALIZED_ATTR);
237         attribute.showDigit = showDigit;
238     }
239 
SetOnHourChangeEvent(const EventMarker & hourChangeEvent)240     void SetOnHourChangeEvent(const EventMarker& hourChangeEvent)
241     {
242         auto& clockEvent = MaybeResetEvent<ClockEvent>(EventTag::SPECIALIZED_EVENT);
243         clockEvent.hourChangeEvent = hourChangeEvent;
244     }
245 
GetOnHourChangeEvent()246     const EventMarker& GetOnHourChangeEvent() const
247     {
248         auto& clockEvent = static_cast<ClockEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
249         return clockEvent.hourChangeEvent;
250     }
251 
252 protected:
253     void InitSpecialized() override;
254     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
255     bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override;
256     bool SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event) override;
257 
258 private:
259     // hours west of Greenwich, for e.g., [hoursWest] is [-8] in  GMT+8.
260     // Valid range of [hoursWest] is [-12, 14]. Set default value to DBL_MAX to use current time zone by default.
261     Dimension defaultSize_;
262 
263     // ratio of digit-radius and half of side length of clock-face-image.
264     // digit-radius is used to calculate digit offset whose range is (0, 1].
265     // e.g., when size of clock-face-image is 200 x 200, digit "3" is [200 / 2 x 0.7 = 70] right of the center.
266     double digitRadiusRatio_ = 0.7;
267     // ratio of digit-size and side length of clock-face-image, which is used to decide font-size of digit.
268     // e.g., when size of clock-face-image is 200 x 200, font-size of digit is 200 x 0.08 = 16
269     // its range is (0, 1.0 / 7.0].
270     double digitSizeRatio_ = 0.08;
271 
272     // image sources and color for day
273     std::string clockFaceSrc_;
274     std::string hourHandSrc_;
275     std::string minuteHandSrc_;
276     std::string secondHandSrc_;
277     Color digitColor_;
278 
279     // image sources and color for night
280     std::string clockFaceNightSrc_;
281     std::string hourHandNightSrc_;
282     std::string minuteHandNightSrc_;
283     std::string secondHandNightSrc_;
284     Color digitColorNight_ = Color::TRANSPARENT;
285 };
286 
287 } // namespace OHOS::Ace
288 
289 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DECLARATION_CLOCK_CLOCK_DECLARATION_H
290