• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_gauge_bridge.h"
16 
17 #include "base/geometry/dimension.h"
18 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19 #include "bridge/declarative_frontend/jsview/js_linear_gradient.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components_ng/pattern/gauge/gauge_paint_property.h"
22 #include "core/components_ng/pattern/gauge/gauge_theme.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/pattern/gauge/gauge_model_ng.h"
25 
26 namespace OHOS::Ace::NG {
27 namespace {
28 constexpr Color ERROR_COLOR = Color(0xFFE84026);
29 constexpr uint32_t NUM_0 = 0;
30 constexpr uint32_t NUM_1 = 1;
31 constexpr uint32_t NUM_2 = 2;
32 const char* GAUGE_NODEPTR_OF_UINODE = "nodePtr_";
33 
ResetColor(ArkUINodeHandle nativeNode)34 void ResetColor(ArkUINodeHandle nativeNode)
35 {
36     auto nodeModifiers = GetArkUINodeModifiers();
37     CHECK_NULL_VOID(nodeModifiers);
38     if (!Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN)) {
39         nodeModifiers->getGaugeModifier()->resetGradientColors(nativeNode);
40     } else {
41         nodeModifiers->getGaugeModifier()->resetColors(nativeNode);
42     }
43 }
44 }
45 
SortColorStopOffset(std::vector<NG::ColorStopArray> & colors)46 void SortColorStopOffset(std::vector<NG::ColorStopArray>& colors)
47 {
48     for (auto& colorStopArray : colors) {
49         std::sort(colorStopArray.begin(), colorStopArray.end(),
50             [](const std::pair<Color, Dimension>& left, const std::pair<Color, Dimension>& right) {
51                 return left.second.Value() < right.second.Value();
52             });
53 
54         auto iter = std::unique(colorStopArray.begin(), colorStopArray.end(),
55             [](const std::pair<Color, Dimension>& left, const std::pair<Color, Dimension>& right) {
56                 return left.second.Value() == right.second.Value();
57             });
58         colorStopArray.erase(iter, colorStopArray.end());
59     }
60 }
61 
ConvertResourceColor(const EcmaVM * vm,const Local<JSValueRef> & item,std::vector<NG::ColorStopArray> & colors)62 void ConvertResourceColor(const EcmaVM* vm, const Local<JSValueRef>& item, std::vector<NG::ColorStopArray>& colors)
63 {
64     Color color;
65     if (!ArkTSUtils::ParseJsColorAlpha(vm, item, color)) {
66         color = ERROR_COLOR;
67     }
68     NG::ColorStopArray colorStopArray;
69     colorStopArray.emplace_back(std::make_pair(color, Dimension(0.0)));
70     colors.emplace_back(colorStopArray);
71 }
72 
ConvertGradientColor(const EcmaVM * vm,const Local<JSValueRef> & itemParam,std::vector<NG::ColorStopArray> & colors,NG::GaugeType & type)73 void ConvertGradientColor(
74     const EcmaVM* vm, const Local<JSValueRef>& itemParam, std::vector<NG::ColorStopArray>& colors, NG::GaugeType& type)
75 {
76     if (!itemParam->IsObject(vm)) {
77         type = NG::GaugeType::TYPE_CIRCULAR_MONOCHROME;
78         return ConvertResourceColor(vm, itemParam, colors);
79     }
80     Framework::JSLinearGradient* jsLinearGradient =
81         static_cast<Framework::JSLinearGradient*>(itemParam->ToObject(vm)->GetNativePointerField(vm, 0));
82     if (!jsLinearGradient) {
83         type = NG::GaugeType::TYPE_CIRCULAR_MONOCHROME;
84         return ConvertResourceColor(vm, itemParam, colors);
85     }
86 
87     type = NG::GaugeType::TYPE_CIRCULAR_SINGLE_SEGMENT_GRADIENT;
88     if (jsLinearGradient->GetGradient().size() == 0) {
89         NG::ColorStopArray colorStopArray;
90         colorStopArray.emplace_back(std::make_pair(ERROR_COLOR, Dimension(0.0)));
91         colors.emplace_back(colorStopArray);
92     } else {
93         colors.emplace_back(jsLinearGradient->GetGradient());
94     }
95 }
96 
SetGradientColorsObject(const EcmaVM * vm,const Local<JSValueRef> & info,std::vector<NG::ColorStopArray> & colors,std::vector<float> & weights,NG::GaugeType & type,ArkUINodeHandle nativeNode)97 void SetGradientColorsObject(const EcmaVM* vm, const Local<JSValueRef>& info, std::vector<NG::ColorStopArray>& colors,
98     std::vector<float>& weights, NG::GaugeType& type, ArkUINodeHandle nativeNode)
99 {
100     ArkUIGradientType gradient;
101     ConvertGradientColor(vm, info, colors, type);
102     auto colorStruct = std::make_unique<uint32_t[]>(colors[0].size());
103     auto offsetStruct = std::make_unique<ArkUILengthType[]>(colors[0].size());
104     std::vector<uint32_t> linearLengths { colors[0].size() };
105     for (uint32_t i = 0; i < colors[0].size(); i++) {
106         colorStruct[i] = colors[0][i].first.GetValue();
107         offsetStruct[i].number = colors[0][i].second.Value();
108         offsetStruct[i].unit = static_cast<int8_t>(colors[0][i].second.Unit());
109     }
110     gradient.color = colorStruct.get();
111     gradient.offset = offsetStruct.get();
112     gradient.gradientLength = &(*linearLengths.begin());
113     gradient.weight = nullptr;
114     gradient.type = static_cast<uint32_t>(type);
115     gradient.length = 1;
116     auto nodeModifiers = GetArkUINodeModifiers();
117     CHECK_NULL_VOID(nodeModifiers);
118     nodeModifiers->getGaugeModifier()->setGradientColors(nativeNode, &gradient, 0);
119 }
120 
SetGradientColorsArray(const EcmaVM * vm,const Local<JSValueRef> & info,std::vector<NG::ColorStopArray> & colors,std::vector<float> & weights,NG::GaugeType & type,ArkUINodeHandle nativeNode)121 void SetGradientColorsArray(const EcmaVM* vm, const Local<JSValueRef>& info, std::vector<NG::ColorStopArray>& colors,
122     std::vector<float>& weights, NG::GaugeType& type, ArkUINodeHandle nativeNode)
123 {
124     ArkUIGradientType gradient;
125     uint32_t totalLength = 0;
126     std::vector<uint32_t> linearLengths(colors.size(), 0);
127     for (uint32_t i = 0; i < colors.size(); i++) {
128         linearLengths[i] = colors[i].size();
129         totalLength += colors[i].size();
130     }
131     auto colorStruct = std::make_unique<uint32_t[]>(totalLength);
132     auto offsetStruct = std::make_unique<ArkUILengthType[]>(totalLength);
133     int32_t pos = 0;
134     for (uint32_t i = 0; i < colors.size(); i++) {
135         for (uint32_t j = 0; j < colors[i].size(); j++, pos++) {
136             colorStruct[pos] = colors[i][j].first.GetValue();
137             offsetStruct[pos].number = colors[i][j].second.Value();
138             offsetStruct[pos].unit = static_cast<int8_t>(colors[i][j].second.Unit());
139         }
140     }
141     gradient.color = colorStruct.get();
142     gradient.offset = offsetStruct.get();
143     gradient.gradientLength = &(*linearLengths.begin());
144     gradient.weight = &(*weights.begin());
145     gradient.type = static_cast<uint32_t>(type);
146     gradient.length = colors.size();
147     auto nodeModifiers = GetArkUINodeModifiers();
148     CHECK_NULL_VOID(nodeModifiers);
149     nodeModifiers->getGaugeModifier()->setGradientColors(nativeNode, &gradient, weights.size());
150 }
151 
SetGradientColors(const EcmaVM * vm,const Local<JSValueRef> & info,ArkUINodeHandle nativeNode)152 void SetGradientColors(const EcmaVM* vm, const Local<JSValueRef>& info, ArkUINodeHandle nativeNode)
153 {
154     auto nodeModifiers = GetArkUINodeModifiers();
155     CHECK_NULL_VOID(nodeModifiers);
156     if (info->IsNull() || info->IsUndefined()) {
157         nodeModifiers->getGaugeModifier()->resetGradientColors(nativeNode);
158         return;
159     }
160     NG::GaugeType type = NG::GaugeType::TYPE_CIRCULAR_MULTI_SEGMENT_GRADIENT;
161     std::vector<NG::ColorStopArray> colors;
162     std::vector<float> weights;
163     if (!info->IsArray(vm)) {
164         SetGradientColorsObject(vm, info, colors, weights, type, nativeNode);
165         return;
166     }
167     auto jsColorsArray = panda::CopyableGlobal<panda::ArrayRef>(vm, info);
168     if (jsColorsArray.IsEmpty() || jsColorsArray->IsUndefined()
169         || jsColorsArray->IsNull() || jsColorsArray->Length(vm) == 0) {
170         nodeModifiers->getGaugeModifier()->resetGradientColors(nativeNode);
171         return;
172     }
173 
174     for (size_t i = 0; i < jsColorsArray->Length(vm); ++i) {
175         if (static_cast<int32_t>(i) >= NG::COLORS_MAX_COUNT) {
176             break;
177         }
178         auto jsValue = jsColorsArray->GetValueAt(vm, info, i);
179         if (!jsValue->IsArray(vm)) {
180             continue;
181         }
182         auto tempColors = panda::CopyableGlobal<panda::ArrayRef>(vm, jsValue);
183         if (tempColors.IsEmpty() || tempColors->IsUndefined() || tempColors->IsNull()) {
184             continue;
185         }
186         // Get weight
187         float weight = tempColors->GetValueAt(vm, jsValue, 1)->ToNumber(vm)->Value();
188         if (NonPositive(weight)) {
189             continue;
190         }
191         weights.push_back(weight);
192         // Get color
193         auto jsColorValue = tempColors->GetValueAt(vm, jsValue, 0);
194         ConvertGradientColor(vm, jsColorValue, colors, type);
195     }
196     type = NG::GaugeType::TYPE_CIRCULAR_MULTI_SEGMENT_GRADIENT;
197     SortColorStopOffset(colors);
198     SetGradientColorsArray(vm, info, colors, weights, type, nativeNode);
199 }
200 
SetColors(ArkUIRuntimeCallInfo * runtimeCallInfo)201 ArkUINativeModuleValue GaugeBridge::SetColors(ArkUIRuntimeCallInfo* runtimeCallInfo)
202 {
203     EcmaVM* vm = runtimeCallInfo->GetVM();
204     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
205     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
206     Local<JSValueRef> jsArg = runtimeCallInfo->GetCallArgRef(NUM_1);
207     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
208     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
209 
210     if (!Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN)) {
211         SetGradientColors(vm, jsArg, nativeNode);
212         return panda::JSValueRef::Undefined(vm);
213     }
214 
215     if (!jsArg->IsArray(vm)) {
216         ResetColor(nativeNode);
217         return panda::JSValueRef::Undefined(vm);
218     }
219     auto jsColor = panda::CopyableGlobal<panda::ArrayRef>(vm, jsArg);
220     if (jsColor.IsEmpty() || jsColor->IsUndefined() || jsColor->IsNull()) {
221         return panda::JSValueRef::Undefined(vm);
222     }
223     size_t length = jsColor->Length(vm);
224     auto colors = std::make_unique<uint32_t[]>(length);
225     auto weights = std::make_unique<float[]>(length);
226 
227     auto theme = ArkTSUtils::GetTheme<ProgressTheme>();
228     for (size_t i = 0; i < length; i++) {
229         auto jsValue = jsColor->GetValueAt(vm, jsArg, i);
230         if (!jsValue->IsArray(vm)) {
231             ResetColor(nativeNode);
232             return panda::JSValueRef::Undefined(vm);
233         }
234         auto handle = panda::CopyableGlobal<panda::ArrayRef>(vm, jsValue);
235         if (handle.IsEmpty() || handle->IsUndefined() || handle->IsNull()) {
236             return panda::JSValueRef::Undefined(vm);
237         }
238         float weight = handle->GetValueAt(vm, jsValue, 1)->ToNumber(vm)->Value();
239         Color selectedColor;
240         if (!ArkTSUtils::ParseJsColorAlpha(vm, handle->GetValueAt(vm, jsValue, 1), selectedColor)) {
241             selectedColor = ERROR_COLOR;
242         }
243         colors[i] = selectedColor.GetValue();
244         if (weight > 0) {
245             weights[i] = weight;
246         } else {
247             weights[i] = 0.0f;
248         }
249     }
250     auto nodeModifiers = GetArkUINodeModifiers();
251     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
252     nodeModifiers->getGaugeModifier()->setColors(nativeNode, colors.get(), weights.get(), length);
253     return panda::JSValueRef::Undefined(vm);
254 }
255 
ResetColors(ArkUIRuntimeCallInfo * runtimeCallInfo)256 ArkUINativeModuleValue GaugeBridge::ResetColors(ArkUIRuntimeCallInfo* runtimeCallInfo)
257 {
258     EcmaVM* vm = runtimeCallInfo->GetVM();
259     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
260     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
261     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
262     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
263     ResetColor(nativeNode);
264     return panda::JSValueRef::Undefined(vm);
265 }
266 
SetGaugeValue(ArkUIRuntimeCallInfo * runtimeCallInfo)267 ArkUINativeModuleValue GaugeBridge::SetGaugeValue(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     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
273     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
274     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
275 
276     auto nodeModifiers = GetArkUINodeModifiers();
277     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
278     if (!secondArg->IsNumber()) {
279         nodeModifiers->getGaugeModifier()->resetGaugeValue(nativeNode);
280         return panda::JSValueRef::Undefined(vm);
281     }
282 
283     float value = static_cast<float>(secondArg->ToNumber(vm)->Value());
284     nodeModifiers->getGaugeModifier()->setGaugeValue(nativeNode, value);
285     return panda::JSValueRef::Undefined(vm);
286 }
287 
ResetGaugeValue(ArkUIRuntimeCallInfo * runtimeCallInfo)288 ArkUINativeModuleValue GaugeBridge::ResetGaugeValue(ArkUIRuntimeCallInfo* runtimeCallInfo)
289 {
290     EcmaVM* vm = runtimeCallInfo->GetVM();
291     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
292     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
293     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
294     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
295     auto nodeModifiers = GetArkUINodeModifiers();
296     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
297     nodeModifiers->getGaugeModifier()->resetGaugeValue(nativeNode);
298     return panda::JSValueRef::Undefined(vm);
299 }
300 
SetGaugeStartAngle(ArkUIRuntimeCallInfo * runtimeCallInfo)301 ArkUINativeModuleValue GaugeBridge::SetGaugeStartAngle(ArkUIRuntimeCallInfo* runtimeCallInfo)
302 {
303     EcmaVM* vm = runtimeCallInfo->GetVM();
304     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
305     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
306     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
307     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
308     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
309 
310     auto nodeModifiers = GetArkUINodeModifiers();
311     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
312     if (!secondArg->IsNumber()) {
313         nodeModifiers->getGaugeModifier()->resetGaugeStartAngle(nativeNode);
314         return panda::JSValueRef::Undefined(vm);
315     }
316 
317     float value = static_cast<float>(secondArg->ToNumber(vm)->Value());
318     nodeModifiers->getGaugeModifier()->setGaugeStartAngle(nativeNode, value);
319     return panda::JSValueRef::Undefined(vm);
320 }
321 
ResetGaugeStartAngle(ArkUIRuntimeCallInfo * runtimeCallInfo)322 ArkUINativeModuleValue GaugeBridge::ResetGaugeStartAngle(ArkUIRuntimeCallInfo* runtimeCallInfo)
323 {
324     EcmaVM* vm = runtimeCallInfo->GetVM();
325     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
326     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
327     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
328     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
329     auto nodeModifiers = GetArkUINodeModifiers();
330     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
331     nodeModifiers->getGaugeModifier()->resetGaugeStartAngle(nativeNode);
332     return panda::JSValueRef::Undefined(vm);
333 }
334 
SetGaugeEndAngle(ArkUIRuntimeCallInfo * runtimeCallInfo)335 ArkUINativeModuleValue GaugeBridge::SetGaugeEndAngle(ArkUIRuntimeCallInfo* runtimeCallInfo)
336 {
337     EcmaVM* vm = runtimeCallInfo->GetVM();
338     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
339     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
340     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
341     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
342     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
343 
344     auto nodeModifiers = GetArkUINodeModifiers();
345     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
346     if (!secondArg->IsNumber()) {
347         nodeModifiers->getGaugeModifier()->resetGaugeEndAngle(nativeNode);
348         return panda::JSValueRef::Undefined(vm);
349     }
350 
351     float value = static_cast<float>(secondArg->ToNumber(vm)->Value());
352     nodeModifiers->getGaugeModifier()->setGaugeEndAngle(nativeNode, value);
353     return panda::JSValueRef::Undefined(vm);
354 }
355 
ResetGaugeEndAngle(ArkUIRuntimeCallInfo * runtimeCallInfo)356 ArkUINativeModuleValue GaugeBridge::ResetGaugeEndAngle(ArkUIRuntimeCallInfo* runtimeCallInfo)
357 {
358     EcmaVM* vm = runtimeCallInfo->GetVM();
359     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
360     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
361     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
362     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
363     auto nodeModifiers = GetArkUINodeModifiers();
364     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
365     nodeModifiers->getGaugeModifier()->resetGaugeEndAngle(nativeNode);
366     return panda::JSValueRef::Undefined(vm);
367 }
368 
SetGaugeStrokeWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)369 ArkUINativeModuleValue GaugeBridge::SetGaugeStrokeWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
370 {
371     EcmaVM* vm = runtimeCallInfo->GetVM();
372     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
373     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
374     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
375     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
376     Local<JSValueRef> jsValue = runtimeCallInfo->GetCallArgRef(1);
377 
378     CalcDimension strokeWidth;
379     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, jsValue, strokeWidth) || strokeWidth.Unit() == DimensionUnit::PERCENT) {
380         strokeWidth = CalcDimension(0);
381     }
382     auto nodeModifiers = GetArkUINodeModifiers();
383     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
384     nodeModifiers->getGaugeModifier()->setGaugeStrokeWidth(
385         nativeNode, strokeWidth.Value(), static_cast<int>(strokeWidth.Unit()));
386     return panda::JSValueRef::Undefined(vm);
387 }
388 
ResetGaugeStrokeWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)389 ArkUINativeModuleValue GaugeBridge::ResetGaugeStrokeWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
390 {
391     EcmaVM* vm = runtimeCallInfo->GetVM();
392     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
393     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
394     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
395     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
396     auto nodeModifiers = GetArkUINodeModifiers();
397     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
398     nodeModifiers->getGaugeModifier()->resetGaugeStrokeWidth(nativeNode);
399     return panda::JSValueRef::Undefined(vm);
400 }
401 
SetGaugeTrackShadow(ArkUIRuntimeCallInfo * runtimeCallInfo)402 ArkUINativeModuleValue GaugeBridge::SetGaugeTrackShadow(ArkUIRuntimeCallInfo* runtimeCallInfo)
403 {
404     EcmaVM* vm = runtimeCallInfo->GetVM();
405     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
406     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
407     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
408     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
409     Local<JSValueRef> jsValue = runtimeCallInfo->GetCallArgRef(1);
410     auto radiusArg = runtimeCallInfo->GetCallArgRef(2);
411     auto offsetXArg = runtimeCallInfo->GetCallArgRef(3);
412     auto offsetYArg = runtimeCallInfo->GetCallArgRef(4);
413 
414     auto nodeModifiers = GetArkUINodeModifiers();
415     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
416     if (jsValue->IsNull()) {
417         nodeModifiers->getGaugeModifier()->setShadowOptions(nativeNode, DEFAULT_GAUGE_SHADOW_RADIUS,
418             DEFAULT_GAUGE_SHADOW_OFFSETX, DEFAULT_GAUGE_SHADOW_OFFSETY, false);
419         return panda::JSValueRef::Undefined(vm);
420     }
421 
422     if (!jsValue->IsObject(vm)) {
423         nodeModifiers->getGaugeModifier()->resetShadowOptions(nativeNode);
424         nodeModifiers->getGaugeModifier()->setIsShowIndicator(nativeNode, true);
425         return panda::JSValueRef::Undefined(vm);
426     }
427 
428     double radius = 0.0;
429     if (!ArkTSUtils::ParseJsDouble(vm, radiusArg, radius)) {
430         radius = DEFAULT_GAUGE_SHADOW_RADIUS;
431     }
432 
433     if (NonPositive(radius)) {
434         radius = DEFAULT_GAUGE_SHADOW_RADIUS;
435     }
436 
437     double offsetX = 0.0;
438     if (!ArkTSUtils::ParseJsDouble(vm, offsetXArg, offsetX)) {
439         offsetX = DEFAULT_GAUGE_SHADOW_OFFSETX;
440     }
441 
442     double offsetY = 0.0;
443     if (!ArkTSUtils::ParseJsDouble(vm, offsetYArg, offsetY)) {
444         offsetY = DEFAULT_GAUGE_SHADOW_OFFSETY;
445     }
446 
447     nodeModifiers->getGaugeModifier()->setShadowOptions(nativeNode, radius, offsetX, offsetY, true);
448     return panda::JSValueRef::Undefined(vm);
449 }
450 
ResetGaugeTrackShadow(ArkUIRuntimeCallInfo * runtimeCallInfo)451 ArkUINativeModuleValue GaugeBridge::ResetGaugeTrackShadow(ArkUIRuntimeCallInfo* runtimeCallInfo)
452 {
453     EcmaVM* vm = runtimeCallInfo->GetVM();
454     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
455     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
456     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
457     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
458     auto nodeModifiers = GetArkUINodeModifiers();
459     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
460     nodeModifiers->getGaugeModifier()->resetShadowOptions(nativeNode);
461     return panda::JSValueRef::Undefined(vm);
462 }
463 
SetGaugeIndicator(ArkUIRuntimeCallInfo * runtimeCallInfo)464 ArkUINativeModuleValue GaugeBridge::SetGaugeIndicator(ArkUIRuntimeCallInfo* runtimeCallInfo)
465 {
466     EcmaVM* vm = runtimeCallInfo->GetVM();
467     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
468     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
469     auto iconArg = runtimeCallInfo->GetCallArgRef(1);
470     auto spaceArg = runtimeCallInfo->GetCallArgRef(2);
471     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
472     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
473     auto nodeModifiers = GetArkUINodeModifiers();
474     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
475     nodeModifiers->getGaugeModifier()->setIsShowIndicator(nativeNode, true);
476     std::string iconPath;
477     if (ArkTSUtils::ParseJsMedia(vm, iconArg, iconPath)) {
478         std::string bundleName;
479         std::string moduleName;
480         ArkTSUtils::GetJsMediaBundleInfo(vm, iconArg, bundleName, moduleName);
481         nodeModifiers->getGaugeModifier()->setIndicatorIconPath(nativeNode,
482             iconPath.c_str(), bundleName.c_str(), moduleName.c_str());
483     } else {
484         nodeModifiers->getGaugeModifier()->resetIndicatorIconPath(nativeNode);
485     }
486     CalcDimension space;
487     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, spaceArg, space, false)) {
488         space = NG::INDICATOR_DISTANCE_TO_TOP;
489     }
490     if (space.IsNegative()) {
491         space = NG::INDICATOR_DISTANCE_TO_TOP;
492     }
493     nodeModifiers->getGaugeModifier()->setIndicatorSpace(nativeNode,
494         space.CalcValue().c_str(), space.Value(), static_cast<int32_t>(space.Unit()));
495     return panda::JSValueRef::Undefined(vm);
496 }
497 
ResetGaugeIndicator(ArkUIRuntimeCallInfo * runtimeCallInfo)498 ArkUINativeModuleValue GaugeBridge::ResetGaugeIndicator(ArkUIRuntimeCallInfo* runtimeCallInfo)
499 {
500     EcmaVM* vm = runtimeCallInfo->GetVM();
501     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
502     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
503     auto valueArg = runtimeCallInfo->GetCallArgRef(1);
504     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
505     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
506     auto nodeModifiers = GetArkUINodeModifiers();
507     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
508     if (valueArg->IsNull()) {
509         nodeModifiers->getGaugeModifier()->setIsShowIndicator(nativeNode, false);
510     } else if (valueArg->IsUndefined() || (!valueArg->IsObject(vm))) {
511         nodeModifiers->getGaugeModifier()->resetIndicatorIconPath(nativeNode);
512         nodeModifiers->getGaugeModifier()->resetIndicatorSpace(nativeNode);
513         nodeModifiers->getGaugeModifier()->setIsShowIndicator(nativeNode, true);
514     }
515     return panda::JSValueRef::Undefined(vm);
516 }
517 
SetContentModifierBuilder(ArkUIRuntimeCallInfo * runtimeCallInfo)518 ArkUINativeModuleValue GaugeBridge::SetContentModifierBuilder(ArkUIRuntimeCallInfo* runtimeCallInfo)
519 {
520     EcmaVM* vm = runtimeCallInfo->GetVM();
521     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
522     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
523     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
524     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
525     auto* frameNode = reinterpret_cast<FrameNode*>(firstArg->ToNativePointer(vm)->Value());
526     if (!secondArg->IsObject(vm)) {
527         GaugeModelNG::SetBuilderFunc(frameNode, nullptr);
528         return panda::JSValueRef::Undefined(vm);
529     }
530     panda::CopyableGlobal<panda::ObjectRef> obj(vm, secondArg);
531     auto containerId = Container::CurrentId();
532     GaugeModelNG::SetBuilderFunc(frameNode,
533         [vm, frameNode, obj = std::move(obj), containerId](
534             GaugeConfiguration config) -> RefPtr<FrameNode> {
535             ContainerScope scope(containerId);
536             auto context = ArkTSUtils::GetContext(vm);
537             const char* keyOfGauge[] = { "value", "min", "max" };
538             Local<JSValueRef> valuesOfGauge[] = { panda::NumberRef::New(vm, config.value_),
539                 panda::NumberRef::New(vm, config.min_), panda::NumberRef::New(vm, config.max_) };
540             auto gauge = panda::ObjectRef::NewWithNamedProperties(vm, ArraySize(keyOfGauge),
541                 keyOfGauge, valuesOfGauge);
542             gauge->SetNativePointerFieldCount(vm, 1);
543             gauge->SetNativePointerField(vm, 0, static_cast<void*>(frameNode));
544             panda::Local<panda::JSValueRef> params[NUM_2] = { context, gauge };
545             LocalScope pandaScope(vm);
546             panda::TryCatch trycatch(vm);
547             auto jsObject = obj.ToLocal();
548             auto makeFunc = jsObject->Get(vm, panda::StringRef::NewFromUtf8(vm, "makeContentModifierNode"));
549             CHECK_EQUAL_RETURN(makeFunc->IsFunction(vm), false, nullptr);
550             panda::Local<panda::FunctionRef> func = makeFunc;
551             auto result = func->Call(vm, jsObject, params, NUM_2);
552             JSNApi::ExecutePendingJob(vm);
553             CHECK_EQUAL_RETURN(result.IsEmpty() || trycatch.HasCaught() || !result->IsObject(vm), true, nullptr);
554             auto resultObj = result->ToObject(vm);
555             panda::Local<panda::JSValueRef> nodeptr =
556                 resultObj->Get(vm, panda::StringRef::NewFromUtf8(vm, GAUGE_NODEPTR_OF_UINODE));
557             CHECK_EQUAL_RETURN(nodeptr.IsEmpty() || nodeptr->IsUndefined() || nodeptr->IsNull(), true, nullptr);
558             auto* frameNode = reinterpret_cast<FrameNode*>(nodeptr->ToNativePointer(vm)->Value());
559             CHECK_NULL_RETURN(frameNode, nullptr);
560             return AceType::Claim(frameNode);
561         });
562     return panda::JSValueRef::Undefined(vm);
563 }
564 
SetGaugePrivacySensitive(ArkUIRuntimeCallInfo * runtimeCallInfo)565 ArkUINativeModuleValue GaugeBridge::SetGaugePrivacySensitive(ArkUIRuntimeCallInfo* runtimeCallInfo)
566 {
567     EcmaVM* vm = runtimeCallInfo->GetVM();
568     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
569     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
570     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
571     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
572     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
573     uint32_t sensitive = false;
574     if (secondArg->IsBoolean()) {
575         sensitive = static_cast<uint32_t>(secondArg->ToBoolean(vm)->Value());
576     }
577     auto nodeModifiers = GetArkUINodeModifiers();
578     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
579     nodeModifiers->getCommonModifier()->setPrivacySensitive(nativeNode, sensitive);
580     return panda::JSValueRef::Undefined(vm);
581 }
582 
ResetGaugePrivacySensitive(ArkUIRuntimeCallInfo * runtimeCallInfo)583 ArkUINativeModuleValue GaugeBridge::ResetGaugePrivacySensitive(ArkUIRuntimeCallInfo* runtimeCallInfo)
584 {
585     EcmaVM* vm = runtimeCallInfo->GetVM();
586     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
587     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
588     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
589     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
590     auto nodeModifiers = GetArkUINodeModifiers();
591     CHECK_NULL_RETURN(nodeModifiers, panda::JSValueRef::Undefined(vm));
592     nodeModifiers->getCommonModifier()->resetPrivacySensitive(nativeNode);
593     return panda::JSValueRef::Undefined(vm);
594 }
595 } // namespace OHOS::Ace::NG
596