• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_UTILS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_UTILS_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components_ng/base/modifier.h"
21 namespace OHOS::Ace {
22 
23 struct AnimationCallbacks {
24     std::function<void()> finishCb;
25     std::function<void()> repeatCb;
26 };
27 class MockImplicitAnimation : public AceType {
28     DECLARE_ACE_TYPE(MockImplicitAnimation, AceType);
29 
30 public:
MockImplicitAnimation(WeakPtr<NG::PropertyBase> prop,AnimationCallbacks cbs,int32_t ticks)31     MockImplicitAnimation(WeakPtr<NG::PropertyBase> prop, AnimationCallbacks cbs, int32_t ticks)
32         : cbs_(std::move(cbs)), prop_(std::move(prop)), remainingTicks_(ticks)
33     {}
34 
Update(AnimationCallbacks cbs,int32_t ticks)35     void Update(AnimationCallbacks cbs, int32_t ticks)
36     {
37         cbs_ = std::move(cbs);
38         remainingTicks_ = ticks;
39         paused_ = false;
40     }
41 
42     /**
43      * @brief move to next frame
44      */
45     void Next();
46 
47     /**
48      * @brief force update the property by @c delta to simulate velocity update
49      * @note currently only supports float type
50      */
51     void ForceUpdate(float delta);
52 
Finished()53     bool Finished() const
54     {
55         return remainingTicks_ <= 0;
56     }
57 
Pause()58     void Pause()
59     {
60         paused_ = true;
61     }
62 
Resume()63     void Resume()
64     {
65         paused_ = false;
66     }
67 
68     /**
69      * @brief immediately terminate the animation without updating the value
70      */
71     void End();
72 
73     /**
74      * @brief jump to the end of the animation and update the value. Requires one more frame to finish.
75      */
76     void JumpToEnd();
77 
78 private:
79     void UpdateProp(const WeakPtr<NG::PropertyBase>& propWk) const;
80 
81     AnimationCallbacks cbs_;
82     WeakPtr<NG::PropertyBase> prop_;
83     int32_t remainingTicks_ = 0;
84     bool paused_ = false;
85 };
86 } // namespace OHOS::Ace
87 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_UTILS_H
88