• 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 "bridge/declarative_frontend/jsview/js_badge.h"
17 
18 #include "base/geometry/dimension.h"
19 #include "base/log/ace_trace.h"
20 #include "core/components/badge/badge_component.h"
21 #include "core/components_ng/base/view_stack_processor.h"
22 #include "core/components_ng/pattern/badge/badge_view.h"
23 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
24 
25 namespace OHOS::Ace::Framework {
26 
Create(const JSCallbackInfo & info)27 void JSBadge::Create(const JSCallbackInfo& info)
28 {
29     if (info.Length() < 1) {
30         LOGE("The argv is wrong, it is supposed to have at least 1 argument");
31         return;
32     }
33 
34     if (!info[0]->IsObject()) {
35         LOGE("The argv is wrong, it is supposed to be a object");
36         return;
37     }
38 
39     if (Container::IsCurrentUseNewPipeline()) {
40         CreateNG(info);
41         return;
42     }
43 
44     auto badge = AceType::MakeRefPtr<OHOS::Ace::BadgeComponent>();
45     SetDefaultTheme(badge);
46 
47     auto obj = JSRef<JSObject>::Cast(info[0]);
48     SetCustomizedTheme(obj, badge);
49 
50     ViewStackProcessor::GetInstance()->ClaimElementId(badge);
51     ViewStackProcessor::GetInstance()->Push(badge);
52 }
53 
CreateNG(const JSCallbackInfo & info)54 void JSBadge::CreateNG(const JSCallbackInfo& info)
55 {
56     auto obj = JSRef<JSObject>::Cast(info[0]);
57 
58     NG::BadgeView::BadgeParameters badgeParameters;
59     auto value = obj->GetProperty("value");
60     if (!value->IsNull() && value->IsString()) {
61         auto label = value->ToString();
62         badgeParameters.badgeValue = label;
63     }
64 
65     auto position = obj->GetProperty("position");
66     if (!position->IsNull() && position->IsNumber()) {
67         badgeParameters.badgePosition = position->ToNumber<int32_t>();
68     }
69     auto style = obj->GetProperty("style");
70     if (!style->IsNull() && style->IsObject()) {
71         auto value = JSRef<JSObject>::Cast(style);
72         JSRef<JSVal> colorValue = value->GetProperty("color");
73         JSRef<JSVal> fontSizeValue = value->GetProperty("fontSize");
74         JSRef<JSVal> badgeSizeValue = value->GetProperty("badgeSize");
75         JSRef<JSVal> badgeColorValue = value->GetProperty("badgeColor");
76 
77         Color colorVal;
78         if (ParseJsColor(colorValue, colorVal)) {
79             badgeParameters.badgeTextColor = colorVal;
80         }
81 
82         Dimension fontSize;
83         if (ParseJsDimensionFp(fontSizeValue, fontSize)) {
84             badgeParameters.badgeFontSize = fontSize;
85         }
86 
87         Dimension badgeSize;
88         if (ParseJsDimensionFp(badgeSizeValue, badgeSize)) {
89             auto badgeTheme = GetTheme<BadgeTheme>();
90             if (!badgeTheme) {
91                 LOGE("Get badge theme error");
92                 return;
93             }
94             if (badgeSize.IsNonNegative() && badgeSize.Unit() != DimensionUnit::PERCENT) {
95                 badgeParameters.badgeCircleSize = badgeSize;
96             } else {
97                 badgeParameters.badgeCircleSize = badgeTheme->GetBadgeCircleSize();
98             }
99         }
100 
101         Color color;
102         if (ParseJsColor(badgeColorValue, color)) {
103             badgeParameters.badgeColor = color;
104         }
105     }
106 
107     auto count = obj->GetProperty("count");
108     if (!count->IsNull() && count->IsNumber()) {
109         badgeParameters.badgeCount = count->ToNumber<int32_t>();
110     }
111     auto maxCount = obj->GetProperty("maxCount");
112     if (!maxCount->IsNull() && maxCount->IsNumber()) {
113         badgeParameters.badgeMaxCount = maxCount->ToNumber<int32_t>();
114     }
115 
116     NG::BadgeView::Create(badgeParameters);
117 }
118 
JSBind(BindingTarget globalObj)119 void JSBadge::JSBind(BindingTarget globalObj)
120 {
121     JSClass<JSBadge>::Declare("Badge");
122 
123     MethodOptions opt = MethodOptions::NONE;
124     JSClass<JSBadge>::StaticMethod("create", &JSBadge::Create, opt);
125 
126     JSClass<JSBadge>::Inherit<JSContainerBase>();
127     JSClass<JSBadge>::Inherit<JSViewAbstract>();
128 
129     JSClass<JSBadge>::Bind(globalObj);
130 }
131 
SetDefaultTheme(OHOS::Ace::RefPtr<OHOS::Ace::BadgeComponent> & badge)132 void JSBadge::SetDefaultTheme(OHOS::Ace::RefPtr<OHOS::Ace::BadgeComponent>& badge)
133 {
134     auto badgeTheme = GetTheme<BadgeTheme>();
135     if (!badgeTheme) {
136         LOGE("Get badge theme error");
137         return;
138     }
139 
140     badge->SetBadgeColor(badgeTheme->GetBadgeColor());
141     badge->SetMessageCount(badgeTheme->GetMessageCount());
142     badge->SetBadgePosition(badgeTheme->GetBadgePosition());
143     badge->SetBadgeTextColor(badgeTheme->GetBadgeTextColor());
144     badge->SetBadgeFontSize(badgeTheme->GetBadgeFontSize());
145     badge->SetBadgeCircleSize(badgeTheme->GetBadgeCircleSize());
146 }
147 
SetCustomizedTheme(const JSRef<JSObject> & obj,OHOS::Ace::RefPtr<OHOS::Ace::BadgeComponent> & badge)148 void JSBadge::SetCustomizedTheme(const JSRef<JSObject>& obj, OHOS::Ace::RefPtr<OHOS::Ace::BadgeComponent>& badge)
149 {
150     auto count = obj->GetProperty("count");
151     if (!count->IsNull() && count->IsNumber()) {
152         auto value = count->ToNumber<int32_t>();
153         badge->SetMessageCount(value);
154     }
155 
156     auto position = obj->GetProperty("position");
157     if (!position->IsNull() && position->IsNumber()) {
158         auto value = position->ToNumber<int32_t>();
159         badge->SetBadgePosition(static_cast<BadgePosition>(value));
160     }
161 
162     auto maxCount = obj->GetProperty("maxCount");
163     if (!maxCount->IsNull() && maxCount->IsNumber()) {
164         auto value = maxCount->ToNumber<int32_t>();
165         badge->SetMaxCount(value);
166     }
167 
168     auto style = obj->GetProperty("style");
169     if (!style->IsNull() && style->IsObject()) {
170         auto value = JSRef<JSObject>::Cast(style);
171         JSRef<JSVal> colorValue = value->GetProperty("color");
172         JSRef<JSVal> fontSizeValue = value->GetProperty("fontSize");
173         JSRef<JSVal> badgeSizeValue = value->GetProperty("badgeSize");
174         JSRef<JSVal> badgeColorValue = value->GetProperty("badgeColor");
175 
176         Color colorVal;
177         if (ParseJsColor(colorValue, colorVal)) {
178             badge->SetBadgeTextColor(colorVal);
179         }
180 
181         Dimension fontSize;
182         if (ParseJsDimensionFp(fontSizeValue, fontSize)) {
183             badge->SetBadgeFontSize(fontSize);
184         }
185 
186         Dimension badgeSize;
187         if (ParseJsDimensionFp(badgeSizeValue, badgeSize)) {
188             if (badgeSize.IsNonNegative()) {
189                 badge->SetBadgeCircleSize(badgeSize);
190             }
191         }
192 
193         Color badgeColor;
194         if (ParseJsColor(badgeColorValue, badgeColor)) {
195             badge->SetBadgeColor(badgeColor);
196         }
197     }
198 
199     auto value = obj->GetProperty("value");
200     if (!value->IsNull() && value->IsString()) {
201         auto label = value->ToString();
202         badge->SetBadgeLabel(label);
203     }
204 }
205 
206 } // namespace OHOS::Ace::Framework