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 std::string & message,int32_t duration,const std::string & bottom,std::function<void (int32_t)> && callback)34 void DialogContainer::ShowToast(int32_t instanceId, const std::string& message, int32_t duration,
35 const std::string& bottom, 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 toastInfo = NG::ToastInfo { .message = message,
49 .duration = duration,
50 .bottom = bottom,
51 .showMode = NG::ToastShowMode::DEFAULT,
52 .alignment = -1,
53 .offset = std::nullopt };
54 delegate->ShowToast(toastInfo, std::move(callback));
55 }
56
CloseToast(int32_t instanceId,int32_t toastId,std::function<void (int32_t)> && callback)57 void DialogContainer::CloseToast(int32_t instanceId, int32_t toastId, std::function<void(int32_t)>&& callback)
58 {
59 auto container = AceType::DynamicCast<AceContainer>(AceEngine::Get().GetContainer(instanceId));
60 CHECK_NULL_VOID(container);
61
62 auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
63 CHECK_NULL_VOID(frontend);
64
65 auto delegate = frontend->GetDelegate();
66 CHECK_NULL_VOID(delegate);
67 delegate->SetToastStopListenerCallback([instanceId = instanceId]() {
68 if (ContainerScope::CurrentId() >= 0) {
69 AceContainer::HideWindow(instanceId);
70 }
71 });
72
73 delegate->CloseToast(toastId, std::move(callback));
74 }
75
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)76 void DialogContainer::ShowDialog(int32_t instanceId, const std::string& title, const std::string& message,
77 const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
78 const std::set<std::string>& callbacks)
79 {
80 TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog begin");
81 auto container = AceType::DynamicCast<AceContainer>(AceEngine::Get().GetContainer(instanceId));
82 CHECK_NULL_VOID(container);
83 auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
84 CHECK_NULL_VOID(frontend);
85 auto delegate = frontend->GetDelegate();
86 CHECK_NULL_VOID(delegate);
87 delegate->ShowDialog(
88 title, message, buttons, autoCancel, std::move(callback), callbacks, [instanceId = instanceId](bool isShow) {
89 TAG_LOGI(
90 AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog HideWindow instanceId = %{public}d", instanceId);
91 if (!isShow) {
92 AceContainer::HideWindow(instanceId);
93 }
94 });
95 }
96
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)97 void DialogContainer::ShowDialog(int32_t instanceId, const PromptDialogAttr& dialogAttr,
98 const std::vector<ButtonInfo>& buttons, std::function<void(int32_t, int32_t)>&& callback,
99 const std::set<std::string>& callbacks)
100 {
101 TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog with attr begin");
102 auto container = AceType::DynamicCast<AceContainer>(AceEngine::Get().GetContainer(instanceId));
103 CHECK_NULL_VOID(container);
104 auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
105 CHECK_NULL_VOID(frontend);
106 auto delegate = frontend->GetDelegate();
107 CHECK_NULL_VOID(delegate);
108 delegate->ShowDialog(dialogAttr, buttons, std::move(callback), callbacks, [instanceId = instanceId](bool isShow) {
109 TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowDialog HideWindow instanceId = %{public}d", instanceId);
110 if (!isShow) {
111 AceContainer::HideWindow(instanceId);
112 }
113 });
114 }
115
ShowActionMenu(int32_t instanceId,const std::string & title,const std::vector<ButtonInfo> & button,std::function<void (int32_t,int32_t)> && callback)116 void DialogContainer::ShowActionMenu(int32_t instanceId, const std::string& title,
117 const std::vector<ButtonInfo>& button, std::function<void(int32_t, int32_t)>&& callback)
118 {
119 auto container = AceType::DynamicCast<AceContainer>(AceEngine::Get().GetContainer(instanceId));
120 CHECK_NULL_VOID(container);
121 auto frontend = AceType::DynamicCast<DeclarativeFrontend>(container->GetFrontend());
122 CHECK_NULL_VOID(frontend);
123 auto delegate = frontend->GetDelegate();
124 CHECK_NULL_VOID(delegate);
125 delegate->ShowActionMenu(title, button, std::move(callback), [instanceId = instanceId](bool isShow) {
126 if (!isShow) {
127 AceContainer::HideWindow(instanceId);
128 }
129 });
130 }
131
ShowToastDialogWindow(int32_t instanceId,int32_t posX,int32_t posY,int32_t width,int32_t height,bool isToast)132 bool DialogContainer::ShowToastDialogWindow(
133 int32_t instanceId, int32_t posX, int32_t posY, int32_t width, int32_t height, bool isToast)
134 {
135 TAG_LOGI(AceLogTag::ACE_DIALOG, "DialogContainer ShowToastDialogWindow begin");
136 auto container = AceType::DynamicCast<AceContainer>(AceEngine::Get().GetContainer(instanceId));
137 CHECK_NULL_RETURN(container, false);
138 auto window = container->GetUIWindowInner();
139 CHECK_NULL_RETURN(window, false);
140 window->SetTransparent(true);
141 if (isToast) {
142 window->SetTouchable(false);
143 }
144 window->SetNeedDefaultAnimation(false);
145 OHOS::Rosen::WMError ret = window->MoveTo(posX, posY);
146 if (ret != OHOS::Rosen::WMError::WM_OK) {
147 TAG_LOGW(AceLogTag::ACE_DIALOG, "DialogContainer ShowToastDialogWindow MoveTo window failed code: %{public}d",
148 static_cast<int32_t>(ret));
149 return false;
150 }
151 ret = window->Resize(width, height);
152 if (ret != OHOS::Rosen::WMError::WM_OK) {
153 TAG_LOGW(AceLogTag::ACE_DIALOG, "DialogContainer ShowToastDialogWindow Resize window failed code: %{public}d",
154 static_cast<int32_t>(ret));
155 return false;
156 }
157 ret = window->Show();
158 if (ret != OHOS::Rosen::WMError::WM_OK) {
159 TAG_LOGE(AceLogTag::ACE_DIALOG, "DialogContainer ShowToastDialogWindow Show window failed code: %{public}d",
160 static_cast<int32_t>(ret));
161 return false;
162 }
163 return true;
164 }
165
166
OnBackPressed(int32_t instanceId)167 bool DialogContainer::OnBackPressed(int32_t instanceId)
168 {
169 return AceContainer::CloseWindow(instanceId);
170 }
171
172 } // namespace OHOS::Ace::Platform
173