/
contextmenu.ts
412 lines (356 loc) · 14.1 KB
/
contextmenu.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as dom from 'vs/base/browser/dom';
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
import { ActionViewItem } from 'vs/base/browser/ui/actionbar/actionViewItems';
import { IAnchor } from 'vs/base/browser/ui/contextview/contextview';
import { IAction, Separator, SubmenuAction } from 'vs/base/common/actions';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { ResolvedKeybinding } from 'vs/base/common/keybindings';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { isIOS } from 'vs/base/common/platform';
import { ICodeEditor, IEditorMouseEvent, MouseTargetType } from 'vs/editor/browser/editorBrowser';
import { EditorAction, EditorContributionInstantiation, registerEditorAction, registerEditorContribution, ServicesAccessor } from 'vs/editor/browser/editorExtensions';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { IEditorContribution, ScrollType } from 'vs/editor/common/editorCommon';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { ITextModel } from 'vs/editor/common/model';
import * as nls from 'vs/nls';
import { IMenuService, MenuId, SubmenuItemAction } from 'vs/platform/actions/common/actions';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService, IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IWorkspaceContextService, isStandaloneEditorWorkspace } from 'vs/platform/workspace/common/workspace';
export class ContextMenuController implements IEditorContribution {
public static readonly ID = 'editor.contrib.contextmenu';
public static get(editor: ICodeEditor): ContextMenuController | null {
return editor.getContribution<ContextMenuController>(ContextMenuController.ID);
}
private readonly _toDispose = new DisposableStore();
private _contextMenuIsBeingShownCount: number = 0;
private readonly _editor: ICodeEditor;
constructor(
editor: ICodeEditor,
@IContextMenuService private readonly _contextMenuService: IContextMenuService,
@IContextViewService private readonly _contextViewService: IContextViewService,
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
@IKeybindingService private readonly _keybindingService: IKeybindingService,
@IMenuService private readonly _menuService: IMenuService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IWorkspaceContextService private readonly _workspaceContextService: IWorkspaceContextService,
) {
this._editor = editor;
this._toDispose.add(this._editor.onContextMenu((e: IEditorMouseEvent) => this._onContextMenu(e)));
this._toDispose.add(this._editor.onMouseWheel((e: IMouseWheelEvent) => {
if (this._contextMenuIsBeingShownCount > 0) {
const view = this._contextViewService.getContextViewElement();
const target = e.srcElement as HTMLElement;
// Event triggers on shadow root host first
// Check if the context view is under this host before hiding it #103169
if (!(target.shadowRoot && dom.getShadowRoot(view) === target.shadowRoot)) {
this._contextViewService.hideContextView();
}
}
}));
this._toDispose.add(this._editor.onKeyDown((e: IKeyboardEvent) => {
if (!this._editor.getOption(EditorOption.contextmenu)) {
return; // Context menu is turned off through configuration
}
if (e.keyCode === KeyCode.ContextMenu) {
// Chrome is funny like that
e.preventDefault();
e.stopPropagation();
this.showContextMenu();
}
}));
}
private _onContextMenu(e: IEditorMouseEvent): void {
if (!this._editor.hasModel()) {
return;
}
if (!this._editor.getOption(EditorOption.contextmenu)) {
this._editor.focus();
// Ensure the cursor is at the position of the mouse click
if (e.target.position && !this._editor.getSelection().containsPosition(e.target.position)) {
this._editor.setPosition(e.target.position);
}
return; // Context menu is turned off through configuration
}
if (e.target.type === MouseTargetType.OVERLAY_WIDGET) {
return; // allow native menu on widgets to support right click on input field for example in find
}
if (e.target.type === MouseTargetType.CONTENT_TEXT && e.target.detail.injectedText) {
return; // allow native menu on injected text
}
e.event.preventDefault();
e.event.stopPropagation();
if (e.target.type === MouseTargetType.SCROLLBAR) {
return this._showScrollbarContextMenu({ x: e.event.posx - 1, width: 2, y: e.event.posy - 1, height: 2 });
}
if (e.target.type !== MouseTargetType.CONTENT_TEXT && e.target.type !== MouseTargetType.CONTENT_EMPTY && e.target.type !== MouseTargetType.TEXTAREA) {
return; // only support mouse click into text or native context menu key for now
}
// Ensure the editor gets focus if it hasn't, so the right events are being sent to other contributions
this._editor.focus();
// Ensure the cursor is at the position of the mouse click
if (e.target.position) {
let hasSelectionAtPosition = false;
for (const selection of this._editor.getSelections()) {
if (selection.containsPosition(e.target.position)) {
hasSelectionAtPosition = true;
break;
}
}
if (!hasSelectionAtPosition) {
this._editor.setPosition(e.target.position);
}
}
// Unless the user triggerd the context menu through Shift+F10, use the mouse position as menu position
let anchor: IAnchor | null = null;
if (e.target.type !== MouseTargetType.TEXTAREA) {
anchor = { x: e.event.posx - 1, width: 2, y: e.event.posy - 1, height: 2 };
}
// Show the context menu
this.showContextMenu(anchor);
}
public showContextMenu(anchor?: IAnchor | null): void {
if (!this._editor.getOption(EditorOption.contextmenu)) {
return; // Context menu is turned off through configuration
}
if (!this._editor.hasModel()) {
return;
}
// Find actions available for menu
const menuActions = this._getMenuActions(this._editor.getModel(),
this._editor.isSimpleWidget ? MenuId.SimpleEditorContext : MenuId.EditorContext);
// Show menu if we have actions to show
if (menuActions.length > 0) {
this._doShowContextMenu(menuActions, anchor);
}
}
private _getMenuActions(model: ITextModel, menuId: MenuId): IAction[] {
const result: IAction[] = [];
// get menu groups
const menu = this._menuService.createMenu(menuId, this._contextKeyService);
const groups = menu.getActions({ arg: model.uri });
menu.dispose();
// translate them into other actions
for (const group of groups) {
const [, actions] = group;
let addedItems = 0;
for (const action of actions) {
if (action instanceof SubmenuItemAction) {
const subActions = this._getMenuActions(model, action.item.submenu);
if (subActions.length > 0) {
result.push(new SubmenuAction(action.id, action.label, subActions));
addedItems++;
}
} else {
result.push(action);
addedItems++;
}
}
if (addedItems) {
result.push(new Separator());
}
}
if (result.length) {
result.pop(); // remove last separator
}
return result;
}
private _doShowContextMenu(actions: IAction[], anchor: IAnchor | null = null): void {
if (!this._editor.hasModel()) {
return;
}
// Disable hover
const oldHoverSetting = this._editor.getOption(EditorOption.hover);
this._editor.updateOptions({
hover: {
enabled: false
}
});
if (!anchor) {
// Ensure selection is visible
this._editor.revealPosition(this._editor.getPosition(), ScrollType.Immediate);
this._editor.render();
const cursorCoords = this._editor.getScrolledVisiblePosition(this._editor.getPosition());
// Translate to absolute editor position
const editorCoords = dom.getDomNodePagePosition(this._editor.getDomNode());
const posx = editorCoords.left + cursorCoords.left;
const posy = editorCoords.top + cursorCoords.top + cursorCoords.height;
anchor = { x: posx, y: posy };
}
const useShadowDOM = this._editor.getOption(EditorOption.useShadowDOM) && !isIOS; // Do not use shadow dom on IOS #122035
// Show menu
this._contextMenuIsBeingShownCount++;
this._contextMenuService.showContextMenu({
domForShadowRoot: useShadowDOM ? this._editor.getDomNode() : undefined,
getAnchor: () => anchor!,
getActions: () => actions,
getActionViewItem: (action) => {
const keybinding = this._keybindingFor(action);
if (keybinding) {
return new ActionViewItem(action, action, { label: true, keybinding: keybinding.getLabel(), isMenu: true });
}
const customActionViewItem = <any>action;
if (typeof customActionViewItem.getActionViewItem === 'function') {
return customActionViewItem.getActionViewItem();
}
return new ActionViewItem(action, action, { icon: true, label: true, isMenu: true });
},
getKeyBinding: (action): ResolvedKeybinding | undefined => {
return this._keybindingFor(action);
},
onHide: (wasCancelled: boolean) => {
this._contextMenuIsBeingShownCount--;
this._editor.updateOptions({
hover: oldHoverSetting
});
}
});
}
private _showScrollbarContextMenu(anchor: IAnchor): void {
if (!this._editor.hasModel()) {
return;
}
if (isStandaloneEditorWorkspace(this._workspaceContextService.getWorkspace())) {
// can't update the configuration properly in the standalone editor
return;
}
const minimapOptions = this._editor.getOption(EditorOption.minimap);
let lastId = 0;
const createAction = (opts: { label: string; enabled?: boolean; checked?: boolean; run: () => void }): IAction => {
return {
id: `menu-action-${++lastId}`,
label: opts.label,
tooltip: '',
class: undefined,
enabled: (typeof opts.enabled === 'undefined' ? true : opts.enabled),
checked: opts.checked,
run: opts.run
};
};
const createSubmenuAction = (label: string, actions: IAction[]): SubmenuAction => {
return new SubmenuAction(
`menu-action-${++lastId}`,
label,
actions,
undefined
);
};
const createEnumAction = <T>(label: string, enabled: boolean, configName: string, configuredValue: T, options: { label: string; value: T }[]): IAction => {
if (!enabled) {
return createAction({ label, enabled, run: () => { } });
}
const createRunner = (value: T) => {
return () => {
this._configurationService.updateValue(configName, value);
};
};
const actions: IAction[] = [];
for (const option of options) {
actions.push(createAction({
label: option.label,
checked: configuredValue === option.value,
run: createRunner(option.value)
}));
}
return createSubmenuAction(
label,
actions
);
};
const actions: IAction[] = [];
actions.push(createAction({
label: nls.localize('context.minimap.minimap', "Minimap"),
checked: minimapOptions.enabled,
run: () => {
this._configurationService.updateValue(`editor.minimap.enabled`, !minimapOptions.enabled);
}
}));
actions.push(new Separator());
actions.push(createAction({
label: nls.localize('context.minimap.renderCharacters', "Render Characters"),
enabled: minimapOptions.enabled,
checked: minimapOptions.renderCharacters,
run: () => {
this._configurationService.updateValue(`editor.minimap.renderCharacters`, !minimapOptions.renderCharacters);
}
}));
actions.push(createEnumAction<'proportional' | 'fill' | 'fit'>(
nls.localize('context.minimap.size', "Vertical size"),
minimapOptions.enabled,
'editor.minimap.size',
minimapOptions.size,
[{
label: nls.localize('context.minimap.size.proportional', "Proportional"),
value: 'proportional'
}, {
label: nls.localize('context.minimap.size.fill', "Fill"),
value: 'fill'
}, {
label: nls.localize('context.minimap.size.fit', "Fit"),
value: 'fit'
}]
));
actions.push(createEnumAction<'always' | 'mouseover'>(
nls.localize('context.minimap.slider', "Slider"),
minimapOptions.enabled,
'editor.minimap.showSlider',
minimapOptions.showSlider,
[{
label: nls.localize('context.minimap.slider.mouseover', "Mouse Over"),
value: 'mouseover'
}, {
label: nls.localize('context.minimap.slider.always', "Always"),
value: 'always'
}]
));
const useShadowDOM = this._editor.getOption(EditorOption.useShadowDOM) && !isIOS; // Do not use shadow dom on IOS #122035
this._contextMenuIsBeingShownCount++;
this._contextMenuService.showContextMenu({
domForShadowRoot: useShadowDOM ? this._editor.getDomNode() : undefined,
getAnchor: () => anchor,
getActions: () => actions,
onHide: (wasCancelled: boolean) => {
this._contextMenuIsBeingShownCount--;
this._editor.focus();
}
});
}
private _keybindingFor(action: IAction): ResolvedKeybinding | undefined {
return this._keybindingService.lookupKeybinding(action.id);
}
public dispose(): void {
if (this._contextMenuIsBeingShownCount > 0) {
this._contextViewService.hideContextView();
}
this._toDispose.dispose();
}
}
class ShowContextMenu extends EditorAction {
constructor() {
super({
id: 'editor.action.showContextMenu',
label: nls.localize('action.showContextMenu.label', "Show Editor Context Menu"),
alias: 'Show Editor Context Menu',
precondition: undefined,
kbOpts: {
kbExpr: EditorContextKeys.textInputFocus,
primary: KeyMod.Shift | KeyCode.F10,
weight: KeybindingWeight.EditorContrib
}
});
}
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
ContextMenuController.get(editor)?.showContextMenu();
}
}
registerEditorContribution(ContextMenuController.ID, ContextMenuController, EditorContributionInstantiation.BeforeFirstInteraction);
registerEditorAction(ShowContextMenu);