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