-
Notifications
You must be signed in to change notification settings - Fork 28k
/
accessibleViewContributions.ts
351 lines (334 loc) · 13.7 KB
/
accessibleViewContributions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Disposable } from 'vs/base/common/lifecycle';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { localize } from 'vs/nls';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { AccessibilityVerbositySettingId, AccessibleViewProviderId, accessibleViewIsShown } from 'vs/workbench/contrib/accessibility/browser/accessibilityConfiguration';
import * as strings from 'vs/base/common/strings';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { getNotificationFromContext } from 'vs/workbench/browser/parts/notifications/notificationsCommands';
import { IListService, WorkbenchList } from 'vs/platform/list/browser/listService';
import { NotificationFocusedContext } from 'vs/workbench/common/contextkeys';
import { IAccessibleViewService, IAccessibleViewOptions, AccessibleViewType } from 'vs/workbench/contrib/accessibility/browser/accessibleView';
import { IHoverService } from 'vs/platform/hover/browser/hover';
import { alert } from 'vs/base/browser/ui/aria/aria';
import { AccessibilityHelpAction, AccessibleViewAction } from 'vs/workbench/contrib/accessibility/browser/accessibleViewActions';
import { IAction } from 'vs/base/common/actions';
import { INotificationViewItem } from 'vs/workbench/common/notifications';
import { ThemeIcon } from 'vs/base/common/themables';
import { Codicon } from 'vs/base/common/codicons';
import { InlineCompletionsController } from 'vs/editor/contrib/inlineCompletions/browser/inlineCompletionsController';
import { InlineCompletionContextKeys } from 'vs/editor/contrib/inlineCompletions/browser/inlineCompletionContextKeys';
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { AccessibilitySignal, IAccessibilitySignalService } from 'vs/platform/accessibilitySignal/browser/accessibilitySignalService';
import { COMMENTS_VIEW_ID, CommentsMenus } from 'vs/workbench/contrib/comments/browser/commentsTreeViewer';
import { IViewsService } from 'vs/workbench/services/views/common/viewsService';
import { CommentsPanel, CONTEXT_KEY_HAS_COMMENTS } from 'vs/workbench/contrib/comments/browser/commentsView';
import { IMenuService } from 'vs/platform/actions/common/actions';
import { MarshalledId } from 'vs/base/common/marshallingIds';
import { HoverController } from 'vs/editor/contrib/hover/browser/hoverController';
export function descriptionForCommand(commandId: string, msg: string, noKbMsg: string, keybindingService: IKeybindingService): string {
const kb = keybindingService.lookupKeybinding(commandId);
if (kb) {
return strings.format(msg, kb.getAriaLabel());
}
return strings.format(noKbMsg, commandId);
}
export class HoverAccessibleViewContribution extends Disposable {
static ID: 'hoverAccessibleViewContribution';
private _options: IAccessibleViewOptions = { language: 'typescript', type: AccessibleViewType.View };
constructor() {
super();
this._register(AccessibleViewAction.addImplementation(95, 'hover', accessor => {
const accessibleViewService = accessor.get(IAccessibleViewService);
const codeEditorService = accessor.get(ICodeEditorService);
const editor = codeEditorService.getActiveCodeEditor() || codeEditorService.getFocusedCodeEditor();
const editorHoverContent = editor ? HoverController.get(editor)?.getWidgetContent() ?? undefined : undefined;
if (!editor || !editorHoverContent) {
return false;
}
this._options.language = editor?.getModel()?.getLanguageId() ?? undefined;
accessibleViewService.show({
id: AccessibleViewProviderId.Hover,
verbositySettingKey: AccessibilityVerbositySettingId.Hover,
provideContent() { return editorHoverContent; },
onClose() {
HoverController.get(editor)?.focus();
},
options: this._options
});
return true;
}, EditorContextKeys.hoverFocused));
this._register(AccessibleViewAction.addImplementation(90, 'extension-hover', accessor => {
const accessibleViewService = accessor.get(IAccessibleViewService);
const contextViewService = accessor.get(IContextViewService);
const contextViewElement = contextViewService.getContextViewElement();
const extensionHoverContent = contextViewElement?.textContent ?? undefined;
const hoverService = accessor.get(IHoverService);
if (contextViewElement.classList.contains('accessible-view-container') || !extensionHoverContent) {
// The accessible view, itself, uses the context view service to display the text. We don't want to read that.
return false;
}
accessibleViewService.show({
id: AccessibleViewProviderId.Hover,
verbositySettingKey: AccessibilityVerbositySettingId.Hover,
provideContent() { return extensionHoverContent; },
onClose() {
hoverService.showAndFocusLastHover();
},
options: this._options
});
return true;
}));
this._register(AccessibilityHelpAction.addImplementation(115, 'accessible-view', accessor => {
accessor.get(IAccessibleViewService).showAccessibleViewHelp();
return true;
}, accessibleViewIsShown));
}
}
export class NotificationAccessibleViewContribution extends Disposable {
static ID: 'notificationAccessibleViewContribution';
constructor() {
super();
this._register(AccessibleViewAction.addImplementation(90, 'notifications', accessor => {
const accessibleViewService = accessor.get(IAccessibleViewService);
const listService = accessor.get(IListService);
const commandService = accessor.get(ICommandService);
const accessibilitySignalService = accessor.get(IAccessibilitySignalService);
function renderAccessibleView(): boolean {
const notification = getNotificationFromContext(listService);
if (!notification) {
return false;
}
commandService.executeCommand('notifications.showList');
let notificationIndex: number | undefined;
let length: number | undefined;
const list = listService.lastFocusedList;
if (list instanceof WorkbenchList) {
notificationIndex = list.indexOf(notification);
length = list.length;
}
if (notificationIndex === undefined) {
return false;
}
function focusList(): void {
commandService.executeCommand('notifications.showList');
if (list && notificationIndex !== undefined) {
list.domFocus();
try {
list.setFocus([notificationIndex]);
} catch { }
}
}
const message = notification.message.original.toString();
if (!message) {
return false;
}
notification.onDidClose(() => accessibleViewService.next());
accessibleViewService.show({
id: AccessibleViewProviderId.Notification,
provideContent: () => {
return notification.source ? localize('notification.accessibleViewSrc', '{0} Source: {1}', message, notification.source) : localize('notification.accessibleView', '{0}', message);
},
onClose(): void {
focusList();
},
next(): void {
if (!list) {
return;
}
focusList();
list.focusNext();
alertFocusChange(notificationIndex, length, 'next');
renderAccessibleView();
},
previous(): void {
if (!list) {
return;
}
focusList();
list.focusPrevious();
alertFocusChange(notificationIndex, length, 'previous');
renderAccessibleView();
},
verbositySettingKey: AccessibilityVerbositySettingId.Notification,
options: { type: AccessibleViewType.View },
actions: getActionsFromNotification(notification, accessibilitySignalService)
});
return true;
}
return renderAccessibleView();
}, NotificationFocusedContext));
}
}
function getActionsFromNotification(notification: INotificationViewItem, accessibilitySignalService: IAccessibilitySignalService): IAction[] | undefined {
let actions = undefined;
if (notification.actions) {
actions = [];
if (notification.actions.primary) {
actions.push(...notification.actions.primary);
}
if (notification.actions.secondary) {
actions.push(...notification.actions.secondary);
}
}
if (actions) {
for (const action of actions) {
action.class = ThemeIcon.asClassName(Codicon.bell);
const initialAction = action.run;
action.run = () => {
initialAction();
notification.close();
};
}
}
const manageExtension = actions?.find(a => a.label.includes('Manage Extension'));
if (manageExtension) {
manageExtension.class = ThemeIcon.asClassName(Codicon.gear);
}
if (actions) {
actions.push({
id: 'clearNotification', label: localize('clearNotification', "Clear Notification"), tooltip: localize('clearNotification', "Clear Notification"), run: () => {
notification.close();
accessibilitySignalService.playSignal(AccessibilitySignal.clear);
}, enabled: true, class: ThemeIcon.asClassName(Codicon.clearAll)
});
}
return actions;
}
export function alertFocusChange(index: number | undefined, length: number | undefined, type: 'next' | 'previous'): void {
if (index === undefined || length === undefined) {
return;
}
const number = index + 1;
if (type === 'next' && number + 1 <= length) {
alert(`Focused ${number + 1} of ${length}`);
} else if (type === 'previous' && number - 1 > 0) {
alert(`Focused ${number - 1} of ${length}`);
}
return;
}
export class CommentAccessibleViewContribution extends Disposable {
static ID: 'commentAccessibleViewContribution';
constructor() {
super();
this._register(AccessibleViewAction.addImplementation(90, 'comment', accessor => {
const accessibleViewService = accessor.get(IAccessibleViewService);
const contextKeyService = accessor.get(IContextKeyService);
const viewsService = accessor.get(IViewsService);
const menuService = accessor.get(IMenuService);
const commentsView = viewsService.getActiveViewWithId<CommentsPanel>(COMMENTS_VIEW_ID);
if (!commentsView) {
return false;
}
const menus = this._register(new CommentsMenus(menuService));
menus.setContextKeyService(contextKeyService);
function renderAccessibleView() {
if (!commentsView) {
return false;
}
const commentNode = commentsView?.focusedCommentNode;
const content = commentsView.focusedCommentInfo?.toString();
if (!commentNode || !content) {
return false;
}
const menuActions = [...menus.getResourceContextActions(commentNode)].filter(i => i.enabled);
const actions = menuActions.map(action => {
return {
...action,
run: () => {
commentsView.focus();
action.run({
thread: commentNode.thread,
$mid: MarshalledId.CommentThread,
commentControlHandle: commentNode.controllerHandle,
commentThreadHandle: commentNode.threadHandle,
});
}
};
});
accessibleViewService.show({
id: AccessibleViewProviderId.Notification,
provideContent: () => {
return content;
},
onClose(): void {
commentsView.focus();
},
next(): void {
commentsView.focus();
commentsView.focusNextNode();
renderAccessibleView();
},
previous(): void {
commentsView.focus();
commentsView.focusPreviousNode();
renderAccessibleView();
},
verbositySettingKey: AccessibilityVerbositySettingId.Comments,
options: { type: AccessibleViewType.View },
actions
});
return true;
}
return renderAccessibleView();
}, CONTEXT_KEY_HAS_COMMENTS));
}
}
export class InlineCompletionsAccessibleViewContribution extends Disposable {
static ID: 'inlineCompletionsAccessibleViewContribution';
private _options: IAccessibleViewOptions = { type: AccessibleViewType.View };
constructor() {
super();
this._register(AccessibleViewAction.addImplementation(95, 'inline-completions', accessor => {
const accessibleViewService = accessor.get(IAccessibleViewService);
const codeEditorService = accessor.get(ICodeEditorService);
const show = () => {
const editor = codeEditorService.getActiveCodeEditor() || codeEditorService.getFocusedCodeEditor();
if (!editor) {
return false;
}
const model = InlineCompletionsController.get(editor)?.model.get();
const state = model?.state.get();
if (!model || !state) {
return false;
}
const lineText = model.textModel.getLineContent(state.primaryGhostText.lineNumber);
const ghostText = state.primaryGhostText.renderForScreenReader(lineText);
if (!ghostText) {
return false;
}
this._options.language = editor.getModel()?.getLanguageId() ?? undefined;
accessibleViewService.show({
id: AccessibleViewProviderId.InlineCompletions,
verbositySettingKey: AccessibilityVerbositySettingId.InlineCompletions,
provideContent() { return lineText + ghostText; },
onClose() {
model.stop();
editor.focus();
},
next() {
model.next();
setTimeout(() => show(), 50);
},
previous() {
model.previous();
setTimeout(() => show(), 50);
},
options: this._options
});
return true;
}; ContextKeyExpr.and(InlineCompletionContextKeys.inlineSuggestionVisible);
return show();
}));
}
}