/* * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "core/common/container.h" #include "core/common/ace_engine.h" #ifdef PLUGIN_COMPONENT_SUPPORTED #include "core/common/plugin_manager.h" #endif namespace OHOS::Ace { int32_t Container::CurrentId() { return ContainerScope::CurrentId(); } int32_t Container::SafelyId() { uint32_t containerCount = ContainerScope::ContainerCount(); if (containerCount == 0) { return INSTANCE_ID_UNDEFINED; } if (containerCount == 1) { return ContainerScope::SingletonId(); } int32_t currentId = ContainerScope::RecentActiveId(); if (currentId >= 0) { return currentId; } currentId = ContainerScope::RecentForegroundId(); if (currentId >= 0) { return currentId; } return ContainerScope::DefaultId(); } int32_t Container::CurrentIdSafely() { int32_t currentId = ContainerScope::CurrentId(); if (currentId >= 0) { return currentId; } return SafelyId(); } RefPtr Container::Current() { return AceEngine::Get().GetContainer(ContainerScope::CurrentId()); } RefPtr Container::CurrentSafely() { return AceEngine::Get().GetContainer(Container::CurrentIdSafely()); } RefPtr Container::CurrentSafelyWithCheck() { int32_t currentId = CurrentId(); if (currentId >= 0) { auto container = GetContainer(currentId); if (container) { return container; } } currentId = SafelyId(); return GetContainer(currentId); } int32_t Container::CurrentIdSafelyWithCheck() { int32_t currentId = CurrentId(); if (currentId >= 0) { if (AceEngine::Get().HasContainer(currentId)) { return currentId; } } return SafelyId(); } RefPtr Container::GetContainer(int32_t containerId) { return AceEngine::Get().GetContainer(containerId); } RefPtr Container::GetActive() { RefPtr activeContainer; AceEngine::Get().NotifyContainers([&activeContainer](const RefPtr& container) { auto front = container->GetFrontend(); if (front && front->IsForeground()) { activeContainer = container; } }); return activeContainer; } RefPtr Container::GetDefault() { RefPtr defaultContainer; AceEngine::Get().NotifyContainers([&defaultContainer](const RefPtr& container) { auto front = container->GetFrontend(); if (front) { defaultContainer = container; } }); return defaultContainer; } RefPtr Container::GetFoucsed() { RefPtr foucsContainer; AceEngine::Get().NotifyContainers([&foucsContainer](const RefPtr& container) { auto pipeline = container->GetPipelineContext(); if (pipeline && pipeline->IsWindowFocused()) { foucsContainer = container; } }); return foucsContainer; } RefPtr Container::CurrentTaskExecutor() { auto curContainer = Current(); CHECK_NULL_RETURN(curContainer, nullptr); return curContainer->GetTaskExecutor(); } RefPtr Container::CurrentTaskExecutorSafely() { auto curContainer = CurrentSafely(); CHECK_NULL_RETURN(curContainer, nullptr); return curContainer->GetTaskExecutor(); } RefPtr Container::CurrentTaskExecutorSafelyWithCheck() { auto curContainer = CurrentSafelyWithCheck(); CHECK_NULL_RETURN(curContainer, nullptr); return curContainer->GetTaskExecutor(); } void Container::UpdateCurrent(int32_t id) { ContainerScope::UpdateCurrent(id); } bool Container::UpdateState(const Frontend::State& state) { std::lock_guard lock(stateMutex_); if (state_ == state) { return false; } state_ = state; return true; } bool Container::Dump(const std::vector& params, std::vector& info) { std::string tip("container not support, type:"); tip.append(AceType::TypeName(this)); info.emplace_back(tip); return true; } bool Container::IsIdAvailable(int32_t id) { return !AceEngine::Get().GetContainer(id); } void Container::SetFontScale(int32_t instanceId, float fontScale) { auto container = AceEngine::Get().GetContainer(instanceId); CHECK_NULL_VOID(container); ContainerScope scope(instanceId); auto pipelineContext = container->GetPipelineContext(); CHECK_NULL_VOID(pipelineContext); pipelineContext->SetFontScale(fontScale); } void Container::SetFontWeightScale(int32_t instanceId, float fontWeightScale) { auto container = AceEngine::Get().GetContainer(instanceId); CHECK_NULL_VOID(container); ContainerScope scope(instanceId); auto pipelineContext = container->GetPipelineContext(); CHECK_NULL_VOID(pipelineContext); pipelineContext->SetFontWeightScale(fontWeightScale); } RefPtr Container::GetDisplayInfo() { return displayManager_->GetDisplayInfo(); } void Container::InitIsFoldable() { displayManager_->InitIsFoldable(); } bool Container::IsFoldable() { return displayManager_->GetIsFoldable(); } FoldStatus Container::GetCurrentFoldStatus() { return displayManager_->GetCurrentFoldStatus(); } std::vector Container::GetCurrentFoldCreaseRegion() { return displayManager_->GetCurrentFoldCreaseRegion(); } template<> int32_t Container::GenerateId() { #ifdef PLUGIN_COMPONENT_SUPPORTED return PluginManager::GetInstance().GetPluginSubContainerId(); #else return INSTANCE_ID_UNDEFINED; #endif } } // namespace OHOS::Ace