1 /*
2 * Copyright (c) 2024 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 #include "dialog_model.h"
16
17 #include "native_dialog.h"
18 #include "native_type.h"
19 #include "node_model.h"
20
21 #include "base/error/error_code.h"
22
23 namespace OHOS::Ace::DialogModel {
Create()24 ArkUI_NativeDialogHandle Create()
25 {
26 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
27 if (!impl) {
28 return nullptr;
29 }
30 auto dialog = impl->getDialogAPI()->create();
31 return new ArkUI_NativeDialog({ dialog });
32 }
33
Dispose(ArkUI_NativeDialogHandle handle)34 void Dispose(ArkUI_NativeDialogHandle handle)
35 {
36 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
37 if (!impl || !handle) {
38 return;
39 }
40 impl->getDialogAPI()->dispose(handle->controller);
41 delete handle;
42 handle = nullptr;
43 }
44
SetContent(ArkUI_NativeDialogHandle handle,ArkUI_NodeHandle content)45 int32_t SetContent(ArkUI_NativeDialogHandle handle, ArkUI_NodeHandle content)
46 {
47 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
48 if (!impl || !handle || !content) {
49 return ERROR_CODE_PARAM_INVALID;
50 }
51 return impl->getDialogAPI()->setContent(handle->controller, content->uiNodeHandle);
52 }
53
RemoveContent(ArkUI_NativeDialogHandle handle)54 int32_t RemoveContent(ArkUI_NativeDialogHandle handle)
55 {
56 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
57 if (!impl || !handle) {
58 return ERROR_CODE_PARAM_INVALID;
59 }
60 return impl->getDialogAPI()->removeContent(handle->controller);
61 }
62
SetContentAlignment(ArkUI_NativeDialogHandle handle,int32_t alignment,float offsetX,float offsetY)63 int32_t SetContentAlignment(ArkUI_NativeDialogHandle handle, int32_t alignment, float offsetX, float offsetY)
64 {
65 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
66 if (!impl || !handle) {
67 return ERROR_CODE_PARAM_INVALID;
68 }
69 return impl->getDialogAPI()->setContentAlignment(handle->controller,
70 alignment, offsetX, offsetY);
71 }
72
ResetContentAlignment(ArkUI_NativeDialogHandle handle)73 int32_t ResetContentAlignment(ArkUI_NativeDialogHandle handle)
74 {
75 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
76 if (!impl || !handle) {
77 return ERROR_CODE_PARAM_INVALID;
78 }
79 return impl->getDialogAPI()->resetContentAlignment(handle->controller);
80 }
81
SetModalMode(ArkUI_NativeDialogHandle handle,bool isModal)82 int32_t SetModalMode(ArkUI_NativeDialogHandle handle, bool isModal)
83 {
84 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
85 if (!impl || !handle) {
86 return ERROR_CODE_PARAM_INVALID;
87 }
88 return impl->getDialogAPI()->setModalMode(handle->controller, isModal);
89 }
90
SetAutoCancel(ArkUI_NativeDialogHandle handle,bool autoCancel)91 int32_t SetAutoCancel(ArkUI_NativeDialogHandle handle, bool autoCancel)
92 {
93 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
94 if (!impl || !handle) {
95 return ERROR_CODE_PARAM_INVALID;
96 }
97 return impl->getDialogAPI()->setAutoCancel(handle->controller, autoCancel);
98 }
99
SetMask(ArkUI_NativeDialogHandle handle,uint32_t maskColor,const ArkUI_Rect * maskRect)100 int32_t SetMask(ArkUI_NativeDialogHandle handle, uint32_t maskColor, const ArkUI_Rect* maskRect)
101 {
102 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
103 if (!impl || !handle) {
104 return ERROR_CODE_PARAM_INVALID;
105 }
106 if (maskRect) {
107 ArkUIRect rect = { maskRect->x, maskRect->y, maskRect->width, maskRect->height };
108 return impl->getDialogAPI()->setMask(handle->controller, maskColor, &rect);
109 } else {
110 return impl->getDialogAPI()->setMask(handle->controller, maskColor, nullptr);
111 }
112 }
113
SetBackgroundColor(ArkUI_NativeDialogHandle handle,uint32_t backgroundColor)114 int32_t SetBackgroundColor(ArkUI_NativeDialogHandle handle, uint32_t backgroundColor)
115 {
116 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
117 if (!impl || !handle) {
118 return ERROR_CODE_PARAM_INVALID;
119 }
120 return impl->getDialogAPI()->setBackgroundColor(handle->controller, backgroundColor);
121 }
122
SetCornerRadius(ArkUI_NativeDialogHandle handle,float topLeft,float topRight,float bottomLeft,float bottomRight)123 int32_t SetCornerRadius(ArkUI_NativeDialogHandle handle, float topLeft, float topRight,
124 float bottomLeft, float bottomRight)
125 {
126 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
127 if (!impl || !handle) {
128 return ERROR_CODE_PARAM_INVALID;
129 }
130 return impl->getDialogAPI()->setCornerRadius(handle->controller,
131 topLeft, topRight, bottomLeft, bottomRight);
132 }
133
SetGridColumnCount(ArkUI_NativeDialogHandle handle,int32_t gridCount)134 int32_t SetGridColumnCount(ArkUI_NativeDialogHandle handle, int32_t gridCount)
135 {
136 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
137 if (!impl || !handle) {
138 return ERROR_CODE_PARAM_INVALID;
139 }
140 return impl->getDialogAPI()->setGridColumnCount(handle->controller, gridCount);
141 }
142
EnableCustomStyle(ArkUI_NativeDialogHandle handle,bool enableCustomStyle)143 int32_t EnableCustomStyle(ArkUI_NativeDialogHandle handle, bool enableCustomStyle)
144 {
145 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
146 if (!impl || !handle) {
147 return ERROR_CODE_PARAM_INVALID;
148 }
149 return impl->getDialogAPI()->enableCustomStyle(handle->controller, enableCustomStyle);
150 }
151
EnableCustomAnimation(ArkUI_NativeDialogHandle handle,bool enableCustomAnimation)152 int32_t EnableCustomAnimation(ArkUI_NativeDialogHandle handle, bool enableCustomAnimation)
153 {
154 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
155 if (!impl || !handle) {
156 return ERROR_CODE_PARAM_INVALID;
157 }
158 return impl->getDialogAPI()->enableCustomAnimation(handle->controller, enableCustomAnimation);
159 }
160
Show(ArkUI_NativeDialogHandle handle,bool showInSubWindow)161 int32_t Show(ArkUI_NativeDialogHandle handle, bool showInSubWindow)
162 {
163 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
164 if (!impl || !handle) {
165 return ERROR_CODE_PARAM_INVALID;
166 }
167 return impl->getDialogAPI()->show(handle->controller, showInSubWindow);
168 }
169
Close(ArkUI_NativeDialogHandle handle)170 int32_t Close(ArkUI_NativeDialogHandle handle)
171 {
172 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
173 if (!impl || !handle) {
174 return ERROR_CODE_PARAM_INVALID;
175 }
176 return impl->getDialogAPI()->close(handle->controller);
177 }
178
RegisterOnWillDismiss(ArkUI_NativeDialogHandle handle,ArkUI_OnWillDismissEvent eventHandler)179 int32_t RegisterOnWillDismiss(ArkUI_NativeDialogHandle handle, ArkUI_OnWillDismissEvent eventHandler)
180 {
181 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
182 if (!impl || !handle) {
183 return ERROR_CODE_PARAM_INVALID;
184 }
185 return impl->getDialogAPI()->registerOnWillDismiss(handle->controller, eventHandler);
186 }
187
RegisterOnWillDismissWithUserData(ArkUI_NativeDialogHandle handle,void * userData,void (* callback)(ArkUI_DialogDismissEvent * event))188 int32_t RegisterOnWillDismissWithUserData(
189 ArkUI_NativeDialogHandle handle, void* userData, void (*callback)(ArkUI_DialogDismissEvent* event))
190 {
191 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
192 if (!impl || !handle) {
193 return ERROR_CODE_PARAM_INVALID;
194 }
195 int result = impl->getDialogAPI()->registerOnWillDismissWithUserData(handle->controller, userData, callback);
196 return result;
197 }
198
199 } // namespace OHOS::Ace::NG::DialogModel
200
201 #ifdef __cplusplus
202 extern "C" {
203 #endif
204
OH_ArkUI_DialogDismissEvent_SetShouldBlockDismiss(ArkUI_DialogDismissEvent * event,bool shouldBlockDismiss)205 void OH_ArkUI_DialogDismissEvent_SetShouldBlockDismiss(ArkUI_DialogDismissEvent* event, bool shouldBlockDismiss)
206 {
207 if (!event) {
208 return;
209 }
210 event->BlockDismiss = shouldBlockDismiss;
211 }
212
OH_ArkUI_DialogDismissEvent_GetUserData(ArkUI_DialogDismissEvent * event)213 void* OH_ArkUI_DialogDismissEvent_GetUserData(ArkUI_DialogDismissEvent* event)
214 {
215 if (!event) {
216 return nullptr;
217 }
218 return event->userData;
219 }
220
OH_ArkUI_DialogDismissEvent_GetDismissReason(ArkUI_DialogDismissEvent * event)221 int32_t OH_ArkUI_DialogDismissEvent_GetDismissReason(ArkUI_DialogDismissEvent* event)
222 {
223 if (!event) {
224 return -1;
225 }
226 return event->reason;
227 }
228
229 #ifdef __cplusplus
230 };
231 #endif
232