Path: blob/main/src/vs/workbench/browser/parts/notifications/notificationsAlerts.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { alert } from '../../../../base/browser/ui/aria/aria.js';6import { localize } from '../../../../nls.js';7import { INotificationViewItem, INotificationsModel, NotificationChangeType, INotificationChangeEvent, NotificationViewItemContentChangeKind } from '../../../common/notifications.js';8import { Disposable } from '../../../../base/common/lifecycle.js';9import { toErrorMessage } from '../../../../base/common/errorMessage.js';10import { NotificationPriority, Severity } from '../../../../platform/notification/common/notification.js';11import { Event } from '../../../../base/common/event.js';1213export class NotificationsAlerts extends Disposable {1415constructor(private readonly model: INotificationsModel) {16super();1718// Alert initial notifications if any19for (const notification of model.notifications) {20this.triggerAriaAlert(notification);21}2223this.registerListeners();24}2526private registerListeners(): void {27this._register(this.model.onDidChangeNotification(e => this.onDidChangeNotification(e)));28}2930private onDidChangeNotification(e: INotificationChangeEvent): void {31if (e.kind === NotificationChangeType.ADD) {3233// ARIA alert for screen readers34this.triggerAriaAlert(e.item);3536// Always log errors to console with full details37if (e.item.severity === Severity.Error) {38if (e.item.message.original instanceof Error) {39console.error(e.item.message.original);40} else {41console.error(toErrorMessage(e.item.message.linkedText.toString(), true));42}43}44}45}4647private triggerAriaAlert(notification: INotificationViewItem): void {48if (notification.priority === NotificationPriority.SILENT) {49return;50}5152// Trigger the alert again whenever the message changes53const listener = notification.onDidChangeContent(e => {54if (e.kind === NotificationViewItemContentChangeKind.MESSAGE) {55this.doTriggerAriaAlert(notification);56}57});5859Event.once(notification.onDidClose)(() => listener.dispose());6061this.doTriggerAriaAlert(notification);62}6364private doTriggerAriaAlert(notification: INotificationViewItem): void {65let alertText: string;66if (notification.severity === Severity.Error) {67alertText = localize('alertErrorMessage', "Error: {0}", notification.message.linkedText.toString());68} else if (notification.severity === Severity.Warning) {69alertText = localize('alertWarningMessage', "Warning: {0}", notification.message.linkedText.toString());70} else {71alertText = localize('alertInfoMessage', "Info: {0}", notification.message.linkedText.toString());72}7374alert(alertText);75}76}777879