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 template<typename T> 52 void ForceUpdate(const T& delta); 53 Finished()54 bool Finished() const 55 { 56 return remainingTicks_ <= 0; 57 } 58 Pause()59 void Pause() 60 { 61 paused_ = true; 62 } 63 Resume()64 void Resume() 65 { 66 paused_ = false; 67 } 68 69 /** 70 * @brief immediately terminate the animation without updating the value 71 */ 72 void End(); 73 74 /** 75 * @brief jump to the end of the animation and update the value. Requires one more frame to finish. 76 */ 77 void JumpToEnd(); 78 79 private: 80 void UpdateProp(const WeakPtr<NG::PropertyBase>& propWk) const; 81 82 AnimationCallbacks cbs_; 83 WeakPtr<NG::PropertyBase> prop_; 84 int32_t remainingTicks_ = 0; 85 bool paused_ = false; 86 }; 87 } // namespace OHOS::Ace 88 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_ANIMATION_UTILS_H 89