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

'use strict';

8
import Severity from 'vs/base/common/severity';
9 10
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IDisposable } from 'vs/base/common/lifecycle';
11
import { IAction } from 'vs/base/common/actions';
12
import Event, { Emitter } from 'vs/base/common/event';
13

14 15
export import Severity = Severity;

16 17
export const INotificationService = createDecorator<INotificationService>('notificationService');

B
Benjamin Pasero 已提交
18
export type NotificationMessage = string | Error;
19

20
export interface INotification {
21 22 23 24

	/**
	 * The severity of the notification. Either `Info`, `Warning` or `Error`.
	 */
25
	severity: Severity;
26 27

	/**
B
Benjamin Pasero 已提交
28 29
	 * The message of the notification. This can either be a `string` or `Error`. Messages
	 * can optionally include links in the format: `[text](link)`
30
	 */
31
	message: NotificationMessage;
32 33 34 35

	/**
	 * The source of the notification appears as additional information.
	 */
36
	source?: string;
37 38 39 40 41 42 43 44 45 46 47 48 49

	/**
	 * Actions to show as part of the notification. Primary actions show up as
	 * buttons as part of the message and will close the notification once clicked.
	 *
	 * Secondary actions are meant to provide additional configuration or context
	 * for the notification and will show up less prominent. A notification does not
	 * close automatically when invoking a secondary action.
	 *
	 * **Note:** If your intent is to show a message with actions to the user, consider
	 * the `IChoiceService` and `IConfirmationService` instead which are optimized for
	 * this usecase and much easier to use!
	 */
50 51 52 53
	actions?: INotificationActions;
}

export interface INotificationActions {
54 55 56 57 58

	/**
	 * Primary actions show up as buttons as part of the message and will close
	 * the notification once clicked.
	 */
59
	primary?: IAction[];
60 61 62 63 64 65

	/**
	 * Secondary actions are meant to provide additional configuration or context
	 * for the notification and will show up less prominent. A notification does not
	 * close automatically when invoking a secondary action.
	 */
66
	secondary?: IAction[];
67
}
68

69
export interface INotificationProgress {
70 71 72 73

	/**
	 * Causes the progress bar to spin infinitley.
	 */
74
	infinite(): void;
75 76 77 78

	/**
	 * Indicate the total amount of work.
	 */
79
	total(value: number): void;
80 81 82 83

	/**
	 * Indicate that a specific chunk of work is done.
	 */
84
	worked(value: number): void;
85 86 87 88

	/**
	 * Indicate that the long running operation is done.
	 */
89 90 91
	done(): void;
}

92
export interface INotificationHandle extends IDisposable {
93 94

	/**
95
	 * Will be fired once the notification is disposed.
96
	 */
97
	readonly onDidDispose: Event<void>;
98 99 100 101 102

	/**
	 * Allows to indicate progress on the notification even after the
	 * notification is already visible.
	 */
103
	readonly progress: INotificationProgress;
104

105 106 107
	/**
	 * Allows to update the severity of the notification.
	 */
108
	updateSeverity(severity: Severity): void;
109 110 111 112 113

	/**
	 * Allows to update the message of the notification even after the
	 * notification is already visible.
	 */
114
	updateMessage(message: NotificationMessage): void;
115 116 117 118 119

	/**
	 * Allows to update the actions of the notification even after the
	 * notification is already visible.
	 */
120
	updateActions(actions?: INotificationActions): void;
121 122 123 124 125 126
}

export interface INotificationService {

	_serviceBrand: any;

127 128 129 130 131 132 133 134
	/**
	 * Show the provided notification to the user. The returned `INotificationHandle`
	 * can be used to control the notification afterwards.
	 *
	 * **Note:** If your intent is to show a message with actions to the user, consider
	 * the `IChoiceService` and `IConfirmationService` instead which are optimized for
	 * this usecase and much easier to use!
	 */
135
	notify(notification: INotification): INotificationHandle;
136

137 138 139 140
	/**
	 * A convinient way of reporting infos. Use the `INotificationService.notify`
	 * method if you need more control over the notification.
	 */
141
	info(message: NotificationMessage | NotificationMessage[]): void;
142 143 144 145 146

	/**
	 * A convinient way of reporting warnings. Use the `INotificationService.notify`
	 * method if you need more control over the notification.
	 */
147
	warn(message: NotificationMessage | NotificationMessage[]): void;
148 149 150 151 152

	/**
	 * A convinient way of reporting errors. Use the `INotificationService.notify`
	 * method if you need more control over the notification.
	 */
153
	error(message: NotificationMessage | NotificationMessage[]): void;
154 155 156 157 158
}

export class NoOpNotification implements INotificationHandle {
	readonly progress = new NoOpProgress();

159
	private _onDidDispose: Emitter<void> = new Emitter();
160

161 162
	public get onDidDispose(): Event<void> {
		return this._onDidDispose.event;
163 164
	}

165
	updateSeverity(severity: Severity): void { }
166
	updateMessage(message: NotificationMessage): void { }
167
	updateActions(actions?: INotificationActions): void { }
168

169
	dispose(): void {
170
		this._onDidDispose.dispose();
171
	}
172 173 174 175 176 177 178
}

export class NoOpProgress implements INotificationProgress {
	infinite(): void { }
	done(): void { }
	total(value: number): void { }
	worked(value: number): void { }
179
}