notificationsToasts.ts 16.7 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import 'vs/css!./media/notificationsToasts';
7
import { INotificationsModel, NotificationChangeType, INotificationChangeEvent, INotificationViewItem, NotificationViewItemLabelKind } from 'vs/workbench/common/notifications';
8
import { IDisposable, dispose, toDisposable } from 'vs/base/common/lifecycle';
9
import { addClass, removeClass, isAncestor, addDisposableListener, EventType, Dimension } from 'vs/base/browser/dom';
10 11
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { NotificationsList } from 'vs/workbench/browser/parts/notifications/notificationsList';
J
Joao Moreno 已提交
12
import { Event } from 'vs/base/common/event';
13
import { IWorkbenchLayoutService, Parts } from 'vs/workbench/services/layout/browser/layoutService';
14
import { Themable, NOTIFICATIONS_TOAST_BORDER } from 'vs/workbench/common/theme';
15 16
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { widgetShadow } from 'vs/platform/theme/common/colorRegistry';
17
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
18
import { NotificationsToastsVisibleContext } from 'vs/workbench/browser/parts/notifications/notificationsCommands';
19
import { IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
20
import { localize } from 'vs/nls';
21
import { Severity } from 'vs/platform/notification/common/notification';
22
import { ScrollbarVisibility } from 'vs/base/common/scrollable';
B
Benjamin Pasero 已提交
23
import { ILifecycleService, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
B
Benjamin Pasero 已提交
24
import { IWindowService } from 'vs/platform/windows/common/windows';
25
import { timeout } from 'vs/base/common/async';
26 27

interface INotificationToast {
28
	item: INotificationViewItem;
29 30
	list: NotificationsList;
	container: HTMLElement;
31
	toast: HTMLElement;
32 33 34
	disposeables: IDisposable[];
}

B
Benjamin Pasero 已提交
35 36 37 38 39 40
enum ToastVisibility {
	HIDDEN_OR_VISIBLE,
	HIDDEN,
	VISIBLE
}

41 42
export class NotificationsToasts extends Themable {

43
	private static MAX_WIDTH = 450;
44
	private static MAX_NOTIFICATIONS = 3;
45

46 47
	private static PURGE_TIMEOUT: { [severity: number]: number } = (() => {
		const intervals = Object.create(null);
48 49 50
		intervals[Severity.Info] = 15000;
		intervals[Severity.Warning] = 18000;
		intervals[Severity.Error] = 20000;
51 52 53 54

		return intervals;
	})();

55 56 57 58
	private notificationsToastsContainer: HTMLElement;
	private workbenchDimensions: Dimension;
	private isNotificationsCenterVisible: boolean;
	private mapNotificationToToast: Map<INotificationViewItem, INotificationToast>;
59
	private notificationsToastsVisibleContextKey: IContextKey<boolean>;
60 61 62 63

	constructor(
		private container: HTMLElement,
		private model: INotificationsModel,
64
		@IInstantiationService private readonly instantiationService: IInstantiationService,
65
		@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService,
66
		@IThemeService themeService: IThemeService,
67
		@IEditorGroupsService private readonly editorGroupService: IEditorGroupsService,
B
Benjamin Pasero 已提交
68
		@IContextKeyService contextKeyService: IContextKeyService,
69 70
		@ILifecycleService private readonly lifecycleService: ILifecycleService,
		@IWindowService private readonly windowService: IWindowService
71 72 73 74
	) {
		super(themeService);

		this.mapNotificationToToast = new Map<INotificationViewItem, INotificationToast>();
75
		this.notificationsToastsVisibleContextKey = NotificationsToastsVisibleContext.bindTo(contextKeyService);
76 77 78 79 80

		this.registerListeners();
	}

	private registerListeners(): void {
B
Benjamin Pasero 已提交
81

82 83 84
		// Layout
		this._register(this.layoutService.onLayout(dimension => this.layout(dimension)));

85 86
		// Delay some tasks until after we can show notifications
		this.onCanShowNotifications().then(() => {
B
Benjamin Pasero 已提交
87 88 89 90 91 92 93

			// Show toast for initial notifications if any
			this.model.notifications.forEach(notification => this.addToast(notification));

			// Update toasts on notification changes
			this._register(this.model.onDidNotificationChange(e => this.onDidNotificationChange(e)));
		});
94 95
	}

J
Johannes Rieken 已提交
96
	private onCanShowNotifications(): Promise<void> {
97 98 99 100 101 102 103 104 105 106 107 108 109

		// Wait for the running phase to ensure we can draw notifications properly
		return this.lifecycleService.when(LifecyclePhase.Ready).then(() => {

			// Push notificiations out until either workbench is restored
			// or some time has ellapsed to reduce pressure on the startup
			return Promise.race([
				this.lifecycleService.when(LifecyclePhase.Restored),
				timeout(2000)
			]);
		});
	}

110 111 112 113 114 115 116 117 118 119 120 121 122 123
	private onDidNotificationChange(e: INotificationChangeEvent): void {
		switch (e.kind) {
			case NotificationChangeType.ADD:
				return this.addToast(e.item);
			case NotificationChangeType.REMOVE:
				return this.removeToast(e.item);
		}
	}

	private addToast(item: INotificationViewItem): void {
		if (this.isNotificationsCenterVisible) {
			return; // do not show toasts while notification center is visibles
		}

124 125 126 127
		if (item.silent) {
			return; // do not show toats for silenced notifications
		}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		// Lazily create toasts containers
		if (!this.notificationsToastsContainer) {
			this.notificationsToastsContainer = document.createElement('div');
			addClass(this.notificationsToastsContainer, 'notifications-toasts');

			this.container.appendChild(this.notificationsToastsContainer);
		}

		// Make Visible
		addClass(this.notificationsToastsContainer, 'visible');

		const itemDisposeables: IDisposable[] = [];

		// Container
		const notificationToastContainer = document.createElement('div');
143
		addClass(notificationToastContainer, 'notification-toast-container');
144 145 146 147 148 149 150 151

		const firstToast = this.notificationsToastsContainer.firstChild;
		if (firstToast) {
			this.notificationsToastsContainer.insertBefore(notificationToastContainer, firstToast); // always first
		} else {
			this.notificationsToastsContainer.appendChild(notificationToastContainer);
		}

152 153 154 155 156
		// Toast
		const notificationToast = document.createElement('div');
		addClass(notificationToast, 'notification-toast');
		notificationToastContainer.appendChild(notificationToast);

157
		// Create toast with item and show
158
		const notificationList = this.instantiationService.createInstance(NotificationsList, notificationToast, {
159 160 161
			ariaLabel: localize('notificationsToast', "Notification Toast"),
			verticalScrollMode: ScrollbarVisibility.Hidden
		});
162
		itemDisposeables.push(notificationList);
B
Benjamin Pasero 已提交
163 164 165 166 167 168 169 170 171

		const toast: INotificationToast = { item, list: notificationList, container: notificationToastContainer, toast: notificationToast, disposeables: itemDisposeables };
		this.mapNotificationToToast.set(item, toast);

		itemDisposeables.push(toDisposable(() => {
			if (this.isVisible(toast)) {
				this.notificationsToastsContainer.removeChild(toast.container);
			}
		}));
172 173 174 175

		// Make visible
		notificationList.show();

176 177 178
		// Layout lists
		const maxDimensions = this.computeMaxDimensions();
		this.layoutLists(maxDimensions.width);
179 180 181 182

		// Show notification
		notificationList.updateNotificationsList(0, 0, [item]);

183 184 185 186
		// Layout container: only after we show the notification to ensure that
		// the height computation takes the content of it into account!
		this.layoutContainer(maxDimensions.height);

187 188
		// Update when item height changes due to expansion
		itemDisposeables.push(item.onDidExpansionChange(() => {
189 190 191
			notificationList.updateNotificationsList(0, 1, [item]);
		}));

192 193 194 195 196 197 198
		// Update when item height potentially changes due to label changes
		itemDisposeables.push(item.onDidLabelChange(e => {
			if (e.kind === NotificationViewItemLabelKind.ACTIONS || e.kind === NotificationViewItemLabelKind.MESSAGE) {
				notificationList.updateNotificationsList(0, 1, [item]);
			}
		}));

199
		// Remove when item gets closed
J
Joao Moreno 已提交
200
		Event.once(item.onDidClose)(() => {
201 202 203
			this.removeToast(item);
		});

B
Benjamin Pasero 已提交
204 205
		// Automatically purge non-sticky notifications
		this.purgeNotification(item, notificationToastContainer, notificationList, itemDisposeables);
206

207 208
		// Theming
		this.updateStyles();
209 210 211

		// Context Key
		this.notificationsToastsVisibleContextKey.set(true);
B
Benjamin Pasero 已提交
212

B
Benjamin Pasero 已提交
213 214 215 216
		// Animate in
		addClass(notificationToast, 'notification-fade-in');
		itemDisposeables.push(addDisposableListener(notificationToast, 'transitionend', () => {
			removeClass(notificationToast, 'notification-fade-in');
217
			addClass(notificationToast, 'notification-fade-in-done');
B
Benjamin Pasero 已提交
218
		}));
219 220
	}

B
Benjamin Pasero 已提交
221 222 223 224 225 226 227
	private purgeNotification(item: INotificationViewItem, notificationToastContainer: HTMLElement, notificationList: NotificationsList, disposables: IDisposable[]): void {

		// Track mouse over item
		let isMouseOverToast = false;
		disposables.push(addDisposableListener(notificationToastContainer, EventType.MOUSE_OVER, () => isMouseOverToast = true));
		disposables.push(addDisposableListener(notificationToastContainer, EventType.MOUSE_OUT, () => isMouseOverToast = false));

228
		// Install Timers to Purge Notification
229
		let purgeTimeoutHandle: any;
230 231
		let listener: IDisposable;

B
Benjamin Pasero 已提交
232
		const hideAfterTimeout = () => {
233

234
			purgeTimeoutHandle = setTimeout(() => {
235 236 237 238 239 240

				// If the notification is sticky or prompting and the window does not have
				// focus, we wait for the window to gain focus again before triggering
				// the timeout again. This prevents an issue where focussing the window
				// could immediately hide the notification because the timeout was triggered
				// again.
241
				if ((item.sticky || item.hasPrompt()) && !this.windowService.hasFocus) {
242 243 244 245 246 247 248 249
					if (!listener) {
						listener = this.windowService.onDidChangeFocus(focus => {
							if (focus) {
								hideAfterTimeout();
							}
						});
						disposables.push(listener);
					}
250 251 252
				}

				// Otherwise...
B
Benjamin Pasero 已提交
253
				else if (
254 255
					item.sticky ||								// never hide sticky notifications
					notificationList.hasFocus() ||				// never hide notifications with focus
256
					isMouseOverToast							// never hide notifications under mouse
B
Benjamin Pasero 已提交
257
				) {
258
					hideAfterTimeout();
B
Benjamin Pasero 已提交
259 260 261 262 263 264 265 266
				} else {
					this.removeToast(item);
				}
			}, NotificationsToasts.PURGE_TIMEOUT[item.severity]);
		};

		hideAfterTimeout();

267
		disposables.push(toDisposable(() => clearTimeout(purgeTimeoutHandle)));
B
Benjamin Pasero 已提交
268 269
	}

270 271
	private removeToast(item: INotificationViewItem): void {
		const notificationToast = this.mapNotificationToToast.get(item);
B
Benjamin Pasero 已提交
272
		let focusGroup = false;
273
		if (notificationToast) {
274 275
			const toastHasDOMFocus = isAncestor(document.activeElement, notificationToast.container);
			if (toastHasDOMFocus) {
B
Benjamin Pasero 已提交
276
				focusGroup = !(this.focusNext() || this.focusPrevious()); // focus next if any, otherwise focus editor
277
			}
278 279 280 281 282 283 284 285

			// Listeners
			dispose(notificationToast.disposeables);

			// Remove from Map
			this.mapNotificationToToast.delete(item);
		}

286 287 288
		// Layout if we still have toasts
		if (this.mapNotificationToToast.size > 0) {
			this.layout(this.workbenchDimensions);
289 290
		}

291 292 293
		// Otherwise hide if no more toasts to show
		else {
			this.doHide();
294

B
Benjamin Pasero 已提交
295 296 297
			// Move focus back to editor group as needed
			if (focusGroup) {
				this.editorGroupService.activeGroup.focus();
298 299
			}
		}
300 301 302 303 304 305
	}

	private removeToasts(): void {
		this.mapNotificationToToast.forEach(toast => dispose(toast.disposeables));
		this.mapNotificationToToast.clear();

306 307 308 309
		this.doHide();
	}

	private doHide(): void {
310 311 312
		if (this.notificationsToastsContainer) {
			removeClass(this.notificationsToastsContainer, 'visible');
		}
313 314 315 316 317

		// Context Key
		this.notificationsToastsVisibleContextKey.set(false);
	}

B
Benjamin Pasero 已提交
318
	hide(): void {
B
Benjamin Pasero 已提交
319
		const focusGroup = isAncestor(document.activeElement, this.notificationsToastsContainer);
320 321 322

		this.removeToasts();

B
Benjamin Pasero 已提交
323 324
		if (focusGroup) {
			this.editorGroupService.activeGroup.focus();
325 326 327
		}
	}

B
Benjamin Pasero 已提交
328
	focus(): boolean {
B
Benjamin Pasero 已提交
329
		const toasts = this.getToasts(ToastVisibility.VISIBLE);
330 331 332 333 334 335 336 337 338
		if (toasts.length > 0) {
			toasts[0].list.focusFirst();

			return true;
		}

		return false;
	}

B
Benjamin Pasero 已提交
339
	focusNext(): boolean {
B
Benjamin Pasero 已提交
340
		const toasts = this.getToasts(ToastVisibility.VISIBLE);
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
		for (let i = 0; i < toasts.length; i++) {
			const toast = toasts[i];
			if (toast.list.hasFocus()) {
				const nextToast = toasts[i + 1];
				if (nextToast) {
					nextToast.list.focusFirst();

					return true;
				}

				break;
			}
		}

		return false;
	}

B
Benjamin Pasero 已提交
358
	focusPrevious(): boolean {
B
Benjamin Pasero 已提交
359
		const toasts = this.getToasts(ToastVisibility.VISIBLE);
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
		for (let i = 0; i < toasts.length; i++) {
			const toast = toasts[i];
			if (toast.list.hasFocus()) {
				const previousToast = toasts[i - 1];
				if (previousToast) {
					previousToast.list.focusFirst();

					return true;
				}

				break;
			}
		}

		return false;
375 376
	}

B
Benjamin Pasero 已提交
377
	focusFirst(): boolean {
B
Benjamin Pasero 已提交
378
		const toast = this.getToasts(ToastVisibility.VISIBLE)[0];
B
Benjamin Pasero 已提交
379 380 381 382 383 384 385 386 387
		if (toast) {
			toast.list.focusFirst();

			return true;
		}

		return false;
	}

B
Benjamin Pasero 已提交
388
	focusLast(): boolean {
B
Benjamin Pasero 已提交
389
		const toasts = this.getToasts(ToastVisibility.VISIBLE);
B
Benjamin Pasero 已提交
390 391 392 393 394 395 396 397 398
		if (toasts.length > 0) {
			toasts[toasts.length - 1].list.focusFirst();

			return true;
		}

		return false;
	}

B
Benjamin Pasero 已提交
399
	update(isCenterVisible: boolean): void {
400 401 402 403 404 405 406 407 408 409 410
		if (this.isNotificationsCenterVisible !== isCenterVisible) {
			this.isNotificationsCenterVisible = isCenterVisible;

			// Hide all toasts when the notificationcenter gets visible
			if (this.isNotificationsCenterVisible) {
				this.removeToasts();
			}
		}
	}

	protected updateStyles(): void {
411
		this.mapNotificationToToast.forEach(t => {
412
			const widgetShadowColor = this.getColor(widgetShadow);
413
			t.toast.style.boxShadow = widgetShadowColor ? `0 0px 8px ${widgetShadowColor}` : null;
414 415 416

			const borderColor = this.getColor(NOTIFICATIONS_TOAST_BORDER);
			t.toast.style.border = borderColor ? `1px solid ${borderColor}` : null;
417 418 419
		});
	}

B
Benjamin Pasero 已提交
420 421
	private getToasts(state: ToastVisibility): INotificationToast[] {
		const notificationToasts: INotificationToast[] = [];
B
Benjamin Pasero 已提交
422 423

		this.mapNotificationToToast.forEach(toast => {
B
Benjamin Pasero 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437
			switch (state) {
				case ToastVisibility.HIDDEN_OR_VISIBLE:
					notificationToasts.push(toast);
					break;
				case ToastVisibility.HIDDEN:
					if (!this.isVisible(toast)) {
						notificationToasts.push(toast);
					}
					break;
				case ToastVisibility.VISIBLE:
					if (this.isVisible(toast)) {
						notificationToasts.push(toast);
					}
					break;
B
Benjamin Pasero 已提交
438 439 440
			}
		});

B
Benjamin Pasero 已提交
441
		return notificationToasts.reverse(); // from newest to oldest
442 443
	}

B
Benjamin Pasero 已提交
444
	layout(dimension: Dimension): void {
445 446
		this.workbenchDimensions = dimension;

447 448 449
		const maxDimensions = this.computeMaxDimensions();

		// Hide toasts that exceed height
450 451 452
		if (maxDimensions.height) {
			this.layoutContainer(maxDimensions.height);
		}
453 454 455

		// Layout all lists of toasts
		this.layoutLists(maxDimensions.width);
456 457 458
	}

	private computeMaxDimensions(): Dimension {
459
		let maxWidth = NotificationsToasts.MAX_WIDTH;
460 461

		let availableWidth = maxWidth;
M
Matt Bierner 已提交
462
		let availableHeight: number | undefined;
463 464 465 466 467

		if (this.workbenchDimensions) {

			// Make sure notifications are not exceding available width
			availableWidth = this.workbenchDimensions.width;
B
Benjamin Pasero 已提交
468
			availableWidth -= (2 * 8); // adjust for paddings left and right
469 470 471

			// Make sure notifications are not exceeding available height
			availableHeight = this.workbenchDimensions.height;
472
			if (this.layoutService.isVisible(Parts.STATUSBAR_PART)) {
473 474 475
				availableHeight -= 22; // adjust for status bar
			}

476
			if (this.layoutService.isVisible(Parts.TITLEBAR_PART)) {
477 478 479 480 481 482
				availableHeight -= 22; // adjust for title bar
			}

			availableHeight -= (2 * 12); // adjust for paddings top and bottom
		}

M
Matt Bierner 已提交
483 484 485
		availableHeight = typeof availableHeight === 'number'
			? Math.round(availableHeight * 0.618) // try to not cover the full height for stacked toasts
			: 0;
486

487
		return new Dimension(Math.min(maxWidth, availableWidth), availableHeight);
488
	}
489

490 491 492 493 494
	private layoutLists(width: number): void {
		this.mapNotificationToToast.forEach(toast => toast.list.layout(width));
	}

	private layoutContainer(heightToGive: number): void {
B
Benjamin Pasero 已提交
495 496
		let visibleToasts = 0;
		this.getToasts(ToastVisibility.HIDDEN_OR_VISIBLE).forEach(toast => {
497 498 499

			// In order to measure the client height, the element cannot have display: none
			toast.container.style.opacity = '0';
B
Benjamin Pasero 已提交
500
			this.setVisibility(toast, true);
501

502
			heightToGive -= toast.container.offsetHeight;
503

B
Benjamin Pasero 已提交
504 505 506 507 508 509 510 511 512
			let makeVisible = false;
			if (visibleToasts === NotificationsToasts.MAX_NOTIFICATIONS) {
				makeVisible = false; // never show more than MAX_NOTIFICATIONS
			} else if (heightToGive >= 0) {
				makeVisible = true; // hide toast if available height is too little
			}

			// Hide or show toast based on context
			this.setVisibility(toast, makeVisible);
513
			toast.container.style.opacity = null;
B
Benjamin Pasero 已提交
514 515 516 517

			if (makeVisible) {
				visibleToasts++;
			}
518 519
		});
	}
B
Benjamin Pasero 已提交
520 521

	private setVisibility(toast: INotificationToast, visible: boolean): void {
522 523 524 525 526 527 528 529 530
		if (this.isVisible(toast) === visible) {
			return;
		}

		if (visible) {
			this.notificationsToastsContainer.appendChild(toast.container);
		} else {
			this.notificationsToastsContainer.removeChild(toast.container);
		}
B
Benjamin Pasero 已提交
531 532 533
	}

	private isVisible(toast: INotificationToast): boolean {
534
		return !!toast.container.parentElement;
B
Benjamin Pasero 已提交
535
	}
536
}