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 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;