1 /*
2 * Copyright (c) 2021 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/bridge/declarative_frontend/ark_theme/theme_apply/js_counter_theme.h"
26 #include "frameworks/core/components/counter/counter_theme.h"
27
28 namespace OHOS::Ace {
29
30 std::unique_ptr<CounterModel> CounterModel::instance_ = nullptr;
31 std::mutex CounterModel::mutex_;
32
GetInstance()33 CounterModel* CounterModel::GetInstance()
34 {
35 if (!instance_) {
36 std::lock_guard<std::mutex> lock(mutex_);
37 if (!instance_) {
38 #ifdef NG_BUILD
39 instance_.reset(new NG::CounterModelNG());
40 #else
41 if (Container::IsCurrentUseNewPipeline()) {
42 instance_.reset(new NG::CounterModelNG());
43 } else {
44 instance_.reset(new Framework::CounterModelImpl());
45 }
46 #endif
47 }
48 }
49 return instance_.get();
50 }
51
52 } // namespace OHOS::Ace
53
54 namespace OHOS::Ace::Framework {
55
JSBind(BindingTarget globalObj)56 void JSCounter::JSBind(BindingTarget globalObj)
57 {
58 JSClass<JSCounter>::Declare("Counter");
59 JSClass<JSCounter>::StaticMethod("create", &JSCounter::Create, MethodOptions::NONE);
60 JSClass<JSCounter>::StaticMethod("enableDec", &JSCounter::JsEnableDec);
61 JSClass<JSCounter>::StaticMethod("enableInc", &JSCounter::JsEnableInc);
62 JSClass<JSCounter>::StaticMethod("onInc", &JSCounter::JsOnInc);
63 JSClass<JSCounter>::StaticMethod("onDec", &JSCounter::JsOnDec);
64 JSClass<JSCounter>::StaticMethod("height", &JSCounter::JSHeight);
65 JSClass<JSCounter>::StaticMethod("width", &JSCounter::JSWidth);
66 JSClass<JSCounter>::StaticMethod("size", &JSCounter::SetSize);
67 JSClass<JSCounter>::StaticMethod("controlWidth", &JSCounter::JSControlWidth);
68 JSClass<JSCounter>::StaticMethod("state", &JSCounter::JSStateChange);
69 JSClass<JSCounter>::StaticMethod("backgroundColor", &JSCounter::JsBackgroundColor);
70 JSClass<JSCounter>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
71 JSClass<JSCounter>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
72 JSClass<JSCounter>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
73 JSClass<JSCounter>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
74 JSClass<JSCounter>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
75 JSClass<JSCounter>::InheritAndBind<JSContainerBase>(globalObj);
76 }
77
JsEnableDec(const JSCallbackInfo & args)78 void JSCounter::JsEnableDec(const JSCallbackInfo& args)
79 {
80 if (args.Length() < 1) {
81 return;
82 }
83 if (!args[0]->IsBoolean()) {
84 CounterModel::GetInstance()->SetEnableDec(true);
85 } else {
86 CounterModel::GetInstance()->SetEnableDec(args[0]->ToBoolean());
87 }
88 }
89
JsEnableInc(const JSCallbackInfo & args)90 void JSCounter::JsEnableInc(const JSCallbackInfo& args)
91 {
92 if (args.Length() < 1) {
93 return;
94 }
95 if (!args[0]->IsBoolean()) {
96 CounterModel::GetInstance()->SetEnableInc(true);
97 } else {
98 CounterModel::GetInstance()->SetEnableInc(args[0]->ToBoolean());
99 }
100 }
101
JsOnInc(const JSCallbackInfo & args)102 void JSCounter::JsOnInc(const JSCallbackInfo& args)
103 {
104 if (args.Length() < 1) {
105 return;
106 }
107 if (!args[0]->IsFunction()) {
108 return;
109 }
110 CounterModel::GetInstance()->SetOnInc(
111 JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
112 args.ReturnSelf();
113 }
114
JsOnDec(const JSCallbackInfo & args)115 void JSCounter::JsOnDec(const JSCallbackInfo& args)
116 {
117 if (args.Length() < 1) {
118 return;
119 }
120 if (!args[0]->IsFunction()) {
121 return;
122 }
123 CounterModel::GetInstance()->SetOnDec(
124 JsEventCallback<void()>(args.GetExecutionContext(), JSRef<JSFunc>::Cast(args[0])));
125 args.ReturnSelf();
126 }
127
JSHeight(const JSCallbackInfo & args)128 void JSCounter::JSHeight(const JSCallbackInfo& args)
129 {
130 if (args.Length() < 1) {
131 return;
132 }
133
134 Dimension value;
135 if (!ConvertFromJSValue(args[0], value)) {
136 return;
137 }
138
139 if (LessNotEqual(value.Value(), 0.0)) {
140 return;
141 }
142 CounterModel::GetInstance()->SetHeight(value);
143 args.ReturnSelf();
144 }
145
JSWidth(const JSCallbackInfo & args)146 void JSCounter::JSWidth(const JSCallbackInfo& args)
147 {
148 if (args.Length() < 1) {
149 return;
150 }
151
152 Dimension value;
153 if (!ConvertFromJSValue(args[0], value)) {
154 return;
155 }
156
157 if (LessNotEqual(value.Value(), 0.0)) {
158 return;
159 }
160 CounterModel::GetInstance()->SetWidth(value);
161 args.ReturnSelf();
162 }
163
SetSize(const JSCallbackInfo & args)164 void JSCounter::SetSize(const JSCallbackInfo& args)
165 {
166 if (args.Length() >= 1 && args[0]->IsObject()) {
167 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
168
169 Dimension height;
170 if (ConvertFromJSValue(obj->GetProperty("height"), height) && height.IsValid()) {
171 if (GreatOrEqual(height.Value(), 0.0)) {
172 CounterModel::GetInstance()->SetHeight(height);
173 }
174 }
175
176 Dimension width;
177 if (ConvertFromJSValue(obj->GetProperty("width"), width) && width.IsValid()) {
178 if (GreatOrEqual(width.Value(), 0.0)) {
179 CounterModel::GetInstance()->SetWidth(width);
180 }
181 }
182 }
183 args.ReturnSelf();
184 }
185
JSControlWidth(const JSCallbackInfo & args)186 void JSCounter::JSControlWidth(const JSCallbackInfo& args)
187 {
188 if (args.Length() < 1) {
189 return;
190 }
191
192 Dimension value;
193 if (!ConvertFromJSValue(args[0], value)) {
194 return;
195 }
196 CounterModel::GetInstance()->SetControlWidth(value);
197 args.ReturnSelf();
198 }
199
JSStateChange(bool state)200 void JSCounter::JSStateChange(bool state)
201 {
202 CounterModel::GetInstance()->SetStateChange(state);
203 }
204
JsBackgroundColor(const JSCallbackInfo & args)205 void JSCounter::JsBackgroundColor(const JSCallbackInfo& args)
206 {
207 if (args.Length() < 1) {
208 return;
209 }
210
211 Color color;
212 if (!ParseJsColor(args[0], color)) {
213 return;
214 }
215 CounterModel::GetInstance()->SetBackgroundColor(color);
216 args.ReturnSelf();
217 }
218
Create()219 void JSCounter::Create()
220 {
221 CounterModel::GetInstance()->Create();
222 JSCounterTheme::ApplyTheme();
223 }
224
225 } // namespace OHOS::Ace::Framework
226