1 /* 2 * Copyright (c) 2021 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_ANIMATION_SCHEDULER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SCHEDULER_H 18 19 #include <functional> 20 21 #include "core/animation/schedule_task.h" 22 #include "core/components/common/properties/animation_option.h" 23 24 namespace OHOS::Ace { 25 26 class PipelineBase; 27 28 class ACE_FORCE_EXPORT Scheduler : public ScheduleTask { 29 DECLARE_ACE_TYPE(Scheduler, ScheduleTask); 30 31 public: 32 using OnFrameCallback = std::function<void(uint64_t)>; 33 Scheduler(OnFrameCallback && callback,const WeakPtr<PipelineBase> & context)34 Scheduler(OnFrameCallback&& callback, const WeakPtr<PipelineBase>& context) 35 : callback_(std::move(callback)), context_(context) 36 {} 37 38 Scheduler() = delete; 39 ~Scheduler()40 ~Scheduler() override 41 { 42 if (IsActive()) { 43 Stop(); 44 } 45 } 46 47 void OnFrame(uint64_t nanoTimestamp) override; 48 49 void Start(); 50 51 void Stop(); 52 IsActive()53 bool IsActive() const 54 { 55 return isRunning_; 56 } 57 GetContext()58 WeakPtr<PipelineBase> GetContext() 59 { 60 return context_; 61 } 62 63 bool Animate(const AnimationOption& option, const RefPtr<Curve>& curve, 64 const std::function<void()> propertyCallback, const std::function<void()>& finishCallBack = nullptr); 65 66 void OpenImplicitAnimation(const AnimationOption& option, const RefPtr<Curve>& curve, 67 const std::function<void()>& finishCallBack = nullptr); 68 69 bool CloseImplicitAnimation(); 70 71 void AddKeyFrame( 72 float fraction, const RefPtr<Curve>& curve, const std::function<void()>& propertyCallback); 73 74 void AddKeyFrame(float fraction, const std::function<void()>& propertyCallback); 75 76 private: 77 int32_t scheduleId_ = 0; 78 bool isRunning_ = false; 79 uint64_t startupTimestamp_ = 0; 80 OnFrameCallback callback_ = nullptr; 81 WeakPtr<PipelineBase> context_; 82 }; 83 84 class SchedulerBuilder { 85 public: Build(Scheduler::OnFrameCallback && callback,const WeakPtr<PipelineBase> & context)86 static RefPtr<Scheduler> Build(Scheduler::OnFrameCallback&& callback, const WeakPtr<PipelineBase>& context) 87 { 88 return AceType::MakeRefPtr<Scheduler>(std::move(callback), context); 89 } 90 }; 91 92 } // namespace OHOS::Ace 93 94 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SCHEDULER_H 95