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