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 #include "core/components/declaration/clock/clock_declaration.h"
17 
18 #include "base/log/event_report.h"
19 #include "core/components/declaration/common/declaration_constants.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21 
22 namespace OHOS::Ace {
23 
24 using namespace Framework;
25 
26 using ClockMap = std::unordered_map<std::string, void (*)(const std::string&, ClockDeclaration&)>;
27 
InitSpecialized()28 void ClockDeclaration::InitSpecialized()
29 {
30     AddSpecializedAttribute(DeclarationConstants::DEFAULT_CLOCK_ATTR);
31     AddSpecializedStyle(DeclarationConstants::DEFAULT_CLOCK_STYLE);
32     AddSpecializedEvent(DeclarationConstants::DEFAULT_CLOCK_EVENT);
33 }
34 
InitializeStyle()35 void ClockDeclaration::InitializeStyle()
36 {
37     auto theme = GetTheme<ClockTheme>();
38     if (!theme) {
39         LOGE("ClockTheme is null!");
40         EventReport::SendComponentException(ComponentExcepType::GET_THEME_ERR);
41         return;
42     }
43     defaultSize_ = theme->GetDefaultSize();
44 }
45 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)46 bool ClockDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
47 {
48     static const ClockMap clockAttrMap = {
49         { DOM_HOURS_WEST,
50             [](const std::string& val, ClockDeclaration& declaration) {
51                 auto& clockAttr = declaration.MaybeResetAttribute<ClockAttribute>(AttributeTag::SPECIALIZED_ATTR);
52                 if (clockAttr.IsValid()) {
53                     clockAttr.hoursWest = StringToDouble(val);
54                 }
55             } },
56         { DOM_SHOW_DIGIT,
57             [](const std::string& val, ClockDeclaration& declaration) {
58                 auto& clockAttr = declaration.MaybeResetAttribute<ClockAttribute>(AttributeTag::SPECIALIZED_ATTR);
59                 if (clockAttr.IsValid()) {
60                     clockAttr.showDigit = StringToBool(val);
61                 }
62             } },
63     };
64     auto clockAttrIter = clockAttrMap.find(attr.first);
65     if (clockAttrIter != clockAttrMap.end()) {
66         clockAttrIter->second(attr.second, *this);
67         return true;
68     }
69     return false;
70 }
71 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)72 bool ClockDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
73 {
74     if (style.first == DOM_DIGIT_FONT_FAMILY) {
75         auto& clockStyle = MaybeResetStyle<ClockStyle>(StyleTag::SPECIALIZED_STYLE);
76         if (clockStyle.IsValid()) {
77             clockStyle.fontFamilies = ConvertStrToFontFamilies(style.second);
78         }
79         return true;
80     }
81     return false;
82 }
83 
SetSpecializedEvent(int32_t pageId,const std::string & eventId,const std::string & event)84 bool ClockDeclaration::SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event)
85 {
86     if (event == "hour") {
87         auto& clockEvent = MaybeResetEvent<ClockEvent>(EventTag::SPECIALIZED_EVENT);
88         if (clockEvent.IsValid()) {
89             clockEvent.hourChangeEvent = EventMarker(eventId, event, pageId);
90         }
91         return true;
92     }
93     return false;
94 }
95 
SetClockConfig(const ClockConfig & clockConfig)96 void ClockDeclaration::SetClockConfig(const ClockConfig& clockConfig)
97 {
98     digitRadiusRatio_ = clockConfig.digitRadiusRatio_;
99     digitSizeRatio_ = clockConfig.digitSizeRatio_;
100 
101     clockFaceSrc_ = ParseImageSrc(clockConfig.clockFaceSrc_);
102     hourHandSrc_ = ParseImageSrc(clockConfig.hourHandSrc_);
103     minuteHandSrc_ = ParseImageSrc(clockConfig.minuteHandSrc_);
104     secondHandSrc_ = ParseImageSrc(clockConfig.secondHandSrc_);
105     digitColor_ = ParseColor(clockConfig.digitColor_);
106 
107     clockFaceNightSrc_ = ParseImageSrc(clockConfig.clockFaceNightSrc_);
108     hourHandNightSrc_ = ParseImageSrc(clockConfig.hourHandNightSrc_);
109     minuteHandNightSrc_ = ParseImageSrc(clockConfig.minuteHandNightSrc_);
110     secondHandNightSrc_ = ParseImageSrc(clockConfig.secondHandNightSrc_);
111     digitColorNight_ = ParseColor(clockConfig.digitColorNight_);
112 }
113 
114 } // namespace OHOS::Ace
115