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_line_bridge.h"
16
17 #include "core/interfaces/native/node/api.h"
18 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19
20 namespace OHOS::Ace::NG {
SetStartPoint(ArkUIRuntimeCallInfo * runtimeCallInfo)21 ArkUINativeModuleValue LineBridge::SetStartPoint(ArkUIRuntimeCallInfo* runtimeCallInfo)
22 {
23 EcmaVM* vm = runtimeCallInfo->GetVM();
24 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
25 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
26 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
27 Local<JSValueRef> jsValue = runtimeCallInfo->GetCallArgRef(1);
28
29 if (!jsValue->IsArray(vm)) {
30 GetArkUIInternalNodeAPI()->GetLineModifier().ResetStartPoint(nativeNode);
31 return panda::JSValueRef::Undefined(vm);
32 }
33
34 auto arrayVal = panda::Local<panda::ArrayRef>(jsValue);
35 auto length = arrayVal->Length(vm);
36 if (length <= 0) {
37 GetArkUIInternalNodeAPI()->GetLineModifier().ResetStartPoint(nativeNode);
38 return panda::JSValueRef::Undefined(vm);
39 }
40
41 CalcDimension star;
42 CalcDimension end;
43 std::string calcStr;
44 Local<JSValueRef> starItem = panda::ArrayRef::GetValueAt(vm, arrayVal, 0);
45 Local<JSValueRef> endItem = panda::ArrayRef::GetValueAt(vm, arrayVal, 1);
46 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, starItem, star, false)) {
47 star = CalcDimension(0, DimensionUnit::VP);
48 }
49 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, endItem, end, false)) {
50 end = CalcDimension(0, DimensionUnit::VP);
51 }
52
53 std::vector<double> pointValues;
54 std::vector<int32_t> pointUnits;
55 std::vector<const char*> pointStr;
56 pointUnits.push_back(static_cast<int>(star.Unit()));
57 pointUnits.push_back(static_cast<int>(end.Unit()));
58 if (star.Unit() == DimensionUnit::CALC) {
59 pointValues.push_back(0);
60 pointStr.push_back(star.CalcValue().c_str());
61 } else {
62 pointValues.push_back(star.Value());
63 pointStr.push_back(calcStr.c_str());
64 }
65
66 if (end.Unit() == DimensionUnit::CALC) {
67 pointValues.push_back(0);
68 pointStr.push_back(end.CalcValue().c_str());
69 } else {
70 pointValues.push_back(end.Value());
71 pointStr.push_back(calcStr.c_str());
72 }
73
74 GetArkUIInternalNodeAPI()->GetLineModifier().
75 SetStartPoint(nativeNode, pointValues.data(), pointUnits.data(), pointStr.data());
76 return panda::JSValueRef::Undefined(vm);
77 }
78
ResetStartPoint(ArkUIRuntimeCallInfo * runtimeCallInfo)79 ArkUINativeModuleValue LineBridge::ResetStartPoint(ArkUIRuntimeCallInfo* runtimeCallInfo)
80 {
81 EcmaVM* vm = runtimeCallInfo->GetVM();
82 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
83 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
84 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
85 GetArkUIInternalNodeAPI()->GetLineModifier().ResetStartPoint(nativeNode);
86 return panda::JSValueRef::Undefined(vm);
87 }
88
SetEndPoint(ArkUIRuntimeCallInfo * runtimeCallInfo)89 ArkUINativeModuleValue LineBridge::SetEndPoint(ArkUIRuntimeCallInfo* runtimeCallInfo)
90 {
91 EcmaVM* vm = runtimeCallInfo->GetVM();
92 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
93 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
94 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
95 Local<JSValueRef> jsValue = runtimeCallInfo->GetCallArgRef(1);
96
97 if (!jsValue->IsArray(vm)) {
98 GetArkUIInternalNodeAPI()->GetLineModifier().ResetEndPoint(nativeNode);
99 return panda::JSValueRef::Undefined(vm);
100 }
101
102 auto arrayVal = panda::Local<panda::ArrayRef>(jsValue);
103 auto length = arrayVal->Length(vm);
104 if (length <= 0) {
105 GetArkUIInternalNodeAPI()->GetLineModifier().ResetEndPoint(nativeNode);
106 return panda::JSValueRef::Undefined(vm);
107 }
108
109 CalcDimension star;
110 CalcDimension end;
111 std::string calcStr;
112 Local<JSValueRef> starItem = panda::ArrayRef::GetValueAt(vm, arrayVal, 0);
113 Local<JSValueRef> endItem = panda::ArrayRef::GetValueAt(vm, arrayVal, 1);
114 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, starItem, star, false)) {
115 star = CalcDimension(0, DimensionUnit::VP);
116 }
117 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, endItem, end, false)) {
118 end = CalcDimension(0, DimensionUnit::VP);
119 }
120
121 std::vector<double> pointValues;
122 std::vector<int32_t> pointUnits;
123 std::vector<const char*> pointStr;
124 pointUnits.push_back(static_cast<int>(star.Unit()));
125 pointUnits.push_back(static_cast<int>(end.Unit()));
126 if (star.Unit() == DimensionUnit::CALC) {
127 pointValues.push_back(0);
128 pointStr.push_back(star.CalcValue().c_str());
129 } else {
130 pointValues.push_back(star.Value());
131 pointStr.push_back(calcStr.c_str());
132 }
133
134 if (end.Unit() == DimensionUnit::CALC) {
135 pointValues.push_back(0);
136 pointStr.push_back(end.CalcValue().c_str());
137 } else {
138 pointValues.push_back(end.Value());
139 pointStr.push_back(calcStr.c_str());
140 }
141
142 GetArkUIInternalNodeAPI()->GetLineModifier().
143 SetEndPoint(nativeNode, pointValues.data(), pointUnits.data(), pointStr.data());
144 return panda::JSValueRef::Undefined(vm);
145 }
146
ResetEndPoint(ArkUIRuntimeCallInfo * runtimeCallInfo)147 ArkUINativeModuleValue LineBridge::ResetEndPoint(ArkUIRuntimeCallInfo* runtimeCallInfo)
148 {
149 EcmaVM* vm = runtimeCallInfo->GetVM();
150 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
151 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
152 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
153 GetArkUIInternalNodeAPI()->GetLineModifier().ResetEndPoint(nativeNode);
154 return panda::JSValueRef::Undefined(vm);
155 }
156 }