From c9c0ceb757748ebed7db2b18a5ab4efaf68edb77 Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Thu, 25 Mar 2021 12:46:10 +0000 Subject: [PATCH 1/5] Add prompt to warn before quitting the application --- src/electron-main.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/electron-main.js b/src/electron-main.js index 8ef8d6e..a6d778a 100644 --- a/src/electron-main.js +++ b/src/electron-main.js @@ -27,7 +27,7 @@ const argv = require('minimist')(process.argv, { alias: {help: "h"}, }); -const {app, ipcMain, powerSaveBlocker, BrowserWindow, Menu, autoUpdater, protocol} = require('electron'); +const {app, ipcMain, powerSaveBlocker, BrowserWindow, Menu, autoUpdater, protocol, dialog} = require('electron'); const AutoLaunch = require('auto-launch'); const path = require('path'); @@ -920,7 +920,22 @@ app.on('ready', async () => { mainWindow.on('closed', () => { mainWindow = global.mainWindow = null; }); - mainWindow.on('close', (e) => { + mainWindow.on('close', async (e) => { + const shouldCancelCloseRequest = dialog.showMessageBoxSync(mainWindow, { + type: "question", + buttons: ["Cancel", "Close Element"], + message: "Are you sure you want to quit?", + defaultId: 1, + cancelId: 0, + checkboxLabel: "Do not show this again", + checkboxChecked: false, + }) === 0; + + if (shouldCancelCloseRequest) { + e.preventDefault(); + return false; + } + // If we are not quitting and have a tray icon then minimize to tray if (!global.appQuitting && (tray.hasTray() || process.platform === 'darwin')) { // On Mac, closing the window just hides it From 68587e84ed789dbf4b434a4157921e58a5ed46d8 Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Thu, 25 Mar 2021 14:15:04 +0000 Subject: [PATCH 2/5] Add user settings for warn before exit --- src/electron-main.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/electron-main.js b/src/electron-main.js index a6d778a..6c41120 100644 --- a/src/electron-main.js +++ b/src/electron-main.js @@ -340,6 +340,12 @@ ipcMain.on('ipcCall', async function(ev, payload) { launcher.disable(); } break; + case 'shouldWarnBeforeExit': + ret = store.get('warnBeforeExit', true); + break; + case 'setWarnBeforeExit': + store.set('warnBeforeExit', args[0]); + break; case 'getMinimizeToTrayEnabled': ret = tray.hasTray(); break; @@ -921,19 +927,21 @@ app.on('ready', async () => { mainWindow = global.mainWindow = null; }); mainWindow.on('close', async (e) => { - const shouldCancelCloseRequest = dialog.showMessageBoxSync(mainWindow, { - type: "question", - buttons: ["Cancel", "Close Element"], - message: "Are you sure you want to quit?", - defaultId: 1, - cancelId: 0, - checkboxLabel: "Do not show this again", - checkboxChecked: false, - }) === 0; + if (store.get('warnBeforeExit', true)) { + const shouldCancelCloseRequest = dialog.showMessageBoxSync(mainWindow, { + type: "question", + buttons: ["Cancel", "Close Element"], + message: "Are you sure you want to quit?", + defaultId: 1, + cancelId: 0, + checkboxLabel: "Do not show this again", + checkboxChecked: false, + }) === 0; - if (shouldCancelCloseRequest) { - e.preventDefault(); - return false; + if (shouldCancelCloseRequest) { + e.preventDefault(); + return false; + } } // If we are not quitting and have a tray icon then minimize to tray From 9cdea580d33b20f807604d883fd323101f28818e Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Thu, 25 Mar 2021 14:34:02 +0000 Subject: [PATCH 3/5] Remove checkbox options for exit dialog --- src/electron-main.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/electron-main.js b/src/electron-main.js index 6c41120..90ef3e0 100644 --- a/src/electron-main.js +++ b/src/electron-main.js @@ -934,8 +934,6 @@ app.on('ready', async () => { message: "Are you sure you want to quit?", defaultId: 1, cancelId: 0, - checkboxLabel: "Do not show this again", - checkboxChecked: false, }) === 0; if (shouldCancelCloseRequest) { From 3d18ff16d18c5a18441e230a0f9ef56875a79059 Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Thu, 25 Mar 2021 14:50:33 +0000 Subject: [PATCH 4/5] Attempt to minimise the window first rather than exiting --- src/electron-main.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/electron-main.js b/src/electron-main.js index 90ef3e0..bf5ecf5 100644 --- a/src/electron-main.js +++ b/src/electron-main.js @@ -927,6 +927,16 @@ app.on('ready', async () => { mainWindow = global.mainWindow = null; }); mainWindow.on('close', async (e) => { + // If we are not quitting and have a tray icon then minimize to tray + if (!global.appQuitting && (tray.hasTray() || process.platform === 'darwin')) { + // On Mac, closing the window just hides it + // (this is generally how single-window Mac apps + // behave, eg. Mail.app) + e.preventDefault(); + mainWindow.hide(); + return false; + } + if (store.get('warnBeforeExit', true)) { const shouldCancelCloseRequest = dialog.showMessageBoxSync(mainWindow, { type: "question", @@ -941,16 +951,6 @@ app.on('ready', async () => { return false; } } - - // If we are not quitting and have a tray icon then minimize to tray - if (!global.appQuitting && (tray.hasTray() || process.platform === 'darwin')) { - // On Mac, closing the window just hides it - // (this is generally how single-window Mac apps - // behave, eg. Mail.app) - e.preventDefault(); - mainWindow.hide(); - return false; - } }); if (process.platform === 'win32') { From d9865555173cc48fe2665e5ed0b49b27683a2710 Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Mon, 29 Mar 2021 12:10:27 +0100 Subject: [PATCH 5/5] Only prompt exit warning for keyboard shortcuts --- src/electron-main.js | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/electron-main.js b/src/electron-main.js index bf5ecf5..dfbc6dd 100644 --- a/src/electron-main.js +++ b/src/electron-main.js @@ -27,7 +27,9 @@ const argv = require('minimist')(process.argv, { alias: {help: "h"}, }); -const {app, ipcMain, powerSaveBlocker, BrowserWindow, Menu, autoUpdater, protocol, dialog} = require('electron'); +const { + app, ipcMain, powerSaveBlocker, BrowserWindow, Menu, autoUpdater, protocol, dialog, globalShortcut, +} = require('electron'); const AutoLaunch = require('auto-launch'); const path = require('path'); @@ -253,6 +255,22 @@ let eventIndex = null; let mainWindow = null; global.appQuitting = false; +const warnBeforeExit = (event) => { + if (store.get('warnBeforeExit', true)) { + const shouldCancelCloseRequest = dialog.showMessageBoxSync(mainWindow, { + type: "question", + buttons: ["Cancel", "Close Element"], + message: "Are you sure you want to quit?", + defaultId: 1, + cancelId: 0, + }) === 0; + + if (shouldCancelCloseRequest) { + event.preventDefault(); + return false; + } + } +}; const deleteContents = async (p) => { for (const entry of await afs.readdir(p)) { @@ -923,6 +941,12 @@ app.on('ready', async () => { } }); + globalShortcut.register("CommandOrControl+Q", warnBeforeExit); + if (process.platform !== 'darwin') { + globalShortcut.register("Alt+F4", warnBeforeExit); + globalShortcut.register("AltGr+F4 ", warnBeforeExit); + } + mainWindow.on('closed', () => { mainWindow = global.mainWindow = null; }); @@ -936,21 +960,6 @@ app.on('ready', async () => { mainWindow.hide(); return false; } - - if (store.get('warnBeforeExit', true)) { - const shouldCancelCloseRequest = dialog.showMessageBoxSync(mainWindow, { - type: "question", - buttons: ["Cancel", "Close Element"], - message: "Are you sure you want to quit?", - defaultId: 1, - cancelId: 0, - }) === 0; - - if (shouldCancelCloseRequest) { - e.preventDefault(); - return false; - } - } }); if (process.platform === 'win32') {