1 /*
2 * Copyright (c) 2022 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 "bridge/declarative_frontend/jsview/models/animator_model_impl.h"
17
18 #ifdef USE_ARK_ENGINE
19 #include "bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h"
20 #endif
21
22 namespace OHOS::Ace::Framework {
23 namespace {
GetCurrentPage()24 RefPtr<JsAcePage> GetCurrentPage()
25 {
26 #ifdef USE_ARK_ENGINE
27 auto page = JsiDeclarativeEngineInstance::GetStagingPage(Container::CurrentId());
28 return page;
29 #endif
30 return nullptr;
31 }
32 } // namespace
33
Create(const std::string & animatorId)34 void AnimatorModelImpl::Create(const std::string& animatorId)
35 {
36 auto page = GetCurrentPage();
37 CHECK_NULL_VOID(page);
38 auto animatorInfo = page->GetAnimatorInfo(animatorId);
39 if (!animatorInfo) {
40 animatorInfo = AceType::MakeRefPtr<AnimatorInfo>();
41 auto animator = CREATE_ANIMATOR();
42 animatorInfo->SetAnimator(animator);
43 page->AddAnimatorInfo(animatorId, animatorInfo);
44 }
45 }
46
GetAnimatorInfo(const std::string & animatorId)47 RefPtr<AnimatorInfo> AnimatorModelImpl::GetAnimatorInfo(const std::string& animatorId)
48 {
49 auto page = GetCurrentPage();
50 CHECK_NULL_RETURN(page, nullptr);
51 auto animatorInfo = page->GetAnimatorInfo(animatorId);
52 return animatorInfo;
53 }
54
AddEventListener(std::function<void ()> && callback,EventOperation operation,const std::string & animatorId)55 void AnimatorModelImpl::AddEventListener(
56 std::function<void()>&& callback, EventOperation operation, const std::string& animatorId)
57 {
58 auto animatorInfo = GetAnimatorInfo(animatorId);
59 CHECK_NULL_VOID(animatorInfo);
60 auto animator = animatorInfo->GetAnimator();
61 CHECK_NULL_VOID(animatorInfo);
62 auto pipelineContext = AceType::DynamicCast<PipelineContext>(PipelineBase::GetCurrentContext());
63 CHECK_NULL_VOID(pipelineContext);
64 WeakPtr<PipelineContext> weakContext = WeakPtr<PipelineContext>(pipelineContext);
65 auto eventMarker = callback ? EventMarker(std::move(callback)) : EventMarker();
66 switch (operation) {
67 case EventOperation::START:
68 animator->ClearStartListeners();
69 if (!eventMarker.IsEmpty()) {
70 animator->AddStartListener(
71 [eventMarker, weakContext] { AceAsyncEvent<void()>::Create(eventMarker, weakContext)(); });
72 }
73 break;
74 case EventOperation::PAUSE:
75 animator->ClearPauseListeners();
76 if (!eventMarker.IsEmpty()) {
77 animator->AddPauseListener(
78 [eventMarker, weakContext] { AceAsyncEvent<void()>::Create(eventMarker, weakContext)(); });
79 }
80 break;
81 case EventOperation::REPEAT:
82 animator->ClearRepeatListeners();
83 if (!eventMarker.IsEmpty()) {
84 animator->AddRepeatListener(
85 [eventMarker, weakContext] { AceAsyncEvent<void()>::Create(eventMarker, weakContext)(); });
86 }
87 break;
88 case EventOperation::CANCEL:
89 animator->ClearIdleListeners();
90 if (!eventMarker.IsEmpty()) {
91 animator->AddIdleListener(
92 [eventMarker, weakContext] { AceAsyncEvent<void()>::Create(eventMarker, weakContext)(); });
93 }
94 break;
95 case EventOperation::FINISH:
96 animator->ClearStopListeners();
97 if (!eventMarker.IsEmpty()) {
98 animator->AddStopListener(
99 [eventMarker, weakContext] { AceAsyncEvent<void()>::Create(eventMarker, weakContext)(); });
100 }
101 break;
102 case EventOperation::NONE:
103 default:
104 break;
105 }
106 }
107 } // namespace OHOS::Ace::Framework
108