1 /*
2 * Copyright (c) 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 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_counter_bridge.h"
16
17 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_frame_node_bridge.h"
18 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19
20 namespace OHOS::Ace::NG {
SetEnableInc(ArkUIRuntimeCallInfo * runtimeCallInfo)21 ArkUINativeModuleValue CounterBridge::SetEnableInc(ArkUIRuntimeCallInfo* runtimeCallInfo)
22 {
23 EcmaVM* vm = runtimeCallInfo->GetVM();
24 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
25 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
26 Local<JSValueRef> flagArg = runtimeCallInfo->GetCallArgRef(1);
27 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
28 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
29 if (flagArg->IsUndefined() || !flagArg->IsBoolean()) {
30 GetArkUINodeModifiers()->getCounterModifier()->resetEnableInc(nativeNode);
31 return panda::JSValueRef::Undefined(vm);
32 }
33 bool flag = flagArg->ToBoolean(vm)->Value();
34 GetArkUINodeModifiers()->getCounterModifier()->setEnableInc(nativeNode, flag);
35 return panda::JSValueRef::Undefined(vm);
36 }
37
ResetEnableInc(ArkUIRuntimeCallInfo * runtimeCallInfo)38 ArkUINativeModuleValue CounterBridge::ResetEnableInc(ArkUIRuntimeCallInfo* runtimeCallInfo)
39 {
40 EcmaVM* vm = runtimeCallInfo->GetVM();
41 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
42 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
43 CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
44 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
45 GetArkUINodeModifiers()->getCounterModifier()->resetEnableInc(nativeNode);
46 return panda::JSValueRef::Undefined(vm);
47 }
48
SetEnableDec(ArkUIRuntimeCallInfo * runtimeCallInfo)49 ArkUINativeModuleValue CounterBridge::SetEnableDec(ArkUIRuntimeCallInfo* runtimeCallInfo)
50 {
51 EcmaVM* vm = runtimeCallInfo->GetVM();
52 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
53 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
54 Local<JSValueRef> flagArg = runtimeCallInfo->GetCallArgRef(1);
55 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
56 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
57 if (flagArg->IsUndefined() || !flagArg->IsBoolean()) {
58 GetArkUINodeModifiers()->getCounterModifier()->resetEnableDec(nativeNode);
59 return panda::JSValueRef::Undefined(vm);
60 }
61 bool flag = flagArg->ToBoolean(vm)->Value();
62 GetArkUINodeModifiers()->getCounterModifier()->setEnableDec(nativeNode, flag);
63 return panda::JSValueRef::Undefined(vm);
64 }
65
ResetEnableDec(ArkUIRuntimeCallInfo * runtimeCallInfo)66 ArkUINativeModuleValue CounterBridge::ResetEnableDec(ArkUIRuntimeCallInfo* runtimeCallInfo)
67 {
68 EcmaVM* vm = runtimeCallInfo->GetVM();
69 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
70 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
71 CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
72 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
73 GetArkUINodeModifiers()->getCounterModifier()->resetEnableDec(nativeNode);
74 return panda::JSValueRef::Undefined(vm);
75 }
76
SetCounterHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)77 ArkUINativeModuleValue CounterBridge::SetCounterHeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
78 {
79 EcmaVM* vm = runtimeCallInfo->GetVM();
80 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
81 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
82 Local<JSValueRef> heightValue = runtimeCallInfo->GetCallArgRef(1);
83 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
84 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
85
86 CalcDimension height;
87 ArkTSUtils::ParseJsDimensionVp(vm, heightValue, height, false);
88 if (LessNotEqual(height.Value(), 0.0)) {
89 GetArkUINodeModifiers()->getCounterModifier()->resetCounterHeight(nativeNode);
90 return panda::JSValueRef::Undefined(vm);
91 }
92 GetArkUINodeModifiers()->getCounterModifier()->setCounterHeight(
93 nativeNode, height.Value(), static_cast<int>(height.Unit()));
94 return panda::JSValueRef::Undefined(vm);
95 }
96
ResetCounterHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)97 ArkUINativeModuleValue CounterBridge::ResetCounterHeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
98 {
99 EcmaVM* vm = runtimeCallInfo->GetVM();
100 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
101 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
102 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
103 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
104 GetArkUINodeModifiers()->getCounterModifier()->resetCounterHeight(nativeNode);
105 return panda::JSValueRef::Undefined(vm);
106 }
107
SetCounterWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)108 ArkUINativeModuleValue CounterBridge::SetCounterWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
109 {
110 EcmaVM* vm = runtimeCallInfo->GetVM();
111 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
112 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
113 Local<JSValueRef> widthValue = runtimeCallInfo->GetCallArgRef(1);
114 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
115 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
116
117 CalcDimension width;
118 ArkTSUtils::ParseJsDimensionVp(vm, widthValue, width, false);
119 if (LessNotEqual(width.Value(), 0.0)) {
120 GetArkUINodeModifiers()->getCounterModifier()->resetCounterWidth(nativeNode);
121 return panda::JSValueRef::Undefined(vm);
122 }
123
124 GetArkUINodeModifiers()->getCounterModifier()->setCounterWidth(
125 nativeNode, width.Value(), static_cast<int>(width.Unit()));
126 return panda::JSValueRef::Undefined(vm);
127 }
128
ResetCounterWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)129 ArkUINativeModuleValue CounterBridge::ResetCounterWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
130 {
131 EcmaVM* vm = runtimeCallInfo->GetVM();
132 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
133 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
134 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
135 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
136 GetArkUINodeModifiers()->getCounterModifier()->resetCounterWidth(nativeNode);
137 return panda::JSValueRef::Undefined(vm);
138 }
139
SetCounterBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)140 ArkUINativeModuleValue CounterBridge::SetCounterBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
141 {
142 EcmaVM* vm = runtimeCallInfo->GetVM();
143 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
144 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
145 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
146 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
147 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
148 Color color;
149 if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color)) {
150 GetArkUINodeModifiers()->getCounterModifier()->resetCounterBackgroundColor(nativeNode);
151 } else {
152 GetArkUINodeModifiers()->getCounterModifier()->setCounterBackgroundColor(nativeNode, color.GetValue());
153 }
154 return panda::JSValueRef::Undefined(vm);
155 }
156
ResetCounterBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)157 ArkUINativeModuleValue CounterBridge::ResetCounterBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
158 {
159 EcmaVM* vm = runtimeCallInfo->GetVM();
160 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
161 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
162 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
163 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
164 GetArkUINodeModifiers()->getCounterModifier()->resetCounterBackgroundColor(nativeNode);
165 return panda::JSValueRef::Undefined(vm);
166 }
167
SetCounterSize(ArkUIRuntimeCallInfo * runtimeCallInfo)168 ArkUINativeModuleValue CounterBridge::SetCounterSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
169 {
170 EcmaVM* vm = runtimeCallInfo->GetVM();
171 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
172 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
173 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
174 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
175 Local<JSValueRef> widthValue = runtimeCallInfo->GetCallArgRef(1); // 1: width Value
176 Local<JSValueRef> heightValue = runtimeCallInfo->GetCallArgRef(2); // 2: height Value
177 CalcDimension width;
178 ArkTSUtils::ParseJsDimensionVp(vm, widthValue, width, false);
179 if (GreatNotEqual(width.Value(), 0.0)) {
180 GetArkUINodeModifiers()->getCounterModifier()->setCounterWidth(
181 nativeNode, width.Value(), static_cast<int>(width.Unit()));
182 }
183 CalcDimension height;
184 ArkTSUtils::ParseJsDimensionVp(vm, heightValue, height, false);
185 if (GreatNotEqual(height.Value(), 0.0)) {
186 GetArkUINodeModifiers()->getCounterModifier()->setCounterHeight(
187 nativeNode, height.Value(), static_cast<int>(height.Unit()));
188 }
189 return panda::JSValueRef::Undefined(vm);
190 }
191
ResetCounterSize(ArkUIRuntimeCallInfo * runtimeCallInfo)192 ArkUINativeModuleValue CounterBridge::ResetCounterSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
193 {
194 EcmaVM* vm = runtimeCallInfo->GetVM();
195 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
196 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
197 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
198 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
199 GetArkUINodeModifiers()->getCounterModifier()->resetCounterWidth(nativeNode);
200 GetArkUINodeModifiers()->getCounterModifier()->resetCounterHeight(nativeNode);
201 return panda::JSValueRef::Undefined(vm);
202 }
203
SetCounterOnInc(ArkUIRuntimeCallInfo * runtimeCallInfo)204 ArkUINativeModuleValue CounterBridge::SetCounterOnInc(ArkUIRuntimeCallInfo* runtimeCallInfo)
205 {
206 EcmaVM* vm = runtimeCallInfo->GetVM();
207 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
208 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
209 Local<JSValueRef> callbackArg = runtimeCallInfo->GetCallArgRef(1);
210 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
211 if (callbackArg->IsUndefined() || callbackArg->IsNull() || !callbackArg->IsFunction(vm)) {
212 GetArkUINodeModifiers()->getCounterModifier()->resetCounterOnInc(nativeNode);
213 return panda::JSValueRef::Undefined(vm);
214 }
215 auto frameNode = reinterpret_cast<FrameNode*>(nativeNode);
216 panda::Local<panda::FunctionRef> func = callbackArg->ToObject(vm);
217 CHECK_NULL_RETURN(frameNode, panda::JSValueRef::Undefined(vm));
218 std::function<void()> callback = [vm, frameNode, func = panda::CopyableGlobal(vm, func)]() {
219 panda::LocalScope pandaScope(vm);
220 panda::TryCatch trycatch(vm);
221 PipelineContext::SetCallBackNode(AceType::WeakClaim(frameNode));
222 func->Call(vm, func.ToLocal(), nullptr, 0);
223 };
224 GetArkUINodeModifiers()->getCounterModifier()->setCounterOnInc(nativeNode, reinterpret_cast<void*>(&callback));
225 return panda::JSValueRef::Undefined(vm);
226 }
227
ResetCounterOnInc(ArkUIRuntimeCallInfo * runtimeCallInfo)228 ArkUINativeModuleValue CounterBridge::ResetCounterOnInc(ArkUIRuntimeCallInfo* runtimeCallInfo)
229 {
230 EcmaVM* vm = runtimeCallInfo->GetVM();
231 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
232 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
233 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
234 CHECK_NULL_RETURN(nativeNode, panda::JSValueRef::Undefined(vm));
235 GetArkUINodeModifiers()->getCounterModifier()->resetCounterOnInc(nativeNode);
236 return panda::JSValueRef::Undefined(vm);
237 }
238
SetCounterOnDec(ArkUIRuntimeCallInfo * runtimeCallInfo)239 ArkUINativeModuleValue CounterBridge::SetCounterOnDec(ArkUIRuntimeCallInfo* runtimeCallInfo)
240 {
241 EcmaVM* vm = runtimeCallInfo->GetVM();
242 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
243 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
244 Local<JSValueRef> callbackArg = runtimeCallInfo->GetCallArgRef(1);
245 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
246 if (callbackArg->IsUndefined() || callbackArg->IsNull() || !callbackArg->IsFunction(vm)) {
247 GetArkUINodeModifiers()->getCounterModifier()->resetCounterOnDec(nativeNode);
248 return panda::JSValueRef::Undefined(vm);
249 }
250 auto frameNode = reinterpret_cast<FrameNode*>(nativeNode);
251 panda::Local<panda::FunctionRef> func = callbackArg->ToObject(vm);
252 CHECK_NULL_RETURN(frameNode, panda::JSValueRef::Undefined(vm));
253 std::function<void()> callback = [vm, frameNode, func = panda::CopyableGlobal(vm, func)]() {
254 panda::LocalScope pandaScope(vm);
255 panda::TryCatch trycatch(vm);
256 PipelineContext::SetCallBackNode(AceType::WeakClaim(frameNode));
257 func->Call(vm, func.ToLocal(), nullptr, 0);
258 };
259 GetArkUINodeModifiers()->getCounterModifier()->setCounterOnDec(nativeNode, reinterpret_cast<void*>(&callback));
260 return panda::JSValueRef::Undefined(vm);
261 }
262
ResetCounterOnDec(ArkUIRuntimeCallInfo * runtimeCallInfo)263 ArkUINativeModuleValue CounterBridge::ResetCounterOnDec(ArkUIRuntimeCallInfo* runtimeCallInfo)
264 {
265 EcmaVM* vm = runtimeCallInfo->GetVM();
266 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
267 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
268 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
269 CHECK_NULL_RETURN(nativeNode, panda::JSValueRef::Undefined(vm));
270 GetArkUINodeModifiers()->getCounterModifier()->resetCounterOnDec(nativeNode);
271 return panda::JSValueRef::Undefined(vm);
272 }
273 } // namespace OHOS::Ace::NG
274