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 #include "core/components_ng/pattern/animator/animator_model_ng.h"
16
17 #include "bridge/declarative_frontend/ng/declarative_frontend_ng.h"
18 #include "core/common/container.h"
19 #include "core/components_ng/pattern/stage/page_pattern.h"
20 #ifdef PLUGIN_COMPONENT_SUPPORTED
21 #include "core/common/plugin_manager.h"
22 #endif
23
24 namespace OHOS::Ace::Framework {
25 namespace {
GetCurrentPage()26 RefPtr<NG::PagePattern> GetCurrentPage()
27 {
28 RefPtr<NG::FrameNode> pageNode;
29 #ifdef PLUGIN_COMPONENT_SUPPORTED
30 if (Container::CurrentId() >= MIN_PLUGIN_SUBCONTAINER_ID) {
31 auto pluginContainer = PluginManager::GetInstance().GetPluginSubContainer(Container::CurrentId());
32 CHECK_NULL_RETURN(pluginContainer, nullptr);
33 pageNode = pluginContainer->GetPluginNode().Upgrade();
34 CHECK_NULL_RETURN(pageNode, nullptr);
35 } else
36 #endif
37 {
38 auto container = Container::Current();
39 CHECK_NULL_RETURN(container, nullptr);
40 auto frontEnd = AceType::DynamicCast<DeclarativeFrontendNG>(container->GetFrontend());
41 CHECK_NULL_RETURN(frontEnd, nullptr);
42 auto pageRouterManager = frontEnd->GetPageRouterManager();
43 CHECK_NULL_RETURN(pageRouterManager, nullptr);
44 pageNode = pageRouterManager->GetCurrentPageNode();
45 CHECK_NULL_RETURN(pageNode, nullptr);
46 }
47 return pageNode->GetPattern<NG::PagePattern>();
48 }
49 } // namespace
50
Create(const std::string & animatorId)51 void AnimatorModelNG::Create(const std::string& animatorId)
52 {
53 auto page = GetCurrentPage();
54 CHECK_NULL_VOID(page);
55 auto animatorInfo = page->GetJsAnimator(animatorId);
56 if (!animatorInfo) {
57 animatorInfo = AceType::MakeRefPtr<AnimatorInfo>();
58 auto animator = CREATE_ANIMATOR(animatorId.c_str());
59 animatorInfo->SetAnimator(animator);
60 page->AddJsAnimator(animatorId, animatorInfo);
61 }
62 }
63
GetAnimatorInfo(const std::string & animatorId)64 RefPtr<AnimatorInfo> AnimatorModelNG::GetAnimatorInfo(const std::string& animatorId)
65 {
66 auto page = GetCurrentPage();
67 CHECK_NULL_RETURN(page, nullptr);
68 auto animatorInfo = page->GetJsAnimator(animatorId);
69 return animatorInfo;
70 }
71 } // namespace OHOS::Ace::Framework
72