1 /*
2 * Copyright (c) 2022-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
16 #include "core/components_ng/pattern/loading_progress/loading_progress_modifier.h"
17
18 #include "base/geometry/arc.h"
19 #include "core/common/container.h"
20 #include "core/components_ng/pattern/loading_progress/loading_progress_utill.h"
21 #include "core/components_ng/render/drawing_prop_convertor.h"
22
23 namespace OHOS::Ace::NG {
24 namespace {
25 constexpr float TOTAL_ANGLE = 360.0f;
26 constexpr float ROTATEX = -116.0f;
27 constexpr float ROTATEY = 30.0f;
28 constexpr float ROTATEZ = 22.0f;
29 constexpr float COUNT = 50.0f;
30 constexpr float HALF = 0.5f;
31 constexpr float DOUBLE = 2.0f;
32 constexpr int32_t SKEWY = 3;
33 constexpr int32_t SCALEY = 4;
34 constexpr float RING_ALPHA = 200 / 255.0f;
35 constexpr int32_t RING_ALPHA_DARK = 255;
36 constexpr int32_t RING_ALPHA_DARK_BACKGROUNG = 150;
37 constexpr Color DEFAULT_COLOR_DARK = Color(0x99FFFFFF);
38 constexpr int32_t TOTAL_POINTS_COUNT = 20;
39 constexpr int32_t TAIL_ANIAMTION_DURATION = 400;
40 constexpr int32_t TRANS_DURATION = 100;
41 constexpr float TOTAL_TAIL_LENGTH = 60.0f;
42 constexpr float TAIL_ALPHA_RATIO = 0.82f;
43 constexpr float INITIAL_SIZE_SCALE = 0.825f;
44 constexpr float INITIAL_OPACITY_SCALE = 0.7f;
45 constexpr float COMET_TAIL_ANGLE = 3.0f;
46 constexpr int32_t LOADING_DURATION = 1200;
47 constexpr float FOLLOW_START = 72.0f;
48 constexpr float FOLLOW_SPAN = 10.0f;
49 constexpr float FULL_COUNT = 100.0f;
50 constexpr float STAGE1 = 0.25f;
51 constexpr float STAGE2 = 0.65f;
52 constexpr float STAGE3 = 0.75f;
53 constexpr float STAGE4 = 0.85f;
54 constexpr float STAGE5 = 1.0f;
55 constexpr float OPACITY1 = 0.2f;
56 constexpr float OPACITY2 = 0.7f;
57 constexpr float OPACITY3 = 1.0f;
58 constexpr float SIZE_SCALE1 = 0.65f;
59 constexpr float SIZE_SCALE2 = 0.825f;
60 constexpr float SIZE_SCALE3 = 0.93f;
61 constexpr float MOVE_STEP = 0.06f;
62 constexpr float TRANS_OPACITY_SPAN = 0.3f;
63 constexpr float FULL_OPACITY = 255.0f;
64 constexpr float FAKE_DELTA = 0.01f;
65 constexpr float BASE_SCALE = 0.707f; // std::sqrt(2)/2
66 constexpr float REFRESH_DARK_MODE_RING_BLUR_RADIUS = 0.4f;
67 } // namespace
LoadingProgressModifier(LoadingProgressOwner loadingProgressOwner)68 LoadingProgressModifier::LoadingProgressModifier(LoadingProgressOwner loadingProgressOwner)
69 : enableLoading_(AceType::MakeRefPtr<PropertyBool>(true)),
70 offset_(AceType::MakeRefPtr<PropertyOffsetF>(OffsetF())),
71 contentSize_(AceType::MakeRefPtr<PropertySizeF>(SizeF())),
72 date_(AceType::MakeRefPtr<AnimatablePropertyFloat>(0.0f)),
73 color_(AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor::TRANSPARENT)),
74 centerDeviation_(AceType::MakeRefPtr<AnimatablePropertyFloat>(0.0f)),
75 cometOpacity_(AceType::MakeRefPtr<AnimatablePropertyFloat>(INITIAL_OPACITY_SCALE)),
76 cometSizeScale_(AceType::MakeRefPtr<AnimatablePropertyFloat>(INITIAL_SIZE_SCALE)),
77 cometTailLen_(AceType::MakeRefPtr<AnimatablePropertyFloat>(TOTAL_TAIL_LENGTH)),
78 sizeScale_(AceType::MakeRefPtr<AnimatablePropertyFloat>(1.0f)),
79 useContentModifier_(AceType::MakeRefPtr<PropertyBool>(false)),
80 loadingProgressOwner_(loadingProgressOwner)
81 {
82 AttachProperty(enableLoading_);
83 AttachProperty(offset_);
84 AttachProperty(contentSize_);
85 AttachProperty(date_);
86 AttachProperty(color_);
87 AttachProperty(centerDeviation_);
88 AttachProperty(cometOpacity_);
89 AttachProperty(cometSizeScale_);
90 AttachProperty(cometTailLen_);
91 };
92
onDraw(DrawingContext & context)93 void LoadingProgressModifier::onDraw(DrawingContext& context)
94 {
95 if (useContentModifier_->Get()) {
96 return;
97 }
98 if (!enableLoading_->Get()) {
99 return;
100 }
101 float date = date_->Get();
102 auto diameter = std::min(contentSize_->Get().Width(), contentSize_->Get().Height());
103 RingParam ringParam;
104 ringParam.strokeWidth = LoadingProgressUtill::GetRingStrokeWidth(diameter) * sizeScale_->Get();
105 ringParam.radius = LoadingProgressUtill::GetRingRadius(diameter) * sizeScale_->Get();
106 ringParam.movement =
107 (ringParam.radius * DOUBLE + ringParam.strokeWidth) * centerDeviation_->Get() * sizeScale_->Get();
108 ringParam.darkRingRadius = LoadingProgressUtill::GetRingDarkRadius(diameter) * sizeScale_->Get();
109 ringParam.darkBackgroundWidth = LoadingProgressUtill::GetRingDarkBackgroundWidth(diameter) * sizeScale_->Get();
110 ringParam.darkBackgroundRadius = LoadingProgressUtill::GetRingDarkBackgroundRadius(diameter) * sizeScale_->Get();
111
112 CometParam cometParam;
113 cometParam.radius = LoadingProgressUtill::GetCometRadius(diameter) * sizeScale_->Get();
114 cometParam.alphaScale = cometOpacity_->Get();
115 cometParam.sizeScale = cometSizeScale_->Get();
116 cometParam.pointCount = GetCometNumber();
117
118 auto orbitRadius = LoadingProgressUtill::GetOrbitRadius(diameter) * sizeScale_->Get();
119 if (date > COUNT) {
120 DrawRing(context, ringParam);
121 DrawOrbit(context, cometParam, orbitRadius, date);
122 } else {
123 DrawOrbit(context, cometParam, orbitRadius, date);
124 DrawRing(context, ringParam);
125 }
126 }
127
DrawRing(DrawingContext & context,const RingParam & ringParam)128 void LoadingProgressModifier::DrawRing(DrawingContext& context, const RingParam& ringParam)
129 {
130 auto& canvas = context.canvas;
131 canvas.Save();
132 RSPen pen;
133 RSFilter filter;
134 auto ringColor = color_->Get();
135 ringColor.BlendOpacity(RING_ALPHA);
136 pen.SetColor(ToRSColor(ringColor));
137 if (Container::CurrentColorMode() == ColorMode::DARK) {
138 if (ringColor.GetValue() == DEFAULT_COLOR_DARK.GetValue()) {
139 ringColor = LinearColor::WHITE;
140 }
141 // draw ring background
142 DrawRingBackground(context, ringParam, ringColor);
143
144 ringColor.BlendOpacity(RING_ALPHA_DARK / FULL_OPACITY);
145 pen.SetColor(ToRSColor(ringColor));
146
147 #ifndef USE_ROSEN_DRAWING
148 filter.SetImageFilter(RSImageFilter::CreateBlurImageFilter(
149 ringParam.darkRingRadius, ringParam.darkRingRadius, RSTileMode::DECAL, nullptr));
150 #else
151 filter.SetImageFilter(RSRecordingImageFilter::CreateBlurImageFilter(
152 ringParam.darkRingRadius, ringParam.darkRingRadius, RSTileMode::DECAL, nullptr));
153 #endif
154 pen.SetFilter(filter);
155 }
156 if (loadingProgressOwner_ == LoadingProgressOwner::REFRESH && Container::CurrentColorMode() == ColorMode::DARK) {
157 filter.SetMaskFilter(RSMaskFilter::CreateBlurMaskFilter(
158 RSBlurType::NORMAL, PipelineBase::GetCurrentDensity() * REFRESH_DARK_MODE_RING_BLUR_RADIUS));
159 pen.SetFilter(filter);
160 }
161 pen.SetWidth(ringParam.strokeWidth);
162 pen.SetAntiAlias(true);
163 canvas.AttachPen(pen);
164 canvas.DrawCircle(
165 { offset_->Get().GetX() + contentSize_->Get().Width() * HALF,
166 offset_->Get().GetY() + contentSize_->Get().Height() * HALF + ringParam.movement },
167 ringParam.radius);
168 canvas.DetachPen();
169 canvas.Restore();
170 }
171
DrawRingBackground(DrawingContext & context,const RingParam & ringParam,LinearColor & ringColor)172 void LoadingProgressModifier::DrawRingBackground(
173 DrawingContext& context, const RingParam& ringParam, LinearColor& ringColor)
174 {
175 auto& canvas = context.canvas;
176 canvas.Save();
177 RSPen pen;
178
179 ringColor.BlendOpacity(RING_ALPHA_DARK_BACKGROUNG / FULL_OPACITY);
180 pen.SetColor(ToRSColor(ringColor));
181
182 RSFilter filter;
183 #ifndef USE_ROSEN_DRAWING
184 filter.SetImageFilter(RSImageFilter::CreateBlurImageFilter(
185 ringParam.darkBackgroundRadius, ringParam.darkBackgroundRadius, RSTileMode::DECAL, nullptr));
186 #else
187 filter.SetImageFilter(RSRecordingImageFilter::CreateBlurImageFilter(
188 ringParam.darkBackgroundRadius, ringParam.darkBackgroundRadius, RSTileMode::DECAL, nullptr));
189 #endif
190 pen.SetFilter(filter);
191 pen.SetWidth(ringParam.darkBackgroundWidth);
192 pen.SetAntiAlias(true);
193 canvas.AttachPen(pen);
194 canvas.DrawCircle({ offset_->Get().GetX() + contentSize_->Get().Width() * HALF,
195 offset_->Get().GetY() + contentSize_->Get().Height() * HALF + ringParam.movement },
196 ringParam.radius);
197 canvas.DetachPen();
198 canvas.Restore();
199 }
200
DrawOrbit(DrawingContext & context,const CometParam & cometParam,float orbitRadius,float date)201 void LoadingProgressModifier::DrawOrbit(
202 DrawingContext& context, const CometParam& cometParam, float orbitRadius, float date)
203 {
204 auto pointCounts = cometParam.pointCount;
205 auto& canvas = context.canvas;
206 float width = contentSize_->Get().Width();
207 float height = contentSize_->Get().Height();
208 double angle = TOTAL_ANGLE * date / FULL_COUNT;
209 RSCamera3D camera;
210 RSMatrix matrix;
211 AdjustMatrix(camera, matrix);
212 auto center = RSPoint(offset_->Get().GetX() + width / 2, offset_->Get().GetY() + height / 2);
213 RSBrush brush;
214 brush.SetAntiAlias(true);
215 canvas.Save();
216 canvas.Translate(center.GetX(), center.GetY());
217 std::vector<RSPoint> points;
218 for (uint32_t i = 0; i < pointCounts; i++) {
219 RSPoint point;
220 float cometAngal = GetCurentCometAngle(angle, pointCounts - i, pointCounts);
221 float rad = cometAngal * PI_NUM / (TOTAL_ANGLE * HALF);
222 point.SetX(std::cos(rad) * orbitRadius);
223 point.SetY(-std::sin(rad) * orbitRadius);
224 points.push_back(point);
225 }
226 std::vector<RSPoint> distPoints(points.size());
227 matrix.MapPoints(distPoints, points, points.size());
228 auto cometColor = color_->Get();
229 float colorAlpha = cometColor.GetAlpha() / FULL_OPACITY;
230 if (Container::CurrentColorMode() == ColorMode::DARK && cometColor.GetValue() == DEFAULT_COLOR_DARK.GetValue()) {
231 colorAlpha = OPACITY3;
232 }
233 auto baseAlpha = colorAlpha * cometParam.alphaScale;
234 for (uint32_t i = 0; i < distPoints.size(); i++) {
235 RSPoint pointCenter = distPoints[i];
236 if (cometColor.GetValue() == Color::FOREGROUND.GetValue()) {
237 brush.SetColor(ToRSColor(cometColor));
238 } else {
239 float setAlpha = GetCurentCometOpacity(baseAlpha, distPoints.size() - i, distPoints.size());
240 if (NearZero(setAlpha)) {
241 continue;
242 }
243 brush.SetColor(
244 ToRSColor(Color::FromRGBO(cometColor.GetRed(), cometColor.GetGreen(), cometColor.GetBlue(), setAlpha)));
245 }
246 canvas.AttachBrush(brush);
247 canvas.DrawCircle(pointCenter, cometParam.radius * cometParam.sizeScale);
248 }
249 canvas.DetachBrush();
250 canvas.Restore();
251 }
252
AdjustMatrix(RSCamera3D & camera,RSMatrix & matrix)253 void LoadingProgressModifier::AdjustMatrix(RSCamera3D& camera, RSMatrix& matrix)
254 {
255 camera.Save();
256 camera.RotateYDegrees(ROTATEY);
257 camera.RotateXDegrees(ROTATEX);
258 camera.RotateZDegrees(ROTATEZ);
259 camera.ApplyToMatrix(matrix);
260 camera.Restore();
261 auto temp = matrix.Get(SKEWY);
262 matrix.Set(RSMatrix::SKEW_Y, matrix.Get(SCALEY));
263 matrix.Set(RSMatrix::SCALE_Y, temp);
264 }
265
StartRecycleRingAnimation()266 void LoadingProgressModifier::StartRecycleRingAnimation()
267 {
268 auto context = PipelineBase::GetCurrentContext();
269 CHECK_NULL_VOID(context);
270 auto previousStageCurve = AceType::MakeRefPtr<CubicCurve>(0.0f, 0.0f, 0.67f, 1.0f);
271 AnimationOption option;
272 option.SetDuration(isVisible_ ? LOADING_DURATION : 0);
273 option.SetCurve(previousStageCurve);
274 if (context->IsFormRender() && !IsDynamicComponent()) {
275 option.SetIteration(1);
276 } else {
277 option.SetIteration(-1);
278 }
279 AnimationUtils::OpenImplicitAnimation(option, previousStageCurve, nullptr);
280 auto middleStageCurve = AceType::MakeRefPtr<CubicCurve>(0.33f, 0.0f, 0.67f, 1.0f);
281 AnimationUtils::AddKeyFrame(
282 STAGE1, middleStageCurve, [weakCenterDeviation = AceType::WeakClaim(AceType::RawPtr(centerDeviation_))]() {
283 auto centerDeviation = weakCenterDeviation.Upgrade();
284 CHECK_NULL_VOID(centerDeviation);
285 centerDeviation->Set(-1 * MOVE_STEP);
286 });
287 auto latterStageCurve = AceType::MakeRefPtr<CubicCurve>(0.33f, 0.0f, 1.0f, 1.0f);
288 AnimationUtils::AddKeyFrame(
289 STAGE3, latterStageCurve, [weakCenterDeviation = AceType::WeakClaim(AceType::RawPtr(centerDeviation_))]() {
290 auto centerDeviation = weakCenterDeviation.Upgrade();
291 CHECK_NULL_VOID(centerDeviation);
292 centerDeviation->Set(MOVE_STEP);
293 });
294 AnimationUtils::AddKeyFrame(
295 STAGE5, latterStageCurve, [weakCenterDeviation = AceType::WeakClaim(AceType::RawPtr(centerDeviation_))]() {
296 auto centerDeviation = weakCenterDeviation.Upgrade();
297 CHECK_NULL_VOID(centerDeviation);
298 centerDeviation->Set(0.0f);
299 });
300 AnimationUtils::CloseImplicitAnimation();
301 }
302
StartRecycleCometAnimation()303 void LoadingProgressModifier::StartRecycleCometAnimation()
304 {
305 auto context = PipelineBase::GetCurrentContext();
306 CHECK_NULL_VOID(context);
307 auto curve = AceType::MakeRefPtr<LinearCurve>();
308 AnimationOption option;
309 option.SetDuration(isVisible_ ? LOADING_DURATION : 0);
310 option.SetCurve(curve);
311 if (context->IsFormRender() && !IsDynamicComponent()) {
312 option.SetIteration(1);
313 } else {
314 option.SetIteration(-1);
315 }
316
317 cometOpacity_->Set(OPACITY2);
318 cometTailLen_->Set(TOTAL_TAIL_LENGTH);
319 AnimationUtils::OpenImplicitAnimation(option, curve, nullptr);
320 AnimationUtils::AddKeyFrame(STAGE1, curve,
321 [weakCometOpacity = AceType::WeakClaim(AceType::RawPtr(cometOpacity_)),
322 weakCometSizeScale = AceType::WeakClaim(AceType::RawPtr(cometSizeScale_))]() {
323 auto cometOpacity = weakCometOpacity.Upgrade();
324 if (cometOpacity) {
325 cometOpacity->Set(OPACITY1);
326 }
327 auto cometSizeScale = weakCometSizeScale.Upgrade();
328 if (cometSizeScale) {
329 cometSizeScale->Set(SIZE_SCALE1);
330 }
331 });
332 AnimationUtils::AddKeyFrame(STAGE2, curve,
333 [weakCometOpacity = AceType::WeakClaim(AceType::RawPtr(cometOpacity_)),
334 weakCometSizeScale = AceType::WeakClaim(AceType::RawPtr(cometSizeScale_))]() {
335 auto cometOpacity = weakCometOpacity.Upgrade();
336 if (cometOpacity) {
337 cometOpacity->Set(OPACITY3);
338 }
339 auto cometSizeScale = weakCometSizeScale.Upgrade();
340 if (cometSizeScale) {
341 cometSizeScale->Set(SIZE_SCALE3);
342 }
343 });
344 AnimationUtils::AddKeyFrame(STAGE3, curve,
345 [weakCometOpacity = AceType::WeakClaim(AceType::RawPtr(cometOpacity_)),
346 weakCometSizeScale = AceType::WeakClaim(AceType::RawPtr(cometSizeScale_))]() {
347 auto cometOpacity = weakCometOpacity.Upgrade();
348 if (cometOpacity) {
349 cometOpacity->Set(OPACITY3);
350 }
351 auto cometSizeScale = weakCometSizeScale.Upgrade();
352 if (cometSizeScale) {
353 cometSizeScale->Set(1.0f);
354 }
355 });
356 AnimationUtils::AddKeyFrame(STAGE4, curve,
357 [weakCometOpacity = AceType::WeakClaim(AceType::RawPtr(cometOpacity_)),
358 weakCometSizeScale = AceType::WeakClaim(AceType::RawPtr(cometSizeScale_))]() {
359 auto cometOpacity = weakCometOpacity.Upgrade();
360 if (cometOpacity) {
361 cometOpacity->Set(OPACITY3);
362 }
363 auto cometSizeScale = weakCometSizeScale.Upgrade();
364 if (cometSizeScale) {
365 cometSizeScale->Set(SIZE_SCALE3);
366 }
367 });
368 AnimationUtils::AddKeyFrame(STAGE5, curve,
369 [weakCometOpacity = AceType::WeakClaim(AceType::RawPtr(cometOpacity_)),
370 weakCometSizeScale = AceType::WeakClaim(AceType::RawPtr(cometSizeScale_))]() {
371 auto cometOpacity = weakCometOpacity.Upgrade();
372 if (cometOpacity) {
373 cometOpacity->Set(OPACITY2);
374 }
375 auto cometSizeScale = weakCometSizeScale.Upgrade();
376 if (cometSizeScale) {
377 cometSizeScale->Set(SIZE_SCALE2);
378 }
379 });
380 AnimationUtils::CloseImplicitAnimation();
381 }
382
StartCometTailAnimation()383 void LoadingProgressModifier::StartCometTailAnimation()
384 {
385 auto curve = AceType::MakeRefPtr<LinearCurve>();
386 AnimationOption option;
387 option.SetDuration(TAIL_ANIAMTION_DURATION);
388 option.SetIteration(1);
389 option.SetCurve(curve);
390 AnimationUtils::Animate(option, [weakCometTailLen = AceType::WeakClaim(AceType::RawPtr(cometTailLen_))]() {
391 auto cometTailLen = weakCometTailLen.Upgrade();
392 CHECK_NULL_VOID(cometTailLen);
393 cometTailLen->Set(TOTAL_TAIL_LENGTH);
394 });
395 }
396
GetCurentCometOpacity(float baseOpacity,uint32_t index,uint32_t totalNumber)397 float LoadingProgressModifier::GetCurentCometOpacity(float baseOpacity, uint32_t index, uint32_t totalNumber)
398 {
399 return baseOpacity * std::pow(TAIL_ALPHA_RATIO, std::clamp(index, 1u, totalNumber) - 1);
400 }
401
GetCurentCometAngle(float baseAngle,uint32_t index,uint32_t totalNumber)402 float LoadingProgressModifier::GetCurentCometAngle(float baseAngle, uint32_t index, uint32_t totalNumber)
403 {
404 return std::fmod((baseAngle - (std::clamp(index, 1u, totalNumber) - 1) * COMET_TAIL_ANGLE), TOTAL_ANGLE);
405 }
406
GetCometNumber()407 uint32_t LoadingProgressModifier::GetCometNumber()
408 {
409 CHECK_NULL_RETURN(cometTailLen_, TOTAL_POINTS_COUNT);
410 return static_cast<uint32_t>(cometTailLen_->Get() / COMET_TAIL_ANGLE);
411 }
412
StartRecycle()413 void LoadingProgressModifier::StartRecycle()
414 {
415 auto context = PipelineBase::GetCurrentContext();
416 CHECK_NULL_VOID(context);
417 if (isLoading_) {
418 return;
419 }
420 sizeScale_->Set(1.0f);
421 if (date_) {
422 isLoading_ = true;
423 date_->Set(0.0f);
424 AnimationOption option = AnimationOption();
425 RefPtr<Curve> curve = AceType::MakeRefPtr<CubicCurve>(0.25f, 0.30f, 0.50f, 0.14f);
426 option.SetDuration(isVisible_ ? LOADING_DURATION : 0);
427 option.SetDelay(0);
428 option.SetCurve(curve);
429 if (context->IsFormRender() && !IsDynamicComponent()) {
430 option.SetIteration(1);
431 } else {
432 option.SetIteration(-1);
433 }
434 AnimationUtils::Animate(option, [weakDate = AceType::WeakClaim(AceType::RawPtr(date_))]() {
435 auto date = weakDate.Upgrade();
436 CHECK_NULL_VOID(date);
437 date->Set(FULL_COUNT);
438 });
439 }
440 cometOpacity_->Set(INITIAL_OPACITY_SCALE);
441 cometSizeScale_->Set(INITIAL_SIZE_SCALE);
442 // ring up and down shift animation
443 StartRecycleRingAnimation();
444 // comet's circle Color transparency and sizeScale animation
445 StartRecycleCometAnimation();
446 }
447
StartTransToRecycleAnimation()448 void LoadingProgressModifier::StartTransToRecycleAnimation()
449 {
450 sizeScale_->Set(1.0f);
451 auto curve = AceType::MakeRefPtr<CubicCurve>(0.6f, 0.2f, 1.0f, 1.0f);
452 AnimationOption option;
453 option.SetDuration(TRANS_DURATION);
454 option.SetIteration(1);
455 option.SetCurve(curve);
456 AnimationUtils::Animate(
457 option,
458 [weakDate = AceType::WeakClaim(AceType::RawPtr(date_)),
459 weakCometOpacity = AceType::WeakClaim(AceType::RawPtr(cometOpacity_)),
460 weakCometSizeScale = AceType::WeakClaim(AceType::RawPtr(cometSizeScale_))]() {
461 auto date = weakDate.Upgrade();
462 if (date) {
463 date->Set(FULL_COUNT);
464 }
465 auto cometOpacity = weakCometOpacity.Upgrade();
466 if (cometOpacity) {
467 cometOpacity->Set(1.0 - TRANS_OPACITY_SPAN);
468 }
469 auto cometSizeScale = weakCometSizeScale.Upgrade();
470 if (cometSizeScale) {
471 cometSizeScale->Set(INITIAL_SIZE_SCALE);
472 }
473 },
474 [weak = AceType::WeakClaim(this)]() {
475 auto modify = weak.Upgrade();
476 CHECK_NULL_VOID(modify);
477 modify->StartRecycle();
478 });
479 StartCometTailAnimation();
480 }
481
ChangeRefreshFollowData(float refreshFollowRatio)482 void LoadingProgressModifier::ChangeRefreshFollowData(float refreshFollowRatio)
483 {
484 auto ratio = CorrectNormalize(refreshFollowRatio);
485 sizeScale_->Set(BASE_SCALE + (1.0 - BASE_SCALE) * ratio);
486 if (isLoading_) {
487 CloseAnimation(FOLLOW_START, COMET_TAIL_ANGLE, 1.0f, 1.0f);
488 }
489 CHECK_NULL_VOID(date_);
490 date_->Set(FOLLOW_START + FOLLOW_SPAN * ratio);
491 cometTailLen_->Set(COMET_TAIL_ANGLE);
492 cometOpacity_->Set(1.0f);
493 cometSizeScale_->Set(1.0f);
494 }
495
ChangeSizeScaleData(float refreshFadeAwayRatio)496 void LoadingProgressModifier::ChangeSizeScaleData(float refreshFadeAwayRatio)
497 {
498 auto ratio = CorrectNormalize(refreshFadeAwayRatio);
499 sizeScale_->Set(BASE_SCALE + (1.0 - BASE_SCALE) * ratio);
500 }
501
CloseAnimation(float date,float cometLen,float cometOpacity,float cometScale)502 void LoadingProgressModifier::CloseAnimation(float date, float cometLen, float cometOpacity, float cometScale)
503 {
504 isLoading_ = false;
505 AnimationOption option = AnimationOption();
506 RefPtr<Curve> curve = AceType::MakeRefPtr<LinearCurve>();
507 option.SetDuration(0);
508 option.SetIteration(1);
509 option.SetCurve(curve);
510 date_->Set(date + FAKE_DELTA);
511 cometTailLen_->Set(cometLen + FAKE_DELTA);
512 cometOpacity_->Set(cometOpacity + FAKE_DELTA);
513 cometSizeScale_->Set(cometScale + FAKE_DELTA);
514 centerDeviation_->Set(0.0f + FAKE_DELTA);
515 AnimationUtils::Animate(option, [weak = AceType::WeakClaim(this), date, cometLen, cometOpacity, cometScale]() {
516 auto curObj = weak.Upgrade();
517 CHECK_NULL_VOID(curObj);
518 curObj->date_->Set(date);
519 curObj->cometTailLen_->Set(cometLen);
520 curObj->cometOpacity_->Set(cometOpacity);
521 curObj->cometSizeScale_->Set(cometScale);
522 curObj->centerDeviation_->Set(0.0f);
523 });
524 }
CorrectNormalize(float originData)525 float LoadingProgressModifier::CorrectNormalize(float originData)
526 {
527 auto ratio = originData;
528 if (ratio < 0.0f) {
529 ratio = 0.0f;
530 }
531 if (ratio > 1.0f) {
532 ratio = 1.0f;
533 };
534 return ratio;
535 }
536 } // namespace OHOS::Ace::NG
537