1 /*
2  * Copyright (c) 2021-2022 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/badge/badge_declaration.h"
17 
18 #include "core/components/declaration/common/declaration_constants.h"
19 #include "frameworks/bridge/common/utils/utils.h"
20 #include "frameworks/core/components/badge/badge_theme.h"
21 
22 namespace OHOS::Ace {
23 
24 using namespace Framework;
25 
InitSpecialized()26 void BadgeDeclaration::InitSpecialized()
27 {
28     AddSpecializedAttribute(DeclarationConstants::DEFAULT_BADGE_ATTR);
29     AddSpecializedStyle(DeclarationConstants::DEFAULT_BADGE_STYLE);
30     AddSpecializedEvent(DeclarationConstants::DEFAULT_BADGE_EVENT);
31 }
32 
InitializeStyle()33 void BadgeDeclaration::InitializeStyle()
34 {
35     auto badgeTheme = GetTheme<BadgeTheme>();
36     if (!badgeTheme) {
37         return;
38     }
39     SetBadgeColor(badgeTheme->GetBadgeColor());
40     SetMessageCount(badgeTheme->GetMessageCount());
41     SetBadgePosition(badgeTheme->GetBadgePosition());
42     SetBadgePositionX(badgeTheme->GetBadgePositionX());
43     SetBadgePositionY(badgeTheme->GetBadgePositionY());
44     SetShowMessage(badgeTheme->GetShowMessage());
45     SetBadgeTextColor(badgeTheme->GetBadgeTextColor());
46     SetBadgeFontSize(badgeTheme->GetBadgeFontSize());
47 }
48 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)49 bool BadgeDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
50 {
51     static const LinearMapNode<void (*)(BadgeDeclaration&, const std::string&)> badgeAttrOperators[] = {
52         { DOM_BADGE_COUNT, [](BadgeDeclaration& declaration, const std::string& value) {
53             declaration.SetMessageCount(static_cast<int64_t>(StringUtils::StringToLongInt(value)));
54             } },
55         { DOM_BADGE_LABEL,
56             [](BadgeDeclaration& declaration, const std::string& value) { declaration.SetBadgeLabel(value); } },
57         { DOM_BADGE_MAX_COUNT,
58             [](BadgeDeclaration& declaration, const std::string& value) {
59                 auto maxCount = static_cast<int64_t>(StringUtils::StringToLongInt(value));
60                 maxCount = maxCount > INT_MAX ? INT_MAX : maxCount;
61                 declaration.SetMaxCount(maxCount);
62             } },
63         { DOM_BADGE_PLACEMENT,
64             [](BadgeDeclaration& declaration, const std::string& value) {
65                 declaration.SetBadgePosition(ConvertStrToBadgePosition(value));
66             } },
67         { DOM_BADGE_VISIBLE, [](BadgeDeclaration& declaration,
68                                  const std::string& value) { declaration.SetShowMessage(StringToBool(value)); } }
69     };
70     auto operatorIter = BinarySearchFindIndex(badgeAttrOperators, ArraySize(badgeAttrOperators), attr.first.c_str());
71     if (operatorIter != -1) {
72         badgeAttrOperators[operatorIter].value(*this, attr.second);
73         return true;
74     }
75     return false;
76 }
77 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)78 bool BadgeDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
79 {
80     const static LinearMapNode<void (*)(BadgeDeclaration&, const std::string&)> badgeOperators[] = {
81         { DOM_BADGE_COLOR,
82             [](BadgeDeclaration& declaration, const std::string& value) {
83                 declaration.SetBadgeColor(declaration.ParseColor(value));
84             } },
85         { DOM_BADGE_CIRCLE_SIZE,
86             [](BadgeDeclaration& declaration, const std::string& value) {
87                 declaration.SetBadgeCircleSize(declaration.ParseDimension(value));
88             } },
89         { DOM_BADGE_TEXT_COLOR,
90             [](BadgeDeclaration& declaration, const std::string& value) {
91                 declaration.SetBadgeTextColor(declaration.ParseColor(value));
92             } },
93         { DOM_BADGE_TEXT_FONT_SIZE,
94             [](BadgeDeclaration& declaration, const std::string& value) {
95                 declaration.SetBadgeFontSize(declaration.ParseDimension(value));
96             } },
97     };
98     auto operatorIter = BinarySearchFindIndex(badgeOperators, ArraySize(badgeOperators), style.first.c_str());
99     if (operatorIter != -1) {
100         badgeOperators[operatorIter].value(*this, style.second);
101         return true;
102     }
103     return false;
104 }
105 
SetSpecializedEvent(int32_t pageId,const std::string & eventId,const std::string & event)106 bool BadgeDeclaration::SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event)
107 {
108     if (event == DOM_CLICK) {
109         EventMarker eventMarker(eventId, event, pageId);
110         eventMarker.SetCatchMode(false);
111         SetClickEvent(eventMarker);
112         return true;
113     } else if (event == DOM_CATCH_BUBBLE_CLICK) {
114         EventMarker eventMarker(eventId, event, pageId);
115         eventMarker.SetCatchMode(true);
116         SetClickEvent(eventMarker);
117         return true;
118     }
119     return false;
120 }
121 
122 } // namespace OHOS::Ace
123