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 SetShowMessage(badgeTheme->GetShowMessage());
44 SetBadgeTextColor(badgeTheme->GetBadgeTextColor());
45 SetBadgeFontSize(badgeTheme->GetBadgeFontSize());
46 }
47
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)48 bool BadgeDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
49 {
50 static const LinearMapNode<void (*)(BadgeDeclaration&, const std::string&)> badgeAttrOperators[] = {
51 { DOM_BADGE_COUNT, [](BadgeDeclaration& declaration, const std::string& value) {
52 declaration.SetMessageCount(static_cast<int64_t>(StringUtils::StringToLongInt(value)));
53 } },
54 { DOM_BADGE_LABEL,
55 [](BadgeDeclaration& declaration, const std::string& value) { declaration.SetBadgeLabel(value); } },
56 { DOM_BADGE_MAX_COUNT,
57 [](BadgeDeclaration& declaration, const std::string& value) {
58 auto maxCount = static_cast<int64_t>(StringUtils::StringToLongInt(value));
59 maxCount = maxCount > INT_MAX ? INT_MAX : maxCount;
60 declaration.SetMaxCount(maxCount);
61 } },
62 { DOM_BADGE_PLACEMENT,
63 [](BadgeDeclaration& declaration, const std::string& value) {
64 declaration.SetBadgePosition(ConvertStrToBadgePosition(value));
65 } },
66 { DOM_BADGE_VISIBLE, [](BadgeDeclaration& declaration,
67 const std::string& value) { declaration.SetShowMessage(StringToBool(value)); } }
68 };
69 auto operatorIter = BinarySearchFindIndex(badgeAttrOperators, ArraySize(badgeAttrOperators), attr.first.c_str());
70 if (operatorIter != -1) {
71 badgeAttrOperators[operatorIter].value(*this, attr.second);
72 return true;
73 }
74 return false;
75 }
76
SetSpecializedStyle(const std::pair<std::string,std::string> & style)77 bool BadgeDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
78 {
79 const static LinearMapNode<void (*)(BadgeDeclaration&, const std::string&)> badgeOperators[] = {
80 { DOM_BADGE_COLOR,
81 [](BadgeDeclaration& declaration, const std::string& value) {
82 declaration.SetBadgeColor(declaration.ParseColor(value));
83 } },
84 { DOM_BADGE_CIRCLE_SIZE,
85 [](BadgeDeclaration& declaration, const std::string& value) {
86 declaration.SetBadgeCircleSize(declaration.ParseDimension(value));
87 } },
88 { DOM_BADGE_TEXT_COLOR,
89 [](BadgeDeclaration& declaration, const std::string& value) {
90 declaration.SetBadgeTextColor(declaration.ParseColor(value));
91 } },
92 { DOM_BADGE_TEXT_FONT_SIZE,
93 [](BadgeDeclaration& declaration, const std::string& value) {
94 declaration.SetBadgeFontSize(declaration.ParseDimension(value));
95 } },
96 };
97 auto operatorIter = BinarySearchFindIndex(badgeOperators, ArraySize(badgeOperators), style.first.c_str());
98 if (operatorIter != -1) {
99 badgeOperators[operatorIter].value(*this, style.second);
100 return true;
101 }
102 return false;
103 }
104
SetSpecializedEvent(int32_t pageId,const std::string & eventId,const std::string & event)105 bool BadgeDeclaration::SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event)
106 {
107 if (event == DOM_CLICK) {
108 EventMarker eventMarker(eventId, event, pageId);
109 eventMarker.SetCatchMode(false);
110 SetClickEvent(eventMarker);
111 return true;
112 } else if (event == DOM_CATCH_BUBBLE_CLICK) {
113 EventMarker eventMarker(eventId, event, pageId);
114 eventMarker.SetCatchMode(true);
115 SetClickEvent(eventMarker);
116 return true;
117 }
118 return false;
119 }
120
121 } // namespace OHOS::Ace
122