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 animatorInfo = page->GetJsAnimator(animatorId);
65 if (!animatorInfo) {
66 animatorInfo = AceType::MakeRefPtr<AnimatorInfo>();
67 TAG_LOGI(AceLogTag::ACE_ANIMATION,
68 "create animator component, id:%{public}s, pageUrl:%{public}s, pagePattern:%{public}p", animatorId.c_str(),
69 page->GetPageUrl().c_str(), AceType::RawPtr(page));
70 auto animator = CREATE_ANIMATOR(animatorId.c_str());
71 animatorInfo->SetAnimator(animator);
72 page->AddJsAnimator(animatorId, animatorInfo);
73 }
74 }
75
GetAnimatorInfo(const std::string & animatorId)76 RefPtr<AnimatorInfo> AnimatorModelNG::GetAnimatorInfo(const std::string& animatorId)
77 {
78 auto page = GetCurrentPage();
79 if (!page) {
80 TAG_LOGW(AceLogTag::ACE_ANIMATION, "look for animator component, but current page is null, id:%{public}s",
81 animatorId.c_str());
82 return nullptr;
83 }
84 auto animatorInfo = page->GetJsAnimator(animatorId);
85 if (!animatorInfo) {
86 TAG_LOGW(AceLogTag::ACE_ANIMATION,
87 "cannot find animator component in current page, id:%{public}s, pageUrl:%{public}s, pagePattern:%{public}p",
88 animatorId.c_str(), page->GetPageUrl().c_str(), AceType::RawPtr(page));
89 }
90 return animatorInfo;
91 }
92 } // namespace OHOS::Ace::Framework
93