Skip to content

Commit 1856b26

Browse files
author
Benjamin Pasero
committed
💄 notifications
1 parent f084f2c commit 1856b26

3 files changed

Lines changed: 62 additions & 45 deletions

File tree

src/vs/platform/notification/common/notification.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,24 @@ export interface INotificationProperties {
3131
silent?: boolean;
3232

3333
/**
34-
* Add a "never show again" action. If user selects the action, the notification will not appear again.
34+
* Adds an action to never show the notification again. The choice will be persisted
35+
* such as future requests will not cause the notification to show again.
3536
*/
36-
neverShowAgainOptions?: INeverShowAgainOptions;
37+
neverShowAgain?: INeverShowAgainOptions;
38+
}
39+
40+
export interface INeverShowAgainOptions {
41+
42+
/**
43+
* The id is used to persist the selection of not showing the notification again.
44+
*/
45+
id: string;
46+
47+
/**
48+
* By default the action will show up as primary action. Setting this to true will
49+
* make it a secondary action instead.
50+
*/
51+
isSecondary?: boolean;
3752
}
3853

3954
export interface INotification extends INotificationProperties {
@@ -178,23 +193,6 @@ export interface IPromptOptions extends INotificationProperties {
178193
onCancel?: () => void;
179194
}
180195

181-
182-
export interface INeverShowAgainOptions {
183-
/**
184-
* Sending prompt id automatically adds a "never show again" action to the prompt.
185-
* If user picks this option, future calls to 'prompt' with the same prompt id will be ignored.
186-
* The id is used to persist the selection to storage
187-
*/
188-
id: string;
189-
190-
/**
191-
*
192-
* Primary: The never show again action will show up as a button on the notificaion.
193-
* Secondary The never show again action will show up under the gear icon.
194-
*/
195-
isSecondary?: boolean;
196-
}
197-
198196
export interface IStatusMessageOptions {
199197

200198
/**
@@ -292,4 +290,4 @@ export class NoOpProgress implements INotificationProgress {
292290
done(): void { }
293291
total(value: number): void { }
294292
worked(value: number): void { }
295-
}
293+
}

src/vs/workbench/contrib/codeEditor/browser/largeFileOptimizations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class LargeFileOptimizationsWarner extends Disposable implements IEditorC
5656
});
5757
}
5858
}
59-
], { neverShowAgainOptions: { id: 'editor.contrib.largeFileOptimizationsWarner' } });
59+
], { neverShowAgain: { id: 'editor.contrib.largeFileOptimizationsWarner' } });
6060
}
6161
}));
6262
}

src/vs/workbench/services/notification/common/notificationService.ts

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import * as nls from 'vs/nls';
67
import { INotificationService, INotification, INotificationHandle, Severity, NotificationMessage, INotificationActions, IPromptChoice, IPromptOptions, IStatusMessageOptions, NoOpNotification } from 'vs/platform/notification/common/notification';
78
import { INotificationsModel, NotificationsModel, ChoiceAction } from 'vs/workbench/common/notifications';
89
import { Disposable, DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
@@ -11,7 +12,6 @@ import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
1112
import { ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';
1213
import { IAction, Action } from 'vs/base/common/actions';
1314
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
14-
import * as nls from 'vs/nls';
1515

1616
export class NotificationService extends Disposable implements INotificationService {
1717

@@ -58,58 +58,77 @@ export class NotificationService extends Disposable implements INotificationServ
5858
}
5959

6060
notify(notification: INotification): INotificationHandle {
61+
const toDispose = new DisposableStore();
6162

63+
// Handle neverShowAgain option accordingly
6264
let handle: INotificationHandle;
63-
if (notification.neverShowAgainOptions) {
64-
const id = notification.neverShowAgainOptions.id;
65-
if (!!this.storageService.get(id, StorageScope.GLOBAL)) {
65+
if (notification.neverShowAgain) {
66+
67+
// If the user already picked to not show the notification
68+
// again, we return with a no-op notification here
69+
const id = notification.neverShowAgain.id;
70+
if (this.storageService.getBoolean(id, StorageScope.GLOBAL)) {
6671
return new NoOpNotification();
6772
}
6873

69-
const neverShowAction = new Action(
70-
'workbench.dialog.choice.neverShowAgain',
74+
const neverShowAgainAction = toDispose.add(new Action(
75+
'workbench.notification.neverShowAgain',
7176
nls.localize('neverShowAgain', "Don't Show Again"),
7277
undefined, true, () => {
78+
79+
// Close notification
7380
handle.close();
81+
82+
// Remember choice
7483
this.storageService.store(id, true, StorageScope.GLOBAL);
84+
7585
return Promise.resolve();
76-
});
86+
}));
7787

78-
notification.actions = notification.actions || {};
79-
if (notification.neverShowAgainOptions.isSecondary) {
80-
notification.actions.secondary = notification.actions.secondary || [];
81-
notification.actions.secondary = [...notification.actions.secondary, neverShowAction];
82-
}
83-
else {
84-
notification.actions.primary = notification.actions.primary || [];
85-
notification.actions.primary = [neverShowAction, ...notification.actions.primary];
88+
// Insert as primary or secondary action
89+
const actions = notification.actions || { primary: [], secondary: [] };
90+
if (!notification.neverShowAgain.isSecondary) {
91+
actions.primary = [neverShowAgainAction, ...(actions.primary || [])]; // action comes first
92+
} else {
93+
actions.secondary = [...(actions.secondary || []), neverShowAgainAction]; // actions comes last
8694
}
95+
96+
notification.actions = actions;
8797
}
8898

99+
// Show notification
89100
handle = this.model.addNotification(notification);
101+
102+
// Cleanup when notification gets disposed
103+
Event.once(handle.onDidClose)(() => toDispose.dispose());
104+
90105
return handle;
91106
}
92107

93108
prompt(severity: Severity, message: string, choices: IPromptChoice[], options?: IPromptOptions): INotificationHandle {
94109
const toDispose = new DisposableStore();
95110

111+
// Handle neverShowAgain option accordingly
112+
if (options && options.neverShowAgain) {
96113

97-
if (options && options.neverShowAgainOptions) {
98-
const id = options.neverShowAgainOptions.id;
99-
if (!!this.storageService.get(id, StorageScope.GLOBAL)) {
114+
// If the user already picked to not show the notification
115+
// again, we return with a no-op notification here
116+
const id = options.neverShowAgain.id;
117+
if (this.storageService.getBoolean(id, StorageScope.GLOBAL)) {
100118
return new NoOpNotification();
101119
}
102120

103121
const neverShowAgainChoice = {
104122
label: nls.localize('neverShowAgain', "Don't Show Again"),
105123
run: () => this.storageService.store(id, true, StorageScope.GLOBAL),
106-
isSecondary: options.neverShowAgainOptions.isSecondary
124+
isSecondary: options.neverShowAgain.isSecondary
107125
};
108-
if (options.neverShowAgainOptions.isSecondary) {
109-
choices = [...choices, neverShowAgainChoice];
110-
}
111-
else {
112-
choices = [neverShowAgainChoice, ...choices];
126+
127+
// Insert as primary or secondary action
128+
if (!options.neverShowAgain.isSecondary) {
129+
choices = [neverShowAgainChoice, ...choices]; // action comes first
130+
} else {
131+
choices = [...choices, neverShowAgainChoice]; // actions comes last
113132
}
114133
}
115134

0 commit comments

Comments
 (0)