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 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
17 
18 namespace OHOS::Ace::NG {
SetEnableInc(ArkUIRuntimeCallInfo * runtimeCallInfo)19 ArkUINativeModuleValue CounterBridge::SetEnableInc(ArkUIRuntimeCallInfo* runtimeCallInfo)
20 {
21     EcmaVM* vm = runtimeCallInfo->GetVM();
22     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
23     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
24     Local<JSValueRef> flagArg = runtimeCallInfo->GetCallArgRef(1);
25     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
26     if (flagArg->IsUndefined() || !flagArg->IsBoolean()) {
27         GetArkUINodeModifiers()->getCounterModifier()->resetEnableInc(nativeNode);
28         return panda::JSValueRef::Undefined(vm);
29     }
30     bool flag = flagArg->ToBoolean(vm)->Value();
31     GetArkUINodeModifiers()->getCounterModifier()->setEnableInc(nativeNode, flag);
32     return panda::JSValueRef::Undefined(vm);
33 }
34 
ResetEnableInc(ArkUIRuntimeCallInfo * runtimeCallInfo)35 ArkUINativeModuleValue CounterBridge::ResetEnableInc(ArkUIRuntimeCallInfo* runtimeCallInfo)
36 {
37     EcmaVM* vm = runtimeCallInfo->GetVM();
38     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
39     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
40     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
41     GetArkUINodeModifiers()->getCounterModifier()->resetEnableInc(nativeNode);
42     return panda::JSValueRef::Undefined(vm);
43 }
44 
SetEnableDec(ArkUIRuntimeCallInfo * runtimeCallInfo)45 ArkUINativeModuleValue CounterBridge::SetEnableDec(ArkUIRuntimeCallInfo* runtimeCallInfo)
46 {
47     EcmaVM* vm = runtimeCallInfo->GetVM();
48     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
49     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
50     Local<JSValueRef> flagArg = runtimeCallInfo->GetCallArgRef(1);
51     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
52     if (flagArg->IsUndefined() || !flagArg->IsBoolean()) {
53         GetArkUINodeModifiers()->getCounterModifier()->resetEnableDec(nativeNode);
54         return panda::JSValueRef::Undefined(vm);
55     }
56     bool flag = flagArg->ToBoolean(vm)->Value();
57     GetArkUINodeModifiers()->getCounterModifier()->setEnableDec(nativeNode, flag);
58     return panda::JSValueRef::Undefined(vm);
59 }
60 
ResetEnableDec(ArkUIRuntimeCallInfo * runtimeCallInfo)61 ArkUINativeModuleValue CounterBridge::ResetEnableDec(ArkUIRuntimeCallInfo* runtimeCallInfo)
62 {
63     EcmaVM* vm = runtimeCallInfo->GetVM();
64     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
65     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
66     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
67     GetArkUINodeModifiers()->getCounterModifier()->resetEnableDec(nativeNode);
68     return panda::JSValueRef::Undefined(vm);
69 }
70 
SetCounterHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)71 ArkUINativeModuleValue CounterBridge::SetCounterHeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
72 {
73     EcmaVM* vm = runtimeCallInfo->GetVM();
74     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
75     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
76     Local<JSValueRef> heightValue = runtimeCallInfo->GetCallArgRef(1);
77     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
78 
79     CalcDimension height;
80     ArkTSUtils::ParseJsDimensionVp(vm, heightValue, height, false);
81     if (LessNotEqual(height.Value(), 0.0)) {
82         GetArkUINodeModifiers()->getCounterModifier()->resetCounterHeight(nativeNode);
83         return panda::JSValueRef::Undefined(vm);
84     }
85     GetArkUINodeModifiers()->getCounterModifier()->setCounterHeight(
86         nativeNode, height.Value(), static_cast<int>(height.Unit()));
87     return panda::JSValueRef::Undefined(vm);
88 }
89 
ResetCounterHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)90 ArkUINativeModuleValue CounterBridge::ResetCounterHeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
91 {
92     EcmaVM* vm = runtimeCallInfo->GetVM();
93     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
94     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
95     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
96     GetArkUINodeModifiers()->getCounterModifier()->resetCounterHeight(nativeNode);
97     return panda::JSValueRef::Undefined(vm);
98 }
99 
SetCounterWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)100 ArkUINativeModuleValue CounterBridge::SetCounterWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
101 {
102     EcmaVM* vm = runtimeCallInfo->GetVM();
103     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
104     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
105     Local<JSValueRef> widthValue = runtimeCallInfo->GetCallArgRef(1);
106     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
107 
108     CalcDimension width;
109     ArkTSUtils::ParseJsDimensionVp(vm, widthValue, width, false);
110     if (LessNotEqual(width.Value(), 0.0)) {
111         GetArkUINodeModifiers()->getCounterModifier()->resetCounterWidth(nativeNode);
112         return panda::JSValueRef::Undefined(vm);
113     }
114 
115     GetArkUINodeModifiers()->getCounterModifier()->setCounterWidth(
116         nativeNode, width.Value(), static_cast<int>(width.Unit()));
117     return panda::JSValueRef::Undefined(vm);
118 }
119 
ResetCounterWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)120 ArkUINativeModuleValue CounterBridge::ResetCounterWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
121 {
122     EcmaVM* vm = runtimeCallInfo->GetVM();
123     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
124     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
125     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
126     GetArkUINodeModifiers()->getCounterModifier()->resetCounterWidth(nativeNode);
127     return panda::JSValueRef::Undefined(vm);
128 }
129 
SetCounterBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)130 ArkUINativeModuleValue CounterBridge::SetCounterBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
131 {
132     EcmaVM* vm = runtimeCallInfo->GetVM();
133     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
134     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
135     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
136     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
137     Color color;
138     if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color)) {
139         GetArkUINodeModifiers()->getCounterModifier()->resetCounterBackgroundColor(nativeNode);
140     } else {
141         GetArkUINodeModifiers()->getCounterModifier()->setCounterBackgroundColor(nativeNode, color.GetValue());
142     }
143     return panda::JSValueRef::Undefined(vm);
144 }
145 
ResetCounterBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)146 ArkUINativeModuleValue CounterBridge::ResetCounterBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
147 {
148     EcmaVM* vm = runtimeCallInfo->GetVM();
149     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
150     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
151     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
152     GetArkUINodeModifiers()->getCounterModifier()->resetCounterBackgroundColor(nativeNode);
153     return panda::JSValueRef::Undefined(vm);
154 }
155 
SetCounterSize(ArkUIRuntimeCallInfo * runtimeCallInfo)156 ArkUINativeModuleValue CounterBridge::SetCounterSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
157 {
158     EcmaVM* vm = runtimeCallInfo->GetVM();
159     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
160     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
161     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
162     Local<JSValueRef> widthValue = runtimeCallInfo->GetCallArgRef(1); // 1: width Value
163     Local<JSValueRef> heightValue = runtimeCallInfo->GetCallArgRef(2); // 2: height Value
164     CalcDimension width;
165     ArkTSUtils::ParseJsDimensionVp(vm, widthValue, width, false);
166     if (GreatNotEqual(width.Value(), 0.0)) {
167         GetArkUINodeModifiers()->getCounterModifier()->setCounterWidth(
168             nativeNode, width.Value(), static_cast<int>(width.Unit()));
169     }
170     CalcDimension height;
171     ArkTSUtils::ParseJsDimensionVp(vm, heightValue, height, false);
172     if (GreatNotEqual(height.Value(), 0.0)) {
173         GetArkUINodeModifiers()->getCounterModifier()->setCounterHeight(
174             nativeNode, height.Value(), static_cast<int>(height.Unit()));
175     }
176     return panda::JSValueRef::Undefined(vm);
177 }
178 
ResetCounterSize(ArkUIRuntimeCallInfo * runtimeCallInfo)179 ArkUINativeModuleValue CounterBridge::ResetCounterSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
180 {
181     EcmaVM* vm = runtimeCallInfo->GetVM();
182     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
183     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
184     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
185     GetArkUINodeModifiers()->getCounterModifier()->resetCounterWidth(nativeNode);
186     GetArkUINodeModifiers()->getCounterModifier()->resetCounterHeight(nativeNode);
187     return panda::JSValueRef::Undefined(vm);
188 }
189 } // namespace OHOS::Ace::NG
190