Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/codeEditor/browser/menuPreventer.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { KeyMod } from '../../../../base/common/keyCodes.js';
7
import { Disposable } from '../../../../base/common/lifecycle.js';
8
import { ICodeEditor } from '../../../../editor/browser/editorBrowser.js';
9
import { EditorContributionInstantiation, registerEditorContribution } from '../../../../editor/browser/editorExtensions.js';
10
import { IEditorContribution } from '../../../../editor/common/editorCommon.js';
11
12
/**
13
* Prevents the top-level menu from showing up when doing Alt + Click in the editor
14
*/
15
export class MenuPreventer extends Disposable implements IEditorContribution {
16
17
public static readonly ID = 'editor.contrib.menuPreventer';
18
19
private _editor: ICodeEditor;
20
private _altListeningMouse: boolean;
21
private _altMouseTriggered: boolean;
22
23
constructor(editor: ICodeEditor) {
24
super();
25
this._editor = editor;
26
this._altListeningMouse = false;
27
this._altMouseTriggered = false;
28
29
// A global crossover handler to prevent menu bar from showing up
30
// When <alt> is hold, we will listen to mouse events and prevent
31
// the release event up <alt> if the mouse is triggered.
32
33
this._register(this._editor.onMouseDown((e) => {
34
if (this._altListeningMouse) {
35
this._altMouseTriggered = true;
36
}
37
}));
38
39
this._register(this._editor.onKeyDown((e) => {
40
if (e.equals(KeyMod.Alt)) {
41
if (!this._altListeningMouse) {
42
this._altMouseTriggered = false;
43
}
44
this._altListeningMouse = true;
45
}
46
}));
47
48
this._register(this._editor.onKeyUp((e) => {
49
if (e.equals(KeyMod.Alt)) {
50
if (this._altMouseTriggered) {
51
e.preventDefault();
52
}
53
this._altListeningMouse = false;
54
this._altMouseTriggered = false;
55
}
56
}));
57
}
58
}
59
60
registerEditorContribution(MenuPreventer.ID, MenuPreventer, EditorContributionInstantiation.BeforeFirstInteraction);
61
62