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
19 #if defined(ENABLE_ROSEN_BACKEND) and !defined(UPLOAD_GPU_DISABLED)
20 #include "adapter/ohos/entrance/ace_rosen_sync_task.h"
21 #endif
22
23 #include "adapter/ohos/entrance/ace_view_ohos.h"
24 #include "core/common/ace_engine.h"
25 #include "core/common/task_executor_impl.h"
26 #include "core/common/text_field_manager.h"
27 #include "frameworks/bridge/common/utils/engine_helper.h"
28 #include "frameworks/bridge/declarative_frontend/declarative_frontend.h"
29
30 namespace OHOS::Ace::Platform {
DialogContainer(int32_t instanceId,FrontendType type)31 DialogContainer::DialogContainer(int32_t instanceId, FrontendType type) : AceContainer(instanceId, type)
32 {}
33
ShowToast(int32_t instanceId,const NG::ToastInfo & toastInfo,std::function<void (int32_t)> && callback)34 void DialogContainer::ShowToast(int32_t instanceId, const NG::ToastInfo& toastInfo,
35 std::function<void(int32_t)>&& callback)
36 {
37 auto container = AceType::DynamicCast<AceContainer>(AceEngine::Get().GetContainer(instanceId));
38 CHECK_NULL_VOID(container);
39 auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
40 CHECK_NULL_VOID(frontend);
41 auto delegate = frontend->GetDelegate();
42 CHECK_NULL_VOID(delegate);
43 delegate->SetToastStopListenerCallback([instanceId = instanceId]() {
44 if (ContainerScope::CurrentId() >= 0) {
45 AceContainer::HideWindow(instanceId);
46 }
47 });
48 auto toastInfoNew = toastInfo;
49 toastInfoNew.showMode = NG::ToastShowMode::DEFAULT;
50 delegate->ShowToast(toastInfoNew, std::move(callback));
51 }
52
CloseToast(int32_t instanceId,int32_t toastId,std::function<void (int32_t)> && callback)53 void DialogContainer::CloseToast(int32_t instanceId, int32_t toastId, std::function<void(int32_t)>&& callback)
54 {
55 auto container = AceType::DynamicCast<AceContainer>(AceEngine::Get().GetContainer(instanceId));
56 CHECK_NULL_VOID(container);
57
58 auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
59 CHECK_NULL_VOID(frontend);
60
61 auto delegate = frontend->GetDelegate();
62 CHECK_NULL_VOID(delegate);
63 delegate->SetToastStopListenerCallback([instanceId = instanceId]() {
64 if (ContainerScope::CurrentId() >= 0) {
65 AceContainer::HideWindow(instanceId);
66 }
67 });
68
69 delegate->CloseToast(toastId, std::move(callback));
70 }
71
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)72 void DialogContainer::ShowDialog(int32_t instanceId, const std::string& title, const std::string& message,
73 const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
74 const std::set<std::string>& callbacks)
75 {
76 TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog begin");
77 auto container = AceType::DynamicCast<AceContainer>(AceEngine::Get().GetContainer(instanceId));
78 CHECK_NULL_VOID(container);
79 auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
80 CHECK_NULL_VOID(frontend);
81 auto delegate = frontend->GetDelegate();
82 CHECK_NULL_VOID(delegate);
83 delegate->ShowDialog(
84 title, message, buttons, autoCancel, std::move(callback), callbacks, [instanceId = instanceId](bool isShow) {
85 TAG_LOGI(
86 AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog HideWindow instanceId = %{public}d", instanceId);
87 if (!isShow) {
88 AceContainer::HideWindow(instanceId);
89 }
90 });
91 }
92
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)93 void DialogContainer::ShowDialog(int32_t instanceId, const PromptDialogAttr& dialogAttr,
94 const std::vector<ButtonInfo>& buttons, std::function<void(int32_t, int32_t)>&& callback,
95 const std::set<std::string>& callbacks)
96 {
97 TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog with attr begin");
98 auto container = AceType::DynamicCast<AceContainer>(AceEngine::Get().GetContainer(instanceId));
99 CHECK_NULL_VOID(container);
100 auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
101 CHECK_NULL_VOID(frontend);
102 auto delegate = frontend->GetDelegate();
103 CHECK_NULL_VOID(delegate);
104 delegate->ShowDialog(dialogAttr, buttons, std::move(callback), callbacks, [instanceId = instanceId](bool isShow) {
105 TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog HideWindow instanceId = %{public}d", instanceId);
106 if (!isShow) {
107 AceContainer::HideWindow(instanceId);
108 }
109 });
110 }
111
ShowActionMenu(int32_t instanceId,const std::string & title,const std::vector<ButtonInfo> & button,std::function<void (int32_t,int32_t)> && callback)112 void DialogContainer::ShowActionMenu(int32_t instanceId, const std::string& title,
113 const std::vector<ButtonInfo>& button, std::function<void(int32_t, int32_t)>&& callback)
114 {
115 auto container = AceType::DynamicCast<AceContainer>(AceEngine::Get().GetContainer(instanceId));
116 CHECK_NULL_VOID(container);
117 auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
118 CHECK_NULL_VOID(frontend);
119 auto delegate = frontend->GetDelegate();
120 CHECK_NULL_VOID(delegate);
121 delegate->ShowActionMenu(title, button, std::move(callback), [instanceId = instanceId](bool isShow) {
122 if (!isShow) {
123 AceContainer::HideWindow(instanceId);
124 }
125 });
126 }
127
ShowToastDialogWindow(int32_t instanceId,int32_t posX,int32_t posY,int32_t width,int32_t height,bool isToast)128 bool DialogContainer::ShowToastDialogWindow(
129 int32_t instanceId, int32_t posX, int32_t posY, int32_t width, int32_t height, bool isToast)
130 {
131 TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowToastDialogWindow begin");
132 auto container = AceType::DynamicCast<AceContainer>(AceEngine::Get().GetContainer(instanceId));
133 CHECK_NULL_RETURN(container, false);
134 auto window = container->GetUIWindowInner();
135 CHECK_NULL_RETURN(window, false);
136 window->SetTransparent(true);
137 if (isToast) {
138 window->SetTouchable(false);
139 }
140 window->SetNeedDefaultAnimation(false);
141 OHOS::Rosen::WMError ret = window->MoveTo(posX, posY);
142 if (ret != OHOS::Rosen::WMError::WM_OK) {
143 TAG_LOGW(AceLogTag::ACE_DIALOG, "DialogContainer ShowToastDialogWindow MoveTo window failed code: %{public}d",
144 static_cast<int32_t>(ret));
145 return false;
146 }
147 ret = window->Resize(width, height);
148 if (ret != OHOS::Rosen::WMError::WM_OK) {
149 TAG_LOGW(AceLogTag::ACE_DIALOG, "DialogContainer ShowToastDialogWindow Resize window failed code: %{public}d",
150 static_cast<int32_t>(ret));
151 return false;
152 }
153 ret = window->Show();
154 if (ret != OHOS::Rosen::WMError::WM_OK) {
155 TAG_LOGE(AceLogTag::ACE_DIALOG, "DialogContainer ShowToastDialogWindow Show window failed code: %{public}d",
156 static_cast<int32_t>(ret));
157 return false;
158 }
159 return true;
160 }
161
162
OnBackPressed(int32_t instanceId)163 bool DialogContainer::OnBackPressed(int32_t instanceId)
164 {
165 return AceContainer::CloseWindow(instanceId);
166 }
167
168 } // namespace OHOS::Ace::Platform
169