Path: blob/main/src/vs/workbench/contrib/codeEditor/browser/menuPreventer.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { KeyMod } from '../../../../base/common/keyCodes.js';6import { Disposable } from '../../../../base/common/lifecycle.js';7import { ICodeEditor } from '../../../../editor/browser/editorBrowser.js';8import { EditorContributionInstantiation, registerEditorContribution } from '../../../../editor/browser/editorExtensions.js';9import { IEditorContribution } from '../../../../editor/common/editorCommon.js';1011/**12* Prevents the top-level menu from showing up when doing Alt + Click in the editor13*/14export class MenuPreventer extends Disposable implements IEditorContribution {1516public static readonly ID = 'editor.contrib.menuPreventer';1718private _editor: ICodeEditor;19private _altListeningMouse: boolean;20private _altMouseTriggered: boolean;2122constructor(editor: ICodeEditor) {23super();24this._editor = editor;25this._altListeningMouse = false;26this._altMouseTriggered = false;2728// A global crossover handler to prevent menu bar from showing up29// When <alt> is hold, we will listen to mouse events and prevent30// the release event up <alt> if the mouse is triggered.3132this._register(this._editor.onMouseDown((e) => {33if (this._altListeningMouse) {34this._altMouseTriggered = true;35}36}));3738this._register(this._editor.onKeyDown((e) => {39if (e.equals(KeyMod.Alt)) {40if (!this._altListeningMouse) {41this._altMouseTriggered = false;42}43this._altListeningMouse = true;44}45}));4647this._register(this._editor.onKeyUp((e) => {48if (e.equals(KeyMod.Alt)) {49if (this._altMouseTriggered) {50e.preventDefault();51}52this._altListeningMouse = false;53this._altMouseTriggered = false;54}55}));56}57}5859registerEditorContribution(MenuPreventer.ID, MenuPreventer, EditorContributionInstantiation.BeforeFirstInteraction);606162