1
2
3 /*
4 * Copyright (c) 2023 Huawei Device Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_progress_bridge.h"
18
19 #include "base/utils/utils.h"
20 #include "core/interfaces/native/node/api.h"
21 #include "core/components/progress/progress_theme.h"
22 #include "core/components_ng/pattern/progress/progress_layout_property.h"
23 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
24 #include "bridge/declarative_frontend/jsview/js_linear_gradient.h"
25
26 namespace OHOS::Ace::NG {
27 constexpr int32_t ARG_NUM_NATIVE_NODE = 0;
28 constexpr int32_t ARG_NUM_VALUE = 1;
29 constexpr int32_t ARG_COLOR_INDEX_VALUE = 1;
30 constexpr int32_t ARG_NUM_STYLE_STROKE_WIDHT = 1;
31 constexpr int32_t ARG_NUM_STYLE_BORDER_WIDHT = 6;
32 constexpr int32_t ARG_NUM_STYLE_PROGRESS_STATUS = 16;
33 constexpr int32_t ARG_NUM_STYLE_FONT_STYLE = 11;
34 constexpr int32_t ARG_NUM_STYLE_SCALE_COUNT = 2;
35 constexpr int32_t ARG_NUM_STYLE_SCALE_WIDHT = 3;
36 constexpr int32_t ARG_NUM_STYLE_FONT_SIZE = 8;
37 constexpr int32_t ARG_NUM_STYLE_ENABLE_SMOOTH_EFFECT = 4;
38 constexpr int32_t ARG_NUM_STYLE_BORDER_COLOR = 5;
39 constexpr int32_t ARG_NUM_STYLE_CONTENT = 7;
40 constexpr int32_t ARG_NUM_STYLE_FONT_WEIGHT = 9;
41 constexpr int32_t ARG_NUM_STYLE_FONT_COLOR = 12;
42 constexpr int32_t ARG_NUM_STYLE_ENABLE_SCAN_EFFECT = 13;
43 constexpr int32_t ARG_NUM_STYLE_SHADOW = 15;
44 constexpr int32_t ARG_NUM_STYLE_SHOW_DEFAULT_PERCENTAHE = 14;
45 constexpr int32_t ARG_NUM_STYLE_FONT_FAMILY = 10;
46 constexpr int32_t ARG_NUM_STYLE_STROKE_RADIUS = 17;
47 constexpr double DEFAULT_PROGRESS_VALUE = 0;
48 constexpr double DEFAULT_STROKE_WIDTH = 4;
49 constexpr double DEFAULT_BORDER_WIDTH = 1;
50 constexpr double DEFAULT_SCALE_WIDTH = 2;
51 constexpr double DEFAULT_STROKE_RADIUS = 0;
52 constexpr int32_t DEFAULT_SCALE_COUNT = 120;
53 constexpr Color DEFAULT_BORDER_COLOR = Color(0x33006cde);
54 constexpr Color DEFAULT_FONT_COLOR = Color(0xff182431);
55 constexpr double DEFAULT_CAPSULE_FONT_SIZE = 12;
56 constexpr NG::ProgressStatus DEFAULT_PROGRESS_STATUS = NG::ProgressStatus::PROGRESSING;
57 constexpr DimensionUnit DEFAULT_CAPSULE_FONT_UNIT = DimensionUnit::FP;
58 const std::vector<Ace::FontStyle> FONT_STYLES = { Ace::FontStyle::NORMAL, Ace::FontStyle::ITALIC };
59 const std::vector<NG::ProgressStatus> STATUS_STYLES = { NG::ProgressStatus::PROGRESSING, NG::ProgressStatus::LOADING };
60
61 namespace {
ConvertProgressRResourceColor(const EcmaVM * vm,const Local<JSValueRef> & item,OHOS::Ace::NG::Gradient & gradient)62 bool ConvertProgressRResourceColor(const EcmaVM* vm, const Local<JSValueRef>& item, OHOS::Ace::NG::Gradient& gradient)
63 {
64 Color color;
65 if (!ArkTSUtils::ParseJsColor(vm, item, color)) {
66 return false;
67 }
68 OHOS::Ace::NG::GradientColor gradientColorStart;
69 gradientColorStart.SetLinearColor(LinearColor(color));
70 gradientColorStart.SetDimension(Dimension(0.0));
71 gradient.AddColor(gradientColorStart);
72 OHOS::Ace::NG::GradientColor gradientColorEnd;
73 gradientColorEnd.SetLinearColor(LinearColor(color));
74 gradientColorEnd.SetDimension(Dimension(1.0));
75 gradient.AddColor(gradientColorEnd);
76 return true;
77 }
78
ConvertProgressResourceColor(const EcmaVM * vm,const Local<JSValueRef> & itemParam,OHOS::Ace::NG::Gradient & gradient)79 bool ConvertProgressResourceColor(
80 const EcmaVM* vm, const Local<JSValueRef>& itemParam, OHOS::Ace::NG::Gradient& gradient)
81 {
82 if (!itemParam->IsObject()) {
83 return ConvertProgressRResourceColor(vm, itemParam, gradient);
84 }
85 Framework::JSLinearGradient* jsLinearGradient =
86 static_cast<Framework::JSLinearGradient*>(itemParam->ToObject(vm)->GetNativePointerField(0));
87 if (!jsLinearGradient) {
88 return ConvertProgressRResourceColor(vm, itemParam, gradient);
89 }
90
91 size_t colorLength = jsLinearGradient->GetGradient().size();
92 if (colorLength == 0) {
93 return false;
94 }
95 for (size_t colorIndex = 0; colorIndex < colorLength; ++colorIndex) {
96 OHOS::Ace::NG::GradientColor gradientColor;
97 gradientColor.SetLinearColor(LinearColor(jsLinearGradient->GetGradient().at(colorIndex).first));
98 gradientColor.SetDimension(jsLinearGradient->GetGradient().at(colorIndex).second);
99 gradient.AddColor(gradientColor);
100 }
101 return true;
102 }
103 } // namespace
104
ResetProgressValue(ArkUIRuntimeCallInfo * runtimeCallInfo)105 ArkUINativeModuleValue ProgressBridge::ResetProgressValue(ArkUIRuntimeCallInfo* runtimeCallInfo)
106 {
107 EcmaVM* vm = runtimeCallInfo->GetVM();
108 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
109 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_NUM_NATIVE_NODE);
110 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
111 GetArkUIInternalNodeAPI()->GetProgressModifier().ResetProgressValue(nativeNode);
112 return panda::JSValueRef::Undefined(vm);
113 }
114
SetProgressValue(ArkUIRuntimeCallInfo * runtimeCallInfo)115 ArkUINativeModuleValue ProgressBridge::SetProgressValue(ArkUIRuntimeCallInfo* runtimeCallInfo)
116 {
117 EcmaVM* vm = runtimeCallInfo->GetVM();
118 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
119 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_NUM_NATIVE_NODE);
120 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_NUM_VALUE);
121 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
122 if (secondArg->IsNumber()) {
123 double value = secondArg->ToNumber(vm)->Value();
124 if (value < DEFAULT_PROGRESS_VALUE) {
125 value = DEFAULT_PROGRESS_VALUE;
126 }
127 GetArkUIInternalNodeAPI()->GetProgressModifier().SetProgressValue(nativeNode, value);
128 } else {
129 GetArkUIInternalNodeAPI()->GetProgressModifier().ResetProgressValue(nativeNode);
130 }
131 return panda::JSValueRef::Undefined(vm);
132 }
133
ResetProgressColor(ArkUIRuntimeCallInfo * runtimeCallInfo)134 ArkUINativeModuleValue ProgressBridge::ResetProgressColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
135 {
136 EcmaVM* vm = runtimeCallInfo->GetVM();
137 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
138 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_NUM_NATIVE_NODE);
139 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
140 GetArkUIInternalNodeAPI()->GetProgressModifier().ResetProgressColor(nativeNode);
141 return panda::JSValueRef::Undefined(vm);
142 }
143
SetProgressColor(ArkUIRuntimeCallInfo * runtimeCallInfo)144 ArkUINativeModuleValue ProgressBridge::SetProgressColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
145 {
146 EcmaVM* vm = runtimeCallInfo->GetVM();
147 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
148 Local<JSValueRef> nativeArg = runtimeCallInfo->GetCallArgRef(ARG_NUM_NATIVE_NODE);
149 Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(ARG_COLOR_INDEX_VALUE);
150 void* nativeNode = nativeArg->ToNativePointer(vm)->Value();
151 Color color;
152 OHOS::Ace::NG::Gradient gradient;
153 if (ArkTSUtils::ParseJsColorAlpha(vm, colorArg, color)) {
154 GetArkUIInternalNodeAPI()->GetProgressModifier().SetProgressColor(nativeNode, color.GetValue());
155 } else if (ConvertProgressResourceColor(vm, colorArg, gradient)) {
156 ArkUIGradientType gradientObj;
157 auto colorlength = gradient.GetColors().size();
158 std::vector<uint32_t> colorValues;
159 std::vector<ArkUILengthType> offsetValues;
160 if (colorlength <= 0) {
161 GetArkUIInternalNodeAPI()->GetProgressModifier().ResetProgressColor(nativeNode);
162 return panda::JSValueRef::Undefined(vm);
163 }
164
165 for (int32_t i = 0; i < static_cast<int32_t>(colorlength); i++) {
166 colorValues.push_back(gradient.GetColors()[i].GetLinearColor().GetValue());
167 offsetValues.push_back(ArkUILengthType { .number = gradient.GetColors()[i].GetDimension().Value(),
168 .unit = static_cast<int8_t>(gradient.GetColors()[i].GetDimension().Unit()) });
169 }
170
171 gradientObj.color = &(*colorValues.begin());
172 gradientObj.offset = &(*offsetValues.begin());
173 GetArkUIInternalNodeAPI()->GetProgressModifier().SetProgressGradientColor(
174 nativeNode, &gradientObj, colorlength);
175 } else {
176 GetArkUIInternalNodeAPI()->GetProgressModifier().ResetProgressColor(nativeNode);
177 }
178
179 return panda::JSValueRef::Undefined(vm);
180 }
181
ResetProgressStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)182 ArkUINativeModuleValue ProgressBridge::ResetProgressStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
183 {
184 EcmaVM* vm = runtimeCallInfo->GetVM();
185 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
186 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_NUM_NATIVE_NODE);
187 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
188 GetArkUIInternalNodeAPI()->GetProgressModifier().ResetProgressStyle(nativeNode);
189 return panda::JSValueRef::Undefined(vm);
190 }
191
ParseStrokeWidth(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)192 void ParseStrokeWidth(
193 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
194 {
195 Local<JSValueRef> strokeWidthArg = runtimeCallInfo->GetCallArgRef(index);
196 CalcDimension strokeWidth = CalcDimension(DEFAULT_STROKE_WIDTH, DimensionUnit::VP);
197 auto theme = ArkTSUtils::GetTheme<ProgressTheme>();
198
199 if (strokeWidthArg->IsString()) {
200 const std::string& value = strokeWidthArg->ToString(vm)->ToString();
201 strokeWidth = StringUtils::StringToDimensionWithUnit(value, DimensionUnit::VP, DEFAULT_STROKE_WIDTH);
202 } else {
203 ArkTSUtils::ParseJsDimension(vm, strokeWidthArg, strokeWidth, DimensionUnit::VP, false);
204 }
205
206 if ((LessOrEqual(strokeWidth.Value(), 0.0f) || strokeWidth.Unit() == DimensionUnit::PERCENT) && theme) {
207 strokeWidth = theme->GetTrackThickness();
208 }
209 if (strokeWidth.IsNegative()) {
210 progressStyle.strokeWidthValue = DEFAULT_STROKE_WIDTH;
211 progressStyle.strokeWidthUnit = static_cast<uint8_t>(DimensionUnit::VP);
212 } else {
213 progressStyle.strokeWidthValue = strokeWidth.Value();
214 progressStyle.strokeWidthUnit = static_cast<uint8_t>(strokeWidth.Unit());
215 }
216 }
217
ParseBorderWidth(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)218 void ParseBorderWidth(
219 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
220 {
221 Local<JSValueRef> borderWidthArg = runtimeCallInfo->GetCallArgRef(index);
222 CalcDimension borderWidth = CalcDimension(DEFAULT_BORDER_WIDTH, DimensionUnit::VP);
223
224 if (borderWidthArg->IsString()) {
225 const std::string& value = borderWidthArg->ToString(vm)->ToString();
226 borderWidth = StringUtils::StringToDimensionWithUnit(value, DimensionUnit::VP, DEFAULT_BORDER_WIDTH);
227 } else {
228 ArkTSUtils::ParseJsDimension(vm, borderWidthArg, borderWidth, DimensionUnit::VP, false);
229 }
230 if (borderWidth.IsNegative()) {
231 progressStyle.borderWidthValue = DEFAULT_BORDER_WIDTH;
232 progressStyle.borderWidthUnit = static_cast<uint8_t>(DimensionUnit::VP);
233 } else {
234 progressStyle.borderWidthValue = borderWidth.Value();
235 progressStyle.borderWidthUnit = static_cast<uint8_t>(borderWidth.Unit());
236 }
237 }
238
ParseScaleCount(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)239 void ParseScaleCount(
240 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
241 {
242 Local<JSValueRef> scaleCountArg = runtimeCallInfo->GetCallArgRef(index);
243 int32_t scaleCount = DEFAULT_SCALE_COUNT;
244 auto theme = ArkTSUtils::GetTheme<ProgressTheme>();
245 if (theme) {
246 scaleCount = theme->GetScaleNumber();
247 }
248
249 if (scaleCountArg->IsNull() || !ArkTSUtils::ParseJsInteger(vm, scaleCountArg, scaleCount)) {
250 scaleCount = DEFAULT_SCALE_COUNT;
251 }
252 if (scaleCount > 1) {
253 progressStyle.scaleCount = scaleCount;
254 } else if (theme) {
255 progressStyle.scaleCount = theme->GetScaleNumber();
256 }
257 }
258
ParseProgressStatus(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)259 void ParseProgressStatus(
260 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
261 {
262 Local<JSValueRef> ProgressStatusArg = runtimeCallInfo->GetCallArgRef(index);
263 NG::ProgressStatus progressStatus = DEFAULT_PROGRESS_STATUS;
264 std::string statusStr;
265 if (ProgressStatusArg->IsUndefined() || ProgressStatusArg->IsNull() ||
266 !ArkTSUtils::ParseJsString(vm, ProgressStatusArg, statusStr)) {
267 progressStatus = DEFAULT_PROGRESS_STATUS;
268 } else {
269 if (statusStr.compare("LOADING") == 0) {
270 progressStatus = NG::ProgressStatus::LOADING;
271 } else {
272 progressStatus = NG::ProgressStatus::PROGRESSING;
273 }
274 }
275 progressStyle.status = static_cast<int8_t>(progressStatus);
276 }
277
ParseScaleWidth(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)278 void ParseScaleWidth(
279 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
280 {
281 Local<JSValueRef> scaleWidthArg = runtimeCallInfo->GetCallArgRef(index);
282 CalcDimension scaleWidth = CalcDimension(DEFAULT_SCALE_WIDTH, DimensionUnit::VP);
283
284 if (scaleWidthArg->IsString()) {
285 const std::string& value = scaleWidthArg->ToString(vm)->ToString();
286 scaleWidth = StringUtils::StringToDimensionWithUnit(value, DimensionUnit::VP, DEFAULT_SCALE_WIDTH);
287 } else {
288 ArkTSUtils::ParseJsDimension(vm, scaleWidthArg, scaleWidth, DimensionUnit::VP, false);
289 }
290 if (scaleWidth.IsNegative()) {
291 scaleWidth = CalcDimension(DEFAULT_SCALE_WIDTH, DimensionUnit::VP);
292 }
293
294 progressStyle.scaleWidthValue = scaleWidth.Value();
295 progressStyle.scaleWidthUnit = static_cast<uint8_t>(scaleWidth.Unit());
296 }
297
ParseStrokeRadius(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)298 void ParseStrokeRadius(
299 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
300 {
301 Local<JSValueRef> strokeRadiusArg = runtimeCallInfo->GetCallArgRef(index);
302 CalcDimension strokeRadius = CalcDimension(DEFAULT_STROKE_RADIUS, DimensionUnit::PERCENT);
303 if (strokeRadiusArg->IsNull() ||
304 !ArkTSUtils::ParseJsDimension(vm, strokeRadiusArg, strokeRadius, DimensionUnit::VP, true)) {
305 strokeRadius.SetUnit(DimensionUnit::PERCENT);
306 }
307
308 progressStyle.strokeRadiusValue = strokeRadius.Value();
309 progressStyle.strokeRadiusUnit = static_cast<uint8_t>(strokeRadius.Unit());
310 }
311
ParseBorderColor(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)312 void ParseBorderColor(
313 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
314 {
315 Local<JSValueRef> borderColorArg = runtimeCallInfo->GetCallArgRef(index);
316 Color borderColor = DEFAULT_BORDER_COLOR;
317
318 if (borderColorArg->IsNull() || !ArkTSUtils::ParseJsColorAlpha(vm, borderColorArg, borderColor)) {
319 borderColor = DEFAULT_BORDER_COLOR;
320 }
321
322 progressStyle.borderColor = borderColor.GetValue();
323 }
324
ParseFontColor(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)325 void ParseFontColor(
326 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
327 {
328 Local<JSValueRef> fontColorArg = runtimeCallInfo->GetCallArgRef(index);
329 Color fontColor = DEFAULT_FONT_COLOR;
330
331 if (fontColorArg->IsNull() || !ArkTSUtils::ParseJsColorAlpha(vm, fontColorArg, fontColor)) {
332 fontColor = DEFAULT_FONT_COLOR;
333 }
334
335 progressStyle.fontColor = fontColor.GetValue();
336 }
337
ParseEnableSmoothEffect(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)338 void ParseEnableSmoothEffect(
339 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
340 {
341 Local<JSValueRef> enableSmoothEffectArg = runtimeCallInfo->GetCallArgRef(index);
342 progressStyle.enableSmoothEffect =
343 (enableSmoothEffectArg->IsBoolean()) ? enableSmoothEffectArg->ToBoolean(vm)->Value() : true;
344 }
345
ParseContent(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)346 void ParseContent(
347 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
348 {
349 Local<JSValueRef> contentArg = runtimeCallInfo->GetCallArgRef(index);
350 std::string contentstr = contentArg->ToString(vm)->ToString();
351 progressStyle.content = (contentArg->IsString()) ? contentstr.c_str() : nullptr;
352 }
353
ParseEnableScanEffect(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)354 void ParseEnableScanEffect(
355 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
356 {
357 Local<JSValueRef> enableScanEffectArg = runtimeCallInfo->GetCallArgRef(index);
358 progressStyle.enableScanEffect =
359 (enableScanEffectArg->IsBoolean()) ? enableScanEffectArg->ToBoolean(vm)->Value() : false;
360 }
361
ParseShadow(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)362 void ParseShadow(
363 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
364 {
365 Local<JSValueRef> shadowArg = runtimeCallInfo->GetCallArgRef(index);
366 progressStyle.shadow = (shadowArg->IsBoolean()) ? shadowArg->ToBoolean(vm)->Value() : false;
367 }
368
ParseShowDefaultPercentage(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)369 void ParseShowDefaultPercentage(
370 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
371 {
372 Local<JSValueRef> showDefaultPercentageArg = runtimeCallInfo->GetCallArgRef(index);
373 progressStyle.showDefaultPercentage =
374 (showDefaultPercentageArg->IsBoolean()) ? showDefaultPercentageArg->ToBoolean(vm)->Value() : false;
375 }
376
ParseCapsuleFontSize(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)377 void ParseCapsuleFontSize(
378 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
379 {
380 Local<JSValueRef> sizeArg = runtimeCallInfo->GetCallArgRef(index);
381
382 CalcDimension fontSize;
383 if (sizeArg->IsNull() || !ArkTSUtils::ParseJsDimensionFp(vm, sizeArg, fontSize) || fontSize.IsNegative() ||
384 fontSize.Unit() == DimensionUnit::PERCENT) {
385 progressStyle.fontInfo.fontSizeNumber = DEFAULT_CAPSULE_FONT_SIZE;
386 progressStyle.fontInfo.fontSizeUnit = static_cast<int8_t>(DEFAULT_CAPSULE_FONT_UNIT);
387 } else {
388 progressStyle.fontInfo.fontSizeNumber = fontSize.Value();
389 progressStyle.fontInfo.fontSizeUnit = static_cast<int8_t>(fontSize.Unit());
390 }
391 }
392
ParseCapsuleFontWeight(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)393 void ParseCapsuleFontWeight(
394 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
395 {
396 Local<JSValueRef> weightArg = runtimeCallInfo->GetCallArgRef(index);
397 auto pipelineContext = PipelineContext::GetCurrentContext();
398 auto theme = pipelineContext->GetTheme<TextTheme>();
399
400 std::string weight;
401 if (!weightArg->IsNull()) {
402 if (weightArg->IsNumber()) {
403 weight = std::to_string(weightArg->Int32Value(vm));
404 } else if (weightArg->IsString()) {
405 weight = weightArg->ToString(vm)->ToString();
406 }
407 progressStyle.fontInfo.fontWeight = static_cast<uint8_t>(Framework::ConvertStrToFontWeight(weight));
408 } else {
409 progressStyle.fontInfo.fontWeight = static_cast<uint8_t>(theme->GetTextStyle().GetFontWeight());
410 }
411 }
412
ParseCapsuleFontStyle(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)413 void ParseCapsuleFontStyle(
414 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
415 {
416 Local<JSValueRef> styleArg = runtimeCallInfo->GetCallArgRef(index);
417 auto pipelineContext = PipelineContext::GetCurrentContext();
418 auto theme = pipelineContext->GetTheme<TextTheme>();
419
420 uint8_t style = static_cast<uint8_t>(theme->GetTextStyle().GetFontStyle());
421 if (!styleArg->IsNull() && styleArg->IsInt()) {
422 style = static_cast<uint8_t>(styleArg->Int32Value(vm));
423 if (style <= 0 || style > static_cast<uint8_t>(FONT_STYLES.size())) {
424 style = static_cast<uint8_t>(theme->GetTextStyle().GetFontStyle());
425 }
426 }
427
428 progressStyle.fontInfo.fontStyle = style;
429 }
430
ParseCapsuleFontFamily(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle,int32_t index)431 void ParseCapsuleFontFamily(
432 const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle, int32_t index)
433 {
434 Local<JSValueRef> familyArg = runtimeCallInfo->GetCallArgRef(index);
435 auto pipelineContext = PipelineContext::GetCurrentContext();
436 auto theme = pipelineContext->GetTheme<TextTheme>();
437
438 std::vector<std::string> fontFamilies;
439 if (familyArg->IsNull() || !ArkTSUtils::ParseJsFontFamilies(vm, familyArg, fontFamilies)) {
440 fontFamilies = theme->GetTextStyle().GetFontFamilies();
441 }
442
443 auto families = std::make_unique<const char* []>(fontFamilies.size());
444 for (uint32_t i = 0; i < fontFamilies.size(); i++) {
445 families[i] = fontFamilies[i].c_str();
446 }
447
448 progressStyle.fontInfo.fontFamilies = families.get();
449 progressStyle.fontInfo.familyLength = fontFamilies.size();
450 }
451
ParseLinearStyle(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle)452 void ParseLinearStyle(const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle)
453 {
454 ParseStrokeWidth(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_STROKE_WIDHT);
455 ParseStrokeRadius(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_STROKE_RADIUS);
456 ParseEnableScanEffect(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_ENABLE_SCAN_EFFECT);
457 ParseEnableSmoothEffect(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_ENABLE_SMOOTH_EFFECT);
458 }
459
ParseRingStyle(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle)460 void ParseRingStyle(const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle)
461 {
462 ParseStrokeWidth(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_STROKE_WIDHT);
463 ParseShadow(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_SHADOW);
464 ParseProgressStatus(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_PROGRESS_STATUS);
465 ParseEnableScanEffect(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_ENABLE_SCAN_EFFECT);
466 ParseEnableSmoothEffect(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_ENABLE_SMOOTH_EFFECT);
467 }
468
ParseCapsuleStyle(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle)469 void ParseCapsuleStyle(const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle)
470 {
471 ParseBorderColor(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_BORDER_COLOR);
472 ParseBorderWidth(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_BORDER_WIDHT);
473 ParseContent(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_CONTENT);
474 ParseFontColor(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_FONT_COLOR);
475 ParseCapsuleFontSize(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_FONT_SIZE);
476 ParseCapsuleFontWeight(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_FONT_WEIGHT);
477 ParseCapsuleFontStyle(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_FONT_STYLE);
478 ParseCapsuleFontFamily(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_FONT_FAMILY);
479 ParseEnableScanEffect(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_ENABLE_SCAN_EFFECT);
480 ParseShowDefaultPercentage(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_SHOW_DEFAULT_PERCENTAHE);
481 ParseEnableSmoothEffect(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_ENABLE_SMOOTH_EFFECT);
482 }
483
ParseProgressStyle(const EcmaVM * vm,ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUIProgressStyle & progressStyle)484 void ParseProgressStyle(const EcmaVM* vm, ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUIProgressStyle& progressStyle)
485 {
486 auto progressTheme = ArkTSUtils::GetTheme<ProgressTheme>();
487 CHECK_NULL_VOID(progressTheme);
488 ParseStrokeWidth(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_STROKE_WIDHT);
489 ParseScaleCount(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_SCALE_COUNT);
490 ParseScaleWidth(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_SCALE_WIDHT);
491 if ((progressStyle.scaleWidthValue <= 0.0) || (progressStyle.scaleWidthValue > progressStyle.strokeWidthValue) ||
492 progressStyle.scaleWidthUnit == static_cast<int8_t>(DimensionUnit::PERCENT)) {
493 progressStyle.scaleWidthValue = progressTheme->GetScaleWidth().Value();
494 progressStyle.scaleWidthUnit = static_cast<int8_t>(progressTheme->GetScaleWidth().Unit());
495 }
496 ParseEnableSmoothEffect(vm, runtimeCallInfo, progressStyle, ARG_NUM_STYLE_ENABLE_SMOOTH_EFFECT);
497 }
498
SetProgressStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)499 ArkUINativeModuleValue ProgressBridge::SetProgressStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
500 {
501 EcmaVM* vm = runtimeCallInfo->GetVM();
502 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
503 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_NUM_NATIVE_NODE);
504 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
505 auto pipelineContext = PipelineContext::GetCurrentContext();
506 auto theme = pipelineContext->GetTheme<TextTheme>();
507
508 auto fontFamilies = theme->GetTextStyle().GetFontFamilies();
509 auto families = std::make_unique<const char* []>(fontFamilies.size());
510 for (uint32_t i = 0; i < fontFamilies.size(); i++) {
511 families[i] = fontFamilies[i].c_str();
512 }
513
514 ArkUIProgressStyle progressStyle = { DEFAULT_STROKE_WIDTH, static_cast<int8_t>(DimensionUnit::VP),
515 DEFAULT_BORDER_WIDTH, static_cast<int8_t>(DimensionUnit::VP), DEFAULT_SCALE_COUNT,
516 static_cast<uint8_t>(DEFAULT_PROGRESS_STATUS), DEFAULT_SCALE_WIDTH, static_cast<int8_t>(DimensionUnit::VP),
517 DEFAULT_STROKE_RADIUS, static_cast<int8_t>(DimensionUnit::PERCENT), true,
518 static_cast<double>(DEFAULT_BORDER_COLOR.GetValue()), nullptr,
519 static_cast<double>(DEFAULT_FONT_COLOR.GetValue()), false, false, false,
520 { DEFAULT_CAPSULE_FONT_SIZE, static_cast<int8_t>(DEFAULT_CAPSULE_FONT_UNIT),
521 static_cast<uint8_t>(theme->GetTextStyle().GetFontWeight()),
522 static_cast<uint8_t>(theme->GetTextStyle().GetFontStyle()), families.get(), fontFamilies.size() } };
523
524 auto* frameNode = reinterpret_cast<FrameNode*>(nativeNode);
525 CHECK_NULL_RETURN(frameNode, panda::JSValueRef::Undefined(vm));
526 auto progressLayoutProperty = frameNode->GetLayoutProperty<ProgressLayoutProperty>();
527 CHECK_NULL_RETURN(progressLayoutProperty, panda::JSValueRef::Undefined(vm));
528 auto progresstype = progressLayoutProperty->GetType();
529 if (progresstype == ProgressType::LINEAR) {
530 ParseLinearStyle(vm, runtimeCallInfo, progressStyle);
531 } else if (progresstype == ProgressType::RING) {
532 ParseRingStyle(vm, runtimeCallInfo, progressStyle);
533 } else if (progresstype == ProgressType::CAPSULE) {
534 ParseCapsuleStyle(vm, runtimeCallInfo, progressStyle);
535 } else {
536 ParseProgressStyle(vm, runtimeCallInfo, progressStyle);
537 }
538 GetArkUIInternalNodeAPI()->GetProgressModifier().SetProgressStyle(nativeNode, &progressStyle);
539 return panda::JSValueRef::Undefined(vm);
540 }
541
SetProgressBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)542 ArkUINativeModuleValue ProgressBridge::SetProgressBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
543 {
544 EcmaVM *vm = runtimeCallInfo->GetVM();
545 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
546 Local<JSValueRef> nativeNodeArg = runtimeCallInfo->GetCallArgRef(0);
547 Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(1);
548 void* nativeNode = nativeNodeArg->ToNativePointer(vm)->Value();
549 Color color;
550 if (!ArkTSUtils::ParseJsColorAlpha(vm, colorArg, color)) {
551 GetArkUIInternalNodeAPI()->GetProgressModifier().ResetProgressBackgroundColor(nativeNode);
552 } else {
553 GetArkUIInternalNodeAPI()->GetProgressModifier().SetProgressBackgroundColor(nativeNode, color.GetValue());
554 }
555
556 return panda::JSValueRef::Undefined(vm);
557 }
558
ResetProgressBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)559 ArkUINativeModuleValue ProgressBridge::ResetProgressBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
560 {
561 EcmaVM* vm = runtimeCallInfo->GetVM();
562 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
563 Local<JSValueRef> nativeNodeArg = runtimeCallInfo->GetCallArgRef(0);
564 void* nativeNode = nativeNodeArg->ToNativePointer(vm)->Value();
565 GetArkUIInternalNodeAPI()->GetProgressModifier().ResetProgressBackgroundColor(nativeNode);
566 return panda::JSValueRef::Undefined(vm);
567 }
568 } // namespace OHOS::Ace::NG
569