UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

134 lines • 6.96 kB
// ***************************************************************************** // Copyright (C) 2017 TypeFox and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { __decorate } from "tslib"; /* eslint-disable @typescript-eslint/no-explicit-any */ import { inject, injectable, postConstruct } from 'inversify'; import { ContextMenuRenderer, ContextMenuAccess, CommonCommands, coordinateFromAnchor } from '../../browser/index.js'; import { ElectronMainMenuFactory } from './electron-main-menu-factory.js'; import { ContextMenuContext } from '../../browser/menu/context-menu-context.js'; import { BrowserContextMenuRenderer } from '../../browser/menu/browser-context-menu-renderer.js'; import { ContextKeyService } from '../../browser/context-key-service.js'; import { PreferenceService } from '../../common/preferences/index.js'; export class ElectronContextMenuAccess extends ContextMenuAccess { constructor(menuHandle) { super({ dispose: () => menuHandle.then(handle => window.electronTheiaCore.closePopup(handle)) }); this.menuHandle = menuHandle; } } export var ElectronTextInputContextMenu; (function (ElectronTextInputContextMenu) { ElectronTextInputContextMenu.MENU_PATH = ['electron_text_input']; ElectronTextInputContextMenu.UNDO_REDO_EDIT_GROUP = [...ElectronTextInputContextMenu.MENU_PATH, '0_undo_redo_group']; ElectronTextInputContextMenu.EDIT_GROUP = [...ElectronTextInputContextMenu.MENU_PATH, '1_edit_group']; ElectronTextInputContextMenu.SELECT_GROUP = [...ElectronTextInputContextMenu.MENU_PATH, '2_select_group']; })(ElectronTextInputContextMenu || (ElectronTextInputContextMenu = {})); let ElectronTextInputContextMenuContribution = class ElectronTextInputContextMenuContribution { onStart() { window.document.addEventListener('contextmenu', event => { if (event.target instanceof HTMLElement) { const target = event.target; if (target.nodeName && (target.nodeName.toLowerCase() === 'input' || target.nodeName.toLowerCase() === 'textarea')) { event.preventDefault(); event.stopPropagation(); this.contextMenuRenderer.render({ anchor: event, contextKeyService: this.contextKeyService, menuPath: ElectronTextInputContextMenu.MENU_PATH, context: event.target, onHide: () => target.focus() }); } } }); } registerMenus(registry) { registry.registerMenuAction(ElectronTextInputContextMenu.UNDO_REDO_EDIT_GROUP, { commandId: CommonCommands.UNDO.id }); registry.registerMenuAction(ElectronTextInputContextMenu.UNDO_REDO_EDIT_GROUP, { commandId: CommonCommands.REDO.id }); registry.registerMenuAction(ElectronTextInputContextMenu.EDIT_GROUP, { commandId: CommonCommands.CUT.id }); registry.registerMenuAction(ElectronTextInputContextMenu.EDIT_GROUP, { commandId: CommonCommands.COPY.id }); registry.registerMenuAction(ElectronTextInputContextMenu.EDIT_GROUP, { commandId: CommonCommands.PASTE.id }); registry.registerMenuAction(ElectronTextInputContextMenu.SELECT_GROUP, { commandId: CommonCommands.SELECT_ALL.id }); } }; __decorate([ inject(ContextMenuRenderer) ], ElectronTextInputContextMenuContribution.prototype, "contextMenuRenderer", void 0); __decorate([ inject(ContextKeyService) ], ElectronTextInputContextMenuContribution.prototype, "contextKeyService", void 0); ElectronTextInputContextMenuContribution = __decorate([ injectable() ], ElectronTextInputContextMenuContribution); export { ElectronTextInputContextMenuContribution }; let ElectronContextMenuRenderer = class ElectronContextMenuRenderer extends BrowserContextMenuRenderer { constructor() { super(); this.useNativeStyle = true; } init() { this.doInit(); } async doInit() { this.useNativeStyle = await window.electronTheiaCore.getTitleBarStyleAtStartup() === 'native'; } doRender(params) { if (this.useNativeStyle) { const contextMenu = this.electronMenuFactory.createElectronContextMenu(params.menuPath, params.menu, params.contextMatcher, params.args, params.context); const { x, y } = coordinateFromAnchor(params.anchor); // the name of the window, e.g. of a secondary window, in which the menu is to be shown; empty for the main window const windowName = params.context?.ownerDocument.defaultView?.name; const menuHandle = window.electronTheiaCore.popup(contextMenu, x, y, () => { if (params.onHide) { params.onHide(); } }, windowName); // native context menu stops the event loop, so there is no keyboard events this.context.resetAltPressed(); return new ElectronContextMenuAccess(menuHandle); } else { const menuAccess = super.doRender(params); const node = menuAccess.menu.node; const topPanelHeight = document.getElementById('theia-top-panel')?.clientHeight ?? 0; // ensure the context menu is not displayed outside of the main area const menuRect = node.getBoundingClientRect(); if (menuRect.top < topPanelHeight) { node.style.top = `${topPanelHeight}px`; node.style.maxHeight = `calc(${node.style.maxHeight} - ${topPanelHeight}px)`; } return menuAccess; } } }; __decorate([ inject(ContextMenuContext) ], ElectronContextMenuRenderer.prototype, "context", void 0); __decorate([ inject(PreferenceService) ], ElectronContextMenuRenderer.prototype, "preferenceService", void 0); __decorate([ inject(ElectronMainMenuFactory) ], ElectronContextMenuRenderer.prototype, "electronMenuFactory", void 0); __decorate([ postConstruct() ], ElectronContextMenuRenderer.prototype, "init", null); ElectronContextMenuRenderer = __decorate([ injectable() ], ElectronContextMenuRenderer); export { ElectronContextMenuRenderer }; //# sourceMappingURL=electron-context-menu-renderer.js.map