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