From b1e08639bebae31873e1916a785d79d29cf5dbd6 Mon Sep 17 00:00:00 2001 From: Vijay Aravamudhan Date: Mon, 13 Sep 2021 11:26:19 +0530 Subject: Better implementation of confirmation while quitting Ferdi (#1919) Remove duplication and reuse method to return typesafe boolean from settings. Better fix for #1879 --- src/environment.js | 1 + src/index.js | 53 ++++++++++++++++++++++++++++++++++------------------- src/lib/Menu.js | 28 ++++++---------------------- src/lib/Tray.js | 1 + 4 files changed, 42 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/environment.js b/src/environment.js index caeada94b..b30e3778d 100644 --- a/src/environment.js +++ b/src/environment.js @@ -171,6 +171,7 @@ export const DEFAULT_APP_SETTINGS = { searchEngine: SEARCH_ENGINE_DDG, useVerticalStyle: false, alwaysShowWorkspaces: false, + liftSingleInstanceLock: false, }; export function aboutAppDetails() { diff --git a/src/index.js b/src/index.js index ae75865c6..7f1f77b4e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ /* eslint-disable import/first */ -import { app, BrowserWindow, ipcMain, session } from 'electron'; +import { app, BrowserWindow, ipcMain, session, dialog } from 'electron'; import { emptyDirSync, ensureFileSync } from 'fs-extra'; import { join } from 'path'; @@ -22,6 +22,7 @@ import { userDataRecipesPath, userDataPath, } from './environment'; +import { ifUndefinedBoolean } from './jsUtils'; import { mainIpcHandler as basicAuthHandler } from './features/basicAuth'; import ipcApi from './electron/ipc-api'; @@ -77,13 +78,14 @@ if (isWindows) { const settings = new Settings('app', DEFAULT_APP_SETTINGS); const proxySettings = new Settings('proxy'); -if (settings.get('sentry')) { +const retrieveSettingValue = (key, defaultValue = true) => ifUndefinedBoolean(settings.get(key), defaultValue); + +if (retrieveSettingValue('sentry')) { // eslint-disable-next-line global-require require('./sentry'); } -// add `liftSingleInstanceLock` to settings.json to override the single instance lock -const liftSingleInstanceLock = settings.get('liftSingleInstanceLock') || false; +const liftSingleInstanceLock = retrieveSettingValue('liftSingleInstanceLock', false); // Force single window const gotTheLock = liftSingleInstanceLock @@ -147,7 +149,7 @@ if ( } // Disable GPU acceleration -if (!settings.get('enableGPUAcceleration')) { +if (!retrieveSettingValue('enableGPUAcceleration', false)) { debug('Disable GPU Acceleration'); app.disableHardwareAcceleration(); } @@ -176,7 +178,7 @@ const createWindow = () => { } // Create the browser window. - const backgroundColor = settings.get('darkMode') + const backgroundColor = retrieveSettingValue('darkMode', false) ? '#1E1E1E' : settings.get('accentColor'); @@ -190,7 +192,7 @@ const createWindow = () => { show: false, titleBarStyle: isMac ? 'hidden' : '', frame: isLinux, - spellcheck: settings.get('enableSpellchecking'), + spellcheck: retrieveSettingValue('enableSpellchecking'), backgroundColor, webPreferences: { nodeIntegration: true, @@ -268,15 +270,14 @@ const createWindow = () => { // when you should delete the corresponding element. if ( !willQuitApp && - (settings.get('runInBackground') === undefined || - settings.get('runInBackground')) + retrieveSettingValue('runInBackground') ) { e.preventDefault(); if (isWindows) { debug('Window: minimize'); mainWindow.minimize(); - if (settings.get('closeToSystemTray')) { + if (retrieveSettingValue('closeToSystemTray')) { debug('Skip taskbar: true'); mainWindow.setSkipTaskbar(true); } @@ -300,7 +301,7 @@ const createWindow = () => { mainWindow.on('minimize', () => { app.wasMaximized = app.isMaximized; - if (settings.get('minimizeToSystemTray')) { + if (retrieveSettingValue('minimizeToSystemTray')) { debug('Skip taskbar: true'); mainWindow.setSkipTaskbar(true); trayIcon.show(); @@ -326,7 +327,7 @@ const createWindow = () => { mainWindow.maximize(); } - if (!settings.get('enableSystemTray')) { + if (!retrieveSettingValue('enableSystemTray')) { debug('Tray: hiding tray icon'); trayIcon.hide(); } @@ -351,7 +352,7 @@ const createWindow = () => { openExternalUrl(url); }); - if (settings.get('startMinimized')) { + if (retrieveSettingValue('startMinimized', false)) { mainWindow.hide(); } else { mainWindow.show(); @@ -551,10 +552,7 @@ ipcMain.on('stop-find-in-page', (e, action) => { app.on('window-all-closed', () => { // On OS X it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q - if ( - settings.get('runInBackground') === undefined || - settings.get('runInBackground') - ) { + if (retrieveSettingValue('runInBackground')) { debug('Window: all windows closed, quit app'); app.quit(); } else { @@ -562,8 +560,25 @@ app.on('window-all-closed', () => { } }); -app.on('before-quit', () => { - willQuitApp = true; +app.on('before-quit', (event) => { + const yesButtonIndex = 0; + let selection = yesButtonIndex; + if (retrieveSettingValue('confirmOnQuit')) { + selection = dialog.showMessageBoxSync(app.mainWindow, { + type: 'question', + message: 'Quit', + detail: 'Do you really want to quit Ferdi?', + buttons: [ + 'Yes', + 'No', + ], + }); + } + if (selection === yesButtonIndex) { + willQuitApp = true; + } else { + event.preventDefault(); + } }); app.on('activate', () => { diff --git a/src/lib/Menu.js b/src/lib/Menu.js index 324838441..5aa575df7 100644 --- a/src/lib/Menu.js +++ b/src/lib/Menu.js @@ -592,26 +592,6 @@ 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), @@ -816,7 +796,9 @@ export default class FranzMenu { { label: intl.formatMessage(globalMessages.quit), accelerator: `${cmdOrCtrlShortcutKey()}+Q`, - click: quitApp, + click() { + app.quit(); + }, }, ], }); @@ -874,7 +856,9 @@ export default class FranzMenu { { label: intl.formatMessage(globalMessages.quit), accelerator: `${cmdOrCtrlShortcutKey()}+Q`, - click: quitApp, + click() { + app.quit(); + }, }, ]; diff --git a/src/lib/Tray.js b/src/lib/Tray.js index f5970f7e7..7360611cd 100644 --- a/src/lib/Tray.js +++ b/src/lib/Tray.js @@ -10,6 +10,7 @@ const INDICATOR_TRAY_PLAIN = 'tray'; const INDICATOR_TRAY_UNREAD = 'tray-unread'; const INDICATOR_TRAY_INDIRECT = 'tray-indirect'; +// TODO: Need to support i18n for a lot of the hard-coded strings in this file export default class TrayIcon { trayIcon = null; -- cgit v1.2.3-54-g00ecf