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 RefPtr<ResourceObject> heightResObj;
88 ArkTSUtils::ParseJsDimensionVp(vm, heightValue, height, heightResObj, false);
89 if (heightValue->IsObject(vm)) {
90 auto obj = heightValue->ToObject(vm);
91 auto layoutPolicy = obj->Get(vm, panda::StringRef::NewFromUtf8(vm, "id_"));
92 if (layoutPolicy->IsString(vm)) {
93 auto policy = ParseLayoutPolicy(layoutPolicy->ToString(vm)->ToString(vm));
94 ViewAbstractModel::GetInstance()->UpdateLayoutPolicyProperty(policy, false);
95 return panda::JSValueRef::Undefined(vm);
96 }
97 } else {
98 ViewAbstractModel::GetInstance()->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, false);
99 }
100 if (LessNotEqual(height.Value(), 0.0)) {
101 GetArkUINodeModifiers()->getCounterModifier()->resetCounterHeight(nativeNode);
102 return panda::JSValueRef::Undefined(vm);
103 }
104 auto heightRawPtr = AceType::RawPtr(heightResObj);
105 GetArkUINodeModifiers()->getCounterModifier()->setCounterHeightRes(
106 nativeNode, height.Value(), static_cast<int>(height.Unit()), heightRawPtr);
107 return panda::JSValueRef::Undefined(vm);
108 }
109
ResetCounterHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)110 ArkUINativeModuleValue CounterBridge::ResetCounterHeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
111 {
112 EcmaVM* vm = runtimeCallInfo->GetVM();
113 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
114 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
115 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
116 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
117 GetArkUINodeModifiers()->getCounterModifier()->resetCounterHeight(nativeNode);
118 ViewAbstractModel::GetInstance()->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, false);
119 return panda::JSValueRef::Undefined(vm);
120 }
121
SetCounterWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)122 ArkUINativeModuleValue CounterBridge::SetCounterWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
123 {
124 EcmaVM* vm = runtimeCallInfo->GetVM();
125 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
126 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
127 Local<JSValueRef> widthValue = runtimeCallInfo->GetCallArgRef(1);
128 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
129 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
130
131 CalcDimension width;
132 RefPtr<ResourceObject> widthResObj;
133 ArkTSUtils::ParseJsDimensionVp(vm, widthValue, width, widthResObj, false);
134 if (widthValue->IsObject(vm)) {
135 auto obj = widthValue->ToObject(vm);
136 auto layoutPolicy = obj->Get(vm, panda::StringRef::NewFromUtf8(vm, "id_"));
137 if (layoutPolicy->IsString(vm)) {
138 auto policy = ParseLayoutPolicy(layoutPolicy->ToString(vm)->ToString(vm));
139 ViewAbstractModel::GetInstance()->UpdateLayoutPolicyProperty(policy, true);
140 return panda::JSValueRef::Undefined(vm);
141 }
142 } else {
143 ViewAbstractModel::GetInstance()->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, true);
144 }
145 if (LessNotEqual(width.Value(), 0.0)) {
146 GetArkUINodeModifiers()->getCounterModifier()->resetCounterWidth(nativeNode);
147 return panda::JSValueRef::Undefined(vm);
148 }
149 auto widthRawPtr = AceType::RawPtr(widthResObj);
150 GetArkUINodeModifiers()->getCounterModifier()->setCounterWidthRes(
151 nativeNode, width.Value(), static_cast<int>(width.Unit()), widthRawPtr);
152 return panda::JSValueRef::Undefined(vm);
153 }
154
ResetCounterWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)155 ArkUINativeModuleValue CounterBridge::ResetCounterWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
156 {
157 EcmaVM* vm = runtimeCallInfo->GetVM();
158 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
159 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
160 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
161 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
162 GetArkUINodeModifiers()->getCounterModifier()->resetCounterWidth(nativeNode);
163 ViewAbstractModel::GetInstance()->UpdateLayoutPolicyProperty(LayoutCalPolicy::NO_MATCH, true);
164 return panda::JSValueRef::Undefined(vm);
165 }
166
SetCounterBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)167 ArkUINativeModuleValue CounterBridge::SetCounterBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
168 {
169 EcmaVM* vm = runtimeCallInfo->GetVM();
170 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
171 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
172 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
173 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
174 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
175 Color color;
176 RefPtr<ResourceObject> colorResObj;
177 auto nodeInfo = ArkTSUtils::MakeNativeNodeInfo(nativeNode);
178 if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color, colorResObj, nodeInfo)) {
179 GetArkUINodeModifiers()->getCounterModifier()->resetCounterBackgroundColor(nativeNode);
180 } else {
181 auto colorRawPtr = AceType::RawPtr(colorResObj);
182 GetArkUINodeModifiers()->getCounterModifier()->setCounterBackgroundColorRes(
183 nativeNode, color.GetValue(), color.GetColorSpace(), colorRawPtr);
184 }
185 return panda::JSValueRef::Undefined(vm);
186 }
187
ResetCounterBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)188 ArkUINativeModuleValue CounterBridge::ResetCounterBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
189 {
190 EcmaVM* vm = runtimeCallInfo->GetVM();
191 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
192 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
193 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
194 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
195 GetArkUINodeModifiers()->getCounterModifier()->resetCounterBackgroundColor(nativeNode);
196 return panda::JSValueRef::Undefined(vm);
197 }
198
SetCounterSize(ArkUIRuntimeCallInfo * runtimeCallInfo)199 ArkUINativeModuleValue CounterBridge::SetCounterSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
200 {
201 EcmaVM* vm = runtimeCallInfo->GetVM();
202 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
203 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
204 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
205 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
206 Local<JSValueRef> widthValue = runtimeCallInfo->GetCallArgRef(1); // 1: width Value
207 Local<JSValueRef> heightValue = runtimeCallInfo->GetCallArgRef(2); // 2: height Value
208 CalcDimension width;
209 RefPtr<ResourceObject> widthResObj;
210 ArkTSUtils::ParseJsDimensionVp(vm, widthValue, width, widthResObj, false);
211 if (GreatNotEqual(width.Value(), 0.0)) {
212 auto widthRawPtr = AceType::RawPtr(widthResObj);
213 GetArkUINodeModifiers()->getCounterModifier()->setCounterWidthRes(
214 nativeNode, width.Value(), static_cast<int>(width.Unit()), widthRawPtr);
215 } else {
216 GetArkUINodeModifiers()->getCounterModifier()->resetCounterWidth(nativeNode);
217 }
218 CalcDimension height;
219 RefPtr<ResourceObject> heightResObj;
220 ArkTSUtils::ParseJsDimensionVp(vm, heightValue, height, heightResObj, false);
221 if (GreatNotEqual(height.Value(), 0.0)) {
222 auto heightRawPtr = AceType::RawPtr(heightResObj);
223 GetArkUINodeModifiers()->getCounterModifier()->setCounterHeightRes(
224 nativeNode, height.Value(), static_cast<int>(height.Unit()), heightRawPtr);
225 } else {
226 GetArkUINodeModifiers()->getCounterModifier()->resetCounterHeight(nativeNode);
227 }
228 return panda::JSValueRef::Undefined(vm);
229 }
230
ResetCounterSize(ArkUIRuntimeCallInfo * runtimeCallInfo)231 ArkUINativeModuleValue CounterBridge::ResetCounterSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
232 {
233 EcmaVM* vm = runtimeCallInfo->GetVM();
234 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
235 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
236 CHECK_NULL_RETURN(nodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
237 auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
238 GetArkUINodeModifiers()->getCounterModifier()->resetCounterWidth(nativeNode);
239 GetArkUINodeModifiers()->getCounterModifier()->resetCounterHeight(nativeNode);
240 return panda::JSValueRef::Undefined(vm);
241 }
242
SetCounterOnInc(ArkUIRuntimeCallInfo * runtimeCallInfo)243 ArkUINativeModuleValue CounterBridge::SetCounterOnInc(ArkUIRuntimeCallInfo* runtimeCallInfo)
244 {
245 EcmaVM* vm = runtimeCallInfo->GetVM();
246 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
247 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
248 Local<JSValueRef> callbackArg = runtimeCallInfo->GetCallArgRef(1);
249 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
250 if (callbackArg->IsUndefined() || callbackArg->IsNull() || !callbackArg->IsFunction(vm)) {
251 GetArkUINodeModifiers()->getCounterModifier()->resetCounterOnInc(nativeNode);
252 return panda::JSValueRef::Undefined(vm);
253 }
254 auto frameNode = reinterpret_cast<FrameNode*>(nativeNode);
255 panda::Local<panda::FunctionRef> func = callbackArg->ToObject(vm);
256 CHECK_NULL_RETURN(frameNode, panda::JSValueRef::Undefined(vm));
257 std::function<void()> callback = [vm, frameNode, func = panda::CopyableGlobal(vm, func)]() {
258 panda::LocalScope pandaScope(vm);
259 panda::TryCatch trycatch(vm);
260 PipelineContext::SetCallBackNode(AceType::WeakClaim(frameNode));
261 func->Call(vm, func.ToLocal(), nullptr, 0);
262 };
263 GetArkUINodeModifiers()->getCounterModifier()->setCounterOnInc(nativeNode, reinterpret_cast<void*>(&callback));
264 return panda::JSValueRef::Undefined(vm);
265 }
266
ResetCounterOnInc(ArkUIRuntimeCallInfo * runtimeCallInfo)267 ArkUINativeModuleValue CounterBridge::ResetCounterOnInc(ArkUIRuntimeCallInfo* runtimeCallInfo)
268 {
269 EcmaVM* vm = runtimeCallInfo->GetVM();
270 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
271 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
272 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
273 CHECK_NULL_RETURN(nativeNode, panda::JSValueRef::Undefined(vm));
274 GetArkUINodeModifiers()->getCounterModifier()->resetCounterOnInc(nativeNode);
275 return panda::JSValueRef::Undefined(vm);
276 }
277
SetCounterOnDec(ArkUIRuntimeCallInfo * runtimeCallInfo)278 ArkUINativeModuleValue CounterBridge::SetCounterOnDec(ArkUIRuntimeCallInfo* runtimeCallInfo)
279 {
280 EcmaVM* vm = runtimeCallInfo->GetVM();
281 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
282 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
283 Local<JSValueRef> callbackArg = runtimeCallInfo->GetCallArgRef(1);
284 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
285 if (callbackArg->IsUndefined() || callbackArg->IsNull() || !callbackArg->IsFunction(vm)) {
286 GetArkUINodeModifiers()->getCounterModifier()->resetCounterOnDec(nativeNode);
287 return panda::JSValueRef::Undefined(vm);
288 }
289 auto frameNode = reinterpret_cast<FrameNode*>(nativeNode);
290 panda::Local<panda::FunctionRef> func = callbackArg->ToObject(vm);
291 CHECK_NULL_RETURN(frameNode, panda::JSValueRef::Undefined(vm));
292 std::function<void()> callback = [vm, frameNode, func = panda::CopyableGlobal(vm, func)]() {
293 panda::LocalScope pandaScope(vm);
294 panda::TryCatch trycatch(vm);
295 PipelineContext::SetCallBackNode(AceType::WeakClaim(frameNode));
296 func->Call(vm, func.ToLocal(), nullptr, 0);
297 };
298 GetArkUINodeModifiers()->getCounterModifier()->setCounterOnDec(nativeNode, reinterpret_cast<void*>(&callback));
299 return panda::JSValueRef::Undefined(vm);
300 }
301
ResetCounterOnDec(ArkUIRuntimeCallInfo * runtimeCallInfo)302 ArkUINativeModuleValue CounterBridge::ResetCounterOnDec(ArkUIRuntimeCallInfo* runtimeCallInfo)
303 {
304 EcmaVM* vm = runtimeCallInfo->GetVM();
305 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
306 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
307 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
308 CHECK_NULL_RETURN(nativeNode, panda::JSValueRef::Undefined(vm));
309 GetArkUINodeModifiers()->getCounterModifier()->resetCounterOnDec(nativeNode);
310 return panda::JSValueRef::Undefined(vm);
311 }
312 } // namespace OHOS::Ace::NG
313