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