From d5fd04626ce22d6194924b75f268fefa41aa2db5 Mon Sep 17 00:00:00 2001 From: Vijay Raghavan Aravamudhan Date: Tue, 7 Sep 2021 18:15:46 +0530 Subject: Add a confirmation when quitting Ferdi and a preference to toggle it (implements #1857) (#1879) Works for the 'Ferdi > Quit' menu, the shortcut key to quit (Cmd/Ctrl+Q) and also from the tray icon. --- src/lib/Menu.js | 37 +++++++++++++++++++++++-------------- src/lib/Tray.js | 24 ++++++++++++++++++++---- 2 files changed, 43 insertions(+), 18 deletions(-) (limited to 'src/lib') diff --git a/src/lib/Menu.js b/src/lib/Menu.js index 8a5af56fb..4f0041be6 100644 --- a/src/lib/Menu.js +++ b/src/lib/Menu.js @@ -27,6 +27,7 @@ import { workspaceActions } from '../features/workspaces/actions'; import { workspaceStore } from '../features/workspaces/index'; import apiBase, { termsBase } from '../api/apiBase'; import { openExternalUrl } from '../helpers/url-helpers'; +import globalMessages from '../i18n/globalMessages'; const menuItems = defineMessages({ edit: { @@ -254,10 +255,6 @@ const menuItems = defineMessages({ id: 'menu.app.autohideMenuBar', defaultMessage: '!!!Auto-hide menu bar', }, - quit: { - id: 'menu.app.quit', - defaultMessage: '!!!Quit', - }, addNewService: { id: 'menu.services.addNewService', defaultMessage: '!!!Add New Service...', @@ -599,6 +596,23 @@ export default class FranzMenu { const tpl = _titleBarTemplateFactory(intl, this.stores.settings.app.locked); const { actions } = this; + // TODO: Extract this into a reusable component and remove the duplications + const quitApp = () => { + const yesButtonIndex = 0; + let selection = yesButtonIndex; + if (window.ferdi.stores.settings.app.confirmOnQuit) { + selection = dialog.showMessageBoxSync(app.mainWindow, { + type: 'question', + message: intl.formatMessage(globalMessages.quit), + detail: intl.formatMessage(globalMessages.quitConfirmation), + buttons: [intl.formatMessage(globalMessages.yes), intl.formatMessage(globalMessages.no)], + }); + } + if (selection === yesButtonIndex) { + app.quit(); + } + }; + if (!isMac) { tpl[1].submenu.push({ label: intl.formatMessage(menuItems.autohideMenuBar), @@ -801,11 +815,9 @@ export default class FranzMenu { type: 'separator', }, { - label: intl.formatMessage(menuItems.quit), - role: 'quit', - click() { - app.quit(); - }, + label: intl.formatMessage(globalMessages.quit), + accelerator: `${cmdOrCtrlShortcutKey()}+Q`, + click: quitApp, }, ], }); @@ -862,12 +874,9 @@ export default class FranzMenu { type: 'separator', }, { - label: intl.formatMessage(menuItems.quit), - role: 'quit', + label: intl.formatMessage(globalMessages.quit), accelerator: `${cmdOrCtrlShortcutKey()}+Q`, - click() { - app.quit(); - }, + click: quitApp, }, ]; diff --git a/src/lib/Tray.js b/src/lib/Tray.js index f5970f7e7..c897d597a 100644 --- a/src/lib/Tray.js +++ b/src/lib/Tray.js @@ -1,5 +1,5 @@ import { - app, Menu, nativeImage, nativeTheme, systemPreferences, Tray, ipcMain, + app, Menu, nativeImage, nativeTheme, systemPreferences, Tray, ipcMain, dialog, } from 'electron'; import { join } from 'path'; import macosVersion from 'macos-version'; @@ -43,9 +43,7 @@ export default class TrayIcon { }, { label: 'Quit Ferdi', - click() { - app.quit(); - }, + click: this.quitApp, }, ]; @@ -178,4 +176,22 @@ export default class TrayIcon { __dirname, '..', 'assets', 'images', type, platform, `${asset}.${FILE_EXTENSION}`, )); } + + // TODO: Extract this into a reusable component and remove the duplications + quitApp = () => { + const yesButtonIndex = 0; + let selection = yesButtonIndex; + if (window.ferdi.stores.settings.app.confirmOnQuit) { + selection = dialog.showMessageBoxSync(app.mainWindow, { + // TODO: Externalize strings + type: 'question', + message: 'Quit', + detail: 'Do you really want to quit Ferdi?', + buttons: ['Yes', 'No'], + }); + } + if (selection === yesButtonIndex) { + app.quit(); + } + }; } -- cgit v1.2.3-70-g09d2