Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/browser/parts/notifications/notificationsAlerts.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { alert } from '../../../../base/browser/ui/aria/aria.js';
7
import { localize } from '../../../../nls.js';
8
import { INotificationViewItem, INotificationsModel, NotificationChangeType, INotificationChangeEvent, NotificationViewItemContentChangeKind } from '../../../common/notifications.js';
9
import { Disposable } from '../../../../base/common/lifecycle.js';
10
import { toErrorMessage } from '../../../../base/common/errorMessage.js';
11
import { NotificationPriority, Severity } from '../../../../platform/notification/common/notification.js';
12
import { Event } from '../../../../base/common/event.js';
13
14
export class NotificationsAlerts extends Disposable {
15
16
constructor(private readonly model: INotificationsModel) {
17
super();
18
19
// Alert initial notifications if any
20
for (const notification of model.notifications) {
21
this.triggerAriaAlert(notification);
22
}
23
24
this.registerListeners();
25
}
26
27
private registerListeners(): void {
28
this._register(this.model.onDidChangeNotification(e => this.onDidChangeNotification(e)));
29
}
30
31
private onDidChangeNotification(e: INotificationChangeEvent): void {
32
if (e.kind === NotificationChangeType.ADD) {
33
34
// ARIA alert for screen readers
35
this.triggerAriaAlert(e.item);
36
37
// Always log errors to console with full details
38
if (e.item.severity === Severity.Error) {
39
if (e.item.message.original instanceof Error) {
40
console.error(e.item.message.original);
41
} else {
42
console.error(toErrorMessage(e.item.message.linkedText.toString(), true));
43
}
44
}
45
}
46
}
47
48
private triggerAriaAlert(notification: INotificationViewItem): void {
49
if (notification.priority === NotificationPriority.SILENT) {
50
return;
51
}
52
53
// Trigger the alert again whenever the message changes
54
const listener = notification.onDidChangeContent(e => {
55
if (e.kind === NotificationViewItemContentChangeKind.MESSAGE) {
56
this.doTriggerAriaAlert(notification);
57
}
58
});
59
60
Event.once(notification.onDidClose)(() => listener.dispose());
61
62
this.doTriggerAriaAlert(notification);
63
}
64
65
private doTriggerAriaAlert(notification: INotificationViewItem): void {
66
let alertText: string;
67
if (notification.severity === Severity.Error) {
68
alertText = localize('alertErrorMessage', "Error: {0}", notification.message.linkedText.toString());
69
} else if (notification.severity === Severity.Warning) {
70
alertText = localize('alertWarningMessage', "Warning: {0}", notification.message.linkedText.toString());
71
} else {
72
alertText = localize('alertInfoMessage', "Info: {0}", notification.message.linkedText.toString());
73
}
74
75
alert(alertText);
76
}
77
}
78
79