1 /*
2 * Copyright (c) 2021-2025 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 "frameworks/bridge/declarative_frontend/jsview/js_counter.h"
17
18 #include "bridge/declarative_frontend/jsview/models/counter_model_impl.h"
19 #include "core/components_ng/pattern/counter/counter_model_ng.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
22 #include "frameworks/bridge/declarative_frontend/engine/functions/js_click_function.h"
23 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
24 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
25 #include "frameworks/core/components/counter/counter_theme.h"
26
27 namespace OHOS::Ace {
28
29 std::unique_ptr<CounterModel> CounterModel::instance_ = nullptr;
30 std::mutex CounterModel::mutex_;
31
GetInstance()32 CounterModel* CounterModel::GetInstance()
33 {
34 if (!instance_) {
35 std::lock_guard<std::mutex> lock(mutex_);
36 if (!instance_) {
37 #ifdef NG_BUILD
38 instance_.reset(new NG::CounterModelNG());
39 #else
40 if (Container::IsCurrentUseNewPipeline()) {
41 instance_.reset(new NG::CounterModelNG());
42 } else {
43 instance_.reset(new Framework::CounterModelImpl());
44 }
45 #endif
46 }
47 }
48 return instance_.get();
49 }
50
51 } // namespace OHOS::Ace
52
53 namespace OHOS::Ace::Framework {
54
JSBind(BindingTarget globalObj)55 void JSCounter::JSBind(BindingTarget globalObj)
56 {
57 JSClass<JSCounter>::Declare("Counter");
58 JSClass<JSCounter>::StaticMethod("create", &JSCounter::Create, MethodOptions::NONE);
59 JSClass<JSCounter>::StaticMethod("enableDec", &JSCounter::JsEnableDec);
60 JSClass<JSCounter>::StaticMethod("enableInc", &JSCounter::JsEnableInc);
61 JSClass<JSCounter>::StaticMethod("onInc", &JSCounter::JsOnInc);
62 JSClass<JSCounter>::StaticMethod("onDec", &JSCounter::JsOnDec);
63 JSClass<JSCounter>::StaticMethod("height", &JSCounter::JSHeight);
64 JSClass<JSCounter>::StaticMethod("width", &JSCounter::JSWidth);
65 JSClass<JSCounter>::StaticMethod("size", &JSCounter::SetSize);
66 JSClass<JSCounter>::StaticMethod("controlWidth", &JSCounter::JSControlWidth);
67 JSClass<JSCounter>::StaticMethod("state", &JSCounter::JSStateChange);
68 JSClass<JSCounter>::StaticMethod("backgroundColor", &JSCounter::JsBackgroundColor);
69 JSClass<JSCounter>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
70 JSClass<JSCounter>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
71 JSClass<JSCounter>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
72 JSClass<JSCounter>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
73 JSClass<JSCounter>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
74 JSClass<JSCounter>::InheritAndBind<JSContainerBase>(globalObj);
75 }
76
JsEnableDec(const JSCallbackInfo & args)77 void JSCounter::JsEnableDec(const JSCallbackInfo& args)
78 {
79 if (args.Length() < 1) {
80 return;
81 }
82 if (!args[0]->IsBoolean()) {
83 CounterModel::GetInstance()->SetEnableDec(true);
84 } else {
85 CounterModel::GetInstance()->SetEnableDec(args[0]->ToBoolean());
86 }
87 }
88
JsEnableInc(const JSCallbackInfo & args)89 void JSCounter::JsEnableInc(const JSCallbackInfo& args)
90 {
91 if (args.Length() < 1) {
92 return;
93 }
94 if (!args[0]->IsBoolean()) {
95 CounterModel::GetInstance()->SetEnableInc(true);
96 } else {
97 CounterModel::GetInstance()->SetEnableInc(args[0]->ToBoolean());
98 }
99 }
100
JsOnInc(const JSCallbackInfo & args)101 void JSCounter::JsOnInc(const JSCallbackInfo& args)
102 {
103 if (args.Length() < 1) {
104 return;
105 }
106 if (!args[0]->IsFunction()) {
107 return;
108 }
109 CounterModel::GetInstance()->SetOnInc(
110 JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
111 args.ReturnSelf();
112 }
113
JsOnDec(const JSCallbackInfo & args)114 void JSCounter::JsOnDec(const JSCallbackInfo& args)
115 {
116 if (args.Length() < 1) {
117 return;
118 }
119 if (!args[0]->IsFunction()) {
120 return;
121 }
122 CounterModel::GetInstance()->SetOnDec(
123 JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
124 args.ReturnSelf();
125 }
126
UpdateLayoutPolicy(const JSCallbackInfo & args,bool isWidth)127 static void UpdateLayoutPolicy(const JSCallbackInfo& args, bool isWidth)
128 {
129 auto jsValue = args[0];
130 LayoutCalPolicy policy = LayoutCalPolicy::NO_MATCH;
131 if (jsValue->IsObject()) {
132 JSRef<JSObject> object = JSRef<JSObject>::Cast(jsValue);
133 CHECK_NULL_VOID(!object->IsEmpty());
134 JSRef<JSVal> layoutPolicy = object->GetProperty("id_");
135 CHECK_NULL_VOID(!layoutPolicy->IsEmpty());
136 if (layoutPolicy->IsString()) {
137 policy = JSContainerBase::ParseLayoutPolicy(layoutPolicy->ToString());
138 }
139 }
140 ViewAbstractModel::GetInstance()->UpdateLayoutPolicyProperty(policy, false);
141 }
142
JSHeight(const JSCallbackInfo & args)143 void JSCounter::JSHeight(const JSCallbackInfo& args)
144 {
145 if (args.Length() < 1) {
146 return;
147 }
148
149 Dimension value;
150 RefPtr<ResourceObject> heightResObj;
151 if (SystemProperties::ConfigChangePerform()) {
152 bool state = ConvertFromJSValue(args[0], value, heightResObj);
153 CounterModel::GetInstance()->CreateWithResourceObj(JsCounterResourceType::Height, heightResObj);
154 if (!state) {
155 UpdateLayoutPolicy(args, false);
156 return;
157 }
158 if (LessNotEqual(value.Value(), 0.0)) {
159 return;
160 }
161 CounterModel::GetInstance()->SetHeight(value);
162 args.ReturnSelf();
163 } else {
164 if (!ConvertFromJSValue(args[0], value)) {
165 UpdateLayoutPolicy(args, false);
166 return;
167 }
168 if (LessNotEqual(value.Value(), 0.0)) {
169 return;
170 }
171 CounterModel::GetInstance()->SetHeight(value);
172 args.ReturnSelf();
173 }
174 }
175
JSWidth(const JSCallbackInfo & args)176 void JSCounter::JSWidth(const JSCallbackInfo& args)
177 {
178 if (args.Length() < 1) {
179 return;
180 }
181
182 Dimension value;
183 RefPtr<ResourceObject> widthResObj;
184 if (SystemProperties::ConfigChangePerform()) {
185 bool state = ConvertFromJSValue(args[0], value, widthResObj);
186 CounterModel::GetInstance()->CreateWithResourceObj(JsCounterResourceType::Width, widthResObj);
187 if (!state) {
188 UpdateLayoutPolicy(args, true);
189 return;
190 }
191 if (LessNotEqual(value.Value(), 0.0)) {
192 return;
193 }
194 CounterModel::GetInstance()->SetWidth(value);
195 args.ReturnSelf();
196 } else {
197 if (!ConvertFromJSValue(args[0], value)) {
198 UpdateLayoutPolicy(args, true);
199 return;
200 }
201 if (LessNotEqual(value.Value(), 0.0)) {
202 return;
203 }
204 CounterModel::GetInstance()->SetWidth(value);
205 args.ReturnSelf();
206 }
207 }
208
SetSize(const JSCallbackInfo & args)209 void JSCounter::SetSize(const JSCallbackInfo& args)
210 {
211 if (args.Length() < 1 || !args[0]->IsObject()) {
212 args.ReturnSelf();
213 return;
214 }
215 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
216
217 Dimension height;
218 RefPtr<ResourceObject> heightResObj;
219 Dimension width;
220 RefPtr<ResourceObject> widthResObj;
221 if (SystemProperties::ConfigChangePerform()) {
222 bool heightState = ConvertFromJSValue(obj->GetProperty("height"), height, heightResObj);
223 CounterModel::GetInstance()->CreateWithResourceObj(JsCounterResourceType::Height, heightResObj);
224 if (heightState) {
225 if (height.IsValid() && GreatOrEqual(height.Value(), 0.0)) {
226 CounterModel::GetInstance()->SetHeight(height);
227 }
228 }
229 bool widthState = ConvertFromJSValue(obj->GetProperty("width"), width, widthResObj);
230 CounterModel::GetInstance()->CreateWithResourceObj(JsCounterResourceType::Width, widthResObj);
231 if (widthState) {
232 if (width.IsValid() && GreatOrEqual(width.Value(), 0.0)) {
233 CounterModel::GetInstance()->SetWidth(width);
234 }
235 }
236 } else {
237 if (ConvertFromJSValue(obj->GetProperty("height"), height) && height.IsValid()) {
238 if (height.IsValid() && GreatOrEqual(height.Value(), 0.0)) {
239 CounterModel::GetInstance()->SetHeight(height);
240 }
241 }
242 if (ConvertFromJSValue(obj->GetProperty("width"), width) && width.IsValid()) {
243 if (width.IsValid() && GreatOrEqual(width.Value(), 0.0)) {
244 CounterModel::GetInstance()->SetWidth(width);
245 }
246 }
247 }
248 args.ReturnSelf();
249 }
250
JSControlWidth(const JSCallbackInfo & args)251 void JSCounter::JSControlWidth(const JSCallbackInfo& args)
252 {
253 if (args.Length() < 1) {
254 return;
255 }
256
257 Dimension value;
258 if (!ConvertFromJSValue(args[0], value)) {
259 return;
260 }
261 CounterModel::GetInstance()->SetControlWidth(value);
262 args.ReturnSelf();
263 }
264
JSStateChange(bool state)265 void JSCounter::JSStateChange(bool state)
266 {
267 CounterModel::GetInstance()->SetStateChange(state);
268 }
269
JsBackgroundColor(const JSCallbackInfo & args)270 void JSCounter::JsBackgroundColor(const JSCallbackInfo& args)
271 {
272 if (args.Length() < 1) {
273 return;
274 }
275
276 Color color;
277 RefPtr<ResourceObject> resObj;
278 if (ParseJsColor(args[0], color, resObj)) {
279 if (SystemProperties::ConfigChangePerform()) {
280 CounterModel::GetInstance()->CreateWithResourceObj(JsCounterResourceType::BackgroundColor, resObj);
281 }
282 CounterModel::GetInstance()->SetBackgroundColor(color);
283 args.ReturnSelf();
284 } else if (SystemProperties::ConfigChangePerform()) {
285 CounterModel::GetInstance()->CreateWithResourceObj(JsCounterResourceType::BackgroundColor, nullptr);
286 }
287 }
288
Create()289 void JSCounter::Create()
290 {
291 CounterModel::GetInstance()->Create();
292 }
293
294 } // namespace OHOS::Ace::Framework
295