|
7 | 7 |
|
8 | 8 | import { CommandsRegistry } from 'vs/platform/commands/common/commands'; |
9 | 9 | 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'; |
10 | 18 |
|
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)); |
14 | 35 |
|
15 | 36 | export interface INotificationsCenterController { |
16 | | - isVisible: boolean; |
| 37 | + readonly isVisible: boolean; |
| 38 | + readonly selected: INotificationViewItem; |
17 | 39 |
|
18 | 40 | show(): void; |
19 | 41 | hide(): void; |
| 42 | + |
| 43 | + clearAll(): void; |
20 | 44 | } |
21 | 45 |
|
22 | 46 | export function registerNotificationCommands(center: INotificationsCenterController): void { |
23 | 47 |
|
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 | + |
26 | 169 |
|
27 | | - // Hide Center |
28 | | - CommandsRegistry.registerCommand(HIDE_NOTFICATIONS_CENTER_COMMAND_ID, () => center.hide()); |
29 | 170 |
|
30 | | - // Toggle Center |
31 | | - CommandsRegistry.registerCommand(TOGGLE_NOTFICATIONS_CENTER_COMMAND_ID, () => center.isVisible ? center.hide() : center.show()); |
32 | 171 |
|
33 | 172 | // TODO@Notification remove me |
34 | 173 | CommandsRegistry.registerCommand('notifications.showInfo', accessor => { |
|
0 commit comments