Skip to content

Commit

Permalink
GH-7767: Added context menu for input and textArea
Browse files Browse the repository at this point in the history
Closes #7767.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
  • Loading branch information
Akos Kitta committed Jun 2, 2020
1 parent 586180a commit 4cc73f0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,62 @@

import * as electron from 'electron';
import { inject, injectable } from 'inversify';
import { ContextMenuRenderer, RenderContextMenuOptions, ContextMenuAccess } from '../../browser';
import { ContextMenuRenderer, RenderContextMenuOptions, ContextMenuAccess, FrontendApplicationContribution, CommonCommands } from '../../browser';
import { ElectronMainMenuFactory } from './electron-main-menu-factory';
import { ContextMenuContext } from '../../browser/menu/context-menu-context';
import { MenuPath, MenuContribution, MenuModelRegistry } from '../../common';

export class ElectronContextMenuAccess extends ContextMenuAccess {
constructor(
public readonly menu: electron.Menu
) {
constructor(readonly menu: electron.Menu) {
super({
dispose: () => menu.closePopup()
});
}
}

export namespace ElectronTextInputContextMenu {
export const MENU_PATH: MenuPath = ['electron_text_input'];
export const UNDO_REDO_EDIT_GROUP = [...MENU_PATH, '0_undo_redo_group'];
export const EDIT_GROUP = [...MENU_PATH, '1_edit_group'];
export const SELECT_GROUP = [...MENU_PATH, '2_select_group'];
}

@injectable()
export class ElectronTextInputContextMenuContribution implements FrontendApplicationContribution, MenuContribution {

@inject(ContextMenuRenderer)
protected readonly contextMenuRenderer: ContextMenuRenderer;

onStart(): void {
window.document.addEventListener('contextmenu', event => {
if (event.target instanceof HTMLElement) {
const target = <HTMLElement>event.target;
if (target.nodeName && (target.nodeName.toLowerCase() === 'input' || target.nodeName.toLowerCase() === 'textarea')) {
event.preventDefault();
event.stopPropagation();
this.contextMenuRenderer.render({
anchor: { x: event.x, y: event.y },
menuPath: ElectronTextInputContextMenu.MENU_PATH,
onHide: () => target.focus()
});
}
}
});
}

registerMenus(registry: MenuModelRegistry): void {
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 });
}

}

@injectable()
export class ElectronContextMenuRenderer extends ContextMenuRenderer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ContainerModule } from 'inversify';
import { CommandContribution, MenuContribution } from '../../common';
import { FrontendApplicationContribution, ContextMenuRenderer, KeybindingContribution, KeybindingContext } from '../../browser';
import { ElectronMainMenuFactory } from './electron-main-menu-factory';
import { ElectronContextMenuRenderer } from './electron-context-menu-renderer';
import { ElectronContextMenuRenderer, ElectronTextInputContextMenuContribution } from './electron-context-menu-renderer';
import { ElectronMenuContribution } from './electron-menu-contribution';

export default new ContainerModule(bind => {
Expand All @@ -33,4 +33,6 @@ export default new ContainerModule(bind => {
for (const serviceIdentifier of [FrontendApplicationContribution, KeybindingContribution, CommandContribution, MenuContribution]) {
bind(serviceIdentifier).toService(ElectronMenuContribution);
}
bind(FrontendApplicationContribution).to(ElectronTextInputContextMenuContribution).inSingletonScope();
bind(MenuContribution).to(ElectronTextInputContextMenuContribution).inSingletonScope();
});

0 comments on commit 4cc73f0

Please sign in to comment.