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