Skip to content

Commit 9a3b1b4

Browse files
author
Benjamin Pasero
committed
notifications - first cut keybindings support with commands
1 parent 4eb771b commit 9a3b1b4

8 files changed

Lines changed: 250 additions & 43 deletions

File tree

src/vs/workbench/browser/parts/notifications/media/notificationsActions.css

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

6-
.vs .monaco-workbench > .notifications-list-container .notification-list-item .notification-list-item-toolbar-container .close-notification-action {
6+
.vs .monaco-workbench > .notifications-list-container .notification-list-item .notification-list-item-toolbar-container .clear-notification-action {
77
background-image: url('close.svg');
88
}
99

10-
.vs-dark .monaco-workbench > .notifications-list-container .notification-list-item .notification-list-item-toolbar-container .close-notification-action,
11-
.hc-black .monaco-workbench > .notifications-list-container .notification-list-item .notification-list-item-toolbar-container .close-notification-action {
10+
.vs-dark .monaco-workbench > .notifications-list-container .notification-list-item .notification-list-item-toolbar-container .clear-notification-action,
11+
.hc-black .monaco-workbench > .notifications-list-container .notification-list-item .notification-list-item-toolbar-container .clear-notification-action {
1212
background-image: url('close-inverse.svg');
1313
}
1414

src/vs/workbench/browser/parts/notifications/notificationCommands.ts

Lines changed: 149 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,167 @@
77

88
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
99
import { INotificationService } from 'vs/platform/notification/common/notification';
10+
import { RawContextKey, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
11+
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
12+
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
13+
import { INotificationViewItem, isNotificationViewItem } from 'vs/workbench/common/notifications';
14+
import { MenuRegistry, MenuId } from 'vs/platform/actions/common/actions';
15+
import { localize } from 'vs/nls';
16+
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
17+
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
1018

11-
export const SHOW_NOTFICATIONS_CENTER_COMMAND_ID = 'notificationsCenter.show';
12-
export const HIDE_NOTFICATIONS_CENTER_COMMAND_ID = 'notificationsCenter.hide';
13-
export const TOGGLE_NOTFICATIONS_CENTER_COMMAND_ID = 'notificationsCenter.toggle';
19+
export const SHOW_NOTFICATIONS_CENTER_COMMAND_ID = 'notifications.show';
20+
export const HIDE_NOTFICATIONS_CENTER_COMMAND_ID = 'notifications.hide';
21+
export const TOGGLE_NOTFICATIONS_CENTER_COMMAND_ID = 'notifications.toggle';
22+
export const COLLAPSE_NOTIFICATION = 'notification.collapse';
23+
export const EXPAND_NOTIFICATION = 'notification.expand';
24+
export const TOGGLE_NOTIFICATION = 'notification.toggle';
25+
export const CLEAR_NOTFICATION = 'notification.clear';
26+
export const CLEAR_ALL_NOTFICATIONS = 'notifications.clearAll';
27+
28+
const notificationsCenterFocusedId = 'notificationsCenterFocus';
29+
export const NotificationsCenterFocusedContext = new RawContextKey<boolean>(notificationsCenterFocusedId, true);
30+
31+
const notificationsCenterVisibleId = 'notificationsCenterVisible';
32+
export const NotificationsCenterVisibleContext = new RawContextKey<boolean>(notificationsCenterVisibleId, false);
33+
34+
export const InNotificationsCenterContext = ContextKeyExpr.and(ContextKeyExpr.has(notificationsCenterFocusedId), ContextKeyExpr.has(notificationsCenterVisibleId));
1435

1536
export interface INotificationsCenterController {
16-
isVisible: boolean;
37+
readonly isVisible: boolean;
38+
readonly selected: INotificationViewItem;
1739

1840
show(): void;
1941
hide(): void;
42+
43+
clearAll(): void;
2044
}
2145

2246
export function registerNotificationCommands(center: INotificationsCenterController): void {
2347

24-
// Show Center
25-
CommandsRegistry.registerCommand(SHOW_NOTFICATIONS_CENTER_COMMAND_ID, () => center.show());
48+
function showCenter(): void {
49+
center.show();
50+
}
51+
52+
function hideCenter(accessor: ServicesAccessor): void {
53+
54+
// Hide center
55+
center.hide();
56+
57+
// Restore focus if we got an editor
58+
const editor = accessor.get(IWorkbenchEditorService).getActiveEditor();
59+
if (editor) {
60+
editor.focus();
61+
}
62+
}
63+
64+
// Show Notifications Cneter
65+
CommandsRegistry.registerCommand(SHOW_NOTFICATIONS_CENTER_COMMAND_ID, () => showCenter());
66+
67+
// Hide Notifications Center
68+
KeybindingsRegistry.registerCommandAndKeybindingRule({
69+
id: HIDE_NOTFICATIONS_CENTER_COMMAND_ID,
70+
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
71+
when: InNotificationsCenterContext,
72+
primary: KeyCode.Escape,
73+
handler: accessor => hideCenter(accessor)
74+
});
75+
76+
// Toggle Notifications Center
77+
CommandsRegistry.registerCommand(TOGGLE_NOTFICATIONS_CENTER_COMMAND_ID, accessor => center.isVisible ? hideCenter(accessor) : showCenter());
78+
79+
// Clear Notification
80+
KeybindingsRegistry.registerCommandAndKeybindingRule({
81+
id: CLEAR_NOTFICATION,
82+
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
83+
when: InNotificationsCenterContext,
84+
primary: KeyCode.Delete,
85+
mac: {
86+
primary: KeyMod.CtrlCmd | KeyCode.Backspace
87+
},
88+
handler: (accessor, args?: any) => {
89+
let notification: INotificationViewItem;
90+
if (isNotificationViewItem(args)) {
91+
notification = args;
92+
} else {
93+
notification = center.selected;
94+
}
95+
96+
if (notification) {
97+
notification.dispose();
98+
}
99+
}
100+
});
101+
102+
// Expand Notification
103+
KeybindingsRegistry.registerCommandAndKeybindingRule({
104+
id: EXPAND_NOTIFICATION,
105+
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
106+
when: InNotificationsCenterContext,
107+
primary: KeyCode.RightArrow,
108+
handler: (accessor, args?: any) => {
109+
let notification: INotificationViewItem;
110+
if (isNotificationViewItem(args)) {
111+
notification = args;
112+
} else {
113+
notification = center.selected;
114+
}
115+
116+
if (notification) {
117+
notification.expand();
118+
}
119+
}
120+
});
121+
122+
// Collapse Notification
123+
KeybindingsRegistry.registerCommandAndKeybindingRule({
124+
id: COLLAPSE_NOTIFICATION,
125+
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
126+
when: InNotificationsCenterContext,
127+
primary: KeyCode.LeftArrow,
128+
handler: (accessor, args?: any) => {
129+
let notification: INotificationViewItem;
130+
if (isNotificationViewItem(args)) {
131+
notification = args;
132+
} else {
133+
notification = center.selected;
134+
}
135+
136+
if (notification) {
137+
notification.collapse();
138+
}
139+
}
140+
});
141+
142+
// Toggle Notification
143+
KeybindingsRegistry.registerCommandAndKeybindingRule({
144+
id: TOGGLE_NOTIFICATION,
145+
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
146+
when: InNotificationsCenterContext,
147+
primary: KeyCode.Space,
148+
handler: accessor => {
149+
const notification = center.selected;
150+
if (notification) {
151+
if (notification.expanded) {
152+
notification.collapse();
153+
} else {
154+
notification.expand();
155+
}
156+
}
157+
}
158+
});
159+
160+
/// Clear All Notifications
161+
CommandsRegistry.registerCommand(CLEAR_ALL_NOTFICATIONS, () => center.clearAll());
162+
163+
// Commands for Command Palette
164+
const category = localize('notifications', "Notifications");
165+
MenuRegistry.appendMenuItem(MenuId.CommandPalette, { command: { id: SHOW_NOTFICATIONS_CENTER_COMMAND_ID, title: localize('showNotifications', "Show Notifications"), category }, when: NotificationsCenterVisibleContext.toNegated() });
166+
MenuRegistry.appendMenuItem(MenuId.CommandPalette, { command: { id: HIDE_NOTFICATIONS_CENTER_COMMAND_ID, title: localize('hideNotifications', "Hide Notifications"), category }, when: NotificationsCenterVisibleContext });
167+
MenuRegistry.appendMenuItem(MenuId.CommandPalette, { command: { id: CLEAR_ALL_NOTFICATIONS, title: localize('clearAllNotifications', "Clear All Notifications"), category } });
168+
26169

27-
// Hide Center
28-
CommandsRegistry.registerCommand(HIDE_NOTFICATIONS_CENTER_COMMAND_ID, () => center.hide());
29170

30-
// Toggle Center
31-
CommandsRegistry.registerCommand(TOGGLE_NOTFICATIONS_CENTER_COMMAND_ID, () => center.isVisible ? center.hide() : center.show());
32171

33172
// TODO@Notification remove me
34173
CommandsRegistry.registerCommand('notifications.showInfo', accessor => {

src/vs/workbench/browser/parts/notifications/notificationsActions.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,64 @@ import { Action, IAction, ActionRunner } from 'vs/base/common/actions';
1212
import { TPromise } from 'vs/base/common/winjs.base';
1313
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
1414
import { INotificationService } from 'vs/platform/notification/common/notification';
15+
import { CLEAR_NOTFICATION, EXPAND_NOTIFICATION, COLLAPSE_NOTIFICATION } from 'vs/workbench/browser/parts/notifications/notificationCommands';
16+
import { ICommandService } from 'vs/platform/commands/common/commands';
1517

16-
export class CloseNotificationAction extends Action {
18+
export class ClearNotificationAction extends Action {
1719

18-
public static readonly ID = 'workbench.action.closeNotification';
20+
public static readonly ID = CLEAR_NOTFICATION;
1921
public static readonly LABEL = localize('closeNotification', "Close Notification");
2022

2123
constructor(
2224
id: string,
23-
label: string
25+
label: string,
26+
@ICommandService private commandService: ICommandService
2427
) {
25-
super(id, label, 'close-notification-action');
28+
super(id, label, 'clear-notification-action');
2629
}
2730

2831
public run(notification: INotificationViewItem): TPromise<any> {
29-
notification.dispose();
32+
this.commandService.executeCommand(CLEAR_NOTFICATION, notification);
3033

3134
return TPromise.as(void 0);
3235
}
3336
}
3437

3538
export class ExpandNotificationAction extends Action {
3639

37-
public static readonly ID = 'workbench.action.expandNotification';
40+
public static readonly ID = EXPAND_NOTIFICATION;
3841
public static readonly LABEL = localize('expandNotification', "Expand Notification");
3942

4043
constructor(
4144
id: string,
42-
label: string
45+
label: string,
46+
@ICommandService private commandService: ICommandService
4347
) {
4448
super(id, label, 'expand-notification-action');
4549
}
4650

4751
public run(notification: INotificationViewItem): TPromise<any> {
48-
notification.expand();
52+
this.commandService.executeCommand(EXPAND_NOTIFICATION, notification);
4953

5054
return TPromise.as(void 0);
5155
}
5256
}
5357

5458
export class CollapseNotificationAction extends Action {
5559

56-
public static readonly ID = 'workbench.action.collapseNotification';
60+
public static readonly ID = COLLAPSE_NOTIFICATION;
5761
public static readonly LABEL = localize('collapseNotification', "Collapse Notification");
5862

5963
constructor(
6064
id: string,
61-
label: string
65+
label: string,
66+
@ICommandService private commandService: ICommandService
6267
) {
6368
super(id, label, 'collapse-notification-action');
6469
}
6570

6671
public run(notification: INotificationViewItem): TPromise<any> {
67-
notification.collapse();
72+
this.commandService.executeCommand(COLLAPSE_NOTIFICATION, notification);
6873

6974
return TPromise.as(void 0);
7075
}

0 commit comments

Comments
 (0)