1 /*
2 * Copyright (c) 2021-2023 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 "bridge/declarative_frontend/jsview/js_badge.h"
17
18 #include "base/geometry/dimension.h"
19 #include "base/log/ace_trace.h"
20 #include "base/utils/utils.h"
21 #include "bridge/declarative_frontend/ark_theme/theme_apply/js_theme.h"
22 #include "bridge/declarative_frontend/ark_theme/theme_apply/js_theme_utils.h"
23 #include "core/components/common/layout/grid_container_info.h"
24 #include "core/components_ng/pattern/badge/badge_model_ng.h"
25 #include "frameworks/bridge/declarative_frontend/jsview/models/badge_model_impl.h"
26
27 namespace OHOS::Ace {
28 std::unique_ptr<BadgeModel> BadgeModel::instance_ = nullptr;
29 std::mutex BadgeModel::mutex_;
30
GetInstance()31 BadgeModel* BadgeModel::GetInstance()
32 {
33 if (!instance_) {
34 std::lock_guard<std::mutex> lock(mutex_);
35 if (!instance_) {
36 #ifdef NG_BUILD
37 instance_.reset(new NG::BadgeModelNG());
38 #else
39 if (Container::IsCurrentUseNewPipeline()) {
40 instance_.reset(new NG::BadgeModelNG());
41 } else {
42 instance_.reset(new Framework::BadgeModelImpl());
43 }
44 #endif
45 }
46 }
47 return instance_.get();
48 }
49 } // namespace OHOS::Ace
50
51 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)52 void JSBadge::Create(const JSCallbackInfo& info)
53 {
54 if (!info[0]->IsObject()) {
55 return;
56 }
57
58 BadgeParameters badgeParameters = CreateBadgeParameters(info);
59 BadgeModel::GetInstance()->Create(badgeParameters);
60 }
61
CreateBadgeParameters(const JSCallbackInfo & info)62 BadgeParameters JSBadge::CreateBadgeParameters(const JSCallbackInfo& info)
63 {
64 BadgeParameters badgeParameters;
65 if (!info[0]->IsObject()) {
66 return badgeParameters;
67 }
68
69 auto themeColors = JSThemeUtils::GetThemeColors();
70
71 auto badgeTheme = GetTheme<BadgeTheme>();
72 CHECK_NULL_RETURN(badgeTheme, BadgeParameters());
73 auto obj = JSRef<JSObject>::Cast(info[0]);
74 auto value = obj->GetProperty("value");
75 if (!value->IsNull() && value->IsString()) {
76 auto label = value->ToString();
77 badgeParameters.badgeValue = label;
78 } else if (!value->IsNull() && value->IsObject()) {
79 std::string valueResult;
80 ParseJsString(value, valueResult);
81 badgeParameters.badgeValue = valueResult;
82 }
83
84 auto position = obj->GetProperty("position");
85 if (!position->IsNull() && position->IsNumber()) {
86 badgeParameters.isPositionXy = false;
87 badgeParameters.badgePosition = position->ToNumber<int32_t>();
88 } else if (!position->IsNull() && position->IsObject()) {
89 badgeParameters.isPositionXy = true;
90 auto postionValue = JSRef<JSObject>::Cast(position);
91 JSRef<JSVal> xVal = postionValue->GetProperty("x");
92 JSRef<JSVal> yVal = postionValue->GetProperty("y");
93 CalcDimension dimenX;
94 CalcDimension dimenY;
95 bool xResult = ParseJsDimensionVp(xVal, dimenX);
96 bool yResult = ParseJsDimensionVp(yVal, dimenY);
97 if (!(xResult || yResult)) {
98 badgeParameters.badgePositionX = badgeTheme->GetBadgePositionX();
99 badgeParameters.badgePositionY = badgeTheme->GetBadgePositionY();
100 } else {
101 badgeParameters.badgePositionX = dimenX;
102 badgeParameters.badgePositionY = dimenY;
103 }
104 }
105
106 auto style = obj->GetProperty("style");
107 if (!style->IsNull() && style->IsObject()) {
108 auto value = JSRef<JSObject>::Cast(style);
109 JSRef<JSVal> colorValue = value->GetProperty("color");
110 JSRef<JSVal> fontSizeValue = value->GetProperty("fontSize");
111 JSRef<JSVal> badgeSizeValue = value->GetProperty("badgeSize");
112 JSRef<JSVal> badgeColorValue = value->GetProperty("badgeColor");
113 JSRef<JSVal> borderColorValue = value->GetProperty("borderColor");
114 JSRef<JSVal> borderWidthValue = value->GetProperty("borderWidth");
115 JSRef<JSVal> fontWeightValue = value->GetProperty("fontWeight");
116
117 bool isDefaultFontSize = true;
118 bool isDefaultBadgeSize = true;
119
120 Color colorVal;
121 if (ParseJsColor(colorValue, colorVal)) {
122 badgeParameters.badgeTextColor = colorVal;
123 } else {
124 if (themeColors) {
125 badgeParameters.badgeTextColor = themeColors->FontOnPrimary();
126 }
127 }
128
129 CalcDimension fontSize;
130 if (ParseJsDimensionNG(fontSizeValue, fontSize, DimensionUnit::FP)) {
131 if (fontSize.IsNonNegative() && fontSize.Unit() != DimensionUnit::PERCENT) {
132 badgeParameters.badgeFontSize = fontSize;
133 isDefaultFontSize = false;
134 } else {
135 badgeParameters.badgeFontSize = badgeTheme->GetBadgeFontSize();
136 }
137 } else if (!fontSizeValue->IsUndefined()) {
138 badgeParameters.badgeFontSize = badgeTheme->GetBadgeFontSize();
139 } else {
140 badgeParameters.badgeFontSize = UNDEFINED_DIMENSION;
141 }
142
143 CalcDimension badgeSize;
144 if (ParseJsDimensionNG(badgeSizeValue, badgeSize, DimensionUnit::FP)) {
145 if (badgeSize.IsNonNegative() && badgeSize.Unit() != DimensionUnit::PERCENT) {
146 badgeParameters.badgeCircleSize = badgeSize;
147 isDefaultBadgeSize = false;
148 } else {
149 badgeParameters.badgeCircleSize = badgeTheme->GetBadgeCircleSize();
150 }
151 } else {
152 badgeParameters.badgeCircleSize = badgeTheme->GetBadgeCircleSize();
153 }
154
155 BadgeModel::GetInstance()->SetIsDefault(isDefaultFontSize, isDefaultBadgeSize);
156 Color color;
157 if (ParseJsColor(badgeColorValue, color)) {
158 badgeParameters.badgeColor = color;
159 } else {
160 if (themeColors) {
161 badgeParameters.badgeColor = themeColors->Warning();
162 }
163 }
164
165 CalcDimension borderWidth;
166 if (ParseJsDimensionVp(borderWidthValue, borderWidth)) {
167 if (borderWidth.IsNonNegative() && borderWidth.Unit() != DimensionUnit::PERCENT) {
168 badgeParameters.badgeBorderWidth = borderWidth;
169 } else {
170 badgeParameters.badgeBorderWidth = badgeTheme->GetBadgeBorderWidth();
171 }
172 } else {
173 badgeParameters.badgeBorderWidth = badgeTheme->GetBadgeBorderWidth();
174 }
175
176 Color borderColor;
177 if (ParseJsColor(borderColorValue, borderColor)) {
178 badgeParameters.badgeBorderColor = borderColor;
179 } else {
180 badgeParameters.badgeBorderColor = themeColors ? themeColors->Warning() : badgeTheme->GetBadgeBorderColor();
181 }
182
183 std::string fontWeight;
184 if (fontWeightValue->IsNumber()) {
185 fontWeight = std::to_string(fontWeightValue->ToNumber<int32_t>());
186 } else {
187 if (!ParseJsString(fontWeightValue, fontWeight)) {
188 badgeParameters.badgeFontWeight = FontWeight::NORMAL;
189 }
190 }
191 badgeParameters.badgeFontWeight = ConvertStrToFontWeight(fontWeight);
192 }
193
194 auto count = obj->GetProperty("count");
195 if (!count->IsNull() && count->IsNumber()) {
196 badgeParameters.badgeCount = count->ToNumber<int32_t>();
197 }
198 auto maxCount = obj->GetProperty("maxCount");
199 if (!maxCount->IsNull() && maxCount->IsNumber()) {
200 badgeParameters.badgeMaxCount = maxCount->ToNumber<int32_t>();
201 } else {
202 badgeParameters.badgeMaxCount = badgeTheme->GetMaxCount();
203 }
204
205 return badgeParameters;
206 }
207
JSBind(BindingTarget globalObj)208 void JSBadge::JSBind(BindingTarget globalObj)
209 {
210 JSClass<JSBadge>::Declare("Badge");
211
212 MethodOptions opt = MethodOptions::NONE;
213 JSClass<JSBadge>::StaticMethod("create", &JSBadge::Create, opt);
214 JSClass<JSBadge>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
215 JSClass<JSBadge>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
216 JSClass<JSBadge>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
217 JSClass<JSBadge>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
218 JSClass<JSBadge>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
219
220 JSClass<JSBadge>::InheritAndBind<JSContainerBase>(globalObj);
221 }
222 } // namespace OHOS::Ace::Framework
223