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