From 32a46677f141be83e10113c42348a3995fd507cf Mon Sep 17 00:00:00 2001 From: zhanghaibo0 Date: Thu, 13 Feb 2025 15:17:00 +0800 Subject: [PATCH] optimize implementation of DialgContainer Signed-off-by: zhanghaibo0 Change-Id: I12af82a51a9f6082b8228cf58697a280b80205cc --- adapter/ohos/entrance/ace_container.cpp | 58 +- adapter/ohos/entrance/ace_container.h | 10 +- adapter/ohos/entrance/dialog_container.cpp | 565 +----------------- adapter/ohos/entrance/dialog_container.h | 174 +----- .../entrance/subwindow/subwindow_ohos.cpp | 16 +- adapter/ohos/entrance/ui_content_impl.cpp | 30 - adapter/ohos/entrance/ui_content_impl.h | 2 - 7 files changed, 89 insertions(+), 766 deletions(-) diff --git a/adapter/ohos/entrance/ace_container.cpp b/adapter/ohos/entrance/ace_container.cpp index 09da2993d75..8db821765ac 100644 --- a/adapter/ohos/entrance/ace_container.cpp +++ b/adapter/ohos/entrance/ace_container.cpp @@ -287,6 +287,16 @@ AceContainer::AceContainer(int32_t instanceId, FrontendType type, useStageModel_ = true; } +AceContainer::AceContainer(int32_t instanceId, FrontendType type) : instanceId_(instanceId), type_(type) +{ + auto taskExecutorImpl = Referenced::MakeRefPtr(); + taskExecutorImpl->InitPlatformThread(true); + taskExecutor_ = taskExecutorImpl; + GetSettings().useUIAsJSThread = true; + GetSettings().usePlatformAsUIThread = true; + GetSettings().usingSharedRuntime = true; +} + AceContainer::~AceContainer() { std::lock_guard lock(destructMutex_); @@ -2456,11 +2466,12 @@ void AceContainer::AttachView(std::shared_ptr window, const RefPtr(context)); } - if (!isSubContainer) { + if (!isSubContainer && !isDialogContainer) { context->SetupRootElement(); } }; @@ -2783,6 +2794,14 @@ void AceContainer::CheckAndSetFontFamily() } } +void AceContainer::SetFontScaleAndWeightScale(int32_t instanceId) +{ + float fontScale = SystemProperties::GetFontScale(); + float fontWeightScale = SystemProperties::GetFontWeightScale(); + Container::SetFontScale(instanceId, fontScale); + Container::SetFontWeightScale(instanceId, fontWeightScale); +} + void AceContainer::SetFontScaleAndWeightScale( const ParsedConfig& parsedConfig, ConfigurationChange& configurationChange) { @@ -3956,4 +3975,39 @@ void AceContainer::SetIsFocusActive(bool isFocusActive) CHECK_NULL_VOID(pipelineContext); pipelineContext->SetIsFocusActive(isFocusActive); } + +bool AceContainer::CloseWindow(int32_t instanceId) +{ + TAG_LOGI(AceLogTag::ACE_DIALOG, "AceContainer CloseWindow begin"); + auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); + CHECK_NULL_RETURN(container, false); + auto window = container->GetUIWindowInner(); + CHECK_NULL_RETURN(window, false); + OHOS::Rosen::WMError ret = window->Close(); + if (ret != OHOS::Rosen::WMError::WM_OK) { + TAG_LOGE(AceLogTag::ACE_DIALOG, "AceContainer CloseWindow Failed to close the window."); + return false; + } + sptr uiWindow = nullptr; + AceContainer::SetUIWindow(instanceId, uiWindow); + return true; +} + +bool AceContainer::HideWindow(int32_t instanceId) +{ + TAG_LOGI(AceLogTag::ACE_DIALOG, "AceContainer HideWindow begin"); + auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); + CHECK_NULL_RETURN(container, false); + auto window = container->GetUIWindowInner(); + CHECK_NULL_RETURN(window, false); + OHOS::Rosen::WMError ret = window->Hide(); + if (ret != OHOS::Rosen::WMError::WM_OK) { + TAG_LOGE(AceLogTag::ACE_DIALOG, "AceContainer HideWindow Failed to hide the window."); + return false; + } + sptr uiWindow = nullptr; + AceContainer::SetUIWindow(instanceId, uiWindow); + return true; +} + } // namespace OHOS::Ace::Platform diff --git a/adapter/ohos/entrance/ace_container.h b/adapter/ohos/entrance/ace_container.h index 4d0b834d485..8a994226fff 100644 --- a/adapter/ohos/entrance/ace_container.h +++ b/adapter/ohos/entrance/ace_container.h @@ -119,6 +119,8 @@ public: std::shared_ptr taskWrapper, bool useCurrentEventRunner = false, bool isSubContainer = false, bool useNewPipeline = false); + AceContainer(int32_t instanceId, FrontendType type); + ~AceContainer() override; bool UpdatePopupUIExtension(const RefPtr& node, @@ -514,6 +516,9 @@ public: static bool UpdatePage(int32_t instanceId, int32_t pageId, const std::string& content); static bool RemoveOverlayBySubwindowManager(int32_t instanceId); + static bool CloseWindow(int32_t instanceId); + static bool HideWindow(int32_t instanceId); + // ArkTsCard static std::shared_ptr GetFormSurfaceNode(int32_t instanceId); @@ -809,6 +814,10 @@ public: void SetIsFocusActive(bool isFocusActive); + void SetFontScaleAndWeightScale(int32_t instanceId); + + sptr GetUIWindowInner() const; + private: virtual bool MaybeRelease() override; void InitializeFrontend(); @@ -819,7 +828,6 @@ private: void AttachView(std::shared_ptr window, const RefPtr& view, double density, float width, float height, uint32_t windowId, UIEnvCallback callback = nullptr); void SetUIWindowInner(sptr uiWindow); - sptr GetUIWindowInner() const; std::weak_ptr GetAbilityInner() const; std::weak_ptr GetRuntimeContextInner() const; diff --git a/adapter/ohos/entrance/dialog_container.cpp b/adapter/ohos/entrance/dialog_container.cpp index 694f962a2c8..af36f4a1fa0 100644 --- a/adapter/ohos/entrance/dialog_container.cpp +++ b/adapter/ohos/entrance/dialog_container.cpp @@ -38,447 +38,13 @@ #include "frameworks/bridge/declarative_frontend/declarative_frontend.h" namespace OHOS::Ace::Platform { -DialogContainer::DialogContainer(int32_t instanceId, FrontendType type) : instanceId_(instanceId), type_(type) -{ - auto taskExecutorImpl = Referenced::MakeRefPtr(); - taskExecutorImpl->InitPlatformThread(true); - taskExecutor_ = taskExecutorImpl; - GetSettings().useUIAsJSThread = true; - GetSettings().usePlatformAsUIThread = true; - GetSettings().usingSharedRuntime = true; -} - -void DialogContainer::InitializeTouchEventCallback() -{ - ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_); - auto&& touchEventCallback = [context = pipelineContext_, id = instanceId_]( - const TouchEvent& event, const std::function& markProcess, - const RefPtr& node) { - ContainerScope scope(id); - context->GetTaskExecutor()->PostTask( - [context, event, markProcess]() { - context->OnTouchEvent(event); - context->NotifyDispatchTouchEventDismiss(event); - CHECK_NULL_VOID(markProcess); - markProcess(); - }, - TaskExecutor::TaskType::UI, "ArkUIDialogTouchEvent"); - }; - aceView_->RegisterTouchEventCallback(touchEventCallback); -} - -void DialogContainer::InitializeMouseEventCallback() -{ - ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_); - auto&& mouseEventCallback = [context = pipelineContext_, id = instanceId_]( - const MouseEvent& event, const std::function& markProcess, - const RefPtr& node) { - ContainerScope scope(id); - context->GetTaskExecutor()->PostTask( - [context, event, markProcess]() { - context->OnMouseEvent(event); - CHECK_NULL_VOID(markProcess); - markProcess(); - }, - TaskExecutor::TaskType::UI, "ArkUIDialogMouseEvent"); - }; - aceView_->RegisterMouseEventCallback(mouseEventCallback); -} - -void DialogContainer::InitializeAxisEventCallback() -{ - ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_); - auto&& axisEventCallback = [context = pipelineContext_, id = instanceId_]( - const AxisEvent& event, const std::function& markProcess, - const RefPtr& node) { - ContainerScope scope(id); - context->GetTaskExecutor()->PostTask( - [context, event, markProcess]() { - context->OnAxisEvent(event); - CHECK_NULL_VOID(markProcess); - markProcess(); - }, - TaskExecutor::TaskType::UI, "ArkUIDialogAxisEvent"); - }; - aceView_->RegisterAxisEventCallback(axisEventCallback); -} - -void DialogContainer::InitializeKeyEventCallback() -{ - ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_); - auto&& keyEventCallback = [context = pipelineContext_, id = instanceId_](const KeyEvent& event) { - ContainerScope scope(id); - bool result = false; - context->GetTaskExecutor()->PostSyncTask( - [context, event, &result]() { result = context->OnNonPointerEvent(event); }, - TaskExecutor::TaskType::UI, "ArkUIDialogKeyEvent"); - return result; - }; - aceView_->RegisterKeyEventCallback(keyEventCallback); -} - -void DialogContainer::InitializeCrownEventCallback() -{ - ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_); - auto&& crownEventCallback = [context = pipelineContext_, id = instanceId_]( - const CrownEvent& event, - const std::function& markProcess) { - ContainerScope scope(id); - bool result = false; - context->GetTaskExecutor()->PostSyncTask( - [context, event, &result, markProcess, id]() { - ContainerScope scope(id); - result = context->OnNonPointerEvent(event); - CHECK_NULL_VOID(markProcess); - markProcess(); - }, - TaskExecutor::TaskType::UI, "ArkUIDialogCrownEvent"); - return result; - }; - aceView_->RegisterCrownEventCallback(crownEventCallback); -} - -void DialogContainer::InitializeRotationEventCallback() -{ - ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_); - auto&& rotationEventCallback = [context = pipelineContext_, id = instanceId_](const RotationEvent& event) { - ContainerScope scope(id); - bool result = false; - context->GetTaskExecutor()->PostSyncTask( - [context, event, &result]() { result = context->OnRotationEvent(event); }, - TaskExecutor::TaskType::UI, "ArkUIDialogRotationEvent"); - return result; - }; - aceView_->RegisterRotationEventCallback(rotationEventCallback); -} - -void DialogContainer::InitializeViewChangeCallback() -{ - ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_); - auto&& viewChangeCallback = [context = pipelineContext_, id = instanceId_](int32_t width, int32_t height, - WindowSizeChangeReason type, - const std::shared_ptr& rsTransaction) { - ContainerScope scope(id); - ACE_SCOPED_TRACE("ViewChangeCallback(%d, %d)", width, height); - context->GetTaskExecutor()->PostTask( - [context, width, height, type, rsTransaction]() { - context->OnSurfaceChanged(width, height, type, rsTransaction); - }, - TaskExecutor::TaskType::UI, "ArkUIDialogSurfaceChanged"); - }; - aceView_->RegisterViewChangeCallback(viewChangeCallback); -} - -void DialogContainer::InitializeDensityChangeCallback() -{ - ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_); - auto&& densityChangeCallback = [context = pipelineContext_, id = instanceId_](double density) { - ContainerScope scope(id); - ACE_SCOPED_TRACE("DensityChangeCallback(%lf)", density); - context->GetTaskExecutor()->PostTask( - [context, density]() { context->OnSurfaceDensityChanged(density); }, - TaskExecutor::TaskType::UI, "ArkUIDialogSurfaceDensityChanged"); - }; - aceView_->RegisterDensityChangeCallback(densityChangeCallback); -} - -void DialogContainer::InitializeSystemBarHeightChangeCallback() -{ - ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_); - auto&& systemBarHeightChangeCallback = [context = pipelineContext_, id = instanceId_]( - double statusBar, double navigationBar) { - ContainerScope scope(id); - ACE_SCOPED_TRACE("SystemBarHeightChangeCallback(%lf, %lf)", statusBar, navigationBar); - context->GetTaskExecutor()->PostTask( - [context, statusBar, navigationBar]() { context->OnSystemBarHeightChanged(statusBar, navigationBar); }, - TaskExecutor::TaskType::UI, "ArkUIDialogSystemBarHeightChanged"); - }; - aceView_->RegisterSystemBarHeightChangeCallback(systemBarHeightChangeCallback); -} - -void DialogContainer::InitializeSurfaceDestroyCallback() -{ - ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_); - auto&& surfaceDestroyCallback = [context = pipelineContext_, id = instanceId_]() { - ContainerScope scope(id); - context->GetTaskExecutor()->PostTask( - [context]() { context->OnSurfaceDestroyed(); }, - TaskExecutor::TaskType::UI, "ArkUIDialogSurfaceDestroyed"); - }; - aceView_->RegisterSurfaceDestroyCallback(surfaceDestroyCallback); -} - -void DialogContainer::InitializeDragEventCallback() -{ - ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_); - auto&& dragEventCallback = [context = pipelineContext_, id = instanceId_]( - const DragPointerEvent& pointerEvent, const DragEventAction& action, - const RefPtr& node) { - ContainerScope scope(id); - context->GetTaskExecutor()->PostTask( - [context, pointerEvent, action, node]() { context->OnDragEvent(pointerEvent, action, node); }, - TaskExecutor::TaskType::UI, "ArkUIDialogDragEvent"); - }; - aceView_->RegisterDragEventCallback(dragEventCallback); -} - -void DialogContainer::InitializeCallback() -{ - ACE_FUNCTION_TRACE(); - InitializeTouchEventCallback(); - InitializeMouseEventCallback(); - InitializeAxisEventCallback(); - InitializeKeyEventCallback(); - InitializeRotationEventCallback(); - InitializeViewChangeCallback(); - InitializeDensityChangeCallback(); - InitializeSystemBarHeightChangeCallback(); - InitializeSurfaceDestroyCallback(); - InitializeDragEventCallback(); - InitializeCrownEventCallback(); -} - -RefPtr DialogContainer::GetContainer(int32_t instanceId) -{ - auto container = AceEngine::Get().GetContainer(instanceId); - CHECK_NULL_RETURN(container, nullptr); - auto dialogContainer = AceType::DynamicCast(container); - return dialogContainer; -} - -void DialogContainer::DestroyContainer(int32_t instanceId, const std::function& destroyCallback) -{ - TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer DestroyContainer begin %{public}d", instanceId); - auto container = AceEngine::Get().GetContainer(instanceId); - CHECK_NULL_VOID(container); - container->Destroy(); - auto taskExecutor = container->GetTaskExecutor(); - CHECK_NULL_VOID(taskExecutor); - taskExecutor->PostSyncTask( - [] { TAG_LOGI(AceLogTag::ACE_DIALOG, "Wait UI thread..."); }, - TaskExecutor::TaskType::UI, "ArkUIDialogWaitLog"); - taskExecutor->PostSyncTask( - [] { TAG_LOGI(AceLogTag::ACE_DIALOG, "Wait JS thread..."); }, - TaskExecutor::TaskType::JS, "ArkUIDialogWaitLog"); - container->DestroyView(); // Stop all threads(ui,gpu,io) for current ability. - taskExecutor->PostTask( - [instanceId, destroyCallback] { - TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer DestroyContainer Remove on Platform thread..."); - EngineHelper::RemoveEngine(instanceId); - AceEngine::Get().RemoveContainer(instanceId); - CHECK_NULL_VOID(destroyCallback); - destroyCallback(); - }, - TaskExecutor::TaskType::PLATFORM, "ArkUIDialogContainerDestroy"); -} - -void DialogContainer::Destroy() -{ - TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer Destroy begin"); - ContainerScope scope(instanceId_); - if (pipelineContext_ && taskExecutor_) { - // 1. Destroy Pipeline on UI thread. - RefPtr& context = pipelineContext_; - if (GetSettings().usePlatformAsUIThread) { - context->Destroy(); - } else { - taskExecutor_->PostTask([context]() { context->Destroy(); }, - TaskExecutor::TaskType::UI, "ArkUIDialogDestoryPipeline"); - } - // 2. Destroy Frontend on JS thread. - RefPtr& frontend = frontend_; - if (GetSettings().usePlatformAsUIThread && GetSettings().useUIAsJSThread) { - frontend->UpdateState(Frontend::State::ON_DESTROY); - frontend->Destroy(); - } else { - taskExecutor_->PostTask( - [frontend]() { - frontend->UpdateState(Frontend::State::ON_DESTROY); - frontend->Destroy(); - }, - TaskExecutor::TaskType::JS, "ArkUIDialogFrontendDestroy"); - } - } - DestroyToastSubwindow(instanceId_); - DestroySelectOverlaySubwindow(instanceId_); - resRegister_.Reset(); - assetManager_.Reset(); -} - -void DialogContainer::DestroyView() -{ - TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer DestroyView begin"); - ContainerScope scope(instanceId_); - std::lock_guard lock(viewMutex_); - aceView_ = nullptr; -} - -void DialogContainer::SetView( - const RefPtr& view, double density, int32_t width, int32_t height, sptr& rsWindow) -{ - CHECK_NULL_VOID(view); - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(view->GetInstanceId())); - CHECK_NULL_VOID(container); -#ifdef ENABLE_ROSEN_BACKEND - auto taskExecutor = container->GetTaskExecutor(); - CHECK_NULL_VOID(taskExecutor); - - auto window = std::make_shared(rsWindow, taskExecutor, view->GetInstanceId()); -#else - auto platformWindow = PlatformWindow::Create(view); - CHECK_NULL_VOID(platformWindow); - auto window = std::make_shared(std::move(platformWindow)); -#endif - container->AttachView(std::move(window), view, density, width, height, rsWindow->GetWindowId()); -} - -void DialogContainer::SetViewNew( - const RefPtr& view, double density, int32_t width, int32_t height, sptr& rsWindow) -{ -#ifdef ENABLE_ROSEN_BACKEND - CHECK_NULL_VOID(view); - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(view->GetInstanceId())); - CHECK_NULL_VOID(container); - auto taskExecutor = container->GetTaskExecutor(); - CHECK_NULL_VOID(taskExecutor); - - auto window = std::make_shared(rsWindow, taskExecutor, view->GetInstanceId()); - container->AttachView(std::move(window), view, density, width, height, rsWindow->GetWindowId()); -#endif -} - -void DialogContainer::AttachView(std::shared_ptr window, const RefPtr& view, double density, - int32_t width, int32_t height, uint32_t windowId) -{ - aceView_ = view; - auto instanceId = aceView_->GetInstanceId(); - auto taskExecutorImpl = AceType::DynamicCast(taskExecutor_); - auto aceView = AceType::DynamicCast(aceView_); - ACE_DCHECK(aceView != nullptr); - taskExecutorImpl->InitOtherThreads(aceView->GetThreadModelImpl()); - ContainerScope scope(instanceId); - // For DECLARATIVE_JS frontend display UI in JS thread temporarily. - taskExecutorImpl->InitJsThread(false); - InitializeFrontend(); - SetUseNewPipeline(); - - InitPipelineContext(std::move(window), instanceId, density, width, height, windowId); - InitializeCallback(); - CheckAndSetFontFamily(); - - taskExecutor_->PostTask([] { FrameReport::GetInstance().Init(); }, - TaskExecutor::TaskType::UI, "ArkUIDialogFrameReportInit"); - ThemeConstants::InitDeviceType(); - // Load custom style at UI thread before frontend attach, to make sure style can be loaded before building dom tree. - RefPtr themeManager = nullptr; - if (SystemProperties::GetResourceDecoupling()) { - auto resAdapter = ResourceAdapter::CreateV2(); - themeManager = AceType::MakeRefPtr(resAdapter); - } else { - themeManager = AceType::MakeRefPtr(); - } - if (themeManager) { - pipelineContext_->SetThemeManager(themeManager); - // Init resource - themeManager->InitResource(resourceInfo_); - taskExecutor_->PostTask( - [themeManager, assetManager = assetManager_, colorScheme = colorScheme_] { - ACE_SCOPED_TRACE("OHOS::LoadThemes()"); - TAG_LOGI(AceLogTag::ACE_DIALOG, "UIContent load theme"); - themeManager->SetColorScheme(colorScheme); - themeManager->LoadCustomTheme(assetManager); - themeManager->LoadResourceThemes(); - }, - TaskExecutor::TaskType::UI, "ArkUIDialogLoadTheme"); - } - aceView_->Launch(); - // Only MainWindow instance will be registered to watch dog. - frontend_->AttachPipelineContext(pipelineContext_); -#if defined(ENABLE_ROSEN_BACKEND) and !defined(UPLOAD_GPU_DISABLED) - pipelineContext_->SetPostRTTaskCallBack([](std::function&& task) { - auto syncTask = std::make_shared(std::move(task)); - Rosen::RSTransactionProxy::GetInstance()->ExecuteSynchronousTask(syncTask); - }); -#endif -} - -void DialogContainer::InitPipelineContext(std::shared_ptr window, int32_t instanceId, double density, - int32_t width, int32_t height, uint32_t windowId) -{ -#ifdef NG_BUILD - TAG_LOGI(AceLogTag::ACE_DIALOG, "New pipeline version creating..."); - pipelineContext_ = AceType::MakeRefPtr( - std::move(window), taskExecutor_, assetManager_, resRegister_, frontend_, instanceId); -#else - if (useNewPipeline_) { - TAG_LOGI(AceLogTag::ACE_DIALOG, "New pipeline version creating..."); - pipelineContext_ = AceType::MakeRefPtr( - std::move(window), taskExecutor_, assetManager_, resRegister_, frontend_, instanceId); - } else { - pipelineContext_ = AceType::MakeRefPtr( - std::move(window), taskExecutor_, assetManager_, resRegister_, frontend_, instanceId); - } -#endif - pipelineContext_->SetRootSize(density, width, height); - pipelineContext_->SetTextFieldManager(AceType::MakeRefPtr()); - pipelineContext_->SetIsRightToLeft(AceApplicationInfo::GetInstance().IsRightToLeft()); - pipelineContext_->SetWindowId(windowId); - pipelineContext_->SetWindowModal(windowModal_); - pipelineContext_->SetDrawDelegate(aceView_->GetDrawDelegate()); - pipelineContext_->SetIsSubPipeline(true); -} - -void DialogContainer::InitializeFrontend() -{ - frontend_ = AceType::MakeRefPtr(); - CHECK_NULL_VOID(frontend_); - frontend_->Initialize(type_, taskExecutor_); - auto front = GetFrontend(); - CHECK_NULL_VOID(front); - front->UpdateState(Frontend::State::ON_CREATE); - front->SetJsMessageDispatcher(AceType::Claim(this)); - front->SetAssetManager(assetManager_); -} - -void DialogContainer::DumpHeapSnapshot(bool isPrivate) -{ - taskExecutor_->PostTask( - [isPrivate, frontend = WeakPtr(frontend_)] { - auto sp = frontend.Upgrade(); - CHECK_NULL_VOID(sp); - sp->DumpHeapSnapshot(isPrivate); - }, - TaskExecutor::TaskType::JS, "ArkUIDialogDumpHeapSnapshot"); -} -void DialogContainer::SetUIWindow(int32_t instanceId, sptr& uiWindow) -{ - CHECK_NULL_VOID(uiWindow); - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); - CHECK_NULL_VOID(container); - container->SetUIWindowInner(uiWindow); -} - -sptr DialogContainer::GetUIWindow(int32_t instanceId) -{ - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); - CHECK_NULL_RETURN(container, nullptr); - return container->GetUIWindowInner(); -} - -void DialogContainer::SetUIWindowInner(sptr uiWindow) -{ - uiWindow_ = std::move(uiWindow); -} - -sptr DialogContainer::GetUIWindowInner() const -{ - return uiWindow_; -} +DialogContainer::DialogContainer(int32_t instanceId, FrontendType type) : AceContainer(instanceId, type) +{} void DialogContainer::ShowToast(int32_t instanceId, const std::string& message, int32_t duration, const std::string& bottom, std::function&& callback) { - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); + auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); CHECK_NULL_VOID(container); auto frontend = AceType::DynamicCast(container->GetFrontend()); CHECK_NULL_VOID(frontend); @@ -486,7 +52,7 @@ void DialogContainer::ShowToast(int32_t instanceId, const std::string& message, CHECK_NULL_VOID(delegate); delegate->SetToastStopListenerCallback([instanceId = instanceId]() { if (ContainerScope::CurrentId() >= 0) { - DialogContainer::HideWindow(instanceId); + AceContainer::HideWindow(instanceId); } }); auto toastInfo = NG::ToastInfo { .message = message, @@ -500,7 +66,7 @@ void DialogContainer::ShowToast(int32_t instanceId, const std::string& message, void DialogContainer::CloseToast(int32_t instanceId, int32_t toastId, std::function&& callback) { - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); + auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); CHECK_NULL_VOID(container); auto frontend = AceType::DynamicCast(container->GetFrontend()); @@ -510,7 +76,7 @@ void DialogContainer::CloseToast(int32_t instanceId, int32_t toastId, std::funct CHECK_NULL_VOID(delegate); delegate->SetToastStopListenerCallback([instanceId = instanceId]() { if (ContainerScope::CurrentId() >= 0) { - DialogContainer::HideWindow(instanceId); + AceContainer::HideWindow(instanceId); } }); @@ -522,7 +88,7 @@ void DialogContainer::ShowDialog(int32_t instanceId, const std::string& title, c const std::set& callbacks) { TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog begin"); - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); + auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); CHECK_NULL_VOID(container); auto frontend = AceType::DynamicCast(container->GetFrontend()); CHECK_NULL_VOID(frontend); @@ -533,7 +99,7 @@ void DialogContainer::ShowDialog(int32_t instanceId, const std::string& title, c TAG_LOGI( AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog HideWindow instanceId = %{public}d", instanceId); if (!isShow) { - DialogContainer::HideWindow(instanceId); + AceContainer::HideWindow(instanceId); } }); } @@ -543,7 +109,7 @@ void DialogContainer::ShowDialog(int32_t instanceId, const PromptDialogAttr& dia const std::set& callbacks) { TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog with attr begin"); - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); + auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); CHECK_NULL_VOID(container); auto frontend = AceType::DynamicCast(container->GetFrontend()); CHECK_NULL_VOID(frontend); @@ -552,7 +118,7 @@ void DialogContainer::ShowDialog(int32_t instanceId, const PromptDialogAttr& dia delegate->ShowDialog(dialogAttr, buttons, std::move(callback), callbacks, [instanceId = instanceId](bool isShow) { TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog HideWindow instanceId = %{public}d", instanceId); if (!isShow) { - DialogContainer::HideWindow(instanceId); + AceContainer::HideWindow(instanceId); } }); } @@ -560,7 +126,7 @@ void DialogContainer::ShowDialog(int32_t instanceId, const PromptDialogAttr& dia void DialogContainer::ShowActionMenu(int32_t instanceId, const std::string& title, const std::vector& button, std::function&& callback) { - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); + auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); CHECK_NULL_VOID(container); auto frontend = AceType::DynamicCast(container->GetFrontend()); CHECK_NULL_VOID(frontend); @@ -568,7 +134,7 @@ void DialogContainer::ShowActionMenu(int32_t instanceId, const std::string& titl CHECK_NULL_VOID(delegate); delegate->ShowActionMenu(title, button, std::move(callback), [instanceId = instanceId](bool isShow) { if (!isShow) { - DialogContainer::HideWindow(instanceId); + AceContainer::HideWindow(instanceId); } }); } @@ -577,7 +143,7 @@ bool DialogContainer::ShowToastDialogWindow( int32_t instanceId, int32_t posX, int32_t posY, int32_t width, int32_t height, bool isToast) { TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowToastDialogWindow begin"); - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); + auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); CHECK_NULL_RETURN(container, false); auto window = container->GetUIWindowInner(); CHECK_NULL_RETURN(window, false); @@ -607,113 +173,10 @@ bool DialogContainer::ShowToastDialogWindow( return true; } -bool DialogContainer::HideWindow(int32_t instanceId) -{ - TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer HideWindow begin"); - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); - CHECK_NULL_RETURN(container, false); - auto window = container->GetUIWindowInner(); - CHECK_NULL_RETURN(window, false); - OHOS::Rosen::WMError ret = window->Hide(); - if (ret != OHOS::Rosen::WMError::WM_OK) { - TAG_LOGE(AceLogTag::ACE_DIALOG, "DialogContainer HideWindow Failed to hide the window."); - return false; - } - sptr uiWindow = nullptr; - DialogContainer::SetUIWindow(instanceId, uiWindow); - return true; -} - -bool DialogContainer::CloseWindow(int32_t instanceId) -{ - TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer CloseWindow begin"); - auto container = AceType::DynamicCast(AceEngine::Get().GetContainer(instanceId)); - CHECK_NULL_RETURN(container, false); - auto window = container->GetUIWindowInner(); - CHECK_NULL_RETURN(window, false); - OHOS::Rosen::WMError ret = window->Close(); - if (ret != OHOS::Rosen::WMError::WM_OK) { - TAG_LOGE(AceLogTag::ACE_DIALOG, "DialogContainer CloseWindow Failed to close the window."); - return false; - } - sptr uiWindow = nullptr; - DialogContainer::SetUIWindow(instanceId, uiWindow); - return true; -} bool DialogContainer::OnBackPressed(int32_t instanceId) { - return DialogContainer::CloseWindow(instanceId); + return AceContainer::CloseWindow(instanceId); } -void DialogContainer::SetFontScaleAndWeightScale(int32_t instanceId) -{ - float fontScale = SystemProperties::GetFontScale(); - float fontWeightScale = SystemProperties::GetFontWeightScale(); - Container::SetFontScale(instanceId, fontScale); - Container::SetFontWeightScale(instanceId, fontWeightScale); -} - -void DialogContainer::UpdateConfiguration(const ParsedConfig& parsedConfig) -{ - if (!parsedConfig.IsValid()) { - LOGW("DialogContainer::OnConfigurationUpdated param is empty"); - return; - } - - CHECK_NULL_VOID(pipelineContext_); - auto themeManager = pipelineContext_->GetThemeManager(); - CHECK_NULL_VOID(themeManager); - auto resConfig = GetResourceConfiguration(); - if (!parsedConfig.colorMode.empty()) { - if (parsedConfig.colorMode == "dark") { - SystemProperties::SetColorMode(ColorMode::DARK); - resConfig.SetColorMode(ColorMode::DARK); - } else { - SystemProperties::SetColorMode(ColorMode::LIGHT); - resConfig.SetColorMode(ColorMode::LIGHT); - } - } - - SetResourceConfiguration(resConfig); - themeManager->UpdateConfig(resConfig); - themeManager->LoadResourceThemes(); - // change color mode and theme to clear image cache - pipelineContext_->ClearImageCache(); -} - -void DialogContainer::CheckAndSetFontFamily() -{ - CHECK_NULL_VOID(pipelineContext_); - auto fontManager = pipelineContext_->GetFontManager(); - CHECK_NULL_VOID(fontManager); - if (fontManager->IsUseAppCustomFont()) { - return; - } - std::string familyName = ""; - std::string path = "/data/themes/a/app"; - if (!IsFontFileExistInPath(path)) { - path = "/data/themes/b/app"; - if (!IsFontFileExistInPath(path)) { - return; - } - } - path = path.append("/fonts/"); - familyName = GetFontFamilyName(path); - if (familyName.empty()) { - return; - } - path = path.append(familyName); - fontManager->SetFontFamily(familyName.c_str(), path.c_str()); -} - -Rect DialogContainer::GetDisplayAvailableRect() const -{ - if (!uiWindow_) { - TAG_LOGW(AceLogTag::ACE_WINDOW, "uiwindow is null, can't get displayId"); - return Rect(); - } - - return displayManager_->GetDisplayAvailableRect(uiWindow_->GetDisplayId()); -} } // namespace OHOS::Ace::Platform diff --git a/adapter/ohos/entrance/dialog_container.h b/adapter/ohos/entrance/dialog_container.h index 45e8fdb9055..34a63c07318 100644 --- a/adapter/ohos/entrance/dialog_container.h +++ b/adapter/ohos/entrance/dialog_container.h @@ -31,131 +31,13 @@ #include "core/components_ng/render/adapter/rosen_window.h" namespace OHOS::Ace::Platform { -class DialogContainer : public Container, public JsMessageDispatcher { - DECLARE_ACE_TYPE(DialogContainer, Container, JsMessageDispatcher); +class DialogContainer : public AceContainer { + DECLARE_ACE_TYPE(DialogContainer, AceContainer); public: explicit DialogContainer(int32_t instanceId, FrontendType type = FrontendType::DECLARATIVE_JS); ~DialogContainer() override = default; - void Initialize() override {}; - void Destroy() override; - void DestroyView() override; - - int32_t GetInstanceId() const override - { - if (aceView_) { - return aceView_->GetInstanceId(); - } - return -1; - } - - RefPtr GetFrontend() const override - { - return frontend_; - } - - ResourceConfiguration GetResourceConfiguration() const override - { - return resourceInfo_.GetResourceConfiguration(); - } - - void SetResourceConfiguration(const ResourceConfiguration& config) - { - resourceInfo_.SetResourceConfiguration(config); - } - - RefPtr GetPlatformResRegister() const override - { - return resRegister_; - } - - RefPtr GetPipelineContext() const override - { - return pipelineContext_; - } - - int32_t GetViewPosX() const override - { - return aceView_ ? aceView_->GetPosX() : 0; - } - - int32_t GetViewPosY() const override - { - return aceView_ ? aceView_->GetPosY() : 0; - } - - void SetWindowId(uint32_t windowId) override - { - windowId_ = windowId; - } - - uint32_t GetWindowId() const override - { - return windowId_; - } - - int32_t GetViewWidth() const override - { - return aceView_ ? aceView_->GetWidth() : 0; - } - - int32_t GetViewHeight() const override - { - return aceView_ ? aceView_->GetHeight() : 0; - } - - RefPtr GetAceView() const override - { - std::lock_guard lock(viewMutex_); - return aceView_; - } - - void* GetView() const override - { - std::lock_guard lock(viewMutex_); - return static_cast(AceType::RawPtr(aceView_)); - } - - RefPtr GetTaskExecutor() const override - { - return taskExecutor_; - } - - void Dispatch( - const std::string& group, std::vector&& data, int32_t id, bool replyToComponent) const override {}; - - void DispatchPluginError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override {}; - - void DispatchSync( - const std::string& group, std::vector&& data, uint8_t** resData, int64_t& position) const override - {} - - std::string GetHostClassName() const override - { - return ""; - } - - void DumpHeapSnapshot(bool isPrivate) override; - - void SetAssetManager(const RefPtr& assetManager) - { - assetManager_ = assetManager; - if (frontend_) { - frontend_->SetAssetManager(assetManager); - } - } - - RefPtr GetAssetManager() const override - { - return assetManager_; - } - - bool IsSubContainer() const override - { - return true; - } - bool IsDialogContainer() const override { return true; @@ -175,62 +57,10 @@ public: static bool ShowToastDialogWindow( int32_t instanceId, int32_t posX, int32_t posY, int32_t width, int32_t height, bool isToast = false); - static bool CloseWindow(int32_t instanceId); - static bool HideWindow(int32_t instanceId); - static void SetUIWindow(int32_t instanceId, sptr& uiWindow); - static sptr GetUIWindow(int32_t instanceId); - - static void DestroyContainer(int32_t instanceId, const std::function& destroyCallback = nullptr); - static RefPtr GetContainer(int32_t instanceId); - static void SetView(const RefPtr& view, double density, int32_t width, int32_t height, - sptr& rsWindow); - static void SetViewNew(const RefPtr& view, double density, int32_t width, int32_t height, - sptr& rsWindow); static bool OnBackPressed(int32_t instanceId); - void SetFontScaleAndWeightScale(int32_t instanceId); - void UpdateConfiguration(const ParsedConfig& parsedConfig); - void CheckAndSetFontFamily() override; - Rect GetDisplayAvailableRect() const override; - private: - void InitPipelineContext(std::shared_ptr window, int32_t instanceId, double density, int32_t width, - int32_t height, uint32_t windowId); - void InitializeFrontend(); - void InitializeCallback(); - void InitializeTouchEventCallback(); - void InitializeMouseEventCallback(); - void InitializeAxisEventCallback(); - void InitializeKeyEventCallback(); - void InitializeRotationEventCallback(); - void InitializeViewChangeCallback(); - void InitializeDensityChangeCallback(); - void InitializeSystemBarHeightChangeCallback(); - void InitializeSurfaceDestroyCallback(); - void InitializeDragEventCallback(); - void InitializeCrownEventCallback(); - void AttachView(std::shared_ptr window, const RefPtr& view, double density, int32_t width, - int32_t height, uint32_t windowId); - void SetUIWindowInner(sptr uiWindow); - sptr GetUIWindowInner() const; - - uint32_t windowId_ = OHOS::Rosen::INVALID_WINDOW_ID; - int32_t instanceId_ = -1; - RefPtr aceView_; - RefPtr taskExecutor_; - RefPtr assetManager_; - RefPtr resRegister_; - RefPtr pipelineContext_; - RefPtr frontend_; - FrontendType type_ = FrontendType::DECLARATIVE_JS; - ResourceInfo resourceInfo_; - sptr uiWindow_ = nullptr; - std::string windowName_; - WindowModal windowModal_ { WindowModal::NORMAL }; - ColorScheme colorScheme_ { ColorScheme::FIRST_VALUE }; - mutable std::mutex viewMutex_; - ACE_DISALLOW_COPY_AND_MOVE(DialogContainer); }; diff --git a/adapter/ohos/entrance/subwindow/subwindow_ohos.cpp b/adapter/ohos/entrance/subwindow/subwindow_ohos.cpp index ca04725be52..967aa9be211 100644 --- a/adapter/ohos/entrance/subwindow/subwindow_ohos.cpp +++ b/adapter/ohos/entrance/subwindow/subwindow_ohos.cpp @@ -442,7 +442,7 @@ std::function SubwindowOhos::GetInitToastDelayTask(const NG::ToastInfo& "update ace view width : %{public}d, height : %{public}d, density : %{public}f,childContainerId : " "%{public}d", width, height, density, childContainerId); - auto container = Platform::DialogContainer::GetContainer(childContainerId); + auto container = Platform::AceContainer::GetContainer(childContainerId); CHECK_NULL_VOID(container); container->SetFontScaleAndWeightScale(childContainerId); auto ret = subwindowOhos->InitToastServiceConfig(); @@ -1268,10 +1268,10 @@ void SubwindowOhos::HideSubWindowNG() CHECK_NULL_VOID(container); if (container->IsDialogContainer()) { if (IsToastWindow()) { - Platform::DialogContainer::HideWindow(Container::CurrentId()); + Platform::AceContainer::HideWindow(Container::CurrentId()); } else { - Platform::DialogContainer::CloseWindow(Container::CurrentId()); - Platform::DialogContainer::DestroyContainer(Container::CurrentId()); + Platform::AceContainer::CloseWindow(Container::CurrentId()); + Platform::AceContainer::DestroyContainer(Container::CurrentId()); } } else { auto context = container->GetPipelineContext(); @@ -1361,9 +1361,9 @@ bool SubwindowOhos::InitToastDialogView(int32_t width, int32_t height, float den } #endif - auto pipelineContext = container->GetPipelineContext(); + auto pipelineContext = DynamicCast(container->GetPipelineContext()); CHECK_NULL_RETURN(pipelineContext, false); - pipelineContext->SetupRootElement(); + pipelineContext->SetupSubRootElement(); auto parentContainer = Platform::AceContainer::GetContainer(parentContainerId_); if (parentContainer) { auto parentPipeline = parentContainer->GetPipelineContext(); @@ -1590,7 +1590,7 @@ void SubwindowOhos::ShowDialogForService(const std::string& title, const std::st } auto childContainerId = subwindowOhos->GetChildContainerId(); ContainerScope scope(childContainerId); - auto container = Platform::DialogContainer::GetContainer(childContainerId); + auto container = Platform::AceContainer::GetContainer(childContainerId); CHECK_NULL_VOID(container); container->SetFontScaleAndWeightScale(childContainerId); Platform::DialogContainer::ShowToastDialogWindow(childContainerId, posX, posY, width, height); @@ -1808,7 +1808,7 @@ void SubwindowOhos::ShowActionMenuForService( } auto childContainerId = subwindowOhos->GetChildContainerId(); ContainerScope scope(childContainerId); - auto container = Platform::DialogContainer::GetContainer(childContainerId); + auto container = Platform::AceContainer::GetContainer(childContainerId); CHECK_NULL_VOID(container); container->SetFontScaleAndWeightScale(childContainerId); Platform::DialogContainer::ShowToastDialogWindow(childContainerId, posX, posY, width, height); diff --git a/adapter/ohos/entrance/ui_content_impl.cpp b/adapter/ohos/entrance/ui_content_impl.cpp index ec1840e7fbc..0d914e06caa 100644 --- a/adapter/ohos/entrance/ui_content_impl.cpp +++ b/adapter/ohos/entrance/ui_content_impl.cpp @@ -2764,13 +2764,6 @@ void BuildParsedConfig(Platform::ParsedConfig& parsedConfig, void UIContentImpl::UpdateConfiguration(const std::shared_ptr& config) { CHECK_NULL_VOID(config); - - auto dialogContainer = Platform::DialogContainer::GetContainer(instanceId_); - if (dialogContainer) { - UpdateDialogContainerConfig(config); - return; - } - StoreConfiguration(config); auto container = Platform::AceContainer::GetContainer(instanceId_); CHECK_NULL_VOID(container); @@ -4486,29 +4479,6 @@ void UIContentImpl::SetForceSplitEnable(bool isForceSplit, const std::string& ho context->SetForceSplitEnable(isForceSplit, homePage); } -void UIContentImpl::UpdateDialogContainerConfig(const std::shared_ptr& config) -{ - CHECK_NULL_VOID(config); - auto container = Platform::DialogContainer::GetContainer(instanceId_); - CHECK_NULL_VOID(container); - auto taskExecutor = container->GetTaskExecutor(); - CHECK_NULL_VOID(taskExecutor); - taskExecutor->PostTask( - [weakContainer = WeakPtr(container), config, instanceId = instanceId_, - bundleName = bundleName_, moduleName = moduleName_]() { - auto container = weakContainer.Upgrade(); - CHECK_NULL_VOID(container); - Platform::ParsedConfig parsedConfig; - parsedConfig.colorMode = config->GetItem(OHOS::AppExecFwk::GlobalConfigurationKey::SYSTEM_COLORMODE); - container->UpdateConfiguration(parsedConfig); - TAG_LOGI(AceLogTag::ACE_DIALOG, - "[%{public}d][%{public}s][%{public}s] UIContentImpl: UpdateDialogContainerConfig called End, " - "name:%{public}s", - instanceId, bundleName.c_str(), moduleName.c_str(), config->GetName().c_str()); - }, - TaskExecutor::TaskType::UI, "ArkUIUIContentUpdateConfiguration"); -} - void UIContentImpl::ProcessDestructCallbacks() { for (auto& [_, callback] : destructCallbacks_) { diff --git a/adapter/ohos/entrance/ui_content_impl.h b/adapter/ohos/entrance/ui_content_impl.h index 3bb31a938dc..01fa8cbeb55 100644 --- a/adapter/ohos/entrance/ui_content_impl.h +++ b/adapter/ohos/entrance/ui_content_impl.h @@ -365,8 +365,6 @@ public: void SetForceSplitEnable(bool isForceSplit, const std::string& homePage) override; - void UpdateDialogContainerConfig(const std::shared_ptr& config); - void AddDestructCallback(void* key, const std::function& callback) { destructCallbacks_.emplace(key, callback); -- Gitee