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     std::string calcStr;
42     Local<JSValueRef> starItem = panda::ArrayRef::GetValueAt(vm, arrayVal, 0);
43     Local<JSValueRef> endItem = panda::ArrayRef::GetValueAt(vm, arrayVal, 1);
44     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, starItem, star, false)) {
45         star = CalcDimension(0, DimensionUnit::VP);
46     }
47     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, endItem, end, false)) {
48         end = CalcDimension(0, DimensionUnit::VP);
49     }
50 
51     std::vector<ArkUI_Float32> pointValues;
52     std::vector<int32_t> pointUnits;
53     std::vector<const char*> pointStr;
54     pointUnits.push_back(static_cast<int>(star.Unit()));
55     pointUnits.push_back(static_cast<int>(end.Unit()));
56     if (star.Unit() == DimensionUnit::CALC) {
57         pointValues.push_back(0);
58         pointStr.push_back(star.CalcValue().c_str());
59     } else {
60         pointValues.push_back(star.Value());
61         pointStr.push_back(calcStr.c_str());
62     }
63 
64     if (end.Unit() == DimensionUnit::CALC) {
65         pointValues.push_back(0);
66         pointStr.push_back(end.CalcValue().c_str());
67     } else {
68         pointValues.push_back(end.Value());
69         pointStr.push_back(calcStr.c_str());
70     }
71 
72     GetArkUINodeModifiers()->getLineModifier()->setStartPoint(nativeNode, pointValues.data(),
73         pointUnits.data(), pointStr.data());
74     return panda::JSValueRef::Undefined(vm);
75 }
76 
ResetStartPoint(ArkUIRuntimeCallInfo * runtimeCallInfo)77 ArkUINativeModuleValue LineBridge::ResetStartPoint(ArkUIRuntimeCallInfo* runtimeCallInfo)
78 {
79     EcmaVM* vm = runtimeCallInfo->GetVM();
80     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
81     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
82     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
83     GetArkUINodeModifiers()->getLineModifier()->resetStartPoint(nativeNode);
84     return panda::JSValueRef::Undefined(vm);
85 }
86 
SetEndPoint(ArkUIRuntimeCallInfo * runtimeCallInfo)87 ArkUINativeModuleValue LineBridge::SetEndPoint(ArkUIRuntimeCallInfo* runtimeCallInfo)
88 {
89     EcmaVM* vm = runtimeCallInfo->GetVM();
90     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
91     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
92     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
93     Local<JSValueRef> jsValue = runtimeCallInfo->GetCallArgRef(1);
94 
95     if (!jsValue->IsArray(vm)) {
96         GetArkUINodeModifiers()->getLineModifier()->resetEndPoint(nativeNode);
97         return panda::JSValueRef::Undefined(vm);
98     }
99 
100     auto arrayVal = panda::Local<panda::ArrayRef>(jsValue);
101     auto length = arrayVal->Length(vm);
102     if (length <= 0) {
103         GetArkUINodeModifiers()->getLineModifier()->resetEndPoint(nativeNode);
104         return panda::JSValueRef::Undefined(vm);
105     }
106 
107     CalcDimension star;
108     CalcDimension end;
109     std::string calcStr;
110     Local<JSValueRef> starItem = panda::ArrayRef::GetValueAt(vm, arrayVal, 0);
111     Local<JSValueRef> endItem = panda::ArrayRef::GetValueAt(vm, arrayVal, 1);
112     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, starItem, star, false)) {
113         star = CalcDimension(0, DimensionUnit::VP);
114     }
115     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, endItem, end, false)) {
116         end = CalcDimension(0, DimensionUnit::VP);
117     }
118 
119     std::vector<ArkUI_Float32> pointValues;
120     std::vector<int32_t> pointUnits;
121     std::vector<const char*> pointStr;
122     pointUnits.push_back(static_cast<int>(star.Unit()));
123     pointUnits.push_back(static_cast<int>(end.Unit()));
124     if (star.Unit() == DimensionUnit::CALC) {
125         pointValues.push_back(0);
126         pointStr.push_back(star.CalcValue().c_str());
127     } else {
128         pointValues.push_back(star.Value());
129         pointStr.push_back(calcStr.c_str());
130     }
131 
132     if (end.Unit() == DimensionUnit::CALC) {
133         pointValues.push_back(0);
134         pointStr.push_back(end.CalcValue().c_str());
135     } else {
136         pointValues.push_back(end.Value());
137         pointStr.push_back(calcStr.c_str());
138     }
139 
140     GetArkUINodeModifiers()->getLineModifier()->setEndPoint(nativeNode,
141         pointValues.data(), pointUnits.data(), pointStr.data());
142     return panda::JSValueRef::Undefined(vm);
143 }
144 
ResetEndPoint(ArkUIRuntimeCallInfo * runtimeCallInfo)145 ArkUINativeModuleValue LineBridge::ResetEndPoint(ArkUIRuntimeCallInfo* runtimeCallInfo)
146 {
147     EcmaVM* vm = runtimeCallInfo->GetVM();
148     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
149     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
150     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
151     GetArkUINodeModifiers()->getLineModifier()->resetEndPoint(nativeNode);
152     return panda::JSValueRef::Undefined(vm);
153 }
154 }