1 /*
2 * Copyright (c) 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 "ui/animation/animation_utils.h"
17
18 #include "core/components_ng/render/animation_utils.h"
19 #include "core/pipeline/pipeline_base.h"
20
21 namespace OHOS::Ace::Kit {
22
23 namespace {
GetWrappedCallback(const std::function<void ()> & callback,const bool flushUITasks)24 std::function<void()> GetWrappedCallback(const std::function<void()>& callback, const bool flushUITasks)
25 {
26 if (!callback) {
27 return nullptr;
28 }
29 if (!flushUITasks) {
30 return callback;
31 }
32
33 auto wrappedCallback = [propertyCallback = callback]() {
34 propertyCallback();
35 auto pipeline = PipelineBase::GetCurrentContext();
36 if (pipeline) {
37 pipeline->FlushUITasks();
38 }
39 };
40 return wrappedCallback;
41 }
42 }
43
44 class AnimationUtils::Animation {
45 private:
46 std::shared_ptr<OHOS::Ace::AnimationUtils::Animation> animation_;
47 friend AnimationUtils;
48 };
49
Animate(const AnimationOption & option,const AnimationCallback & callback,const AnimationCallback & finishCallback,const AnimationCallback & repeatCallback)50 void AnimationUtils::Animate(const AnimationOption& option, const AnimationCallback& callback,
51 const AnimationCallback& finishCallback, const AnimationCallback& repeatCallback)
52 {
53 OHOS::Ace::AnimationUtils::Animate(option, callback, finishCallback, repeatCallback);
54 }
55
StartAnimation(const AnimationOption & option,const AnimationCallback & callback,const AnimationCallback & finishCallback,const AnimationCallback & repeatCallback,bool flushUITasks)56 std::shared_ptr<AnimationUtils::Animation> AnimationUtils::StartAnimation(const AnimationOption& option,
57 const AnimationCallback& callback, const AnimationCallback& finishCallback,
58 const AnimationCallback& repeatCallback, bool flushUITasks)
59 {
60 auto propertyCallback = GetWrappedCallback(callback, flushUITasks);
61 std::shared_ptr<AnimationUtils::Animation> animation = std::make_shared<AnimationUtils::Animation>();
62 auto animations = OHOS::Ace::AnimationUtils::StartAnimation(option, propertyCallback,
63 finishCallback, repeatCallback);
64 animation->animation_ = animations;
65 return animation;
66 }
67
PauseAnimation(const std::shared_ptr<AnimationUtils::Animation> & animation)68 void AnimationUtils::PauseAnimation(const std::shared_ptr<AnimationUtils::Animation>& animation)
69 {
70 CHECK_NULL_VOID(animation);
71 OHOS::Ace::AnimationUtils::PauseAnimation(animation->animation_);
72 }
73
ResumeAnimation(const std::shared_ptr<AnimationUtils::Animation> & animation)74 void AnimationUtils::ResumeAnimation(const std::shared_ptr<AnimationUtils::Animation>& animation)
75 {
76 CHECK_NULL_VOID(animation);
77 OHOS::Ace::AnimationUtils::ResumeAnimation(animation->animation_);
78 }
79
ReverseAnimation(const std::shared_ptr<AnimationUtils::Animation> & animation)80 void AnimationUtils::ReverseAnimation(const std::shared_ptr<AnimationUtils::Animation>& animation)
81 {
82 CHECK_NULL_VOID(animation);
83 OHOS::Ace::AnimationUtils::ReverseAnimation(animation->animation_);
84 }
85
StopAnimation(const std::shared_ptr<AnimationUtils::Animation> & animation)86 void AnimationUtils::StopAnimation(const std::shared_ptr<AnimationUtils::Animation>& animation)
87 {
88 CHECK_NULL_VOID(animation);
89 OHOS::Ace::AnimationUtils::StopAnimation(animation->animation_);
90 animation->animation_.reset();
91 }
92 } // namespace OHOS::Ace::Kit
93