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
JSHeight(const JSCallbackInfo & args)127 void JSCounter::JSHeight(const JSCallbackInfo& args)
128 {
129 if (args.Length() < 1) {
130 return;
131 }
132
133 Dimension value;
134 if (!ConvertFromJSValue(args[0], value)) {
135 return;
136 }
137
138 if (LessNotEqual(value.Value(), 0.0)) {
139 return;
140 }
141 CounterModel::GetInstance()->SetHeight(value);
142 args.ReturnSelf();
143 }
144
JSWidth(const JSCallbackInfo & args)145 void JSCounter::JSWidth(const JSCallbackInfo& args)
146 {
147 if (args.Length() < 1) {
148 return;
149 }
150
151 Dimension value;
152 if (!ConvertFromJSValue(args[0], value)) {
153 return;
154 }
155
156 if (LessNotEqual(value.Value(), 0.0)) {
157 return;
158 }
159 CounterModel::GetInstance()->SetWidth(value);
160 args.ReturnSelf();
161 }
162
SetSize(const JSCallbackInfo & args)163 void JSCounter::SetSize(const JSCallbackInfo& args)
164 {
165 if (args.Length() >= 1 && args[0]->IsObject()) {
166 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
167
168 Dimension height;
169 if (ConvertFromJSValue(obj->GetProperty("height"), height) && height.IsValid()) {
170 if (GreatOrEqual(height.Value(), 0.0)) {
171 CounterModel::GetInstance()->SetHeight(height);
172 }
173 }
174
175 Dimension width;
176 if (ConvertFromJSValue(obj->GetProperty("width"), width) && width.IsValid()) {
177 if (GreatOrEqual(width.Value(), 0.0)) {
178 CounterModel::GetInstance()->SetWidth(width);
179 }
180 }
181 }
182 args.ReturnSelf();
183 }
184
JSControlWidth(const JSCallbackInfo & args)185 void JSCounter::JSControlWidth(const JSCallbackInfo& args)
186 {
187 if (args.Length() < 1) {
188 return;
189 }
190
191 Dimension value;
192 if (!ConvertFromJSValue(args[0], value)) {
193 return;
194 }
195 CounterModel::GetInstance()->SetControlWidth(value);
196 args.ReturnSelf();
197 }
198
JSStateChange(bool state)199 void JSCounter::JSStateChange(bool state)
200 {
201 CounterModel::GetInstance()->SetStateChange(state);
202 }
203
JsBackgroundColor(const JSCallbackInfo & args)204 void JSCounter::JsBackgroundColor(const JSCallbackInfo& args)
205 {
206 if (args.Length() < 1) {
207 return;
208 }
209
210 Color color;
211 if (!ParseJsColor(args[0], color)) {
212 return;
213 }
214 CounterModel::GetInstance()->SetBackgroundColor(color);
215 args.ReturnSelf();
216 }
217
Create()218 void JSCounter::Create()
219 {
220 CounterModel::GetInstance()->Create();
221 }
222
223 } // namespace OHOS::Ace::Framework
224