1 /*
2 * Copyright (c) 2022-2025 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
16 #include "core/components_ng/pattern/progress/progress_model_ng.h"
17
18 #include "base/geometry/dimension.h"
19 #include "core/components/progress/progress_component.h"
20 #include "core/components_ng/base/frame_node.h"
21 #include "core/components_ng/base/view_stack_processor.h"
22 #include "core/components_ng/pattern/pattern.h"
23 #include "core/components_ng/pattern/progress/progress_date.h"
24 #include "core/components_ng/pattern/progress/progress_pattern.h"
25 #include "core/components_ng/pattern/text/text_layout_property.h"
26 #include "core/components_ng/pattern/text/text_pattern.h"
27 #include "core/components_v2/inspector/inspector_constants.h"
28
29 namespace OHOS::Ace::NG {
Create(double min,double value,double cachedValue,double max,NG::ProgressType type)30 void ProgressModelNG::Create(double min, double value, double cachedValue, double max, NG::ProgressType type)
31 {
32 auto* stack = ViewStackProcessor::GetInstance();
33 auto nodeId = stack->ClaimNodeId();
34 ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", V2::PROGRESS_ETS_TAG, nodeId);
35 auto frameNode = FrameNode::GetOrCreateFrameNode(
36 V2::PROGRESS_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<ProgressPattern>(); });
37 stack->Push(frameNode);
38
39 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, Value, value);
40 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, MaxValue, max);
41 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, ProgressType, type);
42 ACE_UPDATE_LAYOUT_PROPERTY(ProgressLayoutProperty, Type, type);
43 auto pipeline = PipelineBase::GetCurrentContext();
44 CHECK_NULL_VOID(pipeline);
45 if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TEN)) {
46 return;
47 }
48 auto progressFocusNode = frameNode->GetFocusHub();
49 CHECK_NULL_VOID(progressFocusNode);
50 if (type == ProgressType::CAPSULE) {
51 progressFocusNode->SetFocusable(true);
52 } else {
53 progressFocusNode->SetFocusable(false);
54 }
55
56 RefPtr<ProgressTheme> theme = pipeline->GetTheme<ProgressTheme>(frameNode->GetThemeScopeId());
57 CHECK_NULL_VOID(theme);
58 auto eventHub = ViewStackProcessor::GetInstance()->GetMainFrameNodeInputEventHub();
59 CHECK_NULL_VOID(eventHub);
60 auto pattern = frameNode->GetPattern<ProgressPattern>();
61 CHECK_NULL_VOID(pattern);
62 if (type == ProgressType::CAPSULE) {
63 if (frameNode->GetChildren().empty()) {
64 auto textNode = FrameNode::CreateFrameNode(
65 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
66 textNode->SetInternal();
67 textNode->MountToParent(frameNode);
68 }
69 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
70 CHECK_NULL_VOID(textHost);
71 SetTextDefaultStyle(textHost, value, max);
72 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
73 eventHub->SetHoverEffect(static_cast<HoverEffectType>(theme->GetCapsuleHoverEffectType()));
74 } else {
75 if (!frameNode->GetChildren().empty()) {
76 frameNode->RemoveChildAtIndex(0);
77 }
78 eventHub->SetHoverEffect(HoverEffectType::NONE);
79 }
80 if (frameNode->GetThemeScopeId()) {
81 if (type == ProgressType::LINEAR || type == ProgressType::MOON) {
82 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, Color, theme->GetTrackSelectedColor());
83 } else if (type == ProgressType::CAPSULE) {
84 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, Color, theme->GetCapsuleSelectColor());
85 }
86 if (type != ProgressType::CAPSULE) {
87 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, BackgroundColor, theme->GetTrackBgColor());
88 }
89 }
90 }
91
CreateFrameNode(int32_t nodeId,double value,double max,NG::ProgressType type)92 RefPtr<FrameNode> ProgressModelNG::CreateFrameNode(int32_t nodeId, double value, double max, NG::ProgressType type)
93 {
94 auto frameNode = FrameNode::CreateFrameNode(V2::PROGRESS_ETS_TAG, nodeId, AceType::MakeRefPtr<ProgressPattern>());
95 CHECK_NULL_RETURN(frameNode, nullptr);
96 auto progressPaintProperty = frameNode->GetPaintProperty<NG::ProgressPaintProperty>();
97 progressPaintProperty->UpdateValue(value);
98 progressPaintProperty->UpdateMaxValue(max);
99 progressPaintProperty->UpdateProgressType(type);
100 auto progressLayoutProperty = frameNode->GetLayoutProperty<NG::ProgressLayoutProperty>();
101 progressLayoutProperty->UpdateType(type);
102 return frameNode;
103 }
104
SetValue(double value)105 void ProgressModelNG::SetValue(double value)
106 {
107 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
108 CHECK_NULL_VOID(frameNode);
109 auto progressPaintProperty = frameNode->GetPaintProperty<NG::ProgressPaintProperty>();
110 CHECK_NULL_VOID(progressPaintProperty);
111 auto maxValue = progressPaintProperty->GetMaxValue();
112 if (value > maxValue) {
113 value = maxValue.value_or(0);
114 }
115 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, Value, value);
116 auto pattern = frameNode->GetPattern<ProgressPattern>();
117 CHECK_NULL_VOID(pattern);
118 if (!pattern->IsTextFromUser()) {
119 SetText(std::nullopt);
120 }
121 }
122
SetColor(const Color & value)123 void ProgressModelNG::SetColor(const Color& value)
124 {
125 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
126 CHECK_NULL_VOID(frameNode);
127 auto pattern = frameNode->GetPattern<ProgressPattern>();
128 CHECK_NULL_VOID(pattern);
129 pattern->SetUserInitiatedColor(true);
130 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, Color, value);
131 }
132
ResetColor()133 void ProgressModelNG::ResetColor()
134 {
135 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
136 CHECK_NULL_VOID(frameNode);
137 auto pattern = frameNode->GetPattern<ProgressPattern>();
138 CHECK_NULL_VOID(pattern);
139 pattern->SetUserInitiatedColor(false);
140 ACE_RESET_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, Color, PROPERTY_UPDATE_RENDER);
141 }
142
SetGradientColor(const Gradient & value)143 void ProgressModelNG::SetGradientColor(const Gradient& value)
144 {
145 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, GradientColor, value);
146 }
147
ResetGradientColor()148 void ProgressModelNG::ResetGradientColor()
149 {
150 ACE_RESET_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, GradientColor, PROPERTY_UPDATE_RENDER);
151 }
152
SetBackgroundColor(const Color & value)153 void ProgressModelNG::SetBackgroundColor(const Color& value)
154 {
155 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
156 CHECK_NULL_VOID(frameNode);
157 auto pattern = frameNode->GetPattern<ProgressPattern>();
158 CHECK_NULL_VOID(pattern);
159 pattern->SetUserInitiatedBgColor(true);
160 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, BackgroundColor, value);
161 }
162
ResetBackgroundColor()163 void ProgressModelNG::ResetBackgroundColor()
164 {
165 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
166 CHECK_NULL_VOID(frameNode);
167 auto pattern = frameNode->GetPattern<ProgressPattern>();
168 CHECK_NULL_VOID(pattern);
169 pattern->SetUserInitiatedBgColor(false);
170 ACE_RESET_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, BackgroundColor, PROPERTY_UPDATE_RENDER);
171 }
172
SetStrokeWidth(const Dimension & value)173 void ProgressModelNG::SetStrokeWidth(const Dimension& value)
174 {
175 ACE_UPDATE_LAYOUT_PROPERTY(ProgressLayoutProperty, StrokeWidth, value);
176 }
177
SetScaleCount(int32_t value)178 void ProgressModelNG::SetScaleCount(int32_t value)
179 {
180 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, ScaleCount, value);
181 }
182
SetScaleWidth(const Dimension & value)183 void ProgressModelNG::SetScaleWidth(const Dimension& value)
184 {
185 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, ScaleWidth, value);
186 }
187
SetBorderColor(const Color & value)188 void ProgressModelNG::SetBorderColor(const Color& value)
189 {
190 if (isCapsule()) {
191 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, BorderColor, value);
192 }
193 }
194
ResetBorderColor()195 void ProgressModelNG::ResetBorderColor()
196 {
197 if (isCapsule()) {
198 ACE_RESET_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, BorderColor, PROPERTY_UPDATE_RENDER);
199 }
200 }
201
SetBorderWidth(const Dimension & value)202 void ProgressModelNG::SetBorderWidth(const Dimension& value)
203 {
204 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, BorderWidth, value);
205 }
206
SetSweepingEffect(bool value)207 void ProgressModelNG::SetSweepingEffect(bool value)
208 {
209 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, EnableScanEffect, value);
210 }
211
SetFontSize(const Dimension & value)212 void ProgressModelNG::SetFontSize(const Dimension& value)
213 {
214 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
215 CHECK_NULL_VOID(frameNode);
216 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
217 CHECK_NULL_VOID(textHost);
218 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
219 CHECK_NULL_VOID(textLayoutProperty);
220 textLayoutProperty->UpdateFontSize(value);
221 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
222 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, TextSize, value);
223 }
224
SetFontColor(const Color & value)225 void ProgressModelNG::SetFontColor(const Color& value)
226 {
227 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
228 CHECK_NULL_VOID(frameNode);
229 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
230 CHECK_NULL_VOID(textHost);
231 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
232 CHECK_NULL_VOID(textLayoutProperty);
233 textLayoutProperty->UpdateTextColor(value);
234 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
235 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, TextColor, value);
236 }
237
ResetFontColor()238 void ProgressModelNG::ResetFontColor()
239 {
240 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
241 CHECK_NULL_VOID(frameNode);
242 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
243 CHECK_NULL_VOID(textHost);
244 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
245 ACE_RESET_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, TextColor, PROPERTY_UPDATE_RENDER);
246 }
247
SetText(const std::optional<std::string> & value)248 void ProgressModelNG::SetText(const std::optional<std::string>& value)
249 {
250 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
251 CHECK_NULL_VOID(frameNode);
252 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
253 CHECK_NULL_VOID(textHost);
254 auto pattern = frameNode->GetPattern<ProgressPattern>();
255 CHECK_NULL_VOID(pattern);
256 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
257 CHECK_NULL_VOID(textLayoutProperty);
258 auto progressPaintProperty = frameNode->GetPaintProperty<NG::ProgressPaintProperty>();
259 CHECK_NULL_VOID(progressPaintProperty);
260 std::string context = "";
261 if (!value.has_value()) {
262 auto maxValue = progressPaintProperty->GetMaxValue();
263 auto curValue = progressPaintProperty->GetValue();
264 int32_t curPercent = curValue.value() * 100 / maxValue.value();
265 std::string number = std::to_string(curPercent) + "%";
266 bool isShowText = progressPaintProperty->GetEnableShowText().value_or(false);
267 if (!isShowText) {
268 number = "";
269 }
270 textLayoutProperty->UpdateContent(number);
271 context = number;
272 pattern->SetTextFromUser(false);
273 } else {
274 textLayoutProperty->UpdateContent(value.value());
275 context = value.value();
276 pattern->SetTextFromUser(true);
277 }
278 textHost->MarkModifyDone();
279 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
280 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, Text, context);
281 }
282
SetFontWeight(const FontWeight & value)283 void ProgressModelNG::SetFontWeight(const FontWeight& value)
284 {
285 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
286 CHECK_NULL_VOID(frameNode);
287 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
288 CHECK_NULL_VOID(textHost);
289 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
290 CHECK_NULL_VOID(textLayoutProperty);
291 textLayoutProperty->UpdateFontWeight(value);
292 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
293 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, FontWeight, value);
294 }
295
SetFontFamily(const std::vector<std::string> & value)296 void ProgressModelNG::SetFontFamily(const std::vector<std::string>& value)
297 {
298 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
299 CHECK_NULL_VOID(frameNode);
300 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
301 CHECK_NULL_VOID(textHost);
302 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
303 CHECK_NULL_VOID(textLayoutProperty);
304 textLayoutProperty->UpdateFontFamily(value);
305 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
306 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, FontFamily, value);
307 }
308
SetItalicFontStyle(const Ace::FontStyle & value)309 void ProgressModelNG::SetItalicFontStyle(const Ace::FontStyle& value)
310 {
311 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
312 CHECK_NULL_VOID(frameNode);
313 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
314 CHECK_NULL_VOID(textHost);
315 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
316 CHECK_NULL_VOID(textLayoutProperty);
317 textLayoutProperty->UpdateItalicFontStyle(value);
318 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
319 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, ItalicFontStyle, value);
320 }
321
SetTextDefaultStyle(const RefPtr<FrameNode> & textNode,double value,double maxValue)322 void ProgressModelNG::SetTextDefaultStyle(const RefPtr<FrameNode>& textNode, double value, double maxValue)
323 {
324 auto pipeline = PipelineBase::GetCurrentContext();
325 CHECK_NULL_VOID(pipeline);
326 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
327 CHECK_NULL_VOID(frameNode);
328 auto textProps = textNode->GetLayoutProperty<TextLayoutProperty>();
329 CHECK_NULL_VOID(textProps);
330 auto renderContext = textNode->GetRenderContext();
331 CHECK_NULL_VOID(renderContext);
332 renderContext->UpdateClipEdge(false);
333 RefPtr<ProgressTheme> progressTheme = pipeline->GetTheme<ProgressTheme>(frameNode->GetThemeScopeId());
334 CHECK_NULL_VOID(progressTheme);
335 auto progressPaintProperty = frameNode->GetPaintProperty<NG::ProgressPaintProperty>();
336 CHECK_NULL_VOID(progressPaintProperty);
337 int32_t curPercent = 0;
338 if (!NearZero(maxValue)) {
339 curPercent = value * 100 / maxValue;
340 }
341 std::string number = std::to_string(curPercent) + "%";
342 textProps->UpdateContent(number);
343 textProps->UpdateFontSize(progressTheme->GetTextSize());
344 textProps->UpdateTextColor(progressTheme->GetTextColor());
345 textProps->UpdateFontWeight(FontWeight::MEDIUM);
346 textProps->UpdateTextOverflow(TextOverflow::ELLIPSIS);
347 textProps->UpdateMaxLines(1);
348 MarginProperty margin;
349 margin.left = CalcLength(progressTheme->GetTextMargin());
350 margin.right = CalcLength(progressTheme->GetTextMargin());
351 margin.top = CalcLength(0.0_vp);
352 margin.bottom = CalcLength(0.0_vp);
353 textProps->UpdateMargin(margin);
354 bool isShowText = progressPaintProperty->GetEnableShowText().value_or(false);
355 if (!isShowText) {
356 number = "";
357 textProps->UpdateContent(number);
358 }
359 textNode->MarkModifyDone();
360 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, Text, number);
361 }
362
SetPaintShadow(bool value)363 void ProgressModelNG::SetPaintShadow(bool value)
364 {
365 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, PaintShadow, value);
366 }
367
SetProgressStatus(ProgressStatus status)368 void ProgressModelNG::SetProgressStatus(ProgressStatus status)
369 {
370 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, ProgressStatus, status);
371 }
372
SetShowText(bool value)373 void ProgressModelNG::SetShowText(bool value)
374 {
375 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, EnableShowText, value);
376 }
377
SetRingSweepingEffect(bool value)378 void ProgressModelNG::SetRingSweepingEffect(bool value)
379 {
380 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, EnableRingScanEffect, value);
381 }
382
SetLinearSweepingEffect(bool value)383 void ProgressModelNG::SetLinearSweepingEffect(bool value)
384 {
385 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, EnableLinearScanEffect, value);
386 }
387
SetSmoothEffect(bool value)388 void ProgressModelNG::SetSmoothEffect(bool value)
389 {
390 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, EnableSmoothEffect, value);
391 }
392
SetStrokeRadius(const Dimension & value)393 void ProgressModelNG::SetStrokeRadius(const Dimension& value)
394 {
395 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, StrokeRadius, value);
396 }
397
ResetStrokeRadius()398 void ProgressModelNG::ResetStrokeRadius()
399 {
400 ACE_RESET_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, StrokeRadius, PROPERTY_UPDATE_RENDER);
401 }
402
SetValue(FrameNode * frameNode,double value)403 void ProgressModelNG::SetValue(FrameNode* frameNode, double value)
404 {
405 auto progressPaintProperty = frameNode->GetPaintProperty<NG::ProgressPaintProperty>();
406 CHECK_NULL_VOID(progressPaintProperty);
407 auto maxValue = progressPaintProperty->GetMaxValue();
408 if (value > maxValue) {
409 value = maxValue.value_or(0);
410 }
411 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, Value, value, frameNode);
412 auto pattern = frameNode->GetPattern<ProgressPattern>();
413 CHECK_NULL_VOID(pattern);
414 if (!pattern->IsTextFromUser()) {
415 SetText(frameNode, std::nullopt);
416 }
417 }
418
SetTotal(FrameNode * frameNode,double max)419 void ProgressModelNG::SetTotal(FrameNode* frameNode, double max)
420 {
421 auto progressPaintProperty = frameNode->GetPaintProperty<NG::ProgressPaintProperty>();
422 CHECK_NULL_VOID(progressPaintProperty);
423 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, MaxValue, max, frameNode);
424 }
425
SetType(FrameNode * frameNode,NG::ProgressType type)426 void ProgressModelNG::SetType(FrameNode* frameNode, NG::ProgressType type)
427 {
428 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, ProgressType, type, frameNode);
429 ACE_UPDATE_NODE_LAYOUT_PROPERTY(ProgressLayoutProperty, Type, type, frameNode);
430 }
431
SetColor(FrameNode * frameNode,const Color & value)432 void ProgressModelNG::SetColor(FrameNode* frameNode, const Color& value)
433 {
434 CHECK_NULL_VOID(frameNode);
435 auto pattern = frameNode->GetPattern<ProgressPattern>();
436 CHECK_NULL_VOID(pattern);
437 pattern->SetUserInitiatedColor(true);
438 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, Color, value, frameNode);
439 }
440
SetGradientColor(FrameNode * frameNode,const Gradient & value)441 void ProgressModelNG::SetGradientColor(FrameNode* frameNode, const Gradient& value)
442 {
443 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, GradientColor, value, frameNode);
444 }
445
SetSmoothEffect(FrameNode * frameNode,bool value)446 void ProgressModelNG::SetSmoothEffect(FrameNode* frameNode, bool value)
447 {
448 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, EnableSmoothEffect, value, frameNode);
449 }
450
SetStrokeWidth(FrameNode * frameNode,const Dimension & value)451 void ProgressModelNG::SetStrokeWidth(FrameNode* frameNode, const Dimension& value)
452 {
453 ACE_UPDATE_NODE_LAYOUT_PROPERTY(ProgressLayoutProperty, StrokeWidth, value, frameNode);
454 }
455
SetLinearSweepingEffect(FrameNode * frameNode,bool value)456 void ProgressModelNG::SetLinearSweepingEffect(FrameNode* frameNode, bool value)
457 {
458 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, EnableLinearScanEffect, value, frameNode);
459 }
460
SetRingSweepingEffect(FrameNode * frameNode,bool value)461 void ProgressModelNG::SetRingSweepingEffect(FrameNode* frameNode, bool value)
462 {
463 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, EnableRingScanEffect, value, frameNode);
464 }
465
SetPaintShadow(FrameNode * frameNode,bool value)466 void ProgressModelNG::SetPaintShadow(FrameNode* frameNode, bool value)
467 {
468 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, PaintShadow, value, frameNode);
469 }
470
SetProgressStatus(FrameNode * frameNode,ProgressStatus status)471 void ProgressModelNG::SetProgressStatus(FrameNode* frameNode, ProgressStatus status)
472 {
473 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, ProgressStatus, status, frameNode);
474 }
475
SetScaleCount(FrameNode * frameNode,int32_t value)476 void ProgressModelNG::SetScaleCount(FrameNode* frameNode, int32_t value)
477 {
478 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, ScaleCount, value, frameNode);
479 }
480
SetScaleWidth(FrameNode * frameNode,const Dimension & value)481 void ProgressModelNG::SetScaleWidth(FrameNode* frameNode, const Dimension& value)
482 {
483 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, ScaleWidth, value, frameNode);
484 }
485
SetBorderWidth(FrameNode * frameNode,const Dimension & value)486 void ProgressModelNG::SetBorderWidth(FrameNode* frameNode, const Dimension& value)
487 {
488 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, BorderWidth, value, frameNode);
489 }
490
SetBorderColor(FrameNode * frameNode,const Color & value)491 void ProgressModelNG::SetBorderColor(FrameNode* frameNode, const Color& value)
492 {
493 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, BorderColor, value, frameNode);
494 }
495
SetSweepingEffect(FrameNode * frameNode,bool value)496 void ProgressModelNG::SetSweepingEffect(FrameNode* frameNode, bool value)
497 {
498 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, EnableScanEffect, value, frameNode);
499 }
500
SetShowText(FrameNode * frameNode,bool value)501 void ProgressModelNG::SetShowText(FrameNode* frameNode, bool value)
502 {
503 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, EnableShowText, value, frameNode);
504 }
505
SetText(FrameNode * frameNode,const std::optional<std::string> & value)506 void ProgressModelNG::SetText(FrameNode* frameNode, const std::optional<std::string>& value)
507 {
508 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
509 CHECK_NULL_VOID(textHost);
510 auto pattern = frameNode->GetPattern<ProgressPattern>();
511 CHECK_NULL_VOID(pattern);
512 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
513 CHECK_NULL_VOID(textLayoutProperty);
514 auto progressPaintProperty = frameNode->GetPaintProperty<NG::ProgressPaintProperty>();
515 CHECK_NULL_VOID(progressPaintProperty);
516 std::string context = "";
517 if (!value.has_value()) {
518 auto maxValue = progressPaintProperty->GetMaxValue();
519 auto curValue = progressPaintProperty->GetValue();
520 int32_t curPercent = curValue.value() * 100 / maxValue.value();
521 std::string number = std::to_string(curPercent) + "%";
522 bool isShowText = progressPaintProperty->GetEnableShowText().value_or(false);
523 if (!isShowText) {
524 number = "";
525 }
526 textLayoutProperty->UpdateContent(number);
527 context = number;
528 pattern->SetTextFromUser(false);
529 } else {
530 textLayoutProperty->UpdateContent(value.value());
531 context = value.value();
532 pattern->SetTextFromUser(true);
533 }
534 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
535 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, Text, context, frameNode);
536 }
537
SetFontColor(FrameNode * frameNode,const Color & value)538 void ProgressModelNG::SetFontColor(FrameNode* frameNode, const Color& value)
539 {
540 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
541 CHECK_NULL_VOID(textHost);
542 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
543 CHECK_NULL_VOID(textLayoutProperty);
544 textLayoutProperty->UpdateTextColor(value);
545 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
546 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, TextColor, value, frameNode);
547 }
548
SetFontSize(FrameNode * frameNode,const Dimension & value)549 void ProgressModelNG::SetFontSize(FrameNode* frameNode, const Dimension& value)
550 {
551 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
552 CHECK_NULL_VOID(textHost);
553 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
554 CHECK_NULL_VOID(textLayoutProperty);
555 textLayoutProperty->UpdateFontSize(value);
556 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
557 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, TextSize, value, frameNode);
558 }
559
SetFontWeight(FrameNode * frameNode,const FontWeight & value)560 void ProgressModelNG::SetFontWeight(FrameNode* frameNode, const FontWeight& value)
561 {
562 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
563 CHECK_NULL_VOID(textHost);
564 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
565 CHECK_NULL_VOID(textLayoutProperty);
566 textLayoutProperty->UpdateFontWeight(value);
567 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
568 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, FontWeight, value, frameNode);
569 }
570
SetFontFamily(FrameNode * frameNode,const std::vector<std::string> & value)571 void ProgressModelNG::SetFontFamily(FrameNode* frameNode, const std::vector<std::string>& value)
572 {
573 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
574 CHECK_NULL_VOID(textHost);
575 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
576 CHECK_NULL_VOID(textLayoutProperty);
577 textLayoutProperty->UpdateFontFamily(value);
578 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
579 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, FontFamily, value, frameNode);
580 }
581
SetItalicFontStyle(FrameNode * frameNode,const Ace::FontStyle & value)582 void ProgressModelNG::SetItalicFontStyle(FrameNode* frameNode, const Ace::FontStyle& value)
583 {
584 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
585 CHECK_NULL_VOID(textHost);
586 auto textLayoutProperty = textHost->GetLayoutProperty<TextLayoutProperty>();
587 CHECK_NULL_VOID(textLayoutProperty);
588 textLayoutProperty->UpdateItalicFontStyle(value);
589 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
590 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, ItalicFontStyle, value, frameNode);
591 }
592
SetStrokeRadius(FrameNode * frameNode,const Dimension & value)593 void ProgressModelNG::SetStrokeRadius(FrameNode* frameNode, const Dimension& value)
594 {
595 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, StrokeRadius, value, frameNode);
596 }
597
ResetStrokeRadius(FrameNode * frameNode)598 void ProgressModelNG::ResetStrokeRadius(FrameNode* frameNode)
599 {
600 ACE_RESET_NODE_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, StrokeRadius, PROPERTY_UPDATE_RENDER, frameNode);
601 }
602
SetBackgroundColor(FrameNode * frameNode,const Color & value)603 void ProgressModelNG::SetBackgroundColor(FrameNode* frameNode, const Color& value)
604 {
605 auto pattern = frameNode->GetPattern<ProgressPattern>();
606 CHECK_NULL_VOID(pattern);
607 pattern->SetUserInitiatedBgColor(true);
608 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, BackgroundColor, value, frameNode);
609 }
610
GetValue(FrameNode * frameNode)611 double ProgressModelNG::GetValue(FrameNode* frameNode)
612 {
613 double value = 0;
614 ACE_GET_NODE_PAINT_PROPERTY_WITH_DEFAULT_VALUE(ProgressPaintProperty, Value, value, frameNode, value);
615 return value;
616 }
617
GetColor(FrameNode * frameNode)618 Color ProgressModelNG::GetColor(FrameNode* frameNode)
619 {
620 Color value;
621 ACE_GET_NODE_PAINT_PROPERTY_WITH_DEFAULT_VALUE(ProgressPaintProperty, Color, value, frameNode, value);
622 return value;
623 }
624
GetTotal(FrameNode * frameNode)625 double ProgressModelNG::GetTotal(FrameNode* frameNode)
626 {
627 double value = 100;
628 ACE_GET_NODE_PAINT_PROPERTY_WITH_DEFAULT_VALUE(ProgressPaintProperty, MaxValue, value, frameNode, value);
629 return value;
630 }
631
GetType(FrameNode * frameNode)632 NG::ProgressType ProgressModelNG::GetType(FrameNode* frameNode)
633 {
634 NG::ProgressType value = ProgressType::LINEAR;
635 ACE_GET_NODE_PAINT_PROPERTY_WITH_DEFAULT_VALUE(ProgressPaintProperty, ProgressType, value, frameNode, value);
636 return value;
637 }
638
SetBuilderFunc(FrameNode * frameNode,ProgressMakeCallback && makeFunc)639 void ProgressModelNG::SetBuilderFunc(FrameNode* frameNode, ProgressMakeCallback&& makeFunc)
640 {
641 CHECK_NULL_VOID(frameNode);
642 auto pattern = frameNode->GetPattern<ProgressPattern>();
643 CHECK_NULL_VOID(pattern);
644 pattern->SetBuilderFunc(std::move(makeFunc));
645 }
646
ProgressInitialize(FrameNode * frameNode,double min,double value,double cachedValue,double max,NG::ProgressType type)647 void ProgressModelNG::ProgressInitialize(
648 FrameNode* frameNode, double min, double value, double cachedValue, double max, NG::ProgressType type)
649 {
650 auto pattern = frameNode->GetPattern<ProgressPattern>();
651 CHECK_NULL_VOID(pattern);
652
653 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, Value, value, frameNode);
654 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, MaxValue, max, frameNode);
655 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, ProgressType, type, frameNode);
656 ACE_UPDATE_NODE_LAYOUT_PROPERTY(ProgressLayoutProperty, Type, type, frameNode);
657 auto pipeline = PipelineBase::GetCurrentContext();
658 CHECK_NULL_VOID(pipeline);
659 if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TEN)) {
660 return;
661 }
662 RefPtr<ProgressTheme> theme = pipeline->GetTheme<ProgressTheme>(frameNode->GetThemeScopeId());
663 auto progressFocusNode = frameNode->GetFocusHub();
664 CHECK_NULL_VOID(progressFocusNode);
665 if (type == ProgressType::CAPSULE) {
666 progressFocusNode->SetFocusable(true);
667 } else {
668 progressFocusNode->SetFocusable(false);
669 }
670
671 auto eventHub = ViewStackProcessor::GetInstance()->GetMainFrameNodeInputEventHub();
672 CHECK_NULL_VOID(eventHub);
673 if (type == ProgressType::CAPSULE) {
674 if (frameNode->GetChildren().empty()) {
675 auto textNode = FrameNode::CreateFrameNode(
676 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
677 textNode->SetInternal();
678 textNode->MountToParent(AceType::Claim(reinterpret_cast<FrameNode*>(frameNode)));
679 }
680 auto textHost = AceType::DynamicCast<FrameNode>(frameNode->GetChildAtIndex(0));
681 CHECK_NULL_VOID(textHost);
682 SetTextDefaultStyle(textHost, value, max);
683 textHost->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
684 eventHub->SetHoverEffect(HoverEffectType::SCALE);
685 eventHub->SetHoverEffect(static_cast<HoverEffectType>(theme->GetCapsuleHoverEffectType()));
686 } else {
687 if (!frameNode->GetChildren().empty()) {
688 frameNode->RemoveChildAtIndex(0);
689 }
690 eventHub->SetHoverEffect(HoverEffectType::NONE);
691 }
692 }
693
SetBorderRadius(const Dimension & value)694 void ProgressModelNG::SetBorderRadius(const Dimension& value)
695 {
696 ACE_UPDATE_PAINT_PROPERTY(ProgressPaintProperty, BorderRadius, value);
697 }
698
ResetBorderRadius()699 void ProgressModelNG::ResetBorderRadius()
700 {
701 ACE_RESET_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, BorderRadius, PROPERTY_UPDATE_RENDER);
702 }
703
SetBorderRadius(FrameNode * frameNode,const Dimension & value)704 void ProgressModelNG::SetBorderRadius(FrameNode* frameNode, const Dimension& value)
705 {
706 ACE_UPDATE_NODE_PAINT_PROPERTY(ProgressPaintProperty, BorderRadius, value, frameNode);
707 }
708
ResetBorderRadius(FrameNode * frameNode)709 void ProgressModelNG::ResetBorderRadius(FrameNode* frameNode)
710 {
711 ACE_RESET_NODE_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, BorderRadius, PROPERTY_UPDATE_RENDER, frameNode);
712 }
713
ResetColor(FrameNode * frameNode)714 void ProgressModelNG::ResetColor(FrameNode* frameNode)
715 {
716 CHECK_NULL_VOID(frameNode);
717 auto pattern = frameNode->GetPattern<ProgressPattern>();
718 CHECK_NULL_VOID(pattern);
719 pattern->SetUserInitiatedColor(false);
720 ACE_RESET_NODE_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, Color, PROPERTY_UPDATE_RENDER, frameNode);
721 }
722
ResetBackgroundColor(FrameNode * frameNode)723 void ProgressModelNG::ResetBackgroundColor(FrameNode* frameNode)
724 {
725 auto pattern = frameNode->GetPattern<ProgressPattern>();
726 CHECK_NULL_VOID(pattern);
727 pattern->SetUserInitiatedBgColor(false);
728 ACE_RESET_NODE_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, BackgroundColor, PROPERTY_UPDATE_RENDER, frameNode);
729 }
730
ResetGradientColor(FrameNode * frameNode)731 void ProgressModelNG::ResetGradientColor(FrameNode* frameNode)
732 {
733 ACE_RESET_NODE_PAINT_PROPERTY_WITH_FLAG(ProgressPaintProperty, GradientColor, PROPERTY_UPDATE_RENDER, frameNode);
734 }
735
isCapsule() const736 bool ProgressModelNG::isCapsule() const
737 {
738 const bool result = false;
739 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
740 CHECK_NULL_RETURN(frameNode, result);
741 auto progressPaintProperty = frameNode->GetPaintProperty<NG::ProgressPaintProperty>();
742 CHECK_NULL_RETURN(progressPaintProperty, result);
743 const auto& progressType = progressPaintProperty->GetProgressType();
744 return progressType == ProgressType::CAPSULE;
745 }
746
SetModifierInitiatedColor(FrameNode * frameNode,bool value)747 void ProgressModelNG::SetModifierInitiatedColor(FrameNode* frameNode, bool value)
748 {
749 CHECK_NULL_VOID(frameNode);
750 auto pattern = frameNode->GetPattern<ProgressPattern>();
751 CHECK_NULL_VOID(pattern);
752 pattern->IsModifierInitiatedColor(value);
753 }
754
SetModifierInitiatedBgColor(FrameNode * frameNode,bool value)755 void ProgressModelNG::SetModifierInitiatedBgColor(FrameNode* frameNode, bool value)
756 {
757 CHECK_NULL_VOID(frameNode);
758 auto pattern = frameNode->GetPattern<ProgressPattern>();
759 CHECK_NULL_VOID(pattern);
760 pattern->IsModifierInitiatedBgColor(value);
761 }
762 } // namespace OHOS::Ace::NG
763