@react-native-ohos/ting 1.2.3-rc.1 → 1.2.3-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (C)
|
|
4
|
+
* Copyright (C) 2025 Huawei Device Co., Ltd.
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -23,24 +23,24 @@
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
import { TurboModule } from '@rnoh/react-native-openharmony/ts';
|
|
26
|
-
import { TM } from '
|
|
27
|
-
import { ComponentContent,
|
|
28
|
-
import {
|
|
26
|
+
import { TM } from '../ets/generated/ts';
|
|
27
|
+
import { ComponentContent, promptAction, window } from '@kit.ArkUI';
|
|
28
|
+
import { AlertBuilderData, AlertOptions, Options, ToastBuilderData, ToastOptions , ToastQueueItem, AlertQueueItem} from './Type';
|
|
29
29
|
import { vibrator } from '@kit.SensorServiceKit';
|
|
30
30
|
import { common2D, drawing } from '@kit.ArkGraphics2D';
|
|
31
31
|
|
|
32
32
|
@Builder
|
|
33
|
-
function buildToast(
|
|
33
|
+
function buildToast(toastBuilderData: ToastBuilderData) {
|
|
34
34
|
Row() {
|
|
35
|
-
if (!toastOptions.icon?.uri) {
|
|
36
|
-
if (toastOptions.preset === 'spinner') {
|
|
35
|
+
if (!toastBuilderData.toastOptions.icon?.uri) {
|
|
36
|
+
if (toastBuilderData.toastOptions.preset === 'spinner') {
|
|
37
37
|
Progress({ value: 0, total: 100, type: ProgressType.Ring })
|
|
38
38
|
.width('100px')
|
|
39
39
|
.height('100px')
|
|
40
|
-
.color(toastOptions.progressColor || '#008080')
|
|
40
|
+
.color(toastBuilderData.toastOptions.progressColor || '#008080')
|
|
41
41
|
.style({ strokeWidth: 5, status: ProgressStatus.LOADING })
|
|
42
42
|
.margin({ left: 7 })
|
|
43
|
-
} else if (toastOptions.preset === 'error') {
|
|
43
|
+
} else if (toastBuilderData.toastOptions.preset === 'error') {
|
|
44
44
|
Path()
|
|
45
45
|
.commands('M52.5 44.17 L81.67 15 L90 23.33 L60.83 52.5 L90 81.67 L81.67 90 L52.5 60.83 L23.33 90 ' +
|
|
46
46
|
'L15 81.67 L44.17 52.5 L15 23.33 L23.33 15 L52.5 44.17 Z')
|
|
@@ -49,7 +49,7 @@ function buildToast(toastOptions: ToastOptions) {
|
|
|
49
49
|
.fill(Color.Red)
|
|
50
50
|
.stroke(Color.Red)
|
|
51
51
|
.margin({ left: 7 })
|
|
52
|
-
} else if (toastOptions.preset === 'done') {
|
|
52
|
+
} else if (toastBuilderData.toastOptions.preset === 'done') {
|
|
53
53
|
Polyline()
|
|
54
54
|
.width('100px')
|
|
55
55
|
.height('100px')
|
|
@@ -57,7 +57,7 @@ function buildToast(toastOptions: ToastOptions) {
|
|
|
57
57
|
.fillOpacity(0)
|
|
58
58
|
.stroke(Color.Green)
|
|
59
59
|
.strokeWidth(4)
|
|
60
|
-
} else if (toastOptions.preset === 'none') {
|
|
60
|
+
} else if (toastBuilderData.toastOptions.preset === 'none') {
|
|
61
61
|
// there is no component.
|
|
62
62
|
} else {
|
|
63
63
|
Polyline()
|
|
@@ -69,22 +69,22 @@ function buildToast(toastOptions: ToastOptions) {
|
|
|
69
69
|
.strokeWidth(4)
|
|
70
70
|
}
|
|
71
71
|
} else {
|
|
72
|
-
if (typeof toastOptions.icon?.uri === 'string') {
|
|
73
|
-
if (!toastOptions.icon?.tintColor) {
|
|
74
|
-
Image(toastOptions.icon.uri as string)
|
|
72
|
+
if (typeof toastBuilderData.toastOptions.icon?.uri === 'string') {
|
|
73
|
+
if (!toastBuilderData.toastOptions.icon?.tintColor) {
|
|
74
|
+
Image(toastBuilderData.toastOptions.icon.uri as string)
|
|
75
75
|
.objectFit(ImageFit.Contain)
|
|
76
76
|
.autoResize(true)
|
|
77
|
-
.width(toastOptions.icon.size || '100px')
|
|
78
|
-
.height(toastOptions.icon.size || '100px')
|
|
77
|
+
.width(toastBuilderData.toastOptions.icon.size || '100px')
|
|
78
|
+
.height(toastBuilderData.toastOptions.icon.size || '100px')
|
|
79
79
|
.borderRadius(17.5)
|
|
80
80
|
.margin({ left: 7 })
|
|
81
81
|
} else {
|
|
82
|
-
Image(toastOptions.icon?.uri as string)
|
|
82
|
+
Image(toastBuilderData.toastOptions.icon?.uri as string)
|
|
83
83
|
.objectFit(ImageFit.Contain)
|
|
84
84
|
.autoResize(true)
|
|
85
|
-
.width(toastOptions.icon.size || '100px')
|
|
86
|
-
.height(toastOptions.icon.size || '100px')
|
|
87
|
-
.colorFilter(drawing.ColorFilter.createBlendModeColorFilter(toastOptions.icon.tintColor as common2D.Color
|
|
85
|
+
.width(toastBuilderData.toastOptions.icon.size || '100px')
|
|
86
|
+
.height(toastBuilderData.toastOptions.icon.size || '100px')
|
|
87
|
+
.colorFilter(drawing.ColorFilter.createBlendModeColorFilter(toastBuilderData.toastOptions.icon.tintColor as common2D.Color
|
|
88
88
|
, drawing.BlendMode.SRC_IN))
|
|
89
89
|
.borderRadius(17.5)
|
|
90
90
|
.margin({ left: 7 })
|
|
@@ -93,17 +93,17 @@ function buildToast(toastOptions: ToastOptions) {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
Column() {
|
|
96
|
-
Text(toastOptions.title || '')
|
|
96
|
+
Text(toastBuilderData.toastOptions.title || '')
|
|
97
97
|
.fontSize(13)
|
|
98
|
-
.fontColor(toastOptions.titleColor || '#000000')
|
|
98
|
+
.fontColor(toastBuilderData.toastOptions.titleColor || '#000000')
|
|
99
99
|
.alignSelf(ItemAlign.Center)
|
|
100
100
|
.maxLines(1)
|
|
101
101
|
.textOverflow({ overflow: TextOverflow.Ellipsis })
|
|
102
102
|
.margin({ bottom: 5 })
|
|
103
103
|
|
|
104
|
-
Text(toastOptions.message || '')
|
|
104
|
+
Text(toastBuilderData.toastOptions.message || '')
|
|
105
105
|
.fontSize(12)
|
|
106
|
-
.fontColor(toastOptions.messageColor || '#BEBEBE')
|
|
106
|
+
.fontColor(toastBuilderData.toastOptions.messageColor || '#BEBEBE')
|
|
107
107
|
.alignSelf(ItemAlign.Center)
|
|
108
108
|
.maxLines(1)
|
|
109
109
|
.textOverflow({ overflow: TextOverflow.Ellipsis })
|
|
@@ -119,23 +119,37 @@ function buildToast(toastOptions: ToastOptions) {
|
|
|
119
119
|
.height('auto')
|
|
120
120
|
.width('auto')
|
|
121
121
|
.borderRadius('50%')
|
|
122
|
-
.backgroundColor(toastOptions.backgroundColor || '#FFFFFF')
|
|
122
|
+
.backgroundColor(toastBuilderData.toastOptions.backgroundColor || '#FFFFFF')
|
|
123
123
|
.padding(5)
|
|
124
|
+
.gesture(
|
|
125
|
+
PanGesture({
|
|
126
|
+
distance: 5,
|
|
127
|
+
fingers: 1,
|
|
128
|
+
direction: PanDirection.All
|
|
129
|
+
})
|
|
130
|
+
.onActionUpdate((event: GestureEvent) => {
|
|
131
|
+
// 处理滑动数据
|
|
132
|
+
if (toastBuilderData.toastOptions.shouldDismissByDrag && toastBuilderData.onDismiss) {
|
|
133
|
+
toastBuilderData.onDismiss();
|
|
134
|
+
}
|
|
135
|
+
})
|
|
136
|
+
)
|
|
124
137
|
}
|
|
125
138
|
|
|
126
139
|
@Builder
|
|
127
|
-
function buildAlert(
|
|
140
|
+
function buildAlert(alertBuilderData: AlertBuilderData) {
|
|
128
141
|
Column() {
|
|
129
|
-
|
|
130
|
-
|
|
142
|
+
// 图标部分
|
|
143
|
+
if (!alertBuilderData.alertOptions.icon?.uri) {
|
|
144
|
+
if (alertBuilderData.alertOptions.preset === 'spinner') {
|
|
131
145
|
Progress({ value: 0, total: 100, type: ProgressType.Ring })
|
|
132
146
|
.width('150px')
|
|
133
147
|
.height('150px')
|
|
134
|
-
.color(alertOptions.progressColor || '#008080')
|
|
148
|
+
.color(alertBuilderData.alertOptions.progressColor || '#008080')
|
|
135
149
|
.alignSelf(ItemAlign.Center)
|
|
136
150
|
.style({ strokeWidth: 5, status: ProgressStatus.LOADING })
|
|
137
151
|
.margin({ top: 30 })
|
|
138
|
-
} else if (alertOptions.preset === 'error') {
|
|
152
|
+
} else if (alertBuilderData.alertOptions.preset === 'error') {
|
|
139
153
|
Path()
|
|
140
154
|
.commands('M 107 140 L 165.34 82.00 L 182.00 98.32 L 123.67 156.67 L 182.00 215.00 L 165.34 231.67 ' +
|
|
141
155
|
'L 107.00 173.33 L 49.33 231.67 L 32 215.00 L 90.34 156.67 L 32 98.32 L 49.33 82.00 L 107.00 140.00 Z')
|
|
@@ -144,7 +158,7 @@ function buildAlert(alertOptions: AlertOptions) {
|
|
|
144
158
|
.fill(Color.Red)
|
|
145
159
|
.stroke(Color.Red)
|
|
146
160
|
.margin({ top: 20 })
|
|
147
|
-
} else if (alertOptions.preset === 'done') {
|
|
161
|
+
} else if (alertBuilderData.alertOptions.preset === 'done') {
|
|
148
162
|
Polyline()
|
|
149
163
|
.width('225px')
|
|
150
164
|
.height('225px')
|
|
@@ -153,7 +167,7 @@ function buildAlert(alertOptions: AlertOptions) {
|
|
|
153
167
|
.stroke(Color.Green)
|
|
154
168
|
.strokeWidth(7)
|
|
155
169
|
.margin({ top: 15 })
|
|
156
|
-
} else if (alertOptions.preset === 'none') {
|
|
170
|
+
} else if (alertBuilderData.alertOptions.preset === 'none') {
|
|
157
171
|
Blank()
|
|
158
172
|
.height('25px')
|
|
159
173
|
.margin({ top: 15 })
|
|
@@ -168,39 +182,40 @@ function buildAlert(alertOptions: AlertOptions) {
|
|
|
168
182
|
.margin({ top: 15 })
|
|
169
183
|
}
|
|
170
184
|
} else {
|
|
171
|
-
if (typeof alertOptions.icon?.uri === 'string') {
|
|
172
|
-
if (!alertOptions.icon.tintColor) {
|
|
173
|
-
Image(alertOptions.icon.uri as string)
|
|
185
|
+
if (typeof alertBuilderData.alertOptions.icon?.uri === 'string') {
|
|
186
|
+
if (!alertBuilderData.alertOptions.icon.tintColor) {
|
|
187
|
+
Image(alertBuilderData.alertOptions.icon.uri as string)
|
|
174
188
|
.objectFit(ImageFit.Contain)
|
|
175
189
|
.autoResize(true)
|
|
176
|
-
.width(alertOptions.icon.size || '225px')
|
|
177
|
-
.height(alertOptions.icon.size || '225px')
|
|
190
|
+
.width(alertBuilderData.alertOptions.icon.size || '225px')
|
|
191
|
+
.height(alertBuilderData.alertOptions.icon.size || '225px')
|
|
178
192
|
.margin({ top: 20 })
|
|
179
193
|
} else {
|
|
180
|
-
Image(alertOptions.icon.uri as string)
|
|
194
|
+
Image(alertBuilderData.alertOptions.icon.uri as string)
|
|
181
195
|
.objectFit(ImageFit.Contain)
|
|
182
196
|
.autoResize(true)
|
|
183
|
-
.width(alertOptions.icon.size || '225px')
|
|
184
|
-
.height(alertOptions.icon.size || '225px')
|
|
185
|
-
.colorFilter(drawing.ColorFilter.createBlendModeColorFilter(alertOptions.icon.tintColor as common2D.Color
|
|
197
|
+
.width(alertBuilderData.alertOptions.icon.size || '225px')
|
|
198
|
+
.height(alertBuilderData.alertOptions.icon.size || '225px')
|
|
199
|
+
.colorFilter(drawing.ColorFilter.createBlendModeColorFilter(alertBuilderData.alertOptions.icon.tintColor as common2D.Color
|
|
186
200
|
, drawing.BlendMode.SRC_IN))
|
|
187
201
|
.margin({ top: 20 })
|
|
188
202
|
}
|
|
189
203
|
}
|
|
190
204
|
}
|
|
191
205
|
|
|
206
|
+
// 文本部分
|
|
192
207
|
Column() {
|
|
193
|
-
Text(alertOptions.title || '')
|
|
208
|
+
Text(alertBuilderData.alertOptions.title || '')
|
|
194
209
|
.fontSize(16)
|
|
195
|
-
.fontColor(alertOptions.titleColor || '#000000')
|
|
210
|
+
.fontColor(alertBuilderData.alertOptions.titleColor || '#000000')
|
|
196
211
|
.alignSelf(ItemAlign.Center)
|
|
197
212
|
.maxLines(1)
|
|
198
213
|
.textOverflow({ overflow: TextOverflow.Ellipsis })
|
|
199
214
|
.margin({ bottom: 5 })
|
|
200
215
|
|
|
201
|
-
Text(alertOptions.message || '')
|
|
216
|
+
Text(alertBuilderData.alertOptions.message || '')
|
|
202
217
|
.fontSize(14)
|
|
203
|
-
.fontColor(alertOptions.messageColor || '#BEBEBE')
|
|
218
|
+
.fontColor(alertBuilderData.alertOptions.messageColor || '#BEBEBE')
|
|
204
219
|
.alignSelf(ItemAlign.Center)
|
|
205
220
|
.maxLines(1)
|
|
206
221
|
.textOverflow({ overflow: TextOverflow.Ellipsis })
|
|
@@ -219,19 +234,23 @@ function buildAlert(alertOptions: AlertOptions) {
|
|
|
219
234
|
minHeight: '200px',
|
|
220
235
|
maxHeight: Infinity
|
|
221
236
|
})
|
|
222
|
-
.backgroundColor(alertOptions.backgroundColor || '#FFFFFF')
|
|
223
|
-
.borderRadius(alertOptions.borderRadius || '24')
|
|
237
|
+
.backgroundColor(alertBuilderData.alertOptions.backgroundColor || '#FFFFFF')
|
|
238
|
+
.borderRadius(alertBuilderData.alertOptions.borderRadius || '24')
|
|
239
|
+
// 添加点击事件 - 当 shouldDismissByTap 为 true 时,点击弹窗内容区域关闭弹窗
|
|
240
|
+
.onClick(() => {
|
|
241
|
+
if (alertBuilderData.alertOptions.shouldDismissByTap && alertBuilderData.onDismiss) {
|
|
242
|
+
alertBuilderData.onDismiss();
|
|
243
|
+
}
|
|
244
|
+
});
|
|
224
245
|
}
|
|
225
246
|
|
|
226
247
|
export class TingTurboModule extends TurboModule implements TM.Ting.Spec {
|
|
227
|
-
private alertPromptAction: PromptAction | null = null;
|
|
228
|
-
private alertComponentContent: ComponentContent<object> | null = null;
|
|
229
|
-
private timeoutId: number | null = null;
|
|
230
|
-
private isCancelled: boolean = false;
|
|
231
248
|
private toastOptionInit: ToastOptions | undefined = undefined;
|
|
232
249
|
private alertOptionInit: AlertOptions | undefined = undefined;
|
|
233
250
|
private PREFIX = "asset://";
|
|
234
251
|
private RAWFILE_PREFIX = "resource://RAWFILE/assets/";
|
|
252
|
+
private toastQueue: Array<ToastQueueItem> = [];
|
|
253
|
+
private alertQueue: Array<AlertQueueItem> = [];
|
|
235
254
|
|
|
236
255
|
async showToast(toastOptions: ToastOptions) {
|
|
237
256
|
if (this.toastOptionInit) {
|
|
@@ -244,51 +263,97 @@ export class TingTurboModule extends TurboModule implements TM.Ting.Spec {
|
|
|
244
263
|
|
|
245
264
|
let option: promptAction.BaseDialogOptions = {
|
|
246
265
|
alignment: DialogAlignment.Top,
|
|
247
|
-
autoCancel:
|
|
266
|
+
autoCancel: false,
|
|
248
267
|
offset: { dx: 0, dy: 50 },
|
|
249
|
-
isModal:
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
.animation({ curve: Curve.Sharp })
|
|
253
|
-
|
|
268
|
+
isModal: false,
|
|
269
|
+
transition: TransitionEffect.asymmetric(
|
|
270
|
+
TransitionEffect.move(TransitionEdge.TOP).animation({ curve: Curve.Sharp }),
|
|
271
|
+
TransitionEffect.move(TransitionEdge.TOP).animation({ curve: Curve.Sharp })
|
|
272
|
+
),
|
|
254
273
|
};
|
|
255
274
|
|
|
256
275
|
if (toastOptions.position === "bottom") {
|
|
257
276
|
option.alignment = DialogAlignment.Bottom;
|
|
258
277
|
option.offset = { dx: 0, dy: -50 };
|
|
259
|
-
option.transition = TransitionEffect.asymmetric(
|
|
260
|
-
.animation({ curve: Curve.Sharp }),
|
|
261
|
-
TransitionEffect.move(TransitionEdge.BOTTOM).animation({ curve: Curve.Sharp })
|
|
278
|
+
option.transition = TransitionEffect.asymmetric(
|
|
279
|
+
TransitionEffect.move(TransitionEdge.BOTTOM).animation({ curve: Curve.Sharp }),
|
|
280
|
+
TransitionEffect.move(TransitionEdge.BOTTOM).animation({ curve: Curve.Sharp })
|
|
281
|
+
);
|
|
262
282
|
}
|
|
263
283
|
|
|
264
|
-
if
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
if (toastOptions.duration && toastOptions.duration !== 3000) {
|
|
269
|
-
toastOptions.duration = toastOptions.duration * 1000;
|
|
270
|
-
if (toastOptions.duration > 0 && toastOptions.duration <= 1000) {
|
|
271
|
-
toastOptions.duration += 500;
|
|
284
|
+
if(toastOptions.duration){
|
|
285
|
+
if (toastOptions.duration > 0 && toastOptions.duration <= 1) {
|
|
286
|
+
toastOptions.duration += 0.5;
|
|
272
287
|
}
|
|
273
288
|
}
|
|
289
|
+
const duration = toastOptions.duration ? toastOptions.duration * 1000 : 3000;
|
|
274
290
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
291
|
+
// 创建关闭函数
|
|
292
|
+
const dismissToast = () => {
|
|
293
|
+
this.clearAllToasts();
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const toastBuilderData: ToastBuilderData = {
|
|
297
|
+
toastOptions: toastOptions,
|
|
298
|
+
onDismiss: dismissToast
|
|
299
|
+
};
|
|
278
300
|
|
|
279
|
-
const
|
|
301
|
+
const componentContent = new ComponentContent(uiContext!, wrapBuilder(buildToast), toastBuilderData);
|
|
280
302
|
|
|
281
303
|
try {
|
|
282
|
-
|
|
304
|
+
// 显示前先清理之前的Toast
|
|
305
|
+
this.clearAllToasts();
|
|
306
|
+
|
|
307
|
+
// 显示新Toast
|
|
308
|
+
promptAction?.openCustomDialog(componentContent, option);
|
|
309
|
+
|
|
310
|
+
// 添加到队列
|
|
311
|
+
const timeoutId = setTimeout(() => {
|
|
312
|
+
promptAction?.closeCustomDialog(componentContent);
|
|
313
|
+
this.removeToastFromQueue(componentContent);
|
|
314
|
+
}, duration);
|
|
315
|
+
|
|
316
|
+
this.toastQueue.push({
|
|
317
|
+
componentContent,
|
|
318
|
+
promptAction,
|
|
319
|
+
timeoutId
|
|
320
|
+
});
|
|
283
321
|
|
|
284
|
-
setTimeout(() => {
|
|
285
|
-
promptAction?.closeCustomDialog(contentNode);
|
|
286
|
-
}, toastOptions.duration || 3000);
|
|
287
322
|
} catch (error) {
|
|
288
323
|
throw new Error('Can\'t open CustomDialog: ' + error);
|
|
289
324
|
}
|
|
290
325
|
}
|
|
291
326
|
|
|
327
|
+
private clearAllToasts() {
|
|
328
|
+
// 清理所有正在显示的Toast
|
|
329
|
+
this.toastQueue.forEach(item => {
|
|
330
|
+
if (item.timeoutId) {
|
|
331
|
+
clearTimeout(item.timeoutId);
|
|
332
|
+
}
|
|
333
|
+
try {
|
|
334
|
+
item.promptAction.closeCustomDialog(item.componentContent);
|
|
335
|
+
} catch (e) {
|
|
336
|
+
// 忽略关闭错误
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
this.toastQueue = [];
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private removeToastFromQueue(componentContent: ComponentContent<object>) {
|
|
343
|
+
const index = this.toastQueue.findIndex(item => item.componentContent === componentContent);
|
|
344
|
+
if (index !== -1) {
|
|
345
|
+
const item = this.toastQueue[index];
|
|
346
|
+
if (item.timeoutId) {
|
|
347
|
+
clearTimeout(item.timeoutId);
|
|
348
|
+
}
|
|
349
|
+
this.toastQueue.splice(index, 1);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
dismissToast(): void {
|
|
354
|
+
this.clearAllToasts();
|
|
355
|
+
}
|
|
356
|
+
|
|
292
357
|
async showAlert(alertOptions: AlertOptions) {
|
|
293
358
|
if (this.alertOptionInit) {
|
|
294
359
|
alertOptions = this.mergeAlertOptions(this.alertOptionInit, alertOptions);
|
|
@@ -296,52 +361,91 @@ export class TingTurboModule extends TurboModule implements TM.Ting.Spec {
|
|
|
296
361
|
|
|
297
362
|
const windowValue = await window.getLastWindow(this.ctx.uiAbilityContext);
|
|
298
363
|
const uiContext = windowValue?.getUIContext();
|
|
299
|
-
|
|
364
|
+
const promptAction = uiContext?.getPromptAction();
|
|
300
365
|
|
|
301
366
|
let option: promptAction.BaseDialogOptions = {
|
|
302
367
|
alignment: DialogAlignment.Center,
|
|
303
|
-
autoCancel:
|
|
304
|
-
isModal:
|
|
368
|
+
autoCancel: false,
|
|
369
|
+
isModal: false,
|
|
305
370
|
maskColor: '#00FF0000'
|
|
306
371
|
};
|
|
307
372
|
|
|
308
|
-
if (alertOptions.shouldDismissByTap === false) {
|
|
309
|
-
option.autoCancel = false;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
373
|
if (alertOptions.backdropOpacity && alertOptions.backdropOpacity >= 1) {
|
|
313
374
|
option.maskColor = '#000000';
|
|
314
375
|
}
|
|
315
376
|
|
|
316
|
-
|
|
317
|
-
alertOptions.duration = alertOptions.duration * 1000;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
if (this.timeoutId) {
|
|
321
|
-
clearTimeout(this.timeoutId);
|
|
322
|
-
this.isCancelled = true; // 标记为取消状态
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
this.isCancelled = false;
|
|
377
|
+
const duration = alertOptions.duration ? alertOptions.duration * 1000 : 3000;
|
|
326
378
|
|
|
327
379
|
if (alertOptions.haptic) {
|
|
328
380
|
option.onDidAppear = () => this.vibrator(alertOptions.haptic as string);
|
|
329
381
|
}
|
|
330
382
|
|
|
331
|
-
|
|
383
|
+
// 创建关闭函数
|
|
384
|
+
const dismissAlert = () => {
|
|
385
|
+
this.clearAllAlerts();
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const alertBuilderData: AlertBuilderData = {
|
|
389
|
+
alertOptions: alertOptions,
|
|
390
|
+
onDismiss: dismissAlert
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
const componentContent = new ComponentContent(uiContext!, wrapBuilder(buildAlert), alertBuilderData);
|
|
332
394
|
|
|
333
395
|
try {
|
|
334
|
-
|
|
396
|
+
// 显示前先清理之前的Alert
|
|
397
|
+
this.clearAllAlerts();
|
|
335
398
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
399
|
+
// 显示新Alert
|
|
400
|
+
promptAction?.openCustomDialog(componentContent, option);
|
|
401
|
+
|
|
402
|
+
// 添加到队列
|
|
403
|
+
const timeoutId = setTimeout(() => {
|
|
404
|
+
promptAction?.closeCustomDialog(componentContent);
|
|
405
|
+
this.removeAlertFromQueue(componentContent);
|
|
406
|
+
}, duration);
|
|
407
|
+
|
|
408
|
+
this.alertQueue.push({
|
|
409
|
+
componentContent,
|
|
410
|
+
promptAction,
|
|
411
|
+
timeoutId
|
|
412
|
+
});
|
|
339
413
|
|
|
340
414
|
} catch (error) {
|
|
341
415
|
throw new Error('Can\'t open CustomDialog: ' + error);
|
|
342
416
|
}
|
|
343
417
|
}
|
|
344
418
|
|
|
419
|
+
private clearAllAlerts() {
|
|
420
|
+
// 清理所有正在显示的Alert
|
|
421
|
+
this.alertQueue.forEach(item => {
|
|
422
|
+
if (item.timeoutId) {
|
|
423
|
+
clearTimeout(item.timeoutId);
|
|
424
|
+
}
|
|
425
|
+
try {
|
|
426
|
+
item.promptAction.closeCustomDialog(item.componentContent);
|
|
427
|
+
} catch (e) {
|
|
428
|
+
// 忽略关闭错误
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
this.alertQueue = [];
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
private removeAlertFromQueue(componentContent: ComponentContent<object>) {
|
|
435
|
+
const index = this.alertQueue.findIndex(item => item.componentContent === componentContent);
|
|
436
|
+
if (index !== -1) {
|
|
437
|
+
const item = this.alertQueue[index];
|
|
438
|
+
if (item.timeoutId) {
|
|
439
|
+
clearTimeout(item.timeoutId);
|
|
440
|
+
}
|
|
441
|
+
this.alertQueue.splice(index, 1);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
dismissAlert(): void {
|
|
446
|
+
this.clearAllAlerts();
|
|
447
|
+
}
|
|
448
|
+
|
|
345
449
|
toast(options: Object): void {
|
|
346
450
|
if (options && typeof options === 'object') {
|
|
347
451
|
const toastOptions = options as ToastOptions;
|
|
@@ -374,18 +478,6 @@ export class TingTurboModule extends TurboModule implements TM.Ting.Spec {
|
|
|
374
478
|
}
|
|
375
479
|
}
|
|
376
480
|
|
|
377
|
-
dismissAlert(): void {
|
|
378
|
-
if (this.timeoutId) {
|
|
379
|
-
clearTimeout(this.timeoutId);
|
|
380
|
-
this.timeoutId = null;
|
|
381
|
-
this.isCancelled = true;
|
|
382
|
-
|
|
383
|
-
if (this.alertPromptAction && this.alertComponentContent) {
|
|
384
|
-
this.alertPromptAction.closeCustomDialog(this.alertComponentContent);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
|
|
389
481
|
setup(options: Object): void {
|
|
390
482
|
const opts = options as Options;
|
|
391
483
|
this.alertOptionInit = opts.alert;
|
|
@@ -511,4 +603,4 @@ export class TingTurboModule extends TurboModule implements TM.Ting.Spec {
|
|
|
511
603
|
}
|
|
512
604
|
return path;
|
|
513
605
|
}
|
|
514
|
-
}
|
|
606
|
+
}
|
|
@@ -23,6 +23,28 @@
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
import { common2D } from '@kit.ArkGraphics2D';
|
|
26
|
+
import { ComponentContent,PromptAction } from '@kit.ArkUI';
|
|
27
|
+
export interface ToastQueueItem {
|
|
28
|
+
componentContent: ComponentContent<object>;
|
|
29
|
+
promptAction: PromptAction;
|
|
30
|
+
timeoutId: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface AlertQueueItem {
|
|
34
|
+
componentContent: ComponentContent<object>;
|
|
35
|
+
promptAction: PromptAction;
|
|
36
|
+
timeoutId: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface AlertBuilderData {
|
|
40
|
+
alertOptions: AlertOptions;
|
|
41
|
+
onDismiss: () => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ToastBuilderData {
|
|
45
|
+
toastOptions: ToastOptions;
|
|
46
|
+
onDismiss: () => void;
|
|
47
|
+
}
|
|
26
48
|
|
|
27
49
|
export interface Icon {
|
|
28
50
|
size?: number;
|
package/harmony/ting.har
CHANGED
|
Binary file
|