Avoid exit listener to hijack other application shortcuts

This commit is contained in:
Germain Souquet 2021-03-31 08:58:24 +01:00
parent d2bc3a8a03
commit 84990559ec

View file

@ -46,6 +46,7 @@ const fs = require('fs');
const afs = fs.promises; const afs = fs.promises;
const crypto = require('crypto'); const crypto = require('crypto');
const { platform } = require('os');
let keytar; let keytar;
try { try {
keytar = require('keytar'); keytar = require('keytar');
@ -255,8 +256,17 @@ let eventIndex = null;
let mainWindow = null; let mainWindow = null;
global.appQuitting = false; global.appQuitting = false;
const warnBeforeExit = (event) => { const exitShortcuts = [
if (store.get('warnBeforeExit', true)) { (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, { const shouldCancelCloseRequest = dialog.showMessageBoxSync(mainWindow, {
type: "question", type: "question",
buttons: ["Cancel", "Close Element"], buttons: ["Cancel", "Close Element"],
@ -267,7 +277,6 @@ const warnBeforeExit = (event) => {
if (shouldCancelCloseRequest) { if (shouldCancelCloseRequest) {
event.preventDefault(); event.preventDefault();
return false;
} }
} }
}; };
@ -941,11 +950,7 @@ app.on('ready', async () => {
} }
}); });
globalShortcut.register("CommandOrControl+Q", warnBeforeExit); mainWindow.webContents.on('before-input-event', warnBeforeExit);
if (process.platform !== 'darwin') {
globalShortcut.register("Alt+F4", warnBeforeExit);
globalShortcut.register("AltGr+F4 ", warnBeforeExit);
}
mainWindow.on('closed', () => { mainWindow.on('closed', () => {
mainWindow = global.mainWindow = null; mainWindow = global.mainWindow = null;