提交 43a42281 编写于 作者: B Benjamin Pasero

notifications - introduce simple alerts

上级 e96078d0
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { alert } from 'vs/base/browser/ui/aria/aria';
import { localize } from 'vs/nls';
import { Severity } from 'vs/platform/message/common/message';
import { INotificationViewItem, INotificationsModel, NotificationChangeType, INotificationChangeEvent } from 'vs/workbench/common/notifications';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
export class NotificationsAlerts {
private toDispose: IDisposable[];
constructor(private model: INotificationsModel) {
this.toDispose = [];
// Alert initial notifications if any
model.notifications.forEach(n => this.ariaAlert(n));
this.registerListeners();
}
private registerListeners(): void {
this.toDispose.push(this.model.onDidNotificationsChange(e => this.onDidNotificationsChange(e)));
}
private onDidNotificationsChange(e: INotificationChangeEvent): void {
if (e.kind === NotificationChangeType.ADD) {
this.ariaAlert(e.item);
}
}
private ariaAlert(notifiation: INotificationViewItem): void {
let alertText: string;
if (notifiation.severity === Severity.Error) {
alertText = localize('alertErrorMessage', "Error: {0}", notifiation.message.value);
} else if (notifiation.severity === Severity.Warning) {
alertText = localize('alertWarningMessage', "Warning: {0}", notifiation.message.value);
} else {
alertText = localize('alertInfoMessage', "Info: {0}", notifiation.message.value);
}
alert(alertText);
}
public dispose(): void {
this.toDispose = dispose(this.toDispose);
}
}
\ No newline at end of file
......@@ -16,13 +16,12 @@ import { IThemeService, registerThemingParticipant, ITheme, ICssStyleCollector }
import { contrastBorder, widgetShadow, textLinkForeground } from 'vs/platform/theme/common/colorRegistry';
import { INotificationViewItem, INotificationsModel, INotificationChangeEvent, NotificationChangeType } from 'vs/workbench/common/notifications';
import { NotificationsListDelegate, NotificationRenderer } from 'vs/workbench/browser/parts/notifications/notificationsViewer';
import { Severity } from 'vs/platform/message/common/message';
import { alert } from 'vs/base/browser/ui/aria/aria';
export class NotificationList extends Themable {
export class NotificationsCenter extends Themable {
private listContainer: HTMLElement;
private list: WorkbenchList<INotificationViewItem>;
private viewModel: INotificationViewItem[];
constructor(
private container: HTMLElement,
......@@ -32,6 +31,8 @@ export class NotificationList extends Themable {
) {
super(themeService);
this.viewModel = [];
this.create();
// Show initial notifications if any
......@@ -94,11 +95,6 @@ export class NotificationList extends Themable {
}
private onNotificationsAdded(index: number, items: INotificationViewItem[]): void {
// Support in Screen Readers too
items.forEach(item => this.ariaAlert(item));
// Update list
this.updateNotificationsList(index, 0, items);
}
......@@ -119,9 +115,24 @@ export class NotificationList extends Themable {
this.hide();
}
// Remember focus/selection
const selection = this.indexToItems(this.list.getSelection());
const focus = this.indexToItems(this.list.getFocus());
// Update view model
this.viewModel.splice(start, deleteCount, ...items);
// Update list
this.list.splice(start, deleteCount, items);
this.list.layout();
// Restore focus/selection
this.list.setSelection(selection.map(s => this.viewModel.indexOf(s)));
this.list.setFocus(focus.map(f => this.viewModel.indexOf(f)));
}
private indexToItems(indeces: number[]): INotificationViewItem[] {
return indeces.map(index => this.viewModel[index]).filter(item => !!item);
}
private show(): void {
......@@ -131,19 +142,6 @@ export class NotificationList extends Themable {
private hide(): void {
removeClass(this.listContainer, 'visible');
}
private ariaAlert(notifiation: INotificationViewItem): void {
let alertText: string;
if (notifiation.severity === Severity.Error) {
alertText = localize('alertErrorMessage', "Error: {0}", notifiation.message.value);
} else if (notifiation.severity === Severity.Warning) {
alertText = localize('alertWarningMessage', "Warning: {0}", notifiation.message.value);
} else {
alertText = localize('alertInfoMessage', "Info: {0}", notifiation.message.value);
}
alert(alertText);
}
}
registerThemingParticipant((theme: ITheme, collector: ICssStyleCollector) => {
......
......@@ -101,7 +101,8 @@ import { ICustomViewsService } from 'vs/workbench/common/views';
import { CustomViewsService } from 'vs/workbench/browser/parts/views/customView';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { NotificationService } from 'vs/workbench/services/notification/common/notificationService';
import { NotificationList } from 'vs/workbench/browser/parts/notifications/notificationsList';
import { NotificationsCenter } from 'vs/workbench/browser/parts/notifications/notificationsCenter';
import { NotificationsAlerts } from 'vs/workbench/browser/parts/notifications/notificationsAlerts';
export const MessagesVisibleContext = new RawContextKey<boolean>('globalMessageVisible', false);
export const EditorsVisibleContext = new RawContextKey<boolean>('editorIsOpen', false);
......@@ -1173,8 +1174,8 @@ export class Workbench implements IPartService {
this.createPanelPart();
this.createStatusbarPart();
// Notifications List
this.createNotificationsList();
// Notification Handlers
this.createNotificationsHandlers();
// Add Workbench to DOM
this.workbenchContainer.build(this.container);
......@@ -1244,9 +1245,15 @@ export class Workbench implements IPartService {
this.statusbarPart.create(statusbarContainer);
}
private createNotificationsList(): void {
const notificationsList = this.instantiationService.createInstance(NotificationList, this.workbench.getHTMLElement(), this.notificationService.model);
this.toUnbind.push(notificationsList);
private createNotificationsHandlers(): void {
// Notification Center
const notificationsCenter = this.instantiationService.createInstance(NotificationsCenter, this.workbench.getHTMLElement(), this.notificationService.model);
this.toUnbind.push(notificationsCenter);
// Notification Alerts
const notificationsAlerts = this.instantiationService.createInstance(NotificationsAlerts, this.notificationService.model);
this.toUnbind.push(notificationsAlerts);
}
public getInstantiationService(): IInstantiationService {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册