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 "core/components/common/layout/grid_container_info.h"
22 #include "core/components_ng/pattern/badge/badge_model_ng.h"
23 #include "frameworks/bridge/declarative_frontend/jsview/models/badge_model_impl.h"
24
25 namespace OHOS::Ace {
26 std::unique_ptr<BadgeModel> BadgeModel::instance_ = nullptr;
27 std::mutex BadgeModel::mutex_;
28
GetInstance()29 BadgeModel* BadgeModel::GetInstance()
30 {
31 if (!instance_) {
32 std::lock_guard<std::mutex> lock(mutex_);
33 if (!instance_) {
34 #ifdef NG_BUILD
35 instance_.reset(new NG::BadgeModelNG());
36 #else
37 if (Container::IsCurrentUseNewPipeline()) {
38 instance_.reset(new NG::BadgeModelNG());
39 } else {
40 instance_.reset(new Framework::BadgeModelImpl());
41 }
42 #endif
43 }
44 }
45 return instance_.get();
46 }
47 } // namespace OHOS::Ace
48
49 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)50 void JSBadge::Create(const JSCallbackInfo& info)
51 {
52 if (!info[0]->IsObject()) {
53 return;
54 }
55
56 BadgeParameters badgeParameters = CreateBadgeParameters(info);
57 BadgeModel::GetInstance()->Create(badgeParameters);
58 }
59
CreateBadgeParameters(const JSCallbackInfo & info)60 BadgeParameters JSBadge::CreateBadgeParameters(const JSCallbackInfo& info)
61 {
62 BadgeParameters badgeParameters;
63 if (!info[0]->IsObject()) {
64 return badgeParameters;
65 }
66
67 auto badgeTheme = GetTheme<BadgeTheme>();
68 CHECK_NULL_RETURN_NOLOG(badgeTheme, BadgeParameters());
69 auto obj = JSRef<JSObject>::Cast(info[0]);
70 auto value = obj->GetProperty("value");
71 if (!value->IsNull() && value->IsString()) {
72 auto label = value->ToString();
73 badgeParameters.badgeValue = label;
74 }
75
76 auto position = obj->GetProperty("position");
77 if (!position->IsNull() && position->IsNumber()) {
78 badgeParameters.isPositionXy = false;
79 badgeParameters.badgePosition = position->ToNumber<int32_t>();
80 } else if (!position->IsNull() && position->IsObject()) {
81 badgeParameters.isPositionXy = true;
82 auto postionValue = JSRef<JSObject>::Cast(position);
83 JSRef<JSVal> xVal = postionValue->GetProperty("x");
84 JSRef<JSVal> yVal = postionValue->GetProperty("y");
85 CalcDimension dimenX;
86 CalcDimension dimenY;
87 bool xResult = ParseJsDimensionVp(xVal, dimenX);
88 bool yResult = ParseJsDimensionVp(yVal, dimenY);
89 if (!(xResult || yResult)) {
90 badgeParameters.badgePositionX = badgeTheme->GetBadgePositionX();
91 badgeParameters.badgePositionY = badgeTheme->GetBadgePositionY();
92 } else {
93 badgeParameters.badgePositionX = dimenX;
94 badgeParameters.badgePositionY = dimenY;
95 }
96 }
97
98 auto style = obj->GetProperty("style");
99 if (!style->IsNull() && style->IsObject()) {
100 auto value = JSRef<JSObject>::Cast(style);
101 JSRef<JSVal> colorValue = value->GetProperty("color");
102 JSRef<JSVal> fontSizeValue = value->GetProperty("fontSize");
103 JSRef<JSVal> badgeSizeValue = value->GetProperty("badgeSize");
104 JSRef<JSVal> badgeColorValue = value->GetProperty("badgeColor");
105 JSRef<JSVal> borderColorValue = value->GetProperty("borderColor");
106 JSRef<JSVal> borderWidthValue = value->GetProperty("borderWidth");
107 JSRef<JSVal> fontWeightValue = value->GetProperty("fontWeight");
108
109 Color colorVal;
110 if (ParseJsColor(colorValue, colorVal)) {
111 badgeParameters.badgeTextColor = colorVal;
112 }
113
114 CalcDimension fontSize;
115 if (ParseJsDimensionNG(fontSizeValue, fontSize, DimensionUnit::FP)) {
116 if (fontSize.IsNonNegative() && fontSize.Unit() != DimensionUnit::PERCENT) {
117 badgeParameters.badgeFontSize = fontSize;
118 } else {
119 badgeParameters.badgeFontSize = badgeTheme->GetBadgeFontSize();
120 }
121 } else if (!fontSizeValue->IsUndefined()) {
122 badgeParameters.badgeFontSize = badgeTheme->GetBadgeFontSize();
123 } else {
124 badgeParameters.badgeFontSize = UNDEFINED_DIMENSION;
125 }
126
127 CalcDimension badgeSize;
128 if (ParseJsDimensionNG(badgeSizeValue, badgeSize, DimensionUnit::FP)) {
129 if (badgeSize.IsNonNegative() && badgeSize.Unit() != DimensionUnit::PERCENT) {
130 badgeParameters.badgeCircleSize = badgeSize;
131 } else {
132 badgeParameters.badgeCircleSize = badgeTheme->GetBadgeCircleSize();
133 }
134 } else {
135 badgeParameters.badgeCircleSize = badgeTheme->GetBadgeCircleSize();
136 }
137
138 Color color;
139 if (ParseJsColor(badgeColorValue, color)) {
140 badgeParameters.badgeColor = color;
141 }
142
143 CalcDimension borderWidth;
144 if (ParseJsDimensionVp(borderWidthValue, borderWidth)) {
145 badgeParameters.badgeBorderWidth = borderWidth;
146 } else {
147 badgeParameters.badgeBorderWidth = badgeTheme->GetBadgeBorderWidth();
148 }
149
150 Color borderColor;
151 if (ParseJsColor(borderColorValue, borderColor)) {
152 badgeParameters.badgeBorderColor = borderColor;
153 } else {
154 badgeParameters.badgeBorderColor = badgeTheme->GetBadgeBorderColor();
155 }
156
157 std::string fontWeight;
158 if (fontWeightValue->IsNumber()) {
159 fontWeight = std::to_string(fontWeightValue->ToNumber<int32_t>());
160 } else {
161 if (!ParseJsString(fontWeightValue, fontWeight)) {
162 badgeParameters.badgeFontWeight = FontWeight::NORMAL;
163 }
164 }
165 badgeParameters.badgeFontWeight = ConvertStrToFontWeight(fontWeight);
166 }
167
168 auto count = obj->GetProperty("count");
169 if (!count->IsNull() && count->IsNumber()) {
170 badgeParameters.badgeCount = count->ToNumber<int32_t>();
171 }
172 auto maxCount = obj->GetProperty("maxCount");
173 if (!maxCount->IsNull() && maxCount->IsNumber()) {
174 badgeParameters.badgeMaxCount = maxCount->ToNumber<int32_t>();
175 } else {
176 badgeParameters.badgeMaxCount = badgeTheme->GetMaxCount();
177 }
178
179 return badgeParameters;
180 }
181
JSBind(BindingTarget globalObj)182 void JSBadge::JSBind(BindingTarget globalObj)
183 {
184 JSClass<JSBadge>::Declare("Badge");
185
186 MethodOptions opt = MethodOptions::NONE;
187 JSClass<JSBadge>::StaticMethod("create", &JSBadge::Create, opt);
188 JSClass<JSBadge>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
189 JSClass<JSBadge>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
190 JSClass<JSBadge>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
191
192 JSClass<JSBadge>::InheritAndBind<JSContainerBase>(globalObj);
193 }
194
SetDefaultTheme(OHOS::Ace::RefPtr<OHOS::Ace::BadgeComponent> & badge)195 void JSBadge::SetDefaultTheme(OHOS::Ace::RefPtr<OHOS::Ace::BadgeComponent>& badge)
196 {
197 auto badgeTheme = GetTheme<BadgeTheme>();
198 if (!badgeTheme) {
199 LOGE("Get badge theme error");
200 return;
201 }
202
203 badge->SetBadgeColor(badgeTheme->GetBadgeColor());
204 badge->SetMessageCount(badgeTheme->GetMessageCount());
205 badge->SetBadgePosition(badgeTheme->GetBadgePosition());
206 badge->SetBadgePositionX(badgeTheme->GetBadgePositionX());
207 badge->SetBadgePositionY(badgeTheme->GetBadgePositionY());
208 badge->SetIsPositionXy(badgeTheme->GetIsPositionXy());
209 badge->SetBadgeTextColor(badgeTheme->GetBadgeTextColor());
210 badge->SetBadgeCircleSize(badgeTheme->GetBadgeCircleSize());
211 }
212
SetCustomizedTheme(const JSRef<JSObject> & obj,OHOS::Ace::RefPtr<OHOS::Ace::BadgeComponent> & badge)213 void JSBadge::SetCustomizedTheme(const JSRef<JSObject>& obj, OHOS::Ace::RefPtr<OHOS::Ace::BadgeComponent>& badge)
214 {
215 auto count = obj->GetProperty("count");
216 if (!count->IsNull() && count->IsNumber()) {
217 auto value = count->ToNumber<int32_t>();
218 badge->SetMessageCount(value);
219 }
220
221 SetPosition(obj, badge);
222
223 auto maxCount = obj->GetProperty("maxCount");
224 if (!maxCount->IsNull() && maxCount->IsNumber()) {
225 auto value = maxCount->ToNumber<int32_t>();
226 badge->SetMaxCount(value);
227 }
228
229 auto style = obj->GetProperty("style");
230 if (!style->IsNull() && style->IsObject()) {
231 auto value = JSRef<JSObject>::Cast(style);
232 JSRef<JSVal> colorValue = value->GetProperty("color");
233 JSRef<JSVal> fontSizeValue = value->GetProperty("fontSize");
234 JSRef<JSVal> badgeSizeValue = value->GetProperty("badgeSize");
235 JSRef<JSVal> badgeColorValue = value->GetProperty("badgeColor");
236
237 Color colorVal;
238 if (ParseJsColor(colorValue, colorVal)) {
239 badge->SetBadgeTextColor(colorVal);
240 }
241
242 CalcDimension fontSize;
243 if (ParseJsDimensionFp(fontSizeValue, fontSize)) {
244 badge->SetBadgeFontSize(fontSize);
245 }
246
247 CalcDimension badgeSize;
248 if (ParseJsDimensionFp(badgeSizeValue, badgeSize)) {
249 if (badgeSize.IsNonNegative()) {
250 badge->SetBadgeCircleSize(badgeSize);
251 }
252 }
253
254 Color badgeColor;
255 if (ParseJsColor(badgeColorValue, badgeColor)) {
256 badge->SetBadgeColor(badgeColor);
257 }
258 }
259
260 auto value = obj->GetProperty("value");
261 if (!value->IsNull() && value->IsString()) {
262 auto label = value->ToString();
263 badge->SetBadgeLabel(label);
264 }
265 }
266
SetPosition(const JSRef<JSObject> & obj,OHOS::Ace::RefPtr<OHOS::Ace::BadgeComponent> & badge)267 void JSBadge::SetPosition(const JSRef<JSObject>& obj, OHOS::Ace::RefPtr<OHOS::Ace::BadgeComponent>& badge)
268 {
269 auto position = obj->GetProperty("position");
270 if (!position->IsNull() && position->IsNumber()) {
271 badge->SetIsPositionXy(false);
272 auto value = position->ToNumber<int32_t>();
273 badge->SetBadgePosition(static_cast<BadgePosition>(value));
274 } else if (!position->IsNull() && position->IsObject()) {
275 badge->SetIsPositionXy(true);
276 auto postionValue = JSRef<JSObject>::Cast(position);
277 JSRef<JSVal> xVal = postionValue->GetProperty("x");
278 JSRef<JSVal> yVal = postionValue->GetProperty("y");
279 CalcDimension dimenX;
280 CalcDimension dimenY;
281 bool xResult = ParseJsDimensionVp(xVal, dimenX);
282 bool yResult = ParseJsDimensionVp(yVal, dimenY);
283 if (xResult && yResult) {
284 badge->SetBadgePositionX(dimenX);
285 badge->SetBadgePositionY(dimenY);
286 }
287 }
288 }
289 } // namespace OHOS::Ace::Framework
290