• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SetKeyboardAvoidDistance(ArkUI_NativeDialogHandle handle,float distance,ArkUI_LengthMetricUnit unit)199 int32_t SetKeyboardAvoidDistance(
200     ArkUI_NativeDialogHandle handle, float distance, ArkUI_LengthMetricUnit unit)
201 {
202     const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
203     if (!impl || !handle) {
204         return ERROR_CODE_PARAM_INVALID;
205     }
206     if (unit < ARKUI_LENGTH_METRIC_UNIT_DEFAULT || unit > ARKUI_LENGTH_METRIC_UNIT_FP) {
207         return ARKUI_ERROR_CODE_PARAM_INVALID;
208     }
209     int result = impl->getDialogAPI()->setKeyboardAvoidDistance(handle->controller, distance, unit);
210     return result;
211 }
212 
SetLevelMode(ArkUI_NativeDialogHandle handle,ArkUI_LevelMode levelMode)213 int32_t SetLevelMode(ArkUI_NativeDialogHandle handle, ArkUI_LevelMode levelMode)
214 {
215     const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
216     if (!impl || !handle) {
217         return ERROR_CODE_PARAM_INVALID;
218     }
219     if (static_cast<int32_t>(levelMode) < static_cast<int32_t>(ARKUI_LEVEL_MODE_OVERLAY) ||
220         static_cast<int32_t>(levelMode) > static_cast<int32_t>(ARKUI_LEVEL_MODE_EMBEDDED)) {
221         return ARKUI_ERROR_CODE_PARAM_INVALID;
222     }
223     return impl->getDialogAPI()->setLevelMode(handle->controller, static_cast<int32_t>(levelMode));
224 }
225 
SetLevelUniqueId(ArkUI_NativeDialogHandle handle,int32_t uniqueId)226 int32_t SetLevelUniqueId(ArkUI_NativeDialogHandle handle, int32_t uniqueId)
227 {
228     const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
229     if (!impl) {
230         return ARKUI_ERROR_CODE_PARAM_INVALID;
231     }
232     if (!handle || uniqueId < 0) {
233         return ARKUI_ERROR_CODE_PARAM_INVALID;
234     }
235     return impl->getDialogAPI()->setLevelUniqueId(handle->controller, uniqueId);
236 }
237 
SetImmersiveMode(ArkUI_NativeDialogHandle handle,ArkUI_ImmersiveMode immersiveMode)238 int32_t SetImmersiveMode(ArkUI_NativeDialogHandle handle, ArkUI_ImmersiveMode immersiveMode)
239 {
240     const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
241     if (!impl || !handle) {
242         return ERROR_CODE_PARAM_INVALID;
243     }
244     if (static_cast<int32_t>(immersiveMode) < static_cast<int32_t>(ARKUI_IMMERSIVE_MODE_DEFAULT) ||
245         static_cast<int32_t>(immersiveMode) > static_cast<int32_t>(ARKUI_IMMERSIVE_MODE_EXTEND)) {
246         return ARKUI_ERROR_CODE_PARAM_INVALID;
247     }
248     return impl->getDialogAPI()->setImmersiveMode(handle->controller, static_cast<int32_t>(immersiveMode));
249 }
250 } // namespace OHOS::Ace::NG::DialogModel
251 
252 #ifdef __cplusplus
253 extern "C" {
254 #endif
255 
OH_ArkUI_DialogDismissEvent_SetShouldBlockDismiss(ArkUI_DialogDismissEvent * event,bool shouldBlockDismiss)256 void OH_ArkUI_DialogDismissEvent_SetShouldBlockDismiss(ArkUI_DialogDismissEvent* event, bool shouldBlockDismiss)
257 {
258     if (!event) {
259         return;
260     }
261     event->BlockDismiss = shouldBlockDismiss;
262 }
263 
OH_ArkUI_DialogDismissEvent_GetUserData(ArkUI_DialogDismissEvent * event)264 void* OH_ArkUI_DialogDismissEvent_GetUserData(ArkUI_DialogDismissEvent* event)
265 {
266     if (!event) {
267         return nullptr;
268     }
269     return event->userData;
270 }
271 
OH_ArkUI_DialogDismissEvent_GetDismissReason(ArkUI_DialogDismissEvent * event)272 int32_t OH_ArkUI_DialogDismissEvent_GetDismissReason(ArkUI_DialogDismissEvent* event)
273 {
274     if (!event) {
275         return -1;
276     }
277     return event->reason;
278 }
279 
280 #ifdef __cplusplus
281 };
282 #endif
283