1 /*
2 * Copyright (c) 2025 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 "dialog_option.h"
17
18 #include "native_dialog.h"
19 #include "node_model.h"
20
21 #include "base/error/error_code.h"
22 #include "base/utils/utils.h"
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 namespace {
29 constexpr int NUM_0 = 0;
30 constexpr int NUM_1 = 1;
31 constexpr int NUM_2 = 2;
32 constexpr int NUM_3 = 3;
33 constexpr int NUM_4 = 4;
34 constexpr int NUM_5 = 5;
35 constexpr int NUM_6 = 6;
36 const int ALLOW_SIZE_7(7);
37 constexpr int COLOR_STRATEGY_STYLE = 1;
38 constexpr int COLOR_STYLE = 2;
39 } // namespace
40
OH_ArkUI_CustomDialog_CreateOptions(ArkUI_NodeHandle content)41 ArkUI_CustomDialogOptions* OH_ArkUI_CustomDialog_CreateOptions(ArkUI_NodeHandle content)
42 {
43 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
44 if (!impl || !content) {
45 return nullptr;
46 }
47 auto dialog = impl->getDialogAPI()->create();
48 auto options = new ArkUI_CustomDialogOptions({ dialog });
49 impl->getDialogAPI()->setContent(options->handle, content->uiNodeHandle);
50 return options;
51 }
52
OH_ArkUI_CustomDialog_DisposeOptions(ArkUI_CustomDialogOptions * options)53 void OH_ArkUI_CustomDialog_DisposeOptions(ArkUI_CustomDialogOptions* options)
54 {
55 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
56 if (!impl || !options) {
57 return;
58 }
59 impl->getDialogAPI()->dispose(options->handle);
60 delete options;
61 options = nullptr;
62 }
63
OH_ArkUI_CustomDialog_OpenDialog(ArkUI_CustomDialogOptions * options,void (* callback)(int32_t dialogId))64 int32_t OH_ArkUI_CustomDialog_OpenDialog(ArkUI_CustomDialogOptions* options, void (*callback)(int32_t dialogId))
65 {
66 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
67 if (!impl || !options) {
68 return ARKUI_ERROR_CODE_PARAM_INVALID;
69 }
70 return impl->getDialogAPI()->openCustomDialog(options->handle, callback);
71 }
72
OH_ArkUI_CustomDialog_UpdateDialog(ArkUI_CustomDialogOptions * options,void (* callback)(int32_t dialogId))73 int32_t OH_ArkUI_CustomDialog_UpdateDialog(ArkUI_CustomDialogOptions* options, void (*callback)(int32_t dialogId))
74 {
75 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
76 if (!impl || !options) {
77 return ARKUI_ERROR_CODE_PARAM_INVALID;
78 }
79 return impl->getDialogAPI()->updateCustomDialog(options->handle, callback);
80 }
81
OH_ArkUI_CustomDialog_CloseDialog(int32_t dialogId)82 int32_t OH_ArkUI_CustomDialog_CloseDialog(int32_t dialogId)
83 {
84 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
85 if (!impl) {
86 return ARKUI_ERROR_CODE_PARAM_INVALID;
87 }
88 return impl->getDialogAPI()->closeCustomDialog(dialogId);
89 }
90
OH_ArkUI_CustomDialog_SetLevelMode(ArkUI_CustomDialogOptions * options,ArkUI_LevelMode levelMode)91 int32_t OH_ArkUI_CustomDialog_SetLevelMode(ArkUI_CustomDialogOptions* options, ArkUI_LevelMode levelMode)
92 {
93 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
94 if (!impl || !options) {
95 return ARKUI_ERROR_CODE_PARAM_INVALID;
96 }
97 if (static_cast<int32_t>(levelMode) < static_cast<int32_t>(ARKUI_LEVEL_MODE_OVERLAY) ||
98 static_cast<int32_t>(levelMode) > static_cast<int32_t>(ARKUI_LEVEL_MODE_EMBEDDED)) {
99 return ARKUI_ERROR_CODE_PARAM_INVALID;
100 }
101 return impl->getDialogAPI()->setLevelMode(options->handle, static_cast<int32_t>(levelMode));
102 }
103
OH_ArkUI_CustomDialog_SetLevelUniqueId(ArkUI_CustomDialogOptions * options,int32_t uniqueId)104 int32_t OH_ArkUI_CustomDialog_SetLevelUniqueId(ArkUI_CustomDialogOptions* options, int32_t uniqueId)
105 {
106 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
107 if (!impl || !options) {
108 return ARKUI_ERROR_CODE_PARAM_INVALID;
109 }
110 if (uniqueId < 0) {
111 return ARKUI_ERROR_CODE_PARAM_INVALID;
112 }
113 return impl->getDialogAPI()->setLevelUniqueId(options->handle, uniqueId);
114 }
115
OH_ArkUI_CustomDialog_SetImmersiveMode(ArkUI_CustomDialogOptions * options,ArkUI_ImmersiveMode immersiveMode)116 int32_t OH_ArkUI_CustomDialog_SetImmersiveMode(ArkUI_CustomDialogOptions* options, ArkUI_ImmersiveMode immersiveMode)
117 {
118 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
119 if (!impl || !options) {
120 return ARKUI_ERROR_CODE_PARAM_INVALID;
121 }
122 if (static_cast<int32_t>(immersiveMode) < static_cast<int32_t>(ARKUI_IMMERSIVE_MODE_DEFAULT) ||
123 static_cast<int32_t>(immersiveMode) > static_cast<int32_t>(ARKUI_IMMERSIVE_MODE_EXTEND)) {
124 return ARKUI_ERROR_CODE_PARAM_INVALID;
125 }
126 return impl->getDialogAPI()->setImmersiveMode(options->handle, static_cast<int32_t>(immersiveMode));
127 }
128
OH_ArkUI_CustomDialog_SetBackgroundColor(ArkUI_CustomDialogOptions * options,uint32_t backgroundColor)129 int32_t OH_ArkUI_CustomDialog_SetBackgroundColor(ArkUI_CustomDialogOptions* options, uint32_t backgroundColor)
130 {
131 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
132 if (!impl || !options) {
133 return ARKUI_ERROR_CODE_PARAM_INVALID;
134 }
135 return impl->getDialogAPI()->setBackgroundColor(options->handle, backgroundColor);
136 }
137
OH_ArkUI_CustomDialog_SetCornerRadius(ArkUI_CustomDialogOptions * options,float topLeft,float topRight,float bottomLeft,float bottomRight)138 int32_t OH_ArkUI_CustomDialog_SetCornerRadius(
139 ArkUI_CustomDialogOptions* options, float topLeft, float topRight, float bottomLeft, float bottomRight)
140 {
141 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
142 if (!impl || !options) {
143 return ARKUI_ERROR_CODE_PARAM_INVALID;
144 }
145 return impl->getDialogAPI()->setCornerRadius(options->handle, topLeft, topRight, bottomLeft, bottomRight);
146 }
147
OH_ArkUI_CustomDialog_SetBorderWidth(ArkUI_CustomDialogOptions * options,float top,float right,float bottom,float left,ArkUI_LengthMetricUnit unit)148 int32_t OH_ArkUI_CustomDialog_SetBorderWidth(
149 ArkUI_CustomDialogOptions* options, float top, float right, float bottom, float left, ArkUI_LengthMetricUnit unit)
150 {
151 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
152 if (!impl || !options) {
153 return ARKUI_ERROR_CODE_PARAM_INVALID;
154 }
155 return impl->getDialogAPI()->setBorderWidth(options->handle, top, right, bottom, left, unit);
156 }
157
OH_ArkUI_CustomDialog_SetBorderColor(ArkUI_CustomDialogOptions * options,uint32_t top,uint32_t right,uint32_t bottom,uint32_t left)158 int32_t OH_ArkUI_CustomDialog_SetBorderColor(
159 ArkUI_CustomDialogOptions* options, uint32_t top, uint32_t right, uint32_t bottom, uint32_t left)
160 {
161 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
162 if (!impl || !options) {
163 return ARKUI_ERROR_CODE_PARAM_INVALID;
164 }
165 return impl->getDialogAPI()->setBorderColor(options->handle, top, right, bottom, left);
166 }
167
OH_ArkUI_CustomDialog_SetBorderStyle(ArkUI_CustomDialogOptions * options,int32_t top,int32_t right,int32_t bottom,int32_t left)168 int32_t OH_ArkUI_CustomDialog_SetBorderStyle(
169 ArkUI_CustomDialogOptions* options, int32_t top, int32_t right, int32_t bottom, int32_t left)
170 {
171 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
172 if (!impl || !options) {
173 return ARKUI_ERROR_CODE_PARAM_INVALID;
174 }
175 return impl->getDialogAPI()->setBorderStyle(options->handle, top, right, bottom, left);
176 }
177
OH_ArkUI_CustomDialog_SetWidth(ArkUI_CustomDialogOptions * options,float width,ArkUI_LengthMetricUnit unit)178 int32_t OH_ArkUI_CustomDialog_SetWidth(ArkUI_CustomDialogOptions* options, float width, ArkUI_LengthMetricUnit unit)
179 {
180 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
181 if (!impl || !options) {
182 return ARKUI_ERROR_CODE_PARAM_INVALID;
183 }
184 return impl->getDialogAPI()->setWidth(options->handle, width, unit);
185 }
186
OH_ArkUI_CustomDialog_SetHeight(ArkUI_CustomDialogOptions * options,float height,ArkUI_LengthMetricUnit unit)187 int32_t OH_ArkUI_CustomDialog_SetHeight(ArkUI_CustomDialogOptions* options, float height, ArkUI_LengthMetricUnit unit)
188 {
189 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
190 if (!impl || !options) {
191 return ARKUI_ERROR_CODE_PARAM_INVALID;
192 }
193 return impl->getDialogAPI()->setHeight(options->handle, height, unit);
194 }
195
OH_ArkUI_CustomDialog_SetShadow(ArkUI_CustomDialogOptions * options,ArkUI_ShadowStyle shadow)196 int32_t OH_ArkUI_CustomDialog_SetShadow(ArkUI_CustomDialogOptions* options, ArkUI_ShadowStyle shadow)
197 {
198 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
199 if (!impl || !options) {
200 return ARKUI_ERROR_CODE_PARAM_INVALID;
201 }
202 return impl->getDialogAPI()->setShadow(options->handle, shadow);
203 }
204
OH_ArkUI_CustomDialog_SetCustomShadow(ArkUI_CustomDialogOptions * options,const ArkUI_AttributeItem * customShadow)205 int32_t OH_ArkUI_CustomDialog_SetCustomShadow(
206 ArkUI_CustomDialogOptions* options, const ArkUI_AttributeItem* customShadow)
207 {
208 if (customShadow->size == 0) {
209 return ARKUI_ERROR_CODE_PARAM_INVALID;
210 }
211 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
212 if (!impl || !options) {
213 return ARKUI_ERROR_CODE_PARAM_INVALID;
214 }
215 ArkUIInt32orFloat32 shadows[ALLOW_SIZE_7] = { 0, { .i32 = NUM_2 }, 0, 0, { .i32 = 0 }, { .u32 = 0 }, { .i32 = 0 } };
216 int length = customShadow->size;
217 if (length > NUM_0) {
218 if (OHOS::Ace::LessNotEqual(customShadow->value[NUM_0].f32, 0.0f)) {
219 return ARKUI_ERROR_CODE_PARAM_INVALID;
220 }
221 shadows[NUM_0].f32 = customShadow->value[NUM_0].f32; // radius
222 }
223 if (length > NUM_2) {
224 shadows[NUM_2].f32 = customShadow->value[NUM_2].f32; // OffsetX
225 }
226 if (length > NUM_3) {
227 shadows[NUM_3].f32 = customShadow->value[NUM_3].f32; // OffsetY
228 }
229 if (length > NUM_4) {
230 if (!OHOS::Ace::InRegion(NUM_0, NUM_1, customShadow->value[NUM_4].i32)) {
231 return ARKUI_ERROR_CODE_PARAM_INVALID;
232 }
233 shadows[NUM_4].i32 = customShadow->value[NUM_4].i32;
234 }
235 if (length > NUM_5) {
236 if (customShadow->value[NUM_1].i32) {
237 if (!OHOS::Ace::InRegion(NUM_0, NUM_2, customShadow->value[NUM_5].i32)) {
238 return ARKUI_ERROR_CODE_PARAM_INVALID;
239 }
240 shadows[NUM_1].i32 = COLOR_STRATEGY_STYLE;
241 shadows[NUM_5].i32 = customShadow->value[NUM_5].i32;
242 } else {
243 shadows[NUM_1].i32 = COLOR_STYLE;
244 shadows[NUM_5].u32 = customShadow->value[NUM_5].u32;
245 }
246 }
247 if (length > NUM_6) {
248 shadows[NUM_6].i32 = customShadow->value[NUM_6].i32;
249 }
250 return impl->getDialogAPI()->setCustomShadow(options->handle, shadows, ALLOW_SIZE_7);
251 }
252
OH_ArkUI_CustomDialog_SetBackgroundBlurStyle(ArkUI_CustomDialogOptions * options,ArkUI_BlurStyle blurStyle)253 int32_t OH_ArkUI_CustomDialog_SetBackgroundBlurStyle(ArkUI_CustomDialogOptions* options, ArkUI_BlurStyle blurStyle)
254 {
255 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
256 if (!impl || !options) {
257 return ARKUI_ERROR_CODE_PARAM_INVALID;
258 }
259 return impl->getDialogAPI()->setBackgroundBlurStyle(options->handle, blurStyle);
260 }
261
OH_ArkUI_CustomDialog_SetAlignment(ArkUI_CustomDialogOptions * options,int32_t alignment,float offsetX,float offsetY)262 int32_t OH_ArkUI_CustomDialog_SetAlignment(
263 ArkUI_CustomDialogOptions* options, int32_t alignment, float offsetX, float offsetY)
264 {
265 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
266 if (!impl || !options) {
267 return ARKUI_ERROR_CODE_PARAM_INVALID;
268 }
269 return impl->getDialogAPI()->setContentAlignment(options->handle, alignment, offsetX, offsetY);
270 }
271
OH_ArkUI_CustomDialog_SetModalMode(ArkUI_CustomDialogOptions * options,bool isModal)272 int32_t OH_ArkUI_CustomDialog_SetModalMode(ArkUI_CustomDialogOptions* options, bool isModal)
273 {
274 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
275 if (!impl || !options) {
276 return ARKUI_ERROR_CODE_PARAM_INVALID;
277 }
278 return impl->getDialogAPI()->setModalMode(options->handle, isModal);
279 }
280
OH_ArkUI_CustomDialog_SetAutoCancel(ArkUI_CustomDialogOptions * options,bool autoCancel)281 int32_t OH_ArkUI_CustomDialog_SetAutoCancel(ArkUI_CustomDialogOptions* options, bool autoCancel)
282 {
283 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
284 if (!impl || !options) {
285 return ARKUI_ERROR_CODE_PARAM_INVALID;
286 }
287 return impl->getDialogAPI()->setAutoCancel(options->handle, autoCancel);
288 }
289
OH_ArkUI_CustomDialog_SetSubwindowMode(ArkUI_CustomDialogOptions * options,bool showInSubwindow)290 int32_t OH_ArkUI_CustomDialog_SetSubwindowMode(ArkUI_CustomDialogOptions* options, bool showInSubwindow)
291 {
292 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
293 if (!impl || !options) {
294 return ARKUI_ERROR_CODE_PARAM_INVALID;
295 }
296 return impl->getDialogAPI()->setSubwindowMode(options->handle, showInSubwindow);
297 }
298
OH_ArkUI_CustomDialog_SetMask(ArkUI_CustomDialogOptions * options,uint32_t maskColor,const ArkUI_Rect * maskRect)299 int32_t OH_ArkUI_CustomDialog_SetMask(
300 ArkUI_CustomDialogOptions* options, uint32_t maskColor, const ArkUI_Rect* maskRect)
301 {
302 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
303 if (!impl || !options) {
304 return ARKUI_ERROR_CODE_PARAM_INVALID;
305 }
306 if (maskRect) {
307 ArkUIRect rect = { maskRect->x, maskRect->y, maskRect->width, maskRect->height };
308 return impl->getDialogAPI()->setMask(options->handle, maskColor, &rect);
309 } else {
310 return impl->getDialogAPI()->setMask(options->handle, maskColor, nullptr);
311 }
312 }
313
OH_ArkUI_CustomDialog_SetKeyboardAvoidMode(ArkUI_CustomDialogOptions * options,ArkUI_KeyboardAvoidMode keyboardAvoidMode)314 int32_t OH_ArkUI_CustomDialog_SetKeyboardAvoidMode(
315 ArkUI_CustomDialogOptions* options, ArkUI_KeyboardAvoidMode keyboardAvoidMode)
316 {
317 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
318 if (!impl || !options) {
319 return ARKUI_ERROR_CODE_PARAM_INVALID;
320 }
321 return impl->getDialogAPI()->setKeyboardAvoidMode(options->handle, keyboardAvoidMode);
322 }
323
OH_ArkUI_CustomDialog_SetHoverModeEnabled(ArkUI_CustomDialogOptions * options,bool enabled)324 int32_t OH_ArkUI_CustomDialog_SetHoverModeEnabled(ArkUI_CustomDialogOptions* options, bool enabled)
325 {
326 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
327 if (!impl || !options) {
328 return ARKUI_ERROR_CODE_PARAM_INVALID;
329 }
330 return impl->getDialogAPI()->enableHoverMode(options->handle, enabled);
331 }
332
OH_ArkUI_CustomDialog_SetHoverModeArea(ArkUI_CustomDialogOptions * options,ArkUI_HoverModeAreaType hoverModeAreaType)333 int32_t OH_ArkUI_CustomDialog_SetHoverModeArea(
334 ArkUI_CustomDialogOptions* options, ArkUI_HoverModeAreaType hoverModeAreaType)
335 {
336 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
337 if (!impl || !options) {
338 return ARKUI_ERROR_CODE_PARAM_INVALID;
339 }
340 return impl->getDialogAPI()->setHoverModeArea(options->handle, hoverModeAreaType);
341 }
342
OH_ArkUI_CustomDialog_RegisterOnWillDismissCallback(ArkUI_CustomDialogOptions * options,void * userData,void (* callback)(ArkUI_DialogDismissEvent * event))343 int32_t OH_ArkUI_CustomDialog_RegisterOnWillDismissCallback(
344 ArkUI_CustomDialogOptions* options, void* userData, void (*callback)(ArkUI_DialogDismissEvent* event))
345 {
346 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
347 if (!impl || !options) {
348 return ARKUI_ERROR_CODE_PARAM_INVALID;
349 }
350 return impl->getDialogAPI()->registerOnWillDismissWithUserData(options->handle, userData, callback);
351 }
352
OH_ArkUI_CustomDialog_RegisterOnWillAppearCallback(ArkUI_CustomDialogOptions * options,void * userData,void (* callback)(void * userData))353 int32_t OH_ArkUI_CustomDialog_RegisterOnWillAppearCallback(
354 ArkUI_CustomDialogOptions* options, void* userData, void (*callback)(void* userData))
355 {
356 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
357 if (!impl || !options) {
358 return ARKUI_ERROR_CODE_PARAM_INVALID;
359 }
360 return impl->getDialogAPI()->registerOnWillAppear(options->handle, userData, callback);
361 }
362
OH_ArkUI_CustomDialog_RegisterOnDidAppearCallback(ArkUI_CustomDialogOptions * options,void * userData,void (* callback)(void * userData))363 int32_t OH_ArkUI_CustomDialog_RegisterOnDidAppearCallback(
364 ArkUI_CustomDialogOptions* options, void* userData, void (*callback)(void* userData))
365 {
366 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
367 if (!impl || !options) {
368 return ARKUI_ERROR_CODE_PARAM_INVALID;
369 }
370 return impl->getDialogAPI()->registerOnDidAppear(options->handle, userData, callback);
371 }
372
OH_ArkUI_CustomDialog_RegisterOnWillDisappearCallback(ArkUI_CustomDialogOptions * options,void * userData,void (* callback)(void * userData))373 int32_t OH_ArkUI_CustomDialog_RegisterOnWillDisappearCallback(
374 ArkUI_CustomDialogOptions* options, void* userData, void (*callback)(void* userData))
375 {
376 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
377 if (!impl || !options) {
378 return ARKUI_ERROR_CODE_PARAM_INVALID;
379 }
380 return impl->getDialogAPI()->registerOnWillDisappear(options->handle, userData, callback);
381 }
382
OH_ArkUI_CustomDialog_RegisterOnDidDisappearCallback(ArkUI_CustomDialogOptions * options,void * userData,void (* callback)(void * userData))383 int32_t OH_ArkUI_CustomDialog_RegisterOnDidDisappearCallback(
384 ArkUI_CustomDialogOptions* options, void* userData, void (*callback)(void* userData))
385 {
386 const auto* impl = OHOS::Ace::NodeModel::GetFullImpl();
387 if (!impl || !options) {
388 return ARKUI_ERROR_CODE_PARAM_INVALID;
389 }
390 return impl->getDialogAPI()->registerOnDidDisappear(options->handle, userData, callback);
391 }
392
393 #ifdef __cplusplus
394 };
395 #endif
396