-
Notifications
You must be signed in to change notification settings - Fork 30.6k
/
Copy pathnotifications.ts
739 lines (557 loc) · 19.3 KB
/
notifications.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { INotification, INotificationHandle, INotificationActions, INotificationProgress, NoOpNotification, Severity, NotificationMessage, IPromptChoice, IStatusMessageOptions, NotificationsFilter, INotificationProgressProperties } from 'vs/platform/notification/common/notification';
import { toErrorMessage } from 'vs/base/common/errorMessage';
import { Event, Emitter } from 'vs/base/common/event';
import { Disposable, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { isPromiseCanceledError } from 'vs/base/common/errors';
import { Action } from 'vs/base/common/actions';
import { isErrorWithActions } from 'vs/base/common/errorsWithActions';
import { find, equals } from 'vs/base/common/arrays';
import { parseLinkedText, LinkedText } from 'vs/base/common/linkedText';
export interface INotificationsModel {
//#region Notifications as Toasts/Center
readonly notifications: INotificationViewItem[];
readonly onDidChangeNotification: Event<INotificationChangeEvent>;
readonly onDidChangeFilter: Event<NotificationsFilter>;
addNotification(notification: INotification): INotificationHandle;
setFilter(filter: NotificationsFilter): void;
//#endregion
//#region Notifications as Status
readonly statusMessage: IStatusMessageViewItem | undefined;
readonly onDidChangeStatusMessage: Event<IStatusMessageChangeEvent>;
showStatusMessage(message: NotificationMessage, options?: IStatusMessageOptions): IDisposable;
//#endregion
}
export const enum NotificationChangeType {
/**
* A notification was added.
*/
ADD,
/**
* A notification changed. Check `detail` property
* on the event for additional information.
*/
CHANGE,
/**
* A notification expanded or collapsed.
*/
EXPAND_COLLAPSE,
/**
* A notification was removed.
*/
REMOVE
}
export interface INotificationChangeEvent {
/**
* The index this notification has in the list of notifications.
*/
index: number;
/**
* The notification this change is about.
*/
item: INotificationViewItem;
/**
* The kind of notification change.
*/
kind: NotificationChangeType;
/**
* Additional detail about the item change. Only applies to
* `NotificationChangeType.CHANGE`.
*/
detail?: NotificationViewItemContentChangeKind
}
export const enum StatusMessageChangeType {
ADD,
REMOVE
}
export interface IStatusMessageViewItem {
message: string;
options?: IStatusMessageOptions;
}
export interface IStatusMessageChangeEvent {
/**
* The status message item this change is about.
*/
item: IStatusMessageViewItem;
/**
* The kind of status message change.
*/
kind: StatusMessageChangeType;
}
export class NotificationHandle extends Disposable implements INotificationHandle {
private readonly _onDidClose = this._register(new Emitter<void>());
readonly onDidClose = this._onDidClose.event;
private readonly _onDidChangeVisibility = this._register(new Emitter<boolean>());
readonly onDidChangeVisibility = this._onDidChangeVisibility.event;
constructor(private readonly item: INotificationViewItem, private readonly onClose: (item: INotificationViewItem) => void) {
super();
this.registerListeners();
}
private registerListeners(): void {
// Visibility
this._register(this.item.onDidChangeVisibility(visible => this._onDidChangeVisibility.fire(visible)));
// Closing
Event.once(this.item.onDidClose)(() => {
this._onDidClose.fire();
this.dispose();
});
}
get progress(): INotificationProgress {
return this.item.progress;
}
updateSeverity(severity: Severity): void {
this.item.updateSeverity(severity);
}
updateMessage(message: NotificationMessage): void {
this.item.updateMessage(message);
}
updateActions(actions?: INotificationActions): void {
this.item.updateActions(actions);
}
close(): void {
this.onClose(this.item);
this.dispose();
}
}
export class NotificationsModel extends Disposable implements INotificationsModel {
private static readonly NO_OP_NOTIFICATION = new NoOpNotification();
private readonly _onDidChangeNotification = this._register(new Emitter<INotificationChangeEvent>());
readonly onDidChangeNotification = this._onDidChangeNotification.event;
private readonly _onDidChangeStatusMessage = this._register(new Emitter<IStatusMessageChangeEvent>());
readonly onDidChangeStatusMessage = this._onDidChangeStatusMessage.event;
private readonly _onDidChangeFilter = this._register(new Emitter<NotificationsFilter>());
readonly onDidChangeFilter = this._onDidChangeFilter.event;
private readonly _notifications: INotificationViewItem[] = [];
get notifications(): INotificationViewItem[] { return this._notifications; }
private _statusMessage: IStatusMessageViewItem | undefined;
get statusMessage(): IStatusMessageViewItem | undefined { return this._statusMessage; }
private filter = NotificationsFilter.OFF;
setFilter(filter: NotificationsFilter): void {
this.filter = filter;
this._onDidChangeFilter.fire(filter);
}
addNotification(notification: INotification): INotificationHandle {
const item = this.createViewItem(notification);
if (!item) {
return NotificationsModel.NO_OP_NOTIFICATION; // return early if this is a no-op
}
// Deduplicate
const duplicate = this.findNotification(item);
if (duplicate) {
duplicate.close();
}
// Add to list as first entry
this._notifications.splice(0, 0, item);
// Events
this._onDidChangeNotification.fire({ item, index: 0, kind: NotificationChangeType.ADD });
// Wrap into handle
return new NotificationHandle(item, item => this.onClose(item));
}
private onClose(item: INotificationViewItem): void {
const liveItem = this.findNotification(item);
if (liveItem && liveItem !== item) {
liveItem.close(); // item could have been replaced with another one, make sure to close the live item
} else {
item.close(); // otherwise just close the item that was passed in
}
}
private findNotification(item: INotificationViewItem): INotificationViewItem | undefined {
return find(this._notifications, notification => notification.equals(item));
}
private createViewItem(notification: INotification): INotificationViewItem | undefined {
const item = NotificationViewItem.create(notification, this.filter);
if (!item) {
return undefined;
}
// Item Events
const fireNotificationChangeEvent = (kind: NotificationChangeType, detail?: NotificationViewItemContentChangeKind) => {
const index = this._notifications.indexOf(item);
if (index >= 0) {
this._onDidChangeNotification.fire({ item, index, kind, detail });
}
};
const itemExpansionChangeListener = item.onDidChangeExpansion(() => fireNotificationChangeEvent(NotificationChangeType.EXPAND_COLLAPSE));
const itemContentChangeListener = item.onDidChangeContent(e => fireNotificationChangeEvent(NotificationChangeType.CHANGE, e.kind));
Event.once(item.onDidClose)(() => {
itemExpansionChangeListener.dispose();
itemContentChangeListener.dispose();
const index = this._notifications.indexOf(item);
if (index >= 0) {
this._notifications.splice(index, 1);
this._onDidChangeNotification.fire({ item, index, kind: NotificationChangeType.REMOVE });
}
});
return item;
}
showStatusMessage(message: NotificationMessage, options?: IStatusMessageOptions): IDisposable {
const item = StatusMessageViewItem.create(message, options);
if (!item) {
return Disposable.None;
}
// Remember as current status message and fire events
this._statusMessage = item;
this._onDidChangeStatusMessage.fire({ kind: StatusMessageChangeType.ADD, item });
return toDisposable(() => {
// Only reset status message if the item is still the one we had remembered
if (this._statusMessage === item) {
this._statusMessage = undefined;
this._onDidChangeStatusMessage.fire({ kind: StatusMessageChangeType.REMOVE, item });
}
});
}
}
export interface INotificationViewItem {
readonly severity: Severity;
readonly sticky: boolean;
readonly silent: boolean;
readonly message: INotificationMessage;
readonly source: string | undefined;
readonly actions: INotificationActions | undefined;
readonly progress: INotificationViewItemProgress;
readonly expanded: boolean;
readonly canCollapse: boolean;
readonly hasProgress: boolean;
readonly onDidChangeExpansion: Event<void>;
readonly onDidChangeVisibility: Event<boolean>;
readonly onDidChangeContent: Event<INotificationViewItemContentChangeEvent>;
readonly onDidClose: Event<void>;
expand(): void;
collapse(skipEvents?: boolean): void;
toggle(): void;
updateSeverity(severity: Severity): void;
updateMessage(message: NotificationMessage): void;
updateActions(actions?: INotificationActions): void;
updateVisibility(visible: boolean): void;
close(): void;
equals(item: INotificationViewItem): boolean;
}
export function isNotificationViewItem(obj: unknown): obj is INotificationViewItem {
return obj instanceof NotificationViewItem;
}
export const enum NotificationViewItemContentChangeKind {
SEVERITY,
MESSAGE,
ACTIONS,
PROGRESS
}
export interface INotificationViewItemContentChangeEvent {
kind: NotificationViewItemContentChangeKind;
}
export interface INotificationViewItemProgressState {
infinite?: boolean;
total?: number;
worked?: number;
done?: boolean;
}
export interface INotificationViewItemProgress extends INotificationProgress {
readonly state: INotificationViewItemProgressState;
dispose(): void;
}
export class NotificationViewItemProgress extends Disposable implements INotificationViewItemProgress {
private readonly _state: INotificationViewItemProgressState;
private readonly _onDidChange = this._register(new Emitter<void>());
readonly onDidChange = this._onDidChange.event;
constructor() {
super();
this._state = Object.create(null);
}
get state(): INotificationViewItemProgressState {
return this._state;
}
infinite(): void {
if (this._state.infinite) {
return;
}
this._state.infinite = true;
this._state.total = undefined;
this._state.worked = undefined;
this._state.done = undefined;
this._onDidChange.fire();
}
done(): void {
if (this._state.done) {
return;
}
this._state.done = true;
this._state.infinite = undefined;
this._state.total = undefined;
this._state.worked = undefined;
this._onDidChange.fire();
}
total(value: number): void {
if (this._state.total === value) {
return;
}
this._state.total = value;
this._state.infinite = undefined;
this._state.done = undefined;
this._onDidChange.fire();
}
worked(value: number): void {
if (typeof this._state.worked === 'number') {
this._state.worked += value;
} else {
this._state.worked = value;
}
this._state.infinite = undefined;
this._state.done = undefined;
this._onDidChange.fire();
}
}
export interface IMessageLink {
href: string;
name: string;
title: string;
offset: number;
length: number;
}
export interface INotificationMessage {
raw: string;
original: NotificationMessage;
linkedText: LinkedText;
}
export class NotificationViewItem extends Disposable implements INotificationViewItem {
private static readonly MAX_MESSAGE_LENGTH = 1000;
private _expanded: boolean | undefined;
private _visible: boolean = false;
private _actions: INotificationActions | undefined;
private _progress: NotificationViewItemProgress | undefined;
private readonly _onDidChangeExpansion = this._register(new Emitter<void>());
readonly onDidChangeExpansion = this._onDidChangeExpansion.event;
private readonly _onDidClose = this._register(new Emitter<void>());
readonly onDidClose = this._onDidClose.event;
private readonly _onDidChangeContent = this._register(new Emitter<INotificationViewItemContentChangeEvent>());
readonly onDidChangeContent = this._onDidChangeContent.event;
private readonly _onDidChangeVisibility = this._register(new Emitter<boolean>());
readonly onDidChangeVisibility = this._onDidChangeVisibility.event;
static create(notification: INotification, filter: NotificationsFilter = NotificationsFilter.OFF): INotificationViewItem | undefined {
if (!notification || !notification.message || isPromiseCanceledError(notification.message)) {
return undefined; // we need a message to show
}
let severity: Severity;
if (typeof notification.severity === 'number') {
severity = notification.severity;
} else {
severity = Severity.Info;
}
const message = NotificationViewItem.parseNotificationMessage(notification.message);
if (!message) {
return undefined; // we need a message to show
}
let actions: INotificationActions | undefined;
if (notification.actions) {
actions = notification.actions;
} else if (isErrorWithActions(notification.message)) {
actions = { primary: notification.message.actions };
}
return new NotificationViewItem(severity, notification.sticky, notification.silent || filter === NotificationsFilter.SILENT || (filter === NotificationsFilter.ERROR && notification.severity !== Severity.Error), message, notification.source, notification.progress, actions);
}
private static parseNotificationMessage(input: NotificationMessage): INotificationMessage | undefined {
let message: string | undefined;
if (input instanceof Error) {
message = toErrorMessage(input, false);
} else if (typeof input === 'string') {
message = input;
}
if (!message) {
return undefined; // we need a message to show
}
const raw = message;
// Make sure message is in the limits
if (message.length > NotificationViewItem.MAX_MESSAGE_LENGTH) {
message = `${message.substr(0, NotificationViewItem.MAX_MESSAGE_LENGTH)}...`;
}
// Remove newlines from messages as we do not support that and it makes link parsing hard
message = message.replace(/(\r\n|\n|\r)/gm, ' ').trim();
// Parse Links
const linkedText = parseLinkedText(message);
return { raw, linkedText, original: input };
}
private constructor(
private _severity: Severity,
private _sticky: boolean | undefined,
private _silent: boolean | undefined,
private _message: INotificationMessage,
private _source: string | undefined,
progress: INotificationProgressProperties | undefined,
actions?: INotificationActions
) {
super();
if (progress) {
this.setProgress(progress);
}
this.setActions(actions);
}
private setProgress(progress: INotificationProgressProperties): void {
if (progress.infinite) {
this.progress.infinite();
} else if (progress.total) {
this.progress.total(progress.total);
if (progress.worked) {
this.progress.worked(progress.worked);
}
}
}
private setActions(actions: INotificationActions = { primary: [], secondary: [] }): void {
this._actions = {
primary: Array.isArray(actions.primary) ? actions.primary : [],
secondary: Array.isArray(actions.secondary) ? actions.secondary : []
};
this._expanded = actions.primary && actions.primary.length > 0;
}
get canCollapse(): boolean {
return !this.hasActions;
}
get expanded(): boolean {
return !!this._expanded;
}
get severity(): Severity {
return this._severity;
}
get sticky(): boolean {
if (this._sticky) {
return true; // explicitly sticky
}
const hasActions = this.hasActions;
if (
(hasActions && this._severity === Severity.Error) || // notification errors with actions are sticky
(!hasActions && this._expanded) || // notifications that got expanded are sticky
(this._progress && !this._progress.state.done) // notifications with running progress are sticky
) {
return true;
}
return false; // not sticky
}
get silent(): boolean {
return !!this._silent;
}
private get hasActions(): boolean {
if (!this._actions) {
return false;
}
if (!this._actions.primary) {
return false;
}
return this._actions.primary.length > 0;
}
get hasProgress(): boolean {
return !!this._progress;
}
get progress(): INotificationViewItemProgress {
if (!this._progress) {
this._progress = this._register(new NotificationViewItemProgress());
this._register(this._progress.onDidChange(() => this._onDidChangeContent.fire({ kind: NotificationViewItemContentChangeKind.PROGRESS })));
}
return this._progress;
}
get message(): INotificationMessage {
return this._message;
}
get source(): string | undefined {
return this._source;
}
get actions(): INotificationActions | undefined {
return this._actions;
}
updateSeverity(severity: Severity): void {
this._severity = severity;
this._onDidChangeContent.fire({ kind: NotificationViewItemContentChangeKind.SEVERITY });
}
updateMessage(input: NotificationMessage): void {
const message = NotificationViewItem.parseNotificationMessage(input);
if (!message) {
return;
}
this._message = message;
this._onDidChangeContent.fire({ kind: NotificationViewItemContentChangeKind.MESSAGE });
}
updateActions(actions?: INotificationActions): void {
this.setActions(actions);
this._onDidChangeContent.fire({ kind: NotificationViewItemContentChangeKind.ACTIONS });
}
updateVisibility(visible: boolean): void {
if (this._visible !== visible) {
this._visible = visible;
this._onDidChangeVisibility.fire(visible);
}
}
expand(): void {
if (this._expanded || !this.canCollapse) {
return;
}
this._expanded = true;
this._onDidChangeExpansion.fire();
}
collapse(skipEvents?: boolean): void {
if (!this._expanded || !this.canCollapse) {
return;
}
this._expanded = false;
if (!skipEvents) {
this._onDidChangeExpansion.fire();
}
}
toggle(): void {
if (this._expanded) {
this.collapse();
} else {
this.expand();
}
}
close(): void {
this._onDidClose.fire();
this.dispose();
}
equals(other: INotificationViewItem): boolean {
if (this.hasProgress || other.hasProgress) {
return false;
}
if (this._source !== other.source) {
return false;
}
if (this._message.raw !== other.message.raw) {
return false;
}
const primaryActions = (this._actions && this._actions.primary) || [];
const otherPrimaryActions = (other.actions && other.actions.primary) || [];
return equals(primaryActions, otherPrimaryActions, (a, b) => (a.id + a.label) === (b.id + b.label));
}
}
export class ChoiceAction extends Action {
private readonly _onDidRun = this._register(new Emitter<void>());
readonly onDidRun = this._onDidRun.event;
private readonly _keepOpen: boolean;
constructor(id: string, choice: IPromptChoice) {
super(id, choice.label, undefined, true, () => {
// Pass to runner
choice.run();
// Emit Event
this._onDidRun.fire();
return Promise.resolve();
});
this._keepOpen = !!choice.keepOpen;
}
get keepOpen(): boolean {
return this._keepOpen;
}
}
class StatusMessageViewItem {
static create(notification: NotificationMessage, options?: IStatusMessageOptions): IStatusMessageViewItem | undefined {
if (!notification || isPromiseCanceledError(notification)) {
return undefined; // we need a message to show
}
let message: string | undefined;
if (notification instanceof Error) {
message = toErrorMessage(notification, false);
} else if (typeof notification === 'string') {
message = notification;
}
if (!message) {
return undefined; // we need a message to show
}
return { message, options };
}
}