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