Merge pull request #177 from vector-im/gsouquet-exit-shortcut

This commit is contained in:
Germain 2021-04-01 15:25:57 +01:00 committed by GitHub
commit d4bf14b47f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,7 +28,7 @@ const argv = require('minimist')(process.argv, {
});
const {
app, ipcMain, powerSaveBlocker, BrowserWindow, Menu, autoUpdater, protocol, dialog, globalShortcut,
app, ipcMain, powerSaveBlocker, BrowserWindow, Menu, autoUpdater, protocol, dialog,
} = require('electron');
const AutoLaunch = require('auto-launch');
const path = require('path');
@ -255,8 +255,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 exitShortcutPressed = exitShortcuts.some(shortcutFn => shortcutFn(input, process.platform));
if (shouldWarnBeforeExit && exitShortcutPressed) {
const shouldCancelCloseRequest = dialog.showMessageBoxSync(mainWindow, {
type: "question",
buttons: ["Cancel", "Close Element"],
@ -267,7 +276,6 @@ const warnBeforeExit = (event) => {
if (shouldCancelCloseRequest) {
event.preventDefault();
return false;
}
}
};
@ -941,11 +949,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;