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