From 024a1013741eb3898c60b690b846a7569ab98d8a Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 13 Feb 2018 23:06:45 -0500 Subject: [PATCH] context menu --- src/main/menu.main.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/main/menu.main.ts b/src/main/menu.main.ts index 82883bf364..b0ef4d7ad6 100644 --- a/src/main/menu.main.ts +++ b/src/main/menu.main.ts @@ -21,6 +21,49 @@ export class MenuMain { private i18nService: I18nService, private messagingService: MessagingService) { } init() { + this.initContextMenu(); + this.initApplicationMenu(); + } + + private initContextMenu() { + const selectionMenu = Menu.buildFromTemplate([ + { role: 'copy' }, + { type: 'separator' }, + { role: 'selectall' }, + ]); + + const inputMenu = Menu.buildFromTemplate([ + { role: 'undo' }, + { role: 'redo' }, + { type: 'separator' }, + { role: 'cut', enabled: false }, + { role: 'copy', enabled: false }, + { role: 'paste' }, + { type: 'separator' }, + { role: 'selectall' }, + ]); + + const inputSelectionMenu = Menu.buildFromTemplate([ + { role: 'cut' }, + { role: 'copy' }, + { role: 'paste' }, + { type: 'separator' }, + { role: 'selectall' }, + ]); + + this.windowMain.win.webContents.on('context-menu', (e, props) => { + const selected = props.selectionText && props.selectionText.trim() !== ''; + if (props.isEditable && selected) { + inputSelectionMenu.popup(this.windowMain.win); + } else if (props.isEditable) { + inputMenu.popup(this.windowMain.win); + } else if (selected) { + selectionMenu.popup(this.windowMain.win); + } + }); + } + + private initApplicationMenu() { const template: MenuItemConstructorOptions[] = [ { label: this.i18nService.t('file'),