From 999e855254f463c05213b184c923705995adcf53 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Wed, 26 Jul 2023 10:05:20 +0200 Subject: [PATCH] add context menu to inputs closes #1217 --- public/contextMenu.js | 30 ++++++++++++++++++++++++++++++ public/electron.js | 3 +++ 2 files changed, 33 insertions(+) create mode 100644 public/contextMenu.js diff --git a/public/contextMenu.js b/public/contextMenu.js new file mode 100644 index 00000000..fd1c5a0e --- /dev/null +++ b/public/contextMenu.js @@ -0,0 +1,30 @@ +const { Menu } = require('electron'); + +// https://github.com/electron/electron/issues/4068#issuecomment-274159726 +module.exports = (window) => { + const selectionMenu = Menu.buildFromTemplate([ + { role: 'copy' }, + { type: 'separator' }, + { role: 'selectall' }, + ]); + + const inputMenu = Menu.buildFromTemplate([ + { role: 'undo' }, + { role: 'redo' }, + { type: 'separator' }, + { role: 'cut' }, + { role: 'copy' }, + { role: 'paste' }, + { type: 'separator' }, + { role: 'selectall' }, + ]); + + window.webContents.on('context-menu', (e, props) => { + const { selectionText, isEditable } = props; + if (isEditable) { + inputMenu.popup(window); + } else if (selectionText && selectionText.trim() !== '') { + selectionMenu.popup(window); + } + }); +}; diff --git a/public/electron.js b/public/electron.js index 6e420649..30659fa2 100644 --- a/public/electron.js +++ b/public/electron.js @@ -16,6 +16,7 @@ const logger = require('./logger'); const menu = require('./menu'); const configStore = require('./configStore'); const { frontendBuildDir } = require('./util'); +const attachContextMenu = require('./contextMenu'); const { checkNewVersion } = require('./update-checker'); @@ -108,6 +109,8 @@ function createWindow() { remote.enable(mainWindow.webContents); + attachContextMenu(mainWindow); + if (isDev) mainWindow.loadURL('http://localhost:3001'); // Need to useloadFile for special characters https://github.com/mifi/lossless-cut/issues/40