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 "core/components_ng/pattern/animator/animator_model_ng.h"
17
18 #ifdef NG_BUILD
19 #include "bridge/declarative_frontend/ng/declarative_frontend_ng.h"
20 #else
21 #include "bridge/declarative_frontend/declarative_frontend.h"
22 #endif
23 #include "core/common/container.h"
24 #include "core/components_ng/pattern/stage/page_pattern.h"
25 #ifdef PLUGIN_COMPONENT_SUPPORTED
26 #include "core/common/plugin_manager.h"
27 #endif
28
29 namespace OHOS::Ace::Framework {
30 namespace {
GetCurrentPage()31 RefPtr<NG::PagePattern> GetCurrentPage()
32 {
33 RefPtr<NG::FrameNode> pageNode;
34 #ifdef PLUGIN_COMPONENT_SUPPORTED
35 if (Container::CurrentId() >= MIN_PLUGIN_SUBCONTAINER_ID) {
36 auto pluginContainer = PluginManager::GetInstance().GetPluginSubContainer(Container::CurrentId());
37 CHECK_NULL_RETURN(pluginContainer, nullptr);
38 pageNode = pluginContainer->GetPluginNode().Upgrade();
39 CHECK_NULL_RETURN(pageNode, nullptr);
40 } else
41 #endif
42 {
43 auto container = Container::Current();
44 CHECK_NULL_RETURN(container, nullptr);
45 #ifdef NG_BUILD
46 auto frontEnd = AceType::DynamicCast<DeclarativeFrontendNG>(container->GetFrontend());
47 #else
48 auto frontEnd = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
49 #endif
50 CHECK_NULL_RETURN(frontEnd, nullptr);
51 auto pageRouterManager = frontEnd->GetPageRouterManager();
52 CHECK_NULL_RETURN(pageRouterManager, nullptr);
53 pageNode = pageRouterManager->GetCurrentPageNode();
54 CHECK_NULL_RETURN(pageNode, nullptr);
55 }
56 return pageNode->GetPattern<NG::PagePattern>();
57 }
58 } // namespace
59
Create(const std::string & animatorId)60 void AnimatorModelNG::Create(const std::string& animatorId)
61 {
62 auto page = GetCurrentPage();
63 CHECK_NULL_VOID(page);
64 auto pageNode = page->GetHost();
65 CHECK_NULL_VOID(pageNode);
66 auto animatorInfo = page->GetJsAnimator(animatorId);
67 if (!animatorInfo) {
68 animatorInfo = AceType::MakeRefPtr<AnimatorInfo>();
69 TAG_LOGI(AceLogTag::ACE_ANIMATION,
70 "create animator component, id:%{public}s, pageUrl:%{public}s, pageId:%{public}d", animatorId.c_str(),
71 page->GetPageUrl().c_str(), pageNode->GetId());
72 auto animator = CREATE_ANIMATOR(animatorId.c_str());
73 animatorInfo->SetAnimator(animator);
74 page->AddJsAnimator(animatorId, animatorInfo);
75 }
76 }
77
GetAnimatorInfo(const std::string & animatorId)78 RefPtr<AnimatorInfo> AnimatorModelNG::GetAnimatorInfo(const std::string& animatorId)
79 {
80 auto page = GetCurrentPage();
81 if (!page) {
82 TAG_LOGW(AceLogTag::ACE_ANIMATION, "look for animator component, but current page is null, id:%{public}s",
83 animatorId.c_str());
84 return nullptr;
85 }
86 auto pageNode = page->GetHost();
87 CHECK_NULL_RETURN(pageNode, nullptr);
88 auto animatorInfo = page->GetJsAnimator(animatorId);
89 if (!animatorInfo) {
90 TAG_LOGW(AceLogTag::ACE_ANIMATION,
91 "cannot find animator component in current page, id:%{public}s, pageUrl:%{public}s, pageId:%{public}d",
92 animatorId.c_str(), page->GetPageUrl().c_str(), pageNode->GetId());
93 }
94 return animatorInfo;
95 }
96 } // namespace OHOS::Ace::Framework
97