• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "adapter/ohos/entrance/dialog_container.h"
17 
18 #include "adapter/ohos/entrance/ace_application_info.h"
19 #if defined(ENABLE_ROSEN_BACKEND) and !defined(UPLOAD_GPU_DISABLED)
20 #include "adapter/ohos/entrance/ace_rosen_sync_task.h"
21 #endif
22 #include "adapter/ohos/entrance/ace_view_ohos.h"
23 #include "base/log/frame_report.h"
24 #include "base/log/log.h"
25 #include "base/utils/utils.h"
26 #include "core/common/ace_engine.h"
27 #include "core/common/container_scope.h"
28 #include "core/common/flutter/flutter_task_executor.h"
29 #include "core/common/text_field_manager.h"
30 #include "core/components/theme/theme_constants.h"
31 #include "core/components/theme/theme_manager_impl.h"
32 #include "core/pipeline/pipeline_context.h"
33 #include "core/pipeline_ng/pipeline_context.h"
34 #include "frameworks/base/subwindow/subwindow_manager.h"
35 #include "frameworks/bridge/common/utils/engine_helper.h"
36 #include "frameworks/bridge/declarative_frontend/declarative_frontend.h"
37 
38 namespace OHOS::Ace::Platform {
DialogContainer(int32_t instanceId,FrontendType type)39 DialogContainer::DialogContainer(int32_t instanceId, FrontendType type) : instanceId_(instanceId), type_(type)
40 {
41     auto flutterTaskExecutor = Referenced::MakeRefPtr<FlutterTaskExecutor>();
42     flutterTaskExecutor->InitPlatformThread(true);
43     taskExecutor_ = flutterTaskExecutor;
44     GetSettings().useUIAsJSThread = true;
45     GetSettings().usePlatformAsUIThread = true;
46     GetSettings().usingSharedRuntime = true;
47 }
48 
InitializeTouchEventCallback()49 void DialogContainer::InitializeTouchEventCallback()
50 {
51     ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_);
52     auto&& touchEventCallback = [context = pipelineContext_, id = instanceId_](
53                                     const TouchEvent& event, const std::function<void()>& markProcess) {
54         ContainerScope scope(id);
55         context->GetTaskExecutor()->PostTask(
56             [context, event, markProcess, id]() {
57                 context->OnTouchEvent(event);
58                 context->NotifyDispatchTouchEventDismiss(event);
59                 CHECK_NULL_VOID_NOLOG(markProcess);
60                 markProcess();
61             },
62             TaskExecutor::TaskType::UI);
63     };
64     aceView_->RegisterTouchEventCallback(touchEventCallback);
65 }
66 
InitializeMouseEventCallback()67 void DialogContainer::InitializeMouseEventCallback()
68 {
69     ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_);
70     auto&& mouseEventCallback = [context = pipelineContext_, id = instanceId_](
71                                     const MouseEvent& event, const std::function<void()>& markProcess) {
72         ContainerScope scope(id);
73         context->GetTaskExecutor()->PostTask(
74             [context, event, markProcess, id]() {
75                 context->OnMouseEvent(event);
76                 CHECK_NULL_VOID_NOLOG(markProcess);
77                 markProcess();
78             },
79             TaskExecutor::TaskType::UI);
80     };
81     aceView_->RegisterMouseEventCallback(mouseEventCallback);
82 }
83 
InitializeAxisEventCallback()84 void DialogContainer::InitializeAxisEventCallback()
85 {
86     ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_);
87     auto&& axisEventCallback = [context = pipelineContext_, id = instanceId_](
88                                    const AxisEvent& event, const std::function<void()>& markProcess) {
89         ContainerScope scope(id);
90         context->GetTaskExecutor()->PostTask(
91             [context, event, markProcess, id]() {
92                 context->OnAxisEvent(event);
93                 CHECK_NULL_VOID_NOLOG(markProcess);
94                 markProcess();
95             },
96             TaskExecutor::TaskType::UI);
97     };
98     aceView_->RegisterAxisEventCallback(axisEventCallback);
99 }
100 
InitializeKeyEventCallback()101 void DialogContainer::InitializeKeyEventCallback()
102 {
103     ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_);
104     auto&& keyEventCallback = [context = pipelineContext_, id = instanceId_](const KeyEvent& event) {
105         ContainerScope scope(id);
106         bool result = false;
107         context->GetTaskExecutor()->PostSyncTask(
108             [context, event, &result]() { result = context->OnKeyEvent(event); }, TaskExecutor::TaskType::UI);
109         return result;
110     };
111     aceView_->RegisterKeyEventCallback(keyEventCallback);
112 }
113 
InitializeRotationEventCallback()114 void DialogContainer::InitializeRotationEventCallback()
115 {
116     ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_);
117     auto&& rotationEventCallback = [context = pipelineContext_, id = instanceId_](const RotationEvent& event) {
118         ContainerScope scope(id);
119         bool result = false;
120         context->GetTaskExecutor()->PostSyncTask(
121             [context, event, &result]() { result = context->OnRotationEvent(event); }, TaskExecutor::TaskType::UI);
122         return result;
123     };
124     aceView_->RegisterRotationEventCallback(rotationEventCallback);
125 }
126 
InitializeViewChangeCallback()127 void DialogContainer::InitializeViewChangeCallback()
128 {
129     ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_);
130     auto&& viewChangeCallback = [context = pipelineContext_, id = instanceId_](int32_t width, int32_t height,
131                                     WindowSizeChangeReason type,
132                                     const std::shared_ptr<Rosen::RSTransaction>& rsTransaction) {
133         ContainerScope scope(id);
134         ACE_SCOPED_TRACE("ViewChangeCallback(%d, %d)", width, height);
135         context->GetTaskExecutor()->PostTask(
136             [context, width, height, type, rsTransaction]() {
137                 context->OnSurfaceChanged(width, height, type, rsTransaction);
138             },
139             TaskExecutor::TaskType::UI);
140     };
141     aceView_->RegisterViewChangeCallback(viewChangeCallback);
142 }
143 
InitializeDensityChangeCallback()144 void DialogContainer::InitializeDensityChangeCallback()
145 {
146     ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_);
147     auto&& densityChangeCallback = [context = pipelineContext_, id = instanceId_](double density) {
148         ContainerScope scope(id);
149         ACE_SCOPED_TRACE("DensityChangeCallback(%lf)", density);
150         context->GetTaskExecutor()->PostTask(
151             [context, density]() { context->OnSurfaceDensityChanged(density); }, TaskExecutor::TaskType::UI);
152     };
153     aceView_->RegisterDensityChangeCallback(densityChangeCallback);
154 }
155 
InitializeSystemBarHeightChangeCallback()156 void DialogContainer::InitializeSystemBarHeightChangeCallback()
157 {
158     ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_);
159     auto&& systemBarHeightChangeCallback = [context = pipelineContext_, id = instanceId_](
160                                                double statusBar, double navigationBar) {
161         ContainerScope scope(id);
162         ACE_SCOPED_TRACE("SystemBarHeightChangeCallback(%lf, %lf)", statusBar, navigationBar);
163         context->GetTaskExecutor()->PostTask(
164             [context, statusBar, navigationBar]() { context->OnSystemBarHeightChanged(statusBar, navigationBar); },
165             TaskExecutor::TaskType::UI);
166     };
167     aceView_->RegisterSystemBarHeightChangeCallback(systemBarHeightChangeCallback);
168 }
169 
InitializeSurfaceDestroyCallback()170 void DialogContainer::InitializeSurfaceDestroyCallback()
171 {
172     ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_);
173     auto&& surfaceDestroyCallback = [context = pipelineContext_, id = instanceId_]() {
174         ContainerScope scope(id);
175         context->GetTaskExecutor()->PostTask(
176             [context]() { context->OnSurfaceDestroyed(); }, TaskExecutor::TaskType::UI);
177     };
178     aceView_->RegisterSurfaceDestroyCallback(surfaceDestroyCallback);
179 }
180 
InitializeDragEventCallback()181 void DialogContainer::InitializeDragEventCallback()
182 {
183     ACE_DCHECK(aceView_ && taskExecutor_ && pipelineContext_);
184     auto&& dragEventCallback = [context = pipelineContext_, id = instanceId_](
185                                    int32_t x, int32_t y, const DragEventAction& action) {
186         ContainerScope scope(id);
187         context->GetTaskExecutor()->PostTask(
188             [context, x, y, action]() { context->OnDragEvent(x, y, action); }, TaskExecutor::TaskType::UI);
189     };
190     aceView_->RegisterDragEventCallback(dragEventCallback);
191 }
192 
InitializeCallback()193 void DialogContainer::InitializeCallback()
194 {
195     ACE_FUNCTION_TRACE();
196     InitializeTouchEventCallback();
197     InitializeMouseEventCallback();
198     InitializeAxisEventCallback();
199     InitializeKeyEventCallback();
200     InitializeRotationEventCallback();
201     InitializeViewChangeCallback();
202     InitializeDensityChangeCallback();
203     InitializeSystemBarHeightChangeCallback();
204     InitializeSurfaceDestroyCallback();
205     InitializeDragEventCallback();
206 }
207 
GetContainer(int32_t instanceId)208 RefPtr<DialogContainer> DialogContainer::GetContainer(int32_t instanceId)
209 {
210     auto container = AceEngine::Get().GetContainer(instanceId);
211     CHECK_NULL_RETURN_NOLOG(container, nullptr);
212     auto dialogContainer = AceType::DynamicCast<DialogContainer>(container);
213     return dialogContainer;
214 }
215 
DestroyContainer(int32_t instanceId,const std::function<void ()> & destroyCallback)216 void DialogContainer::DestroyContainer(int32_t instanceId, const std::function<void()>& destroyCallback)
217 {
218     LOGI("DialogContainer::DestroyContainer begin %{public}d", instanceId);
219     auto container = AceEngine::Get().GetContainer(instanceId);
220     CHECK_NULL_VOID(container);
221     container->Destroy();
222     auto taskExecutor = container->GetTaskExecutor();
223     CHECK_NULL_VOID(taskExecutor);
224     taskExecutor->PostSyncTask([] { LOGI("Wait UI thread..."); }, TaskExecutor::TaskType::UI);
225     taskExecutor->PostSyncTask([] { LOGI("Wait JS thread..."); }, TaskExecutor::TaskType::JS);
226     container->DestroyView(); // Stop all threads(ui,gpu,io) for current ability.
227     taskExecutor->PostTask(
228         [instanceId, destroyCallback] {
229             LOGI("DialogContainer::DestroyContainer Remove on Platform thread...");
230             EngineHelper::RemoveEngine(instanceId);
231             AceEngine::Get().RemoveContainer(instanceId);
232             CHECK_NULL_VOID_NOLOG(destroyCallback);
233             destroyCallback();
234         },
235         TaskExecutor::TaskType::PLATFORM);
236     LOGI("DialogContainer::DestroyContainer end");
237 }
238 
Destroy()239 void DialogContainer::Destroy()
240 {
241     LOGI("DialogContainer::Destroy begin");
242     ContainerScope scope(instanceId_);
243     if (pipelineContext_ && taskExecutor_) {
244         // 1. Destroy Pipeline on UI thread.
245         RefPtr<PipelineBase>& context = pipelineContext_;
246         if (GetSettings().usePlatformAsUIThread) {
247             context->Destroy();
248         } else {
249             taskExecutor_->PostTask([context]() { context->Destroy(); }, TaskExecutor::TaskType::UI);
250         }
251         // 2. Destroy Frontend on JS thread.
252         RefPtr<Frontend>& frontend = frontend_;
253         if (GetSettings().usePlatformAsUIThread && GetSettings().useUIAsJSThread) {
254             frontend->UpdateState(Frontend::State::ON_DESTROY);
255             frontend->Destroy();
256         } else {
257             taskExecutor_->PostTask(
258                 [frontend]() {
259                     frontend->UpdateState(Frontend::State::ON_DESTROY);
260                     frontend->Destroy();
261                 },
262                 TaskExecutor::TaskType::JS);
263         }
264     }
265     resRegister_.Reset();
266     assetManager_.Reset();
267     LOGI("DialogContainer::Destroy end");
268 }
269 
DestroyView()270 void DialogContainer::DestroyView()
271 {
272     LOGI("DialogContainer::DestroyView begin");
273     ContainerScope scope(instanceId_);
274     CHECK_NULL_VOID_NOLOG(aceView_);
275     auto* aceView = static_cast<AceViewOhos*>(aceView_);
276     if (aceView) {
277         aceView->DecRefCount();
278     }
279     aceView_ = nullptr;
280     LOGI("DialogContainer::DestroyView end");
281 }
282 
SetView(AceView * view,double density,int32_t width,int32_t height,sptr<OHOS::Rosen::Window> & rsWindow)283 void DialogContainer::SetView(
284     AceView* view, double density, int32_t width, int32_t height, sptr<OHOS::Rosen::Window>& rsWindow)
285 {
286     CHECK_NULL_VOID(view);
287     auto container = AceType::DynamicCast<DialogContainer>(AceEngine::Get().GetContainer(view->GetInstanceId()));
288     CHECK_NULL_VOID(container);
289 #ifdef ENABLE_ROSEN_BACKEND
290     auto taskExecutor = container->GetTaskExecutor();
291     CHECK_NULL_VOID(taskExecutor);
292 
293     std::unique_ptr<Window> window = std::make_unique<NG::RosenWindow>(rsWindow, taskExecutor, view->GetInstanceId());
294 #else
295     auto platformWindow = PlatformWindow::Create(view);
296     CHECK_NULL_VOID(platformWindow);
297     std::unique_ptr<Window> window = std::make_unique<Window>(std::move(platformWindow));
298 #endif
299     container->AttachView(std::move(window), view, density, width, height, rsWindow->GetWindowId());
300 }
301 
SetViewNew(AceView * view,double density,int32_t width,int32_t height,sptr<OHOS::Rosen::Window> & rsWindow)302 void DialogContainer::SetViewNew(
303     AceView* view, double density, int32_t width, int32_t height, sptr<OHOS::Rosen::Window>& rsWindow)
304 {
305 #ifdef ENABLE_ROSEN_BACKEND
306     CHECK_NULL_VOID(view);
307     auto container = AceType::DynamicCast<DialogContainer>(AceEngine::Get().GetContainer(view->GetInstanceId()));
308     CHECK_NULL_VOID(container);
309     auto taskExecutor = container->GetTaskExecutor();
310     CHECK_NULL_VOID(taskExecutor);
311 
312     std::unique_ptr<Window> window = std::make_unique<NG::RosenWindow>(rsWindow, taskExecutor, view->GetInstanceId());
313     container->AttachView(std::move(window), view, density, width, height, rsWindow->GetWindowId());
314 #endif
315 }
316 
AttachView(std::unique_ptr<Window> window,AceView * view,double density,int32_t width,int32_t height,uint32_t windowId)317 void DialogContainer::AttachView(
318     std::unique_ptr<Window> window, AceView* view, double density, int32_t width, int32_t height, uint32_t windowId)
319 {
320     aceView_ = view;
321     auto instanceId = aceView_->GetInstanceId();
322     auto flutterTaskExecutor = AceType::DynamicCast<FlutterTaskExecutor>(taskExecutor_);
323     auto* aceView = static_cast<AceViewOhos*>(aceView_);
324     ACE_DCHECK(aceView != nullptr);
325     flutterTaskExecutor->InitOtherThreads(aceView->GetThreadModel());
326     ContainerScope scope(instanceId);
327     // For DECLARATIVE_JS frontend display UI in JS thread temporarily.
328     flutterTaskExecutor->InitJsThread(false);
329     InitializeFrontend();
330     SetUseNewPipeline();
331 
332     InitPipelineContext(std::move(window), instanceId, density, width, height, windowId);
333     InitializeCallback();
334 
335     taskExecutor_->PostTask([] { FrameReport::GetInstance().Init(); }, TaskExecutor::TaskType::UI);
336     ThemeConstants::InitDeviceType();
337     // Load custom style at UI thread before frontend attach, to make sure style can be loaded before building dom tree.
338     auto themeManager = AceType::MakeRefPtr<ThemeManagerImpl>();
339     if (themeManager) {
340         pipelineContext_->SetThemeManager(themeManager);
341         // Init resource
342         themeManager->InitResource(resourceInfo_);
343         taskExecutor_->PostTask(
344             [themeManager, assetManager = assetManager_, colorScheme = colorScheme_] {
345                 ACE_SCOPED_TRACE("OHOS::LoadThemes()");
346                 LOGI("UIContent load theme");
347                 themeManager->SetColorScheme(colorScheme);
348                 themeManager->LoadCustomTheme(assetManager);
349                 themeManager->LoadResourceThemes();
350             },
351             TaskExecutor::TaskType::UI);
352     }
353     aceView_->Launch();
354     // Only MainWindow instance will be registered to watch dog.
355     frontend_->AttachPipelineContext(pipelineContext_);
356 #if defined(ENABLE_ROSEN_BACKEND) and !defined(UPLOAD_GPU_DISABLED)
357     pipelineContext_->SetPostRTTaskCallBack([](std::function<void()>&& task) {
358         auto syncTask = std::make_shared<AceRosenSyncTask>(std::move(task));
359         Rosen::RSTransactionProxy::GetInstance()->ExecuteSynchronousTask(syncTask);
360     });
361 #endif
362 }
363 
InitPipelineContext(std::unique_ptr<Window> window,int32_t instanceId,double density,int32_t width,int32_t height,uint32_t windowId)364 void DialogContainer::InitPipelineContext(std::unique_ptr<Window> window, int32_t instanceId, double density,
365     int32_t width, int32_t height, uint32_t windowId)
366 {
367     if (useNewPipeline_) {
368         LOGI("New pipeline version creating...");
369         pipelineContext_ = AceType::MakeRefPtr<NG::PipelineContext>(
370             std::move(window), taskExecutor_, assetManager_, resRegister_, frontend_, instanceId);
371     } else {
372         pipelineContext_ = AceType::MakeRefPtr<PipelineContext>(
373             std::move(window), taskExecutor_, assetManager_, resRegister_, frontend_, instanceId);
374     }
375     pipelineContext_->SetRootSize(density, width, height);
376     pipelineContext_->SetTextFieldManager(AceType::MakeRefPtr<TextFieldManager>());
377     pipelineContext_->SetIsRightToLeft(AceApplicationInfo::GetInstance().IsRightToLeft());
378     pipelineContext_->SetWindowId(windowId);
379     pipelineContext_->SetWindowModal(windowModal_);
380     pipelineContext_->SetDrawDelegate(aceView_->GetDrawDelegate());
381     pipelineContext_->SetIsSubPipeline(true);
382 }
383 
InitializeFrontend()384 void DialogContainer::InitializeFrontend()
385 {
386     frontend_ = AceType::MakeRefPtr<DeclarativeFrontend>();
387     CHECK_NULL_VOID(frontend_);
388     frontend_->Initialize(type_, taskExecutor_);
389     auto front = GetFrontend();
390     CHECK_NULL_VOID(front);
391     front->UpdateState(Frontend::State::ON_CREATE);
392     front->SetJsMessageDispatcher(AceType::Claim(this));
393     front->SetAssetManager(assetManager_);
394 }
395 
DumpHeapSnapshot(bool isPrivate)396 void DialogContainer::DumpHeapSnapshot(bool isPrivate)
397 {
398     taskExecutor_->PostTask(
399         [isPrivate, frontend = WeakPtr<Frontend>(frontend_)] {
400             auto sp = frontend.Upgrade();
401             CHECK_NULL_VOID_NOLOG(sp);
402             sp->DumpHeapSnapshot(isPrivate);
403         },
404         TaskExecutor::TaskType::JS);
405 }
SetUIWindow(int32_t instanceId,sptr<OHOS::Rosen::Window> & uiWindow)406 void DialogContainer::SetUIWindow(int32_t instanceId, sptr<OHOS::Rosen::Window>& uiWindow)
407 {
408     CHECK_NULL_VOID_NOLOG(uiWindow);
409     auto container = AceType::DynamicCast<DialogContainer>(AceEngine::Get().GetContainer(instanceId));
410     CHECK_NULL_VOID_NOLOG(container);
411     container->SetUIWindowInner(uiWindow);
412 }
413 
GetUIWindow(int32_t instanceId)414 sptr<OHOS::Rosen::Window> DialogContainer::GetUIWindow(int32_t instanceId)
415 {
416     auto container = AceType::DynamicCast<DialogContainer>(AceEngine::Get().GetContainer(instanceId));
417     CHECK_NULL_RETURN_NOLOG(container, nullptr);
418     return container->GetUIWindowInner();
419 }
420 
SetUIWindowInner(sptr<OHOS::Rosen::Window> uiWindow)421 void DialogContainer::SetUIWindowInner(sptr<OHOS::Rosen::Window> uiWindow)
422 {
423     uiWindow_ = std::move(uiWindow);
424 }
425 
GetUIWindowInner() const426 sptr<OHOS::Rosen::Window> DialogContainer::GetUIWindowInner() const
427 {
428     return uiWindow_;
429 }
430 
ShowToast(int32_t instanceId,const std::string & message,int32_t duration,const std::string & bottom)431 void DialogContainer::ShowToast(
432     int32_t instanceId, const std::string& message, int32_t duration, const std::string& bottom)
433 {
434     LOGI("DialogContainer::ShowToast begin");
435     auto container = AceType::DynamicCast<DialogContainer>(AceEngine::Get().GetContainer(instanceId));
436     CHECK_NULL_VOID(container);
437     auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
438     CHECK_NULL_VOID(frontend);
439     auto delegate = frontend->GetDelegate();
440     CHECK_NULL_VOID(delegate);
441     delegate->SetToastStopListenerCallback([instanceId = instanceId]() {
442         LOGI("DialogContainer::ShowToast HideWindow instanceId = %{public}d", instanceId);
443         if (ContainerScope::CurrentId() >= 0) {
444             DialogContainer::HideWindow(instanceId);
445         }
446     });
447     delegate->ShowToast(message, duration, bottom);
448     LOGI("DialogContainer::ShowToast end");
449 }
450 
ShowDialog(int32_t instanceId,const std::string & title,const std::string & message,const std::vector<ButtonInfo> & buttons,bool autoCancel,std::function<void (int32_t,int32_t)> && callback,const std::set<std::string> & callbacks)451 void DialogContainer::ShowDialog(int32_t instanceId, const std::string& title, const std::string& message,
452     const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
453     const std::set<std::string>& callbacks)
454 {
455     LOGI("DialogContainer::ShowDialog begin");
456     auto container = AceType::DynamicCast<DialogContainer>(AceEngine::Get().GetContainer(instanceId));
457     CHECK_NULL_VOID(container);
458     auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
459     CHECK_NULL_VOID(frontend);
460     auto delegate = frontend->GetDelegate();
461     CHECK_NULL_VOID(delegate);
462     delegate->ShowDialog(
463         title, message, buttons, autoCancel, std::move(callback), callbacks, [instanceId = instanceId](bool isShow) {
464             LOGI("DialogContainer::ShowDialog HideWindow instanceId = %{public}d", instanceId);
465             if (!isShow) {
466                 DialogContainer::HideWindow(instanceId);
467             }
468         });
469     LOGI("DialogContainer::ShowDialog end");
470 }
471 
ShowDialog(int32_t instanceId,const PromptDialogAttr & dialogAttr,const std::vector<ButtonInfo> & buttons,std::function<void (int32_t,int32_t)> && callback,const std::set<std::string> & callbacks)472 void DialogContainer::ShowDialog(int32_t instanceId, const PromptDialogAttr& dialogAttr,
473     const std::vector<ButtonInfo>& buttons, std::function<void(int32_t, int32_t)>&& callback,
474     const std::set<std::string>& callbacks)
475 {
476     LOGI("DialogContainer::ShowDialog begin");
477     auto container = AceType::DynamicCast<DialogContainer>(AceEngine::Get().GetContainer(instanceId));
478     CHECK_NULL_VOID(container);
479     auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
480     CHECK_NULL_VOID(frontend);
481     auto delegate = frontend->GetDelegate();
482     CHECK_NULL_VOID(delegate);
483     delegate->ShowDialog(
484         dialogAttr, buttons, std::move(callback), callbacks, [instanceId = instanceId](bool isShow) {
485             LOGI("DialogContainer::ShowDialog HideWindow instanceId = %{public}d", instanceId);
486             if (!isShow) {
487                 DialogContainer::HideWindow(instanceId);
488             }
489         });
490     LOGI("DialogContainer::ShowDialog end");
491 }
492 
ShowActionMenu(int32_t instanceId,const std::string & title,const std::vector<ButtonInfo> & button,std::function<void (int32_t,int32_t)> && callback)493 void DialogContainer::ShowActionMenu(int32_t instanceId, const std::string& title,
494     const std::vector<ButtonInfo>& button, std::function<void(int32_t, int32_t)>&& callback)
495 {
496     LOGI("DialogContainer::ShowActionMenu begin");
497     auto container = AceType::DynamicCast<DialogContainer>(AceEngine::Get().GetContainer(instanceId));
498     CHECK_NULL_VOID(container);
499     auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
500     CHECK_NULL_VOID(frontend);
501     auto delegate = frontend->GetDelegate();
502     CHECK_NULL_VOID(delegate);
503     delegate->ShowActionMenu(title, button, std::move(callback), [instanceId = instanceId](bool isShow) {
504         LOGI("DialogContainer::ShowActionMenu HideWindow instanceId = %{public}d", instanceId);
505         if (!isShow) {
506             DialogContainer::HideWindow(instanceId);
507         }
508     });
509     LOGI("DialogContainer::ShowActionMenu end");
510 }
511 
ShowToastDialogWindow(int32_t instanceId,int32_t posX,int32_t posY,int32_t width,int32_t height,bool isToast)512 bool DialogContainer::ShowToastDialogWindow(
513     int32_t instanceId, int32_t posX, int32_t posY, int32_t width, int32_t height, bool isToast)
514 {
515     LOGI("DialogContainer::ShowToastDialogWindow begin");
516     auto container = AceType::DynamicCast<DialogContainer>(AceEngine::Get().GetContainer(instanceId));
517     CHECK_NULL_RETURN(container, false);
518     auto window = container->GetUIWindowInner();
519     CHECK_NULL_RETURN(window, false);
520     window->SetTransparent(true);
521     if (isToast) {
522         window->SetTouchable(false);
523     }
524     window->SetNeedDefaultAnimation(false);
525     OHOS::Rosen::WMError ret = window->Show();
526     if (ret != OHOS::Rosen::WMError::WM_OK) {
527         LOGE("DialogContainer::ShowToastDialogWindow Show window failed code: %{public}d", static_cast<int32_t>(ret));
528         return false;
529     }
530     ret = window->MoveTo(posX, posY);
531     if (ret != OHOS::Rosen::WMError::WM_OK) {
532         LOGE("DialogContainer::ShowToastDialogWindow MoveTo window failed code: %{public}d", static_cast<int32_t>(ret));
533         return false;
534     }
535     ret = window->Resize(width, height);
536     if (ret != OHOS::Rosen::WMError::WM_OK) {
537         LOGE("DialogContainer::ShowToastDialogWindow Resize window failed code: %{public}d", static_cast<int32_t>(ret));
538         return false;
539     }
540     LOGI("DialogContainer::ShowToastDialogWindow end");
541     return true;
542 }
543 
HideWindow(int32_t instanceId)544 bool DialogContainer::HideWindow(int32_t instanceId)
545 {
546     LOGI("DialogContainer::HideWindow begin");
547     auto container = AceType::DynamicCast<DialogContainer>(AceEngine::Get().GetContainer(instanceId));
548     CHECK_NULL_RETURN(container, false);
549     auto window = container->GetUIWindowInner();
550     CHECK_NULL_RETURN(window, false);
551     OHOS::Rosen::WMError ret = window->Hide();
552     if (ret != OHOS::Rosen::WMError::WM_OK) {
553         LOGE("DialogContainer::HideWindow Failed to hide the window.");
554         return false;
555     }
556     sptr<OHOS::Rosen::Window> uiWindow = nullptr;
557     DialogContainer::SetUIWindow(instanceId, uiWindow);
558     LOGI("DialogContainer::HideWindow end");
559     return true;
560 }
561 
CloseWindow(int32_t instanceId)562 bool DialogContainer::CloseWindow(int32_t instanceId)
563 {
564     LOGI("DialogContainer::CloseWindow begin");
565     auto container = AceType::DynamicCast<DialogContainer>(AceEngine::Get().GetContainer(instanceId));
566     CHECK_NULL_RETURN(container, false);
567     auto window = container->GetUIWindowInner();
568     CHECK_NULL_RETURN(window, false);
569     OHOS::Rosen::WMError ret = window->Close();
570     if (ret != OHOS::Rosen::WMError::WM_OK) {
571         LOGE("DialogContainer::CloseWindow Failed to close the window.");
572         return false;
573     }
574     sptr<OHOS::Rosen::Window> uiWindow = nullptr;
575     DialogContainer::SetUIWindow(instanceId, uiWindow);
576     LOGI("DialogContainer::CloseWindow end");
577     return true;
578 }
579 
OnBackPressed(int32_t instanceId)580 bool DialogContainer::OnBackPressed(int32_t instanceId)
581 {
582     LOGI("DialogContainer::OnBackPressed");
583     bool ret = DialogContainer::CloseWindow(instanceId);
584     if (!ret) {
585         LOGE("DialogContainer::OnBackPressed close window faied.");
586     }
587     return ret;
588 }
589 } // namespace OHOS::Ace::Platform
590