123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#include "base/subwindow/subwindow_manager.h" 1723b3eb3cSopenharmony_ci 1823b3eb3cSopenharmony_ci#include "core/pipeline_ng/pipeline_context.h" 1923b3eb3cSopenharmony_ci 2023b3eb3cSopenharmony_cinamespace OHOS::Ace { 2123b3eb3cSopenharmony_ci 2223b3eb3cSopenharmony_cistd::mutex SubwindowManager::instanceMutex_; 2323b3eb3cSopenharmony_cistd::shared_ptr<SubwindowManager> SubwindowManager::instance_; 2423b3eb3cSopenharmony_cithread_local RefPtr<Subwindow> SubwindowManager::currentSubwindow_; 2523b3eb3cSopenharmony_ci 2623b3eb3cSopenharmony_cistd::shared_ptr<SubwindowManager> SubwindowManager::GetInstance() 2723b3eb3cSopenharmony_ci{ 2823b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(instanceMutex_); 2923b3eb3cSopenharmony_ci if (!instance_) { 3023b3eb3cSopenharmony_ci instance_ = std::make_shared<SubwindowManager>(); 3123b3eb3cSopenharmony_ci } 3223b3eb3cSopenharmony_ci return instance_; 3323b3eb3cSopenharmony_ci} 3423b3eb3cSopenharmony_ci 3523b3eb3cSopenharmony_civoid SubwindowManager::AddContainerId(uint32_t windowId, int32_t containerId) 3623b3eb3cSopenharmony_ci{ 3723b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 3823b3eb3cSopenharmony_ci auto result = containerMap_.try_emplace(windowId, containerId); 3923b3eb3cSopenharmony_ci if (!result.second) { 4023b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "Already have container of this windowId, windowId: %{public}u", windowId); 4123b3eb3cSopenharmony_ci } 4223b3eb3cSopenharmony_ci} 4323b3eb3cSopenharmony_ci 4423b3eb3cSopenharmony_civoid SubwindowManager::RemoveContainerId(uint32_t windowId) 4523b3eb3cSopenharmony_ci{ 4623b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 4723b3eb3cSopenharmony_ci containerMap_.erase(windowId); 4823b3eb3cSopenharmony_ci} 4923b3eb3cSopenharmony_ci 5023b3eb3cSopenharmony_ciint32_t SubwindowManager::GetContainerId(uint32_t windowId) 5123b3eb3cSopenharmony_ci{ 5223b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 5323b3eb3cSopenharmony_ci auto result = containerMap_.find(windowId); 5423b3eb3cSopenharmony_ci if (result != containerMap_.end()) { 5523b3eb3cSopenharmony_ci return result->second; 5623b3eb3cSopenharmony_ci } else { 5723b3eb3cSopenharmony_ci return -1; 5823b3eb3cSopenharmony_ci } 5923b3eb3cSopenharmony_ci} 6023b3eb3cSopenharmony_ci 6123b3eb3cSopenharmony_civoid SubwindowManager::AddParentContainerId(int32_t containerId, int32_t parentContainerId) 6223b3eb3cSopenharmony_ci{ 6323b3eb3cSopenharmony_ci TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "Container id is %{public}d, parent id is %{public}d.", containerId, 6423b3eb3cSopenharmony_ci parentContainerId); 6523b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(parentMutex_); 6623b3eb3cSopenharmony_ci parentContainerMap_.try_emplace(containerId, parentContainerId); 6723b3eb3cSopenharmony_ci} 6823b3eb3cSopenharmony_ci 6923b3eb3cSopenharmony_civoid SubwindowManager::RemoveParentContainerId(int32_t containerId) 7023b3eb3cSopenharmony_ci{ 7123b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(parentMutex_); 7223b3eb3cSopenharmony_ci parentContainerMap_.erase(containerId); 7323b3eb3cSopenharmony_ci} 7423b3eb3cSopenharmony_ci 7523b3eb3cSopenharmony_ciint32_t SubwindowManager::GetParentContainerId(int32_t containerId) 7623b3eb3cSopenharmony_ci{ 7723b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(parentMutex_); 7823b3eb3cSopenharmony_ci auto result = parentContainerMap_.find(containerId); 7923b3eb3cSopenharmony_ci if (result != parentContainerMap_.end()) { 8023b3eb3cSopenharmony_ci return result->second; 8123b3eb3cSopenharmony_ci } else { 8223b3eb3cSopenharmony_ci return -1; 8323b3eb3cSopenharmony_ci } 8423b3eb3cSopenharmony_ci} 8523b3eb3cSopenharmony_ci 8623b3eb3cSopenharmony_ciint32_t SubwindowManager::GetSubContainerId(int32_t parentContainerId) 8723b3eb3cSopenharmony_ci{ 8823b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(parentMutex_); 8923b3eb3cSopenharmony_ci for (auto it = parentContainerMap_.begin(); it != parentContainerMap_.end(); it++) { 9023b3eb3cSopenharmony_ci if (it->second == parentContainerId) { 9123b3eb3cSopenharmony_ci return it->first; 9223b3eb3cSopenharmony_ci } 9323b3eb3cSopenharmony_ci } 9423b3eb3cSopenharmony_ci return -1; 9523b3eb3cSopenharmony_ci} 9623b3eb3cSopenharmony_ci 9723b3eb3cSopenharmony_civoid SubwindowManager::AddSubwindow(int32_t instanceId, RefPtr<Subwindow> subwindow) 9823b3eb3cSopenharmony_ci{ 9923b3eb3cSopenharmony_ci if (!subwindow) { 10023b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "add subwindow failed."); 10123b3eb3cSopenharmony_ci return; 10223b3eb3cSopenharmony_ci } 10323b3eb3cSopenharmony_ci TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "Add subwindow into map, instanceId is %{public}d, subwindow id is %{public}d.", 10423b3eb3cSopenharmony_ci instanceId, subwindow->GetSubwindowId()); 10523b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(subwindowMutex_); 10623b3eb3cSopenharmony_ci auto result = subwindowMap_.try_emplace(instanceId, subwindow); 10723b3eb3cSopenharmony_ci if (!result.second) { 10823b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "Add failed of this instance %{public}d", instanceId); 10923b3eb3cSopenharmony_ci return; 11023b3eb3cSopenharmony_ci } 11123b3eb3cSopenharmony_ci} 11223b3eb3cSopenharmony_ci 11323b3eb3cSopenharmony_civoid SubwindowManager::AddToastSubwindow(int32_t instanceId, RefPtr<Subwindow> subwindow) 11423b3eb3cSopenharmony_ci{ 11523b3eb3cSopenharmony_ci if (!subwindow) { 11623b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "add toast subwindow failed."); 11723b3eb3cSopenharmony_ci return; 11823b3eb3cSopenharmony_ci } 11923b3eb3cSopenharmony_ci TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "Add toast into map, instanceId is %{public}d, subwindow id is %{public}d.", 12023b3eb3cSopenharmony_ci instanceId, subwindow->GetSubwindowId()); 12123b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(toastMutex_); 12223b3eb3cSopenharmony_ci auto result = toastWindowMap_.try_emplace(instanceId, subwindow); 12323b3eb3cSopenharmony_ci if (!result.second) { 12423b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "Add toast failed of this instance %{public}d", instanceId); 12523b3eb3cSopenharmony_ci return; 12623b3eb3cSopenharmony_ci } 12723b3eb3cSopenharmony_ci} 12823b3eb3cSopenharmony_ci 12923b3eb3cSopenharmony_civoid SubwindowManager::AddSystemToastWindow(int32_t instanceId, RefPtr<Subwindow> subwindow) 13023b3eb3cSopenharmony_ci{ 13123b3eb3cSopenharmony_ci if (!subwindow) { 13223b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "add system toast subwindow failed."); 13323b3eb3cSopenharmony_ci return; 13423b3eb3cSopenharmony_ci } 13523b3eb3cSopenharmony_ci TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, 13623b3eb3cSopenharmony_ci "Add system toast into map, instanceId is %{public}d, subwindow id is %{public}d.", 13723b3eb3cSopenharmony_ci instanceId, subwindow->GetSubwindowId()); 13823b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(systemToastMutex_); 13923b3eb3cSopenharmony_ci auto result = systemToastWindowMap_.try_emplace(instanceId, subwindow); 14023b3eb3cSopenharmony_ci if (!result.second) { 14123b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "Add system toast failed of this instance %{public}d", instanceId); 14223b3eb3cSopenharmony_ci return; 14323b3eb3cSopenharmony_ci } 14423b3eb3cSopenharmony_ci} 14523b3eb3cSopenharmony_ci 14623b3eb3cSopenharmony_civoid SubwindowManager::DeleteHotAreas(int32_t instanceId, int32_t nodeId) 14723b3eb3cSopenharmony_ci{ 14823b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow; 14923b3eb3cSopenharmony_ci if (instanceId != -1) { 15023b3eb3cSopenharmony_ci // get the subwindow which overlay node in, not current 15123b3eb3cSopenharmony_ci subwindow = GetSubwindow(instanceId >= MIN_SUBCONTAINER_ID ? GetParentContainerId(instanceId) : instanceId); 15223b3eb3cSopenharmony_ci } else { 15323b3eb3cSopenharmony_ci subwindow = GetCurrentWindow(); 15423b3eb3cSopenharmony_ci } 15523b3eb3cSopenharmony_ci if (subwindow) { 15623b3eb3cSopenharmony_ci subwindow->DeleteHotAreas(nodeId); 15723b3eb3cSopenharmony_ci } 15823b3eb3cSopenharmony_ci} 15923b3eb3cSopenharmony_civoid SubwindowManager::RemoveSubwindow(int32_t instanceId) 16023b3eb3cSopenharmony_ci{ 16123b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(subwindowMutex_); 16223b3eb3cSopenharmony_ci subwindowMap_.erase(instanceId); 16323b3eb3cSopenharmony_ci} 16423b3eb3cSopenharmony_ci 16523b3eb3cSopenharmony_ciconst RefPtr<Subwindow> SubwindowManager::GetSubwindow(int32_t instanceId) 16623b3eb3cSopenharmony_ci{ 16723b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(subwindowMutex_); 16823b3eb3cSopenharmony_ci auto result = subwindowMap_.find(instanceId); 16923b3eb3cSopenharmony_ci if (result != subwindowMap_.end()) { 17023b3eb3cSopenharmony_ci return result->second; 17123b3eb3cSopenharmony_ci } else { 17223b3eb3cSopenharmony_ci return nullptr; 17323b3eb3cSopenharmony_ci } 17423b3eb3cSopenharmony_ci} 17523b3eb3cSopenharmony_ci 17623b3eb3cSopenharmony_ciconst RefPtr<Subwindow> SubwindowManager::GetToastSubwindow(int32_t instanceId) 17723b3eb3cSopenharmony_ci{ 17823b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(toastMutex_); 17923b3eb3cSopenharmony_ci auto result = toastWindowMap_.find(instanceId); 18023b3eb3cSopenharmony_ci if (result != toastWindowMap_.end()) { 18123b3eb3cSopenharmony_ci return result->second; 18223b3eb3cSopenharmony_ci } 18323b3eb3cSopenharmony_ci return nullptr; 18423b3eb3cSopenharmony_ci} 18523b3eb3cSopenharmony_ci 18623b3eb3cSopenharmony_ciconst RefPtr<Subwindow> SubwindowManager::GetSystemToastWindow(int32_t instanceId) 18723b3eb3cSopenharmony_ci{ 18823b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(systemToastMutex_); 18923b3eb3cSopenharmony_ci auto result = systemToastWindowMap_.find(instanceId); 19023b3eb3cSopenharmony_ci if (result != systemToastWindowMap_.end()) { 19123b3eb3cSopenharmony_ci return result->second; 19223b3eb3cSopenharmony_ci } 19323b3eb3cSopenharmony_ci return nullptr; 19423b3eb3cSopenharmony_ci} 19523b3eb3cSopenharmony_ci 19623b3eb3cSopenharmony_ciconst RefPtr<Subwindow> SubwindowManager::GetOrCreateSubwindow(int32_t instanceId) 19723b3eb3cSopenharmony_ci{ 19823b3eb3cSopenharmony_ci auto subwindow = GetSubwindow(instanceId); 19923b3eb3cSopenharmony_ci if (subwindow) { 20023b3eb3cSopenharmony_ci return subwindow; 20123b3eb3cSopenharmony_ci } 20223b3eb3cSopenharmony_ci 20323b3eb3cSopenharmony_ci subwindow = Subwindow::CreateSubwindow(instanceId); 20423b3eb3cSopenharmony_ci if (!subwindow) { 20523b3eb3cSopenharmony_ci TAG_LOGE(AceLogTag::ACE_SUB_WINDOW, "create sub window failed"); 20623b3eb3cSopenharmony_ci return nullptr; 20723b3eb3cSopenharmony_ci } 20823b3eb3cSopenharmony_ci AddSubwindow(instanceId, subwindow); 20923b3eb3cSopenharmony_ci return subwindow; 21023b3eb3cSopenharmony_ci} 21123b3eb3cSopenharmony_ci 21223b3eb3cSopenharmony_ciint32_t SubwindowManager::GetDialogSubwindowInstanceId(int32_t SubwindowId) 21323b3eb3cSopenharmony_ci{ 21423b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "get dialog subwindow instanceid enter"); 21523b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(subwindowMutex_); 21623b3eb3cSopenharmony_ci for (auto it = subwindowMap_.begin(); it != subwindowMap_.end(); it++) { 21723b3eb3cSopenharmony_ci if (it->second->GetSubwindowId() == SubwindowId) { 21823b3eb3cSopenharmony_ci return it->first; 21923b3eb3cSopenharmony_ci } 22023b3eb3cSopenharmony_ci } 22123b3eb3cSopenharmony_ci return 0; 22223b3eb3cSopenharmony_ci} 22323b3eb3cSopenharmony_ci 22423b3eb3cSopenharmony_civoid SubwindowManager::SetCurrentSubwindow(const RefPtr<Subwindow>& subwindow) 22523b3eb3cSopenharmony_ci{ 22623b3eb3cSopenharmony_ci currentSubwindow_ = subwindow; 22723b3eb3cSopenharmony_ci} 22823b3eb3cSopenharmony_ci 22923b3eb3cSopenharmony_ciconst RefPtr<Subwindow>& SubwindowManager::GetCurrentWindow() 23023b3eb3cSopenharmony_ci{ 23123b3eb3cSopenharmony_ci return currentSubwindow_; 23223b3eb3cSopenharmony_ci} 23323b3eb3cSopenharmony_ci 23423b3eb3cSopenharmony_ciRect SubwindowManager::GetParentWindowRect() 23523b3eb3cSopenharmony_ci{ 23623b3eb3cSopenharmony_ci Rect rect; 23723b3eb3cSopenharmony_ci CHECK_NULL_RETURN(currentSubwindow_, rect); 23823b3eb3cSopenharmony_ci return currentSubwindow_->GetParentWindowRect(); 23923b3eb3cSopenharmony_ci} 24023b3eb3cSopenharmony_ci 24123b3eb3cSopenharmony_ciRefPtr<Subwindow> SubwindowManager::ShowPreviewNG(bool isStartDraggingFromSubWindow) 24223b3eb3cSopenharmony_ci{ 24323b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 24423b3eb3cSopenharmony_ci auto subwindow = 24523b3eb3cSopenharmony_ci GetOrCreateSubwindow(containerId >= MIN_SUBCONTAINER_ID ? GetParentContainerId(containerId) : containerId); 24623b3eb3cSopenharmony_ci if (!subwindow) { 24723b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 24823b3eb3cSopenharmony_ci return nullptr; 24923b3eb3cSopenharmony_ci } 25023b3eb3cSopenharmony_ci if (!subwindow->ShowPreviewNG(isStartDraggingFromSubWindow)) { 25123b3eb3cSopenharmony_ci return nullptr; 25223b3eb3cSopenharmony_ci } 25323b3eb3cSopenharmony_ci return subwindow; 25423b3eb3cSopenharmony_ci} 25523b3eb3cSopenharmony_ci 25623b3eb3cSopenharmony_civoid SubwindowManager::ShowMenuNG(const RefPtr<NG::FrameNode>& menuNode, const NG::MenuParam& menuParam, 25723b3eb3cSopenharmony_ci const RefPtr<NG::FrameNode>& targetNode, const NG::OffsetF& offset) 25823b3eb3cSopenharmony_ci{ 25923b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show menu ng enter"); 26023b3eb3cSopenharmony_ci CHECK_NULL_VOID(targetNode); 26123b3eb3cSopenharmony_ci auto pipelineContext = targetNode->GetContext(); 26223b3eb3cSopenharmony_ci CHECK_NULL_VOID(pipelineContext); 26323b3eb3cSopenharmony_ci auto containerId = pipelineContext->GetInstanceId(); 26423b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubwindow(containerId); 26523b3eb3cSopenharmony_ci if (!subwindow) { 26623b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 26723b3eb3cSopenharmony_ci return; 26823b3eb3cSopenharmony_ci } 26923b3eb3cSopenharmony_ci subwindow->ShowMenuNG(menuNode, menuParam, targetNode, offset); 27023b3eb3cSopenharmony_ci} 27123b3eb3cSopenharmony_ci 27223b3eb3cSopenharmony_civoid SubwindowManager::ShowMenuNG(std::function<void()>&& buildFunc, std::function<void()>&& previewBuildFunc, 27323b3eb3cSopenharmony_ci const NG::MenuParam& menuParam, const RefPtr<NG::FrameNode>& targetNode, const NG::OffsetF& offset) 27423b3eb3cSopenharmony_ci{ 27523b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show menu ng enter"); 27623b3eb3cSopenharmony_ci CHECK_NULL_VOID(targetNode); 27723b3eb3cSopenharmony_ci auto pipelineContext = targetNode->GetContext(); 27823b3eb3cSopenharmony_ci CHECK_NULL_VOID(pipelineContext); 27923b3eb3cSopenharmony_ci auto containerId = pipelineContext->GetInstanceId(); 28023b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubwindow(containerId); 28123b3eb3cSopenharmony_ci if (!subwindow) { 28223b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 28323b3eb3cSopenharmony_ci return; 28423b3eb3cSopenharmony_ci } 28523b3eb3cSopenharmony_ci subwindow->ShowMenuNG(std::move(buildFunc), std::move(previewBuildFunc), menuParam, targetNode, offset); 28623b3eb3cSopenharmony_ci} 28723b3eb3cSopenharmony_ci 28823b3eb3cSopenharmony_civoid SubwindowManager::HidePreviewNG() 28923b3eb3cSopenharmony_ci{ 29023b3eb3cSopenharmony_ci auto subwindow = GetCurrentWindow(); 29123b3eb3cSopenharmony_ci if (subwindow) { 29223b3eb3cSopenharmony_ci subwindow->HidePreviewNG(); 29323b3eb3cSopenharmony_ci } 29423b3eb3cSopenharmony_ci} 29523b3eb3cSopenharmony_ci 29623b3eb3cSopenharmony_civoid SubwindowManager::HideMenuNG(const RefPtr<NG::FrameNode>& menu, int32_t targetId) 29723b3eb3cSopenharmony_ci{ 29823b3eb3cSopenharmony_ci TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "hide menu ng enter"); 29923b3eb3cSopenharmony_ci auto subwindow = GetCurrentWindow(); 30023b3eb3cSopenharmony_ci if (subwindow) { 30123b3eb3cSopenharmony_ci subwindow->HideMenuNG(menu, targetId); 30223b3eb3cSopenharmony_ci } 30323b3eb3cSopenharmony_ci} 30423b3eb3cSopenharmony_ci 30523b3eb3cSopenharmony_civoid SubwindowManager::HideMenuNG(bool showPreviewAnimation, bool startDrag) 30623b3eb3cSopenharmony_ci{ 30723b3eb3cSopenharmony_ci TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "hide menu ng enter"); 30823b3eb3cSopenharmony_ci auto subwindow = GetCurrentWindow(); 30923b3eb3cSopenharmony_ci if (subwindow) { 31023b3eb3cSopenharmony_ci subwindow->HideMenuNG(showPreviewAnimation, startDrag); 31123b3eb3cSopenharmony_ci } 31223b3eb3cSopenharmony_ci} 31323b3eb3cSopenharmony_ci 31423b3eb3cSopenharmony_civoid SubwindowManager::UpdateHideMenuOffsetNG( 31523b3eb3cSopenharmony_ci const NG::OffsetF& offset, float menuScale, bool isRedragStart, int32_t menuWrapperId) 31623b3eb3cSopenharmony_ci{ 31723b3eb3cSopenharmony_ci auto subwindow = GetCurrentWindow(); 31823b3eb3cSopenharmony_ci if (subwindow) { 31923b3eb3cSopenharmony_ci subwindow->UpdateHideMenuOffsetNG(offset, menuScale, isRedragStart, menuWrapperId); 32023b3eb3cSopenharmony_ci } 32123b3eb3cSopenharmony_ci} 32223b3eb3cSopenharmony_ci 32323b3eb3cSopenharmony_civoid SubwindowManager::ContextMenuSwitchDragPreviewAnimation(const RefPtr<NG::FrameNode>& dragPreviewNode, 32423b3eb3cSopenharmony_ci const NG::OffsetF& offset) 32523b3eb3cSopenharmony_ci{ 32623b3eb3cSopenharmony_ci CHECK_NULL_VOID(dragPreviewNode); 32723b3eb3cSopenharmony_ci auto subwindow = GetCurrentWindow(); 32823b3eb3cSopenharmony_ci if (subwindow) { 32923b3eb3cSopenharmony_ci subwindow->ContextMenuSwitchDragPreviewAnimationtNG(dragPreviewNode, offset); 33023b3eb3cSopenharmony_ci } 33123b3eb3cSopenharmony_ci} 33223b3eb3cSopenharmony_ci 33323b3eb3cSopenharmony_civoid SubwindowManager::UpdatePreviewPosition() 33423b3eb3cSopenharmony_ci{ 33523b3eb3cSopenharmony_ci auto subwindow = GetCurrentWindow(); 33623b3eb3cSopenharmony_ci if (subwindow) { 33723b3eb3cSopenharmony_ci subwindow->UpdatePreviewPosition(); 33823b3eb3cSopenharmony_ci } 33923b3eb3cSopenharmony_ci} 34023b3eb3cSopenharmony_ci 34123b3eb3cSopenharmony_cibool SubwindowManager::GetMenuPreviewCenter(NG::OffsetF& offset) 34223b3eb3cSopenharmony_ci{ 34323b3eb3cSopenharmony_ci auto subwindow = GetCurrentWindow(); 34423b3eb3cSopenharmony_ci if (subwindow) { 34523b3eb3cSopenharmony_ci return subwindow->GetMenuPreviewCenter(offset); 34623b3eb3cSopenharmony_ci } 34723b3eb3cSopenharmony_ci return false; 34823b3eb3cSopenharmony_ci} 34923b3eb3cSopenharmony_ci 35023b3eb3cSopenharmony_civoid SubwindowManager::ClearMenuNG(int32_t instanceId, int32_t targetId, bool inWindow, bool showAnimation) 35123b3eb3cSopenharmony_ci{ 35223b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "clear menu ng enter"); 35323b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow; 35423b3eb3cSopenharmony_ci if (instanceId != -1) { 35523b3eb3cSopenharmony_ci#ifdef OHOS_STANDARD_SYSTEM 35623b3eb3cSopenharmony_ci // get the subwindow which overlay node in, not current 35723b3eb3cSopenharmony_ci subwindow = GetSubwindow(instanceId >= MIN_SUBCONTAINER_ID ? GetParentContainerId(instanceId) : instanceId); 35823b3eb3cSopenharmony_ci#else 35923b3eb3cSopenharmony_ci subwindow = 36023b3eb3cSopenharmony_ci GetSubwindow(GetParentContainerId(instanceId) != -1 ? GetParentContainerId(instanceId) : instanceId); 36123b3eb3cSopenharmony_ci#endif 36223b3eb3cSopenharmony_ci } else { 36323b3eb3cSopenharmony_ci subwindow = GetCurrentWindow(); 36423b3eb3cSopenharmony_ci } 36523b3eb3cSopenharmony_ci if (subwindow) { 36623b3eb3cSopenharmony_ci subwindow->ClearMenuNG(targetId, inWindow, showAnimation); 36723b3eb3cSopenharmony_ci } 36823b3eb3cSopenharmony_ci} 36923b3eb3cSopenharmony_ci 37023b3eb3cSopenharmony_civoid SubwindowManager::ClearPopupInSubwindow(int32_t instanceId) 37123b3eb3cSopenharmony_ci{ 37223b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "clear popup in subwindow enter"); 37323b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow; 37423b3eb3cSopenharmony_ci if (instanceId != -1) { 37523b3eb3cSopenharmony_ci // get the subwindow which overlay node in, not current 37623b3eb3cSopenharmony_ci subwindow = GetSubwindow(instanceId >= MIN_SUBCONTAINER_ID ? GetParentContainerId(instanceId) : instanceId); 37723b3eb3cSopenharmony_ci } else { 37823b3eb3cSopenharmony_ci subwindow = GetCurrentWindow(); 37923b3eb3cSopenharmony_ci } 38023b3eb3cSopenharmony_ci if (subwindow) { 38123b3eb3cSopenharmony_ci subwindow->ClearPopupNG(); 38223b3eb3cSopenharmony_ci } 38323b3eb3cSopenharmony_ci} 38423b3eb3cSopenharmony_ci 38523b3eb3cSopenharmony_civoid SubwindowManager::ShowPopupNG(const RefPtr<NG::FrameNode>& targetNode, const NG::PopupInfo& popupInfo, 38623b3eb3cSopenharmony_ci const std::function<void(int32_t)>&& onWillDismiss, bool interactiveDismiss) 38723b3eb3cSopenharmony_ci{ 38823b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show popup ng enter"); 38923b3eb3cSopenharmony_ci CHECK_NULL_VOID(targetNode); 39023b3eb3cSopenharmony_ci auto pipelineContext = targetNode->GetContext(); 39123b3eb3cSopenharmony_ci CHECK_NULL_VOID(pipelineContext); 39223b3eb3cSopenharmony_ci auto containerId = pipelineContext->GetInstanceId(); 39323b3eb3cSopenharmony_ci 39423b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubwindow(containerId); 39523b3eb3cSopenharmony_ci if (!subwindow) { 39623b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 39723b3eb3cSopenharmony_ci return; 39823b3eb3cSopenharmony_ci } 39923b3eb3cSopenharmony_ci subwindow->ShowPopupNG(targetNode->GetId(), popupInfo, std::move(onWillDismiss), interactiveDismiss); 40023b3eb3cSopenharmony_ci} 40123b3eb3cSopenharmony_ci 40223b3eb3cSopenharmony_civoid SubwindowManager::HidePopupNG(int32_t targetId, int32_t instanceId) 40323b3eb3cSopenharmony_ci{ 40423b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "hide popup ng enter"); 40523b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow; 40623b3eb3cSopenharmony_ci if (instanceId != -1) { 40723b3eb3cSopenharmony_ci // get the subwindow which overlay node in, not current 40823b3eb3cSopenharmony_ci subwindow = GetSubwindow(instanceId >= MIN_SUBCONTAINER_ID ? GetParentContainerId(instanceId) : instanceId); 40923b3eb3cSopenharmony_ci } else { 41023b3eb3cSopenharmony_ci subwindow = GetCurrentWindow(); 41123b3eb3cSopenharmony_ci } 41223b3eb3cSopenharmony_ci 41323b3eb3cSopenharmony_ci if (subwindow) { 41423b3eb3cSopenharmony_ci subwindow->HidePopupNG(targetId); 41523b3eb3cSopenharmony_ci } 41623b3eb3cSopenharmony_ci} 41723b3eb3cSopenharmony_ci 41823b3eb3cSopenharmony_civoid SubwindowManager::ShowPopup(const RefPtr<Component>& newComponent, bool disableTouchEvent) 41923b3eb3cSopenharmony_ci{ 42023b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show popup enter"); 42123b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 42223b3eb3cSopenharmony_ci auto taskExecutor = Container::CurrentTaskExecutor(); 42323b3eb3cSopenharmony_ci CHECK_NULL_VOID(taskExecutor); 42423b3eb3cSopenharmony_ci taskExecutor->PostTask( 42523b3eb3cSopenharmony_ci [containerId, newComponentWeak = WeakPtr<Component>(newComponent), disableTouchEvent] { 42623b3eb3cSopenharmony_ci auto subwindow = SubwindowManager::GetInstance()->GetOrCreateSubwindow(containerId); 42723b3eb3cSopenharmony_ci if (!subwindow) { 42823b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 42923b3eb3cSopenharmony_ci return; 43023b3eb3cSopenharmony_ci } 43123b3eb3cSopenharmony_ci auto newComponent = newComponentWeak.Upgrade(); 43223b3eb3cSopenharmony_ci CHECK_NULL_VOID(newComponent); 43323b3eb3cSopenharmony_ci subwindow->ShowPopup(newComponent, disableTouchEvent); 43423b3eb3cSopenharmony_ci }, 43523b3eb3cSopenharmony_ci TaskExecutor::TaskType::PLATFORM, "ArkUISubwindowShowPopup"); 43623b3eb3cSopenharmony_ci} 43723b3eb3cSopenharmony_ci 43823b3eb3cSopenharmony_cibool SubwindowManager::CancelPopup(const std::string& id) 43923b3eb3cSopenharmony_ci{ 44023b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "cancel popup enter"); 44123b3eb3cSopenharmony_ci auto subwindow = GetCurrentWindow(); 44223b3eb3cSopenharmony_ci if (subwindow) { 44323b3eb3cSopenharmony_ci return subwindow->CancelPopup(id); 44423b3eb3cSopenharmony_ci } 44523b3eb3cSopenharmony_ci return false; 44623b3eb3cSopenharmony_ci} 44723b3eb3cSopenharmony_ci 44823b3eb3cSopenharmony_civoid SubwindowManager::ShowMenu(const RefPtr<Component>& newComponent) 44923b3eb3cSopenharmony_ci{ 45023b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show menu enter"); 45123b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 45223b3eb3cSopenharmony_ci auto taskExecutor = Container::CurrentTaskExecutor(); 45323b3eb3cSopenharmony_ci CHECK_NULL_VOID(taskExecutor); 45423b3eb3cSopenharmony_ci taskExecutor->PostTask( 45523b3eb3cSopenharmony_ci [containerId, weakMenu = AceType::WeakClaim(AceType::RawPtr(newComponent))] { 45623b3eb3cSopenharmony_ci auto menu = weakMenu.Upgrade(); 45723b3eb3cSopenharmony_ci CHECK_NULL_VOID(menu); 45823b3eb3cSopenharmony_ci auto subwindow = SubwindowManager::GetInstance()->GetOrCreateSubwindow(containerId); 45923b3eb3cSopenharmony_ci if (!subwindow) { 46023b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 46123b3eb3cSopenharmony_ci return; 46223b3eb3cSopenharmony_ci } 46323b3eb3cSopenharmony_ci subwindow->ShowMenu(menu); 46423b3eb3cSopenharmony_ci }, 46523b3eb3cSopenharmony_ci TaskExecutor::TaskType::PLATFORM, "ArkUISubwindowShowMenu"); 46623b3eb3cSopenharmony_ci} 46723b3eb3cSopenharmony_ci 46823b3eb3cSopenharmony_civoid SubwindowManager::CloseMenu() 46923b3eb3cSopenharmony_ci{ 47023b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "close menu enter"); 47123b3eb3cSopenharmony_ci auto subwindow = GetCurrentWindow(); 47223b3eb3cSopenharmony_ci if (subwindow) { 47323b3eb3cSopenharmony_ci subwindow->CloseMenu(); 47423b3eb3cSopenharmony_ci } 47523b3eb3cSopenharmony_ci} 47623b3eb3cSopenharmony_ci 47723b3eb3cSopenharmony_civoid SubwindowManager::ClearMenu() 47823b3eb3cSopenharmony_ci{ 47923b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "clear menu enter"); 48023b3eb3cSopenharmony_ci auto subwindow = GetCurrentWindow(); 48123b3eb3cSopenharmony_ci if (subwindow) { 48223b3eb3cSopenharmony_ci subwindow->ClearMenu(); 48323b3eb3cSopenharmony_ci } 48423b3eb3cSopenharmony_ci} 48523b3eb3cSopenharmony_ci 48623b3eb3cSopenharmony_civoid SubwindowManager::SetHotAreas(const std::vector<Rect>& rects, int32_t nodeId, int32_t instanceId) 48723b3eb3cSopenharmony_ci{ 48823b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "set hot areas enter"); 48923b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow; 49023b3eb3cSopenharmony_ci if (instanceId != -1) { 49123b3eb3cSopenharmony_ci // get the subwindow which overlay node in, not current 49223b3eb3cSopenharmony_ci subwindow = GetSubwindow(instanceId >= MIN_SUBCONTAINER_ID ? GetParentContainerId(instanceId) : instanceId); 49323b3eb3cSopenharmony_ci } else { 49423b3eb3cSopenharmony_ci subwindow = GetCurrentWindow(); 49523b3eb3cSopenharmony_ci } 49623b3eb3cSopenharmony_ci 49723b3eb3cSopenharmony_ci if (subwindow) { 49823b3eb3cSopenharmony_ci subwindow->SetHotAreas(rects, nodeId); 49923b3eb3cSopenharmony_ci } 50023b3eb3cSopenharmony_ci} 50123b3eb3cSopenharmony_ci 50223b3eb3cSopenharmony_ciRefPtr<NG::FrameNode> SubwindowManager::ShowDialogNG( 50323b3eb3cSopenharmony_ci const DialogProperties& dialogProps, std::function<void()>&& buildFunc) 50423b3eb3cSopenharmony_ci{ 50523b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show dialog ng enter"); 50623b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 50723b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubwindow(containerId); 50823b3eb3cSopenharmony_ci if (!subwindow) { 50923b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 51023b3eb3cSopenharmony_ci return nullptr; 51123b3eb3cSopenharmony_ci } 51223b3eb3cSopenharmony_ci return subwindow->ShowDialogNG(dialogProps, std::move(buildFunc)); 51323b3eb3cSopenharmony_ci} 51423b3eb3cSopenharmony_ciRefPtr<NG::FrameNode> SubwindowManager::ShowDialogNGWithNode(const DialogProperties& dialogProps, 51523b3eb3cSopenharmony_ci const RefPtr<NG::UINode>& customNode) 51623b3eb3cSopenharmony_ci{ 51723b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show dialog ng enter"); 51823b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 51923b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubwindow(containerId); 52023b3eb3cSopenharmony_ci if (!subwindow) { 52123b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 52223b3eb3cSopenharmony_ci return nullptr; 52323b3eb3cSopenharmony_ci } 52423b3eb3cSopenharmony_ci return subwindow->ShowDialogNGWithNode(dialogProps, customNode); 52523b3eb3cSopenharmony_ci} 52623b3eb3cSopenharmony_civoid SubwindowManager::CloseDialogNG(const RefPtr<NG::FrameNode>& dialogNode) 52723b3eb3cSopenharmony_ci{ 52823b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "close dialog ng enter"); 52923b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 53023b3eb3cSopenharmony_ci auto subwindow = GetSubwindow(containerId); 53123b3eb3cSopenharmony_ci if (!subwindow) { 53223b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get subwindow failed."); 53323b3eb3cSopenharmony_ci return; 53423b3eb3cSopenharmony_ci } 53523b3eb3cSopenharmony_ci return subwindow->CloseDialogNG(dialogNode); 53623b3eb3cSopenharmony_ci} 53723b3eb3cSopenharmony_ci 53823b3eb3cSopenharmony_civoid SubwindowManager::OpenCustomDialogNG(const DialogProperties& dialogProps, std::function<void(int32_t)>&& callback) 53923b3eb3cSopenharmony_ci{ 54023b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show customDialog ng enter"); 54123b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 54223b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubwindow(containerId); 54323b3eb3cSopenharmony_ci if (!subwindow) { 54423b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 54523b3eb3cSopenharmony_ci return; 54623b3eb3cSopenharmony_ci } 54723b3eb3cSopenharmony_ci return subwindow->OpenCustomDialogNG(dialogProps, std::move(callback)); 54823b3eb3cSopenharmony_ci} 54923b3eb3cSopenharmony_ci 55023b3eb3cSopenharmony_civoid SubwindowManager::CloseCustomDialogNG(int32_t dialogId) 55123b3eb3cSopenharmony_ci{ 55223b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "close customDialog ng enter"); 55323b3eb3cSopenharmony_ci for (auto &overlay : GetAllSubOverlayManager()) { 55423b3eb3cSopenharmony_ci CHECK_NULL_VOID(overlay); 55523b3eb3cSopenharmony_ci if (overlay->GetDialogMap().find(dialogId) != overlay->GetDialogMap().end()) { 55623b3eb3cSopenharmony_ci return overlay->CloseCustomDialog(dialogId); 55723b3eb3cSopenharmony_ci } 55823b3eb3cSopenharmony_ci } 55923b3eb3cSopenharmony_ci} 56023b3eb3cSopenharmony_ci 56123b3eb3cSopenharmony_civoid SubwindowManager::CloseCustomDialogNG(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)>&& callback) 56223b3eb3cSopenharmony_ci{ 56323b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "close customDialog ng enter"); 56423b3eb3cSopenharmony_ci for (auto &overlay : GetAllSubOverlayManager()) { 56523b3eb3cSopenharmony_ci CHECK_NULL_VOID(overlay); 56623b3eb3cSopenharmony_ci overlay->CloseCustomDialog(node, std::move(callback)); 56723b3eb3cSopenharmony_ci } 56823b3eb3cSopenharmony_ci} 56923b3eb3cSopenharmony_ci 57023b3eb3cSopenharmony_civoid SubwindowManager::UpdateCustomDialogNG( 57123b3eb3cSopenharmony_ci const WeakPtr<NG::UINode>& node, const PromptDialogAttr &dialogAttr, std::function<void(int32_t)>&& callback) 57223b3eb3cSopenharmony_ci{ 57323b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "update customDialog ng enter"); 57423b3eb3cSopenharmony_ci DialogProperties dialogProperties = { 57523b3eb3cSopenharmony_ci .autoCancel = dialogAttr.autoCancel, 57623b3eb3cSopenharmony_ci .maskColor = dialogAttr.maskColor, 57723b3eb3cSopenharmony_ci .isSysBlurStyle = false 57823b3eb3cSopenharmony_ci }; 57923b3eb3cSopenharmony_ci if (dialogAttr.alignment.has_value()) { 58023b3eb3cSopenharmony_ci dialogProperties.alignment = dialogAttr.alignment.value(); 58123b3eb3cSopenharmony_ci } 58223b3eb3cSopenharmony_ci if (dialogAttr.offset.has_value()) { 58323b3eb3cSopenharmony_ci dialogProperties.offset = dialogAttr.offset.value(); 58423b3eb3cSopenharmony_ci } 58523b3eb3cSopenharmony_ci for (auto &overlay : GetAllSubOverlayManager()) { 58623b3eb3cSopenharmony_ci if (overlay) { 58723b3eb3cSopenharmony_ci overlay->UpdateCustomDialog(node, dialogProperties, std::move(callback)); 58823b3eb3cSopenharmony_ci } 58923b3eb3cSopenharmony_ci } 59023b3eb3cSopenharmony_ci} 59123b3eb3cSopenharmony_ci 59223b3eb3cSopenharmony_civoid SubwindowManager::HideDialogSubWindow(int32_t instanceId) 59323b3eb3cSopenharmony_ci{ 59423b3eb3cSopenharmony_ci TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "hide dialog subwindow enter"); 59523b3eb3cSopenharmony_ci auto subwindow = GetSubwindow(instanceId >= MIN_SUBCONTAINER_ID ? GetParentContainerId(instanceId) : instanceId); 59623b3eb3cSopenharmony_ci CHECK_NULL_VOID(subwindow); 59723b3eb3cSopenharmony_ci auto overlay = subwindow->GetOverlayManager(); 59823b3eb3cSopenharmony_ci CHECK_NULL_VOID(overlay); 59923b3eb3cSopenharmony_ci if (overlay->GetDialogMap().size() == 0) { 60023b3eb3cSopenharmony_ci subwindow->HideSubWindowNG(); 60123b3eb3cSopenharmony_ci } else { 60223b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "fail to hide dialog subwindow, instanceId is %{public}d.", instanceId); 60323b3eb3cSopenharmony_ci } 60423b3eb3cSopenharmony_ci} 60523b3eb3cSopenharmony_ci 60623b3eb3cSopenharmony_civoid SubwindowManager::AddDialogSubwindow(int32_t instanceId, const RefPtr<Subwindow>& subwindow) 60723b3eb3cSopenharmony_ci{ 60823b3eb3cSopenharmony_ci if (!subwindow) { 60923b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "Add dialog subwindow failed, the subwindow is null."); 61023b3eb3cSopenharmony_ci return; 61123b3eb3cSopenharmony_ci } 61223b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(dialogSubwindowMutex_); 61323b3eb3cSopenharmony_ci auto result = dialogSubwindowMap_.try_emplace(instanceId, subwindow); 61423b3eb3cSopenharmony_ci if (!result.second) { 61523b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "Add dialog failed of this instance %{public}d", instanceId); 61623b3eb3cSopenharmony_ci return; 61723b3eb3cSopenharmony_ci } 61823b3eb3cSopenharmony_ci} 61923b3eb3cSopenharmony_ci 62023b3eb3cSopenharmony_ciconst RefPtr<Subwindow> SubwindowManager::GetDialogSubwindow(int32_t instanceId) 62123b3eb3cSopenharmony_ci{ 62223b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(dialogSubwindowMutex_); 62323b3eb3cSopenharmony_ci auto result = dialogSubwindowMap_.find(instanceId); 62423b3eb3cSopenharmony_ci if (result != dialogSubwindowMap_.end()) { 62523b3eb3cSopenharmony_ci return result->second; 62623b3eb3cSopenharmony_ci } else { 62723b3eb3cSopenharmony_ci return nullptr; 62823b3eb3cSopenharmony_ci } 62923b3eb3cSopenharmony_ci} 63023b3eb3cSopenharmony_ci 63123b3eb3cSopenharmony_civoid SubwindowManager::SetCurrentDialogSubwindow(const RefPtr<Subwindow>& subwindow) 63223b3eb3cSopenharmony_ci{ 63323b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "set current dialog subwindow enter"); 63423b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(currentDialogSubwindowMutex_); 63523b3eb3cSopenharmony_ci currentDialogSubwindow_ = subwindow; 63623b3eb3cSopenharmony_ci} 63723b3eb3cSopenharmony_ci 63823b3eb3cSopenharmony_ciconst RefPtr<Subwindow>& SubwindowManager::GetCurrentDialogWindow() 63923b3eb3cSopenharmony_ci{ 64023b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "get current dialog window enter"); 64123b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(currentDialogSubwindowMutex_); 64223b3eb3cSopenharmony_ci return currentDialogSubwindow_; 64323b3eb3cSopenharmony_ci} 64423b3eb3cSopenharmony_ci 64523b3eb3cSopenharmony_ciRefPtr<Subwindow> SubwindowManager::GetOrCreateSubWindow() 64623b3eb3cSopenharmony_ci{ 64723b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 64823b3eb3cSopenharmony_ci auto subwindow = GetDialogSubwindow(containerId); 64923b3eb3cSopenharmony_ci if (!subwindow) { 65023b3eb3cSopenharmony_ci subwindow = Subwindow::CreateSubwindow(containerId); 65123b3eb3cSopenharmony_ci CHECK_NULL_RETURN(subwindow, nullptr); 65223b3eb3cSopenharmony_ci AddDialogSubwindow(containerId, subwindow); 65323b3eb3cSopenharmony_ci } 65423b3eb3cSopenharmony_ci return subwindow; 65523b3eb3cSopenharmony_ci} 65623b3eb3cSopenharmony_ci 65723b3eb3cSopenharmony_ciRefPtr<Subwindow> SubwindowManager::GetOrCreateSystemSubWindow(int32_t containerId) 65823b3eb3cSopenharmony_ci{ 65923b3eb3cSopenharmony_ci auto subwindow = GetSystemToastWindow(containerId); 66023b3eb3cSopenharmony_ci if (!subwindow) { 66123b3eb3cSopenharmony_ci subwindow = Subwindow::CreateSubwindow(containerId); 66223b3eb3cSopenharmony_ci CHECK_NULL_RETURN(subwindow, nullptr); 66323b3eb3cSopenharmony_ci AddSystemToastWindow(containerId, subwindow); 66423b3eb3cSopenharmony_ci } 66523b3eb3cSopenharmony_ci return subwindow; 66623b3eb3cSopenharmony_ci} 66723b3eb3cSopenharmony_ci 66823b3eb3cSopenharmony_civoid SubwindowManager::ShowToastNG(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback) 66923b3eb3cSopenharmony_ci{ 67023b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show toast ng enter"); 67123b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 67223b3eb3cSopenharmony_ci auto windowType = GetToastWindowType(containerId); 67323b3eb3cSopenharmony_ci auto container = Container::GetContainer(containerId); 67423b3eb3cSopenharmony_ci CHECK_NULL_VOID(container); 67523b3eb3cSopenharmony_ci auto windowId = container->GetWindowId(); 67623b3eb3cSopenharmony_ci // Get the parent window ID before the asynchronous operation 67723b3eb3cSopenharmony_ci auto mainWindowId = container->GetParentMainWindowId(windowId); 67823b3eb3cSopenharmony_ci // for ability 67923b3eb3cSopenharmony_ci auto taskExecutor = Container::CurrentTaskExecutor(); 68023b3eb3cSopenharmony_ci CHECK_NULL_VOID(taskExecutor); 68123b3eb3cSopenharmony_ci taskExecutor->PostTask( 68223b3eb3cSopenharmony_ci [containerId, toastInfo, callbackParam = std::move(callback), windowType, mainWindowId] { 68323b3eb3cSopenharmony_ci auto subwindow = SubwindowManager::GetInstance()->GetOrCreateToastWindowNG( 68423b3eb3cSopenharmony_ci containerId, windowType, mainWindowId); 68523b3eb3cSopenharmony_ci CHECK_NULL_VOID(subwindow); 68623b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "before show toast : %{public}d", containerId); 68723b3eb3cSopenharmony_ci subwindow->ShowToast(toastInfo, std::move(const_cast<std::function<void(int32_t)>&&>(callbackParam))); 68823b3eb3cSopenharmony_ci }, 68923b3eb3cSopenharmony_ci TaskExecutor::TaskType::PLATFORM, "ArkUISubwindowShowToastNG"); 69023b3eb3cSopenharmony_ci} 69123b3eb3cSopenharmony_ci 69223b3eb3cSopenharmony_ciToastWindowType SubwindowManager::GetToastWindowType(int32_t instanceId) 69323b3eb3cSopenharmony_ci{ 69423b3eb3cSopenharmony_ci auto parentContainer = Container::GetContainer(instanceId); 69523b3eb3cSopenharmony_ci CHECK_NULL_RETURN(parentContainer, ToastWindowType::TOAST_IN_TYPE_TOAST); 69623b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "GetToastWindowType instanceId: %{public}d", instanceId); 69723b3eb3cSopenharmony_ci // toast window should be TOAST_IN_TYPE_APP_SUB_WINDOW when parent window is dialog window. 69823b3eb3cSopenharmony_ci if (parentContainer->IsMainWindow() || parentContainer->IsSubWindow() || 69923b3eb3cSopenharmony_ci parentContainer->IsDialogWindow()) { 70023b3eb3cSopenharmony_ci return ToastWindowType::TOAST_IN_TYPE_APP_SUB_WINDOW; 70123b3eb3cSopenharmony_ci } else if (parentContainer->IsScenceBoardWindow()) { 70223b3eb3cSopenharmony_ci return ToastWindowType::TOAST_IN_TYPE_SYSTEM_FLOAT; 70323b3eb3cSopenharmony_ci } else if (parentContainer->IsSystemWindow()) { 70423b3eb3cSopenharmony_ci return ToastWindowType::TOAST_IN_TYPE_SYSTEM_SUB_WINDOW; 70523b3eb3cSopenharmony_ci } else if (parentContainer->IsUIExtensionWindow()) { 70623b3eb3cSopenharmony_ci if (parentContainer->IsHostMainWindow() || parentContainer->IsHostSubWindow() || 70723b3eb3cSopenharmony_ci parentContainer->IsHostDialogWindow()) { 70823b3eb3cSopenharmony_ci return ToastWindowType::TOAST_IN_TYPE_APP_SUB_WINDOW; 70923b3eb3cSopenharmony_ci } else if (parentContainer->IsHostSceneBoardWindow()) { 71023b3eb3cSopenharmony_ci return ToastWindowType::TOAST_IN_TYPE_SYSTEM_FLOAT; 71123b3eb3cSopenharmony_ci } else if (parentContainer->IsHostSystemWindow()) { 71223b3eb3cSopenharmony_ci return ToastWindowType::TOAST_IN_TYPE_SYSTEM_SUB_WINDOW; 71323b3eb3cSopenharmony_ci } 71423b3eb3cSopenharmony_ci } 71523b3eb3cSopenharmony_ci return ToastWindowType::TOAST_IN_TYPE_TOAST; 71623b3eb3cSopenharmony_ci} 71723b3eb3cSopenharmony_ci 71823b3eb3cSopenharmony_civoid SubwindowManager::ShowToast(const NG::ToastInfo& toastInfo, std::function<void(int32_t)>&& callback) 71923b3eb3cSopenharmony_ci{ 72023b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show toast enter"); 72123b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 72223b3eb3cSopenharmony_ci auto isTopMost = toastInfo.showMode == NG::ToastShowMode::TOP_MOST; 72323b3eb3cSopenharmony_ci // for pa service 72423b3eb3cSopenharmony_ci if ((isTopMost && containerId >= MIN_PA_SERVICE_ID && containerId < MIN_SUBCONTAINER_ID) || 72523b3eb3cSopenharmony_ci (!isTopMost && containerId >= MIN_PA_SERVICE_ID) || containerId < 0) { 72623b3eb3cSopenharmony_ci auto subwindow = toastInfo.showMode == NG::ToastShowMode::SYSTEM_TOP_MOST ? 72723b3eb3cSopenharmony_ci GetOrCreateSystemSubWindow(containerId) : GetOrCreateSubWindow(); 72823b3eb3cSopenharmony_ci CHECK_NULL_VOID(subwindow); 72923b3eb3cSopenharmony_ci subwindow->SetIsSystemTopMost(toastInfo.showMode == NG::ToastShowMode::SYSTEM_TOP_MOST); 73023b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "before show toast"); 73123b3eb3cSopenharmony_ci subwindow->ShowToast(toastInfo, std::move(callback)); 73223b3eb3cSopenharmony_ci } else { 73323b3eb3cSopenharmony_ci // for ability 73423b3eb3cSopenharmony_ci auto parentContainer = Container::GetContainer(containerId); 73523b3eb3cSopenharmony_ci // in scenceboard, system_top_most needs to go the old way, 73623b3eb3cSopenharmony_ci // default and top_most need to go showToastNG 73723b3eb3cSopenharmony_ci if (toastInfo.showMode == NG::ToastShowMode::TOP_MOST || 73823b3eb3cSopenharmony_ci (parentContainer && parentContainer->IsScenceBoardWindow() && 73923b3eb3cSopenharmony_ci toastInfo.showMode != NG::ToastShowMode::SYSTEM_TOP_MOST)) { 74023b3eb3cSopenharmony_ci ShowToastNG(toastInfo, std::move(callback)); 74123b3eb3cSopenharmony_ci return; 74223b3eb3cSopenharmony_ci } 74323b3eb3cSopenharmony_ci auto taskExecutor = Container::CurrentTaskExecutor(); 74423b3eb3cSopenharmony_ci CHECK_NULL_VOID(taskExecutor); 74523b3eb3cSopenharmony_ci taskExecutor->PostTask( 74623b3eb3cSopenharmony_ci [containerId, toastInfo, callbackParam = std::move(callback)] { 74723b3eb3cSopenharmony_ci auto subwindow = SubwindowManager::GetInstance()->GetOrCreateToastWindow(containerId, toastInfo.showMode); 74823b3eb3cSopenharmony_ci CHECK_NULL_VOID(subwindow); 74923b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "before show toast : %{public}d", containerId); 75023b3eb3cSopenharmony_ci subwindow->ShowToast(toastInfo, std::move(const_cast<std::function<void(int32_t)>&&>(callbackParam))); 75123b3eb3cSopenharmony_ci }, 75223b3eb3cSopenharmony_ci TaskExecutor::TaskType::PLATFORM, "ArkUISubwindowShowToast"); 75323b3eb3cSopenharmony_ci } 75423b3eb3cSopenharmony_ci} 75523b3eb3cSopenharmony_ci 75623b3eb3cSopenharmony_civoid SubwindowManager::CloseToast( 75723b3eb3cSopenharmony_ci const int32_t toastId, const NG::ToastShowMode& showMode, std::function<void(int32_t)>&& callback) 75823b3eb3cSopenharmony_ci{ 75923b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "close toast enter"); 76023b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 76123b3eb3cSopenharmony_ci 76223b3eb3cSopenharmony_ci if (containerId >= MIN_PA_SERVICE_ID || containerId < 0) { 76323b3eb3cSopenharmony_ci // for pa service 76423b3eb3cSopenharmony_ci auto subwindow = showMode == NG::ToastShowMode::SYSTEM_TOP_MOST ? 76523b3eb3cSopenharmony_ci GetSystemToastWindow(containerId) : GetDialogSubwindow(containerId); 76623b3eb3cSopenharmony_ci CHECK_NULL_VOID(subwindow); 76723b3eb3cSopenharmony_ci subwindow->SetIsSystemTopMost(showMode == NG::ToastShowMode::SYSTEM_TOP_MOST); 76823b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "before close toast"); 76923b3eb3cSopenharmony_ci subwindow->CloseToast(toastId, std::move(callback)); 77023b3eb3cSopenharmony_ci } else { 77123b3eb3cSopenharmony_ci // for ability 77223b3eb3cSopenharmony_ci if (showMode == NG::ToastShowMode::TOP_MOST) { 77323b3eb3cSopenharmony_ci auto subwindow = GetToastSubwindow(containerId); 77423b3eb3cSopenharmony_ci subwindow->CloseToast(toastId, std::move(callback)); 77523b3eb3cSopenharmony_ci return; 77623b3eb3cSopenharmony_ci } 77723b3eb3cSopenharmony_ci auto manager = SubwindowManager::GetInstance(); 77823b3eb3cSopenharmony_ci CHECK_NULL_VOID(manager); 77923b3eb3cSopenharmony_ci auto subwindow = showMode == NG::ToastShowMode::SYSTEM_TOP_MOST ? 78023b3eb3cSopenharmony_ci GetSystemToastWindow(containerId) : GetSubwindow(containerId); 78123b3eb3cSopenharmony_ci CHECK_NULL_VOID(subwindow); 78223b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "before close toast : %{public}d", containerId); 78323b3eb3cSopenharmony_ci subwindow->CloseToast(toastId, std::move(callback)); 78423b3eb3cSopenharmony_ci } 78523b3eb3cSopenharmony_ci} 78623b3eb3cSopenharmony_ci 78723b3eb3cSopenharmony_ciRefPtr<Subwindow> SubwindowManager::GetOrCreateToastWindow(int32_t containerId, const NG::ToastShowMode& showMode) 78823b3eb3cSopenharmony_ci{ 78923b3eb3cSopenharmony_ci auto isSystemTopMost = (showMode == NG::ToastShowMode::SYSTEM_TOP_MOST); 79023b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow = nullptr; 79123b3eb3cSopenharmony_ci if (isSystemTopMost) { 79223b3eb3cSopenharmony_ci subwindow = GetSystemToastWindow(containerId); 79323b3eb3cSopenharmony_ci } else { 79423b3eb3cSopenharmony_ci subwindow = GetSubwindow(containerId); 79523b3eb3cSopenharmony_ci } 79623b3eb3cSopenharmony_ci 79723b3eb3cSopenharmony_ci if (!subwindow) { 79823b3eb3cSopenharmony_ci subwindow = Subwindow::CreateSubwindow(containerId); 79923b3eb3cSopenharmony_ci if (!subwindow) { 80023b3eb3cSopenharmony_ci TAG_LOGE(AceLogTag::ACE_SUB_WINDOW, "create sub window failed"); 80123b3eb3cSopenharmony_ci return nullptr; 80223b3eb3cSopenharmony_ci } 80323b3eb3cSopenharmony_ci subwindow->SetIsSystemTopMost(isSystemTopMost); 80423b3eb3cSopenharmony_ci subwindow->SetAboveApps(showMode == NG::ToastShowMode::TOP_MOST); 80523b3eb3cSopenharmony_ci if (isSystemTopMost) { 80623b3eb3cSopenharmony_ci AddSystemToastWindow(containerId, subwindow); 80723b3eb3cSopenharmony_ci } else { 80823b3eb3cSopenharmony_ci AddSubwindow(containerId, subwindow); 80923b3eb3cSopenharmony_ci } 81023b3eb3cSopenharmony_ci } 81123b3eb3cSopenharmony_ci 81223b3eb3cSopenharmony_ci return subwindow; 81323b3eb3cSopenharmony_ci} 81423b3eb3cSopenharmony_ci 81523b3eb3cSopenharmony_ciRefPtr<Subwindow> SubwindowManager::GetOrCreateToastWindowNG(int32_t containerId, 81623b3eb3cSopenharmony_ci const ToastWindowType& windowType, uint32_t mainWindowId) 81723b3eb3cSopenharmony_ci{ 81823b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow = GetToastSubwindow(containerId); 81923b3eb3cSopenharmony_ci if (!subwindow) { 82023b3eb3cSopenharmony_ci subwindow = Subwindow::CreateSubwindow(containerId); 82123b3eb3cSopenharmony_ci if (!subwindow) { 82223b3eb3cSopenharmony_ci TAG_LOGE(AceLogTag::ACE_SUB_WINDOW, "create toast subwindow failed"); 82323b3eb3cSopenharmony_ci return nullptr; 82423b3eb3cSopenharmony_ci } 82523b3eb3cSopenharmony_ci subwindow->SetToastWindowType(windowType); 82623b3eb3cSopenharmony_ci subwindow->SetMainWindowId(mainWindowId); 82723b3eb3cSopenharmony_ci AddToastSubwindow(containerId, subwindow); 82823b3eb3cSopenharmony_ci } 82923b3eb3cSopenharmony_ci return subwindow; 83023b3eb3cSopenharmony_ci} 83123b3eb3cSopenharmony_ci 83223b3eb3cSopenharmony_civoid SubwindowManager::ClearToastInSubwindow() 83323b3eb3cSopenharmony_ci{ 83423b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "clear toast in subwindow enter"); 83523b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 83623b3eb3cSopenharmony_ci // Get active container when current instanceid is less than 0 83723b3eb3cSopenharmony_ci if (containerId < 0) { 83823b3eb3cSopenharmony_ci auto container = Container::GetActive(); 83923b3eb3cSopenharmony_ci if (container) { 84023b3eb3cSopenharmony_ci containerId = container->GetInstanceId(); 84123b3eb3cSopenharmony_ci } 84223b3eb3cSopenharmony_ci } 84323b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow; 84423b3eb3cSopenharmony_ci // The main window does not need to clear Toast 84523b3eb3cSopenharmony_ci if (containerId != -1 && containerId < MIN_SUBCONTAINER_ID) { 84623b3eb3cSopenharmony_ci // get the subwindow which overlay node in, not current 84723b3eb3cSopenharmony_ci auto parentContainerId = containerId >= MIN_SUBCONTAINER_ID ? 84823b3eb3cSopenharmony_ci GetParentContainerId(containerId) : containerId; 84923b3eb3cSopenharmony_ci subwindow = GetToastSubwindow(parentContainerId); 85023b3eb3cSopenharmony_ci } 85123b3eb3cSopenharmony_ci if (subwindow) { 85223b3eb3cSopenharmony_ci subwindow->ClearToast(); 85323b3eb3cSopenharmony_ci } 85423b3eb3cSopenharmony_ci} 85523b3eb3cSopenharmony_ci 85623b3eb3cSopenharmony_civoid SubwindowManager::ShowDialog(const std::string& title, const std::string& message, 85723b3eb3cSopenharmony_ci const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& napiCallback, 85823b3eb3cSopenharmony_ci const std::set<std::string>& dialogCallbacks) 85923b3eb3cSopenharmony_ci{ 86023b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show dialog enter"); 86123b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 86223b3eb3cSopenharmony_ci // Get active container when current instanceid is less than 0 86323b3eb3cSopenharmony_ci if (containerId < 0) { 86423b3eb3cSopenharmony_ci auto container = Container::GetActive(); 86523b3eb3cSopenharmony_ci if (container) { 86623b3eb3cSopenharmony_ci containerId = container->GetInstanceId(); 86723b3eb3cSopenharmony_ci } 86823b3eb3cSopenharmony_ci } 86923b3eb3cSopenharmony_ci // for pa service 87023b3eb3cSopenharmony_ci if (containerId >= MIN_PA_SERVICE_ID || containerId < 0) { 87123b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubWindow(); 87223b3eb3cSopenharmony_ci CHECK_NULL_VOID(subwindow); 87323b3eb3cSopenharmony_ci subwindow->ShowDialog(title, message, buttons, autoCancel, std::move(napiCallback), dialogCallbacks); 87423b3eb3cSopenharmony_ci // for ability 87523b3eb3cSopenharmony_ci } else { 87623b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubwindow(containerId); 87723b3eb3cSopenharmony_ci if (!subwindow) { 87823b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 87923b3eb3cSopenharmony_ci return; 88023b3eb3cSopenharmony_ci } 88123b3eb3cSopenharmony_ci subwindow->ShowDialog(title, message, buttons, autoCancel, std::move(napiCallback), dialogCallbacks); 88223b3eb3cSopenharmony_ci } 88323b3eb3cSopenharmony_ci} 88423b3eb3cSopenharmony_ci 88523b3eb3cSopenharmony_civoid SubwindowManager::ShowDialog(const PromptDialogAttr& dialogAttr, const std::vector<ButtonInfo>& buttons, 88623b3eb3cSopenharmony_ci std::function<void(int32_t, int32_t)>&& napiCallback, const std::set<std::string>& dialogCallbacks) 88723b3eb3cSopenharmony_ci{ 88823b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show dialog enter"); 88923b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 89023b3eb3cSopenharmony_ci // Get active container when current instanceid is less than 0 89123b3eb3cSopenharmony_ci if (containerId < 0) { 89223b3eb3cSopenharmony_ci auto container = Container::GetActive(); 89323b3eb3cSopenharmony_ci if (container) { 89423b3eb3cSopenharmony_ci containerId = container->GetInstanceId(); 89523b3eb3cSopenharmony_ci } 89623b3eb3cSopenharmony_ci } 89723b3eb3cSopenharmony_ci // for pa service 89823b3eb3cSopenharmony_ci if (containerId >= MIN_PA_SERVICE_ID || containerId < 0) { 89923b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubWindow(); 90023b3eb3cSopenharmony_ci CHECK_NULL_VOID(subwindow); 90123b3eb3cSopenharmony_ci subwindow->ShowDialog(dialogAttr, buttons, std::move(napiCallback), dialogCallbacks); 90223b3eb3cSopenharmony_ci // for ability 90323b3eb3cSopenharmony_ci } else { 90423b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubwindow(containerId); 90523b3eb3cSopenharmony_ci if (!subwindow) { 90623b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 90723b3eb3cSopenharmony_ci return; 90823b3eb3cSopenharmony_ci } 90923b3eb3cSopenharmony_ci subwindow->ShowDialog(dialogAttr, buttons, std::move(napiCallback), dialogCallbacks); 91023b3eb3cSopenharmony_ci } 91123b3eb3cSopenharmony_ci} 91223b3eb3cSopenharmony_ci 91323b3eb3cSopenharmony_civoid SubwindowManager::ShowActionMenu( 91423b3eb3cSopenharmony_ci const std::string& title, const std::vector<ButtonInfo>& button, std::function<void(int32_t, int32_t)>&& callback) 91523b3eb3cSopenharmony_ci{ 91623b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "show action menu enter"); 91723b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 91823b3eb3cSopenharmony_ci // Get active container when current instanceid is less than 0 91923b3eb3cSopenharmony_ci if (containerId < 0) { 92023b3eb3cSopenharmony_ci auto container = Container::GetActive(); 92123b3eb3cSopenharmony_ci if (container) { 92223b3eb3cSopenharmony_ci containerId = container->GetInstanceId(); 92323b3eb3cSopenharmony_ci } 92423b3eb3cSopenharmony_ci } 92523b3eb3cSopenharmony_ci // for pa service 92623b3eb3cSopenharmony_ci if (containerId >= MIN_PA_SERVICE_ID || containerId < 0) { 92723b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubWindow(); 92823b3eb3cSopenharmony_ci CHECK_NULL_VOID(subwindow); 92923b3eb3cSopenharmony_ci subwindow->ShowActionMenu(title, button, std::move(callback)); 93023b3eb3cSopenharmony_ci // for ability 93123b3eb3cSopenharmony_ci } else { 93223b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubwindow(containerId); 93323b3eb3cSopenharmony_ci if (!subwindow) { 93423b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 93523b3eb3cSopenharmony_ci return; 93623b3eb3cSopenharmony_ci } 93723b3eb3cSopenharmony_ci subwindow->ShowActionMenu(title, button, std::move(callback)); 93823b3eb3cSopenharmony_ci } 93923b3eb3cSopenharmony_ci} 94023b3eb3cSopenharmony_ci 94123b3eb3cSopenharmony_civoid SubwindowManager::CloseDialog(int32_t instanceId) 94223b3eb3cSopenharmony_ci{ 94323b3eb3cSopenharmony_ci auto subwindow = GetDialogSubwindow(instanceId); 94423b3eb3cSopenharmony_ci if (!subwindow) { 94523b3eb3cSopenharmony_ci subwindow = GetSubwindow(instanceId); 94623b3eb3cSopenharmony_ci if (subwindow) { 94723b3eb3cSopenharmony_ci subwindow->Close(); 94823b3eb3cSopenharmony_ci return; 94923b3eb3cSopenharmony_ci } 95023b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get dialog subwindow failed."); 95123b3eb3cSopenharmony_ci return; 95223b3eb3cSopenharmony_ci } 95323b3eb3cSopenharmony_ci auto subContainerId = GetSubContainerId(instanceId); 95423b3eb3cSopenharmony_ci if (subContainerId > -1) { 95523b3eb3cSopenharmony_ci subwindow->CloseDialog(subContainerId); 95623b3eb3cSopenharmony_ci } 95723b3eb3cSopenharmony_ci} 95823b3eb3cSopenharmony_ci 95923b3eb3cSopenharmony_civoid SubwindowManager::OpenCustomDialog(const PromptDialogAttr &dialogAttr, std::function<void(int32_t)> &&callback) 96023b3eb3cSopenharmony_ci{ 96123b3eb3cSopenharmony_ci PromptDialogAttr tmpPromptAttr = dialogAttr; 96223b3eb3cSopenharmony_ci tmpPromptAttr.showInSubWindow = false; 96323b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 96423b3eb3cSopenharmony_ci // for pa service 96523b3eb3cSopenharmony_ci TAG_LOGI(AceLogTag::ACE_SUB_WINDOW, "container %{public}d open the custom dialog", containerId); 96623b3eb3cSopenharmony_ci if (containerId >= MIN_PA_SERVICE_ID || containerId < 0) { 96723b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubWindow(); 96823b3eb3cSopenharmony_ci CHECK_NULL_VOID(subwindow); 96923b3eb3cSopenharmony_ci subwindow->OpenCustomDialog(tmpPromptAttr, std::move(callback)); 97023b3eb3cSopenharmony_ci // for ability 97123b3eb3cSopenharmony_ci } else { 97223b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubwindow(containerId); 97323b3eb3cSopenharmony_ci if (!subwindow) { 97423b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 97523b3eb3cSopenharmony_ci return; 97623b3eb3cSopenharmony_ci } 97723b3eb3cSopenharmony_ci subwindow->OpenCustomDialog(tmpPromptAttr, std::move(callback)); 97823b3eb3cSopenharmony_ci } 97923b3eb3cSopenharmony_ci return; 98023b3eb3cSopenharmony_ci} 98123b3eb3cSopenharmony_ci 98223b3eb3cSopenharmony_civoid SubwindowManager::CloseCustomDialog(const int32_t dialogId) 98323b3eb3cSopenharmony_ci{ 98423b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 98523b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "CloseCustomDialog dialogId = %{public}d, containerId = %{public}d.", 98623b3eb3cSopenharmony_ci dialogId, containerId); 98723b3eb3cSopenharmony_ci auto subwindow = GetDialogSubwindow(containerId); 98823b3eb3cSopenharmony_ci if (!subwindow) { 98923b3eb3cSopenharmony_ci return; 99023b3eb3cSopenharmony_ci } 99123b3eb3cSopenharmony_ci subwindow->CloseCustomDialog(dialogId); 99223b3eb3cSopenharmony_ci return; 99323b3eb3cSopenharmony_ci} 99423b3eb3cSopenharmony_ci 99523b3eb3cSopenharmony_civoid SubwindowManager::CloseCustomDialog(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)> &&callback) 99623b3eb3cSopenharmony_ci{ 99723b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 99823b3eb3cSopenharmony_ci auto subwindow = GetDialogSubwindow(containerId); 99923b3eb3cSopenharmony_ci if (!subwindow) { 100023b3eb3cSopenharmony_ci return; 100123b3eb3cSopenharmony_ci } 100223b3eb3cSopenharmony_ci subwindow->CloseCustomDialog(node, std::move(callback)); 100323b3eb3cSopenharmony_ci return; 100423b3eb3cSopenharmony_ci} 100523b3eb3cSopenharmony_ci 100623b3eb3cSopenharmony_civoid SubwindowManager::HideSubWindowNG() 100723b3eb3cSopenharmony_ci{ 100823b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow; 100923b3eb3cSopenharmony_ci auto container = Container::Current(); 101023b3eb3cSopenharmony_ci CHECK_NULL_VOID(container); 101123b3eb3cSopenharmony_ci if (container->IsDialogContainer()) { 101223b3eb3cSopenharmony_ci subwindow = GetCurrentDialogWindow(); 101323b3eb3cSopenharmony_ci } else { 101423b3eb3cSopenharmony_ci subwindow = GetCurrentWindow(); 101523b3eb3cSopenharmony_ci } 101623b3eb3cSopenharmony_ci if (subwindow) { 101723b3eb3cSopenharmony_ci subwindow->HideSubWindowNG(); 101823b3eb3cSopenharmony_ci } 101923b3eb3cSopenharmony_ci} 102023b3eb3cSopenharmony_ci 102123b3eb3cSopenharmony_civoid SubwindowManager::HideToastSubWindowNG() 102223b3eb3cSopenharmony_ci{ 102323b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "hide toast subwindow enter"); 102423b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow; 102523b3eb3cSopenharmony_ci auto container = Container::Current(); 102623b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 102723b3eb3cSopenharmony_ci CHECK_NULL_VOID(container); 102823b3eb3cSopenharmony_ci if (container->IsDialogContainer()) { 102923b3eb3cSopenharmony_ci subwindow = GetCurrentDialogWindow(); 103023b3eb3cSopenharmony_ci } else if (containerId != -1) { 103123b3eb3cSopenharmony_ci auto parentContainerId = containerId >= MIN_SUBCONTAINER_ID ? 103223b3eb3cSopenharmony_ci GetParentContainerId(containerId) : containerId; 103323b3eb3cSopenharmony_ci subwindow = GetToastSubwindow(parentContainerId); 103423b3eb3cSopenharmony_ci } 103523b3eb3cSopenharmony_ci if (subwindow) { 103623b3eb3cSopenharmony_ci subwindow->HideSubWindowNG(); 103723b3eb3cSopenharmony_ci } 103823b3eb3cSopenharmony_ci} 103923b3eb3cSopenharmony_ci 104023b3eb3cSopenharmony_civoid SubwindowManager::RequestFocusSubwindow(int32_t instanceId) 104123b3eb3cSopenharmony_ci{ 104223b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow; 104323b3eb3cSopenharmony_ci if (instanceId != -1) { 104423b3eb3cSopenharmony_ci // get the subwindow which overlay node in, not current 104523b3eb3cSopenharmony_ci subwindow = GetSubwindow(instanceId >= MIN_SUBCONTAINER_ID ? GetParentContainerId(instanceId) : instanceId); 104623b3eb3cSopenharmony_ci } else { 104723b3eb3cSopenharmony_ci subwindow = GetCurrentWindow(); 104823b3eb3cSopenharmony_ci } 104923b3eb3cSopenharmony_ci if (subwindow) { 105023b3eb3cSopenharmony_ci subwindow->RequestFocus(); 105123b3eb3cSopenharmony_ci } 105223b3eb3cSopenharmony_ci} 105323b3eb3cSopenharmony_ci 105423b3eb3cSopenharmony_cibool SubwindowManager::GetShown() 105523b3eb3cSopenharmony_ci{ 105623b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 105723b3eb3cSopenharmony_ci auto subwindow = GetOrCreateSubwindow(containerId); 105823b3eb3cSopenharmony_ci if (!subwindow) { 105923b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, "get or create subwindow failed"); 106023b3eb3cSopenharmony_ci return false; 106123b3eb3cSopenharmony_ci } 106223b3eb3cSopenharmony_ci return subwindow->GetShown(); 106323b3eb3cSopenharmony_ci} 106423b3eb3cSopenharmony_ci 106523b3eb3cSopenharmony_civoid SubwindowManager::ResizeWindowForFoldStatus(int32_t parentContainerId) 106623b3eb3cSopenharmony_ci{ 106723b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 106823b3eb3cSopenharmony_ci auto subwindow = parentContainerId < 0 ? GetDialogSubwindow(parentContainerId) : GetToastSubwindow(containerId); 106923b3eb3cSopenharmony_ci if (!subwindow) { 107023b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_SUB_WINDOW, 107123b3eb3cSopenharmony_ci "Get Subwindow error, containerId = %{public}d, parentContainerId = %{public}d", containerId, 107223b3eb3cSopenharmony_ci parentContainerId); 107323b3eb3cSopenharmony_ci return; 107423b3eb3cSopenharmony_ci } 107523b3eb3cSopenharmony_ci subwindow->ResizeWindowForFoldStatus(parentContainerId); 107623b3eb3cSopenharmony_ci} 107723b3eb3cSopenharmony_ci 107823b3eb3cSopenharmony_civoid SubwindowManager::MarkDirtyDialogSafeArea() 107923b3eb3cSopenharmony_ci{ 108023b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 108123b3eb3cSopenharmony_ci auto subwindow = SubwindowManager::GetInstance()->GetSubwindow(containerId); 108223b3eb3cSopenharmony_ci CHECK_NULL_VOID(subwindow); 108323b3eb3cSopenharmony_ci if (subwindow) { 108423b3eb3cSopenharmony_ci subwindow->MarkDirtyDialogSafeArea(); 108523b3eb3cSopenharmony_ci } 108623b3eb3cSopenharmony_ci subwindow = GetToastSubwindow(containerId); 108723b3eb3cSopenharmony_ci if (subwindow) { 108823b3eb3cSopenharmony_ci subwindow->MarkDirtyDialogSafeArea(); 108923b3eb3cSopenharmony_ci } 109023b3eb3cSopenharmony_ci} 109123b3eb3cSopenharmony_ci 109223b3eb3cSopenharmony_civoid SubwindowManager::HideSystemTopMostWindow() 109323b3eb3cSopenharmony_ci{ 109423b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 109523b3eb3cSopenharmony_ci if (containerId < 0) { 109623b3eb3cSopenharmony_ci auto container = Container::GetActive(); 109723b3eb3cSopenharmony_ci if (container) { 109823b3eb3cSopenharmony_ci containerId = container->GetInstanceId(); 109923b3eb3cSopenharmony_ci } 110023b3eb3cSopenharmony_ci } 110123b3eb3cSopenharmony_ci auto parentContainerId = containerId >= MIN_SUBCONTAINER_ID ? 110223b3eb3cSopenharmony_ci GetParentContainerId(containerId) : containerId; 110323b3eb3cSopenharmony_ci auto subwindow = GetSystemToastWindow(parentContainerId); 110423b3eb3cSopenharmony_ci if (subwindow) { 110523b3eb3cSopenharmony_ci subwindow->HideSubWindowNG(); 110623b3eb3cSopenharmony_ci } else { 110723b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "can not find systemTopMost window when hide window"); 110823b3eb3cSopenharmony_ci } 110923b3eb3cSopenharmony_ci} 111023b3eb3cSopenharmony_ci 111123b3eb3cSopenharmony_civoid SubwindowManager::ClearToastInSystemSubwindow() 111223b3eb3cSopenharmony_ci{ 111323b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "clear toast in system subwindow enter"); 111423b3eb3cSopenharmony_ci auto containerId = Container::CurrentId(); 111523b3eb3cSopenharmony_ci if (containerId < 0) { 111623b3eb3cSopenharmony_ci auto container = Container::GetActive(); 111723b3eb3cSopenharmony_ci if (container) { 111823b3eb3cSopenharmony_ci containerId = container->GetInstanceId(); 111923b3eb3cSopenharmony_ci } 112023b3eb3cSopenharmony_ci } 112123b3eb3cSopenharmony_ci auto parentContainerId = containerId >= MIN_SUBCONTAINER_ID ? 112223b3eb3cSopenharmony_ci GetParentContainerId(containerId) : containerId; 112323b3eb3cSopenharmony_ci auto subwindow = GetSystemToastWindow(parentContainerId); 112423b3eb3cSopenharmony_ci if (subwindow) { 112523b3eb3cSopenharmony_ci subwindow->ClearToast(); 112623b3eb3cSopenharmony_ci } else { 112723b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "can not find systemTopMost window when clear system toast"); 112823b3eb3cSopenharmony_ci } 112923b3eb3cSopenharmony_ci} 113023b3eb3cSopenharmony_civoid SubwindowManager::OnWindowSizeChanged(int32_t instanceId, Rect windowRect, WindowSizeChangeReason reason) 113123b3eb3cSopenharmony_ci{ 113223b3eb3cSopenharmony_ci auto container = Container::GetContainer(instanceId); 113323b3eb3cSopenharmony_ci CHECK_NULL_VOID(container); 113423b3eb3cSopenharmony_ci if (!container->IsUIExtensionWindow() || uiExtensionWindowRect_ == windowRect) { 113523b3eb3cSopenharmony_ci return; 113623b3eb3cSopenharmony_ci } 113723b3eb3cSopenharmony_ci auto subContainer = Container::GetContainer(GetSubContainerId(instanceId)); 113823b3eb3cSopenharmony_ci CHECK_NULL_VOID(subContainer); 113923b3eb3cSopenharmony_ci auto pipeline = AceType::DynamicCast<NG::PipelineContext>(subContainer->GetPipelineContext()); 114023b3eb3cSopenharmony_ci CHECK_NULL_VOID(pipeline); 114123b3eb3cSopenharmony_ci auto overlayManager = pipeline->GetOverlayManager(); 114223b3eb3cSopenharmony_ci CHECK_NULL_VOID(overlayManager); 114323b3eb3cSopenharmony_ci overlayManager->OnUIExtensionWindowSizeChange(); 114423b3eb3cSopenharmony_ci uiExtensionWindowRect_ = windowRect; 114523b3eb3cSopenharmony_ci} 114623b3eb3cSopenharmony_ci 114723b3eb3cSopenharmony_ciRefPtr<NG::FrameNode> SubwindowManager::GetSubwindowDialogNodeWithExistContent(const RefPtr<NG::UINode>& node) 114823b3eb3cSopenharmony_ci{ 114923b3eb3cSopenharmony_ci for (auto &overlay : GetAllSubOverlayManager()) { 115023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(overlay, nullptr); 115123b3eb3cSopenharmony_ci auto dialogNode = overlay->GetDialogNodeWithExistContent(node); 115223b3eb3cSopenharmony_ci if (dialogNode) { 115323b3eb3cSopenharmony_ci return dialogNode; 115423b3eb3cSopenharmony_ci } 115523b3eb3cSopenharmony_ci } 115623b3eb3cSopenharmony_ci return nullptr; 115723b3eb3cSopenharmony_ci} 115823b3eb3cSopenharmony_ci 115923b3eb3cSopenharmony_civoid SubwindowManager::SetRect(const NG::RectF& rect, int32_t instanceId) 116023b3eb3cSopenharmony_ci{ 116123b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "set subwindow rect enter"); 116223b3eb3cSopenharmony_ci RefPtr<Subwindow> subwindow; 116323b3eb3cSopenharmony_ci if (instanceId != -1) { 116423b3eb3cSopenharmony_ci // get the subwindow which overlay node in, not current 116523b3eb3cSopenharmony_ci subwindow = GetSubwindow(instanceId >= MIN_SUBCONTAINER_ID ? GetParentContainerId(instanceId) : instanceId); 116623b3eb3cSopenharmony_ci } else { 116723b3eb3cSopenharmony_ci subwindow = GetCurrentWindow(); 116823b3eb3cSopenharmony_ci } 116923b3eb3cSopenharmony_ci 117023b3eb3cSopenharmony_ci if (subwindow) { 117123b3eb3cSopenharmony_ci subwindow->SetRect(rect); 117223b3eb3cSopenharmony_ci } else { 117323b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_SUB_WINDOW, "can not get subwindow, set rect failed"); 117423b3eb3cSopenharmony_ci } 117523b3eb3cSopenharmony_ci} 117623b3eb3cSopenharmony_ci 117723b3eb3cSopenharmony_cibool SubwindowManager::IsFreeMultiWindow(int32_t instanceId) const 117823b3eb3cSopenharmony_ci{ 117923b3eb3cSopenharmony_ci auto parentContainerId = instanceId >= MIN_SUBCONTAINER_ID 118023b3eb3cSopenharmony_ci ? SubwindowManager::GetInstance()->GetParentContainerId(instanceId) 118123b3eb3cSopenharmony_ci : instanceId; 118223b3eb3cSopenharmony_ci auto subWindow = SubwindowManager::GetInstance()->GetSubwindow(parentContainerId); 118323b3eb3cSopenharmony_ci CHECK_NULL_RETURN(subWindow, false); 118423b3eb3cSopenharmony_ci return subWindow->IsFreeMultiWindow(); 118523b3eb3cSopenharmony_ci} 118623b3eb3cSopenharmony_ci 118723b3eb3cSopenharmony_ciconst std::vector<RefPtr<NG::OverlayManager>> SubwindowManager::GetAllSubOverlayManager() 118823b3eb3cSopenharmony_ci{ 118923b3eb3cSopenharmony_ci std::lock_guard<std::mutex> lock(subwindowMutex_); 119023b3eb3cSopenharmony_ci std::vector<RefPtr<NG::OverlayManager>> subOverlayManager; 119123b3eb3cSopenharmony_ci subOverlayManager.reserve(subwindowMap_.size()); 119223b3eb3cSopenharmony_ci auto iter = subwindowMap_.begin(); 119323b3eb3cSopenharmony_ci while (iter != subwindowMap_.end()) { 119423b3eb3cSopenharmony_ci auto subwindow = iter->second; 119523b3eb3cSopenharmony_ci if (subwindow) { 119623b3eb3cSopenharmony_ci subOverlayManager.push_back(subwindow->GetOverlayManager()); 119723b3eb3cSopenharmony_ci } 119823b3eb3cSopenharmony_ci iter++; 119923b3eb3cSopenharmony_ci } 120023b3eb3cSopenharmony_ci return subOverlayManager; 120123b3eb3cSopenharmony_ci} 120223b3eb3cSopenharmony_ci} // namespace OHOS::Ace 1203