From 84990559ecbbeb25c1867654b0f6366688d2dcad Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Wed, 31 Mar 2021 08:58:24 +0100 Subject: [PATCH] Avoid exit listener to hijack other application shortcuts --- src/electron-main.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/electron-main.js b/src/electron-main.js index dfbc6dd..87161b5 100644 --- a/src/electron-main.js +++ b/src/electron-main.js @@ -46,6 +46,7 @@ const fs = require('fs'); const afs = fs.promises; const crypto = require('crypto'); +const { platform } = require('os'); let keytar; try { keytar = require('keytar'); @@ -255,8 +256,17 @@ let eventIndex = null; let mainWindow = null; global.appQuitting = false; -const warnBeforeExit = (event) => { - if (store.get('warnBeforeExit', true)) { +const exitShortcuts = [ + (input, platform) => platform !== 'darwin' && input.alt && input.code === 'F4', + (input, platform) => platform !== 'darwin' && input.control && input.code === 'KeyQ', + (input, platform) => platform === 'darwin' && input.meta && input.code === 'KeyQ', +]; + +const warnBeforeExit = (event, input) => { + const shouldWarnBeforeExit = store.get('warnBeforeExit', true); + const shortcutPressed = exitShortcuts.some(shortcutFn => shortcutFn(input, process.platform)); + + if (shouldWarnBeforeExit && shortcutPressed) { const shouldCancelCloseRequest = dialog.showMessageBoxSync(mainWindow, { type: "question", buttons: ["Cancel", "Close Element"], @@ -267,7 +277,6 @@ const warnBeforeExit = (event) => { if (shouldCancelCloseRequest) { event.preventDefault(); - return false; } } }; @@ -941,11 +950,7 @@ app.on('ready', async () => { } }); - globalShortcut.register("CommandOrControl+Q", warnBeforeExit); - if (process.platform !== 'darwin') { - globalShortcut.register("Alt+F4", warnBeforeExit); - globalShortcut.register("AltGr+F4 ", warnBeforeExit); - } + mainWindow.webContents.on('before-input-event', warnBeforeExit); mainWindow.on('closed', () => { mainWindow = global.mainWindow = null;