diff options
author | Vijay Aravamudhan <vraravam@users.noreply.github.com> | 2021-09-13 11:26:19 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-13 11:26:19 +0530 |
commit | b1e08639bebae31873e1916a785d79d29cf5dbd6 (patch) | |
tree | a66066e463be2f8f1fcd026d22fdd99760cd0885 /src/index.js | |
parent | 5.6.2-nightly.2 [skip ci] (diff) | |
download | ferdium-app-b1e08639bebae31873e1916a785d79d29cf5dbd6.tar.gz ferdium-app-b1e08639bebae31873e1916a785d79d29cf5dbd6.tar.zst ferdium-app-b1e08639bebae31873e1916a785d79d29cf5dbd6.zip |
Better implementation of confirmation while quitting Ferdi (#1919)
Remove duplication and reuse method to return typesafe boolean from settings.
Better fix for #1879
Diffstat (limited to 'src/index.js')
-rw-r--r-- | src/index.js | 53 |
1 files changed, 34 insertions, 19 deletions
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 @@ | |||
1 | /* eslint-disable import/first */ | 1 | /* eslint-disable import/first */ |
2 | 2 | ||
3 | import { app, BrowserWindow, ipcMain, session } from 'electron'; | 3 | import { app, BrowserWindow, ipcMain, session, dialog } from 'electron'; |
4 | 4 | ||
5 | import { emptyDirSync, ensureFileSync } from 'fs-extra'; | 5 | import { emptyDirSync, ensureFileSync } from 'fs-extra'; |
6 | import { join } from 'path'; | 6 | import { join } from 'path'; |
@@ -22,6 +22,7 @@ import { | |||
22 | userDataRecipesPath, | 22 | userDataRecipesPath, |
23 | userDataPath, | 23 | userDataPath, |
24 | } from './environment'; | 24 | } from './environment'; |
25 | import { ifUndefinedBoolean } from './jsUtils'; | ||
25 | 26 | ||
26 | import { mainIpcHandler as basicAuthHandler } from './features/basicAuth'; | 27 | import { mainIpcHandler as basicAuthHandler } from './features/basicAuth'; |
27 | import ipcApi from './electron/ipc-api'; | 28 | import ipcApi from './electron/ipc-api'; |
@@ -77,13 +78,14 @@ if (isWindows) { | |||
77 | const settings = new Settings('app', DEFAULT_APP_SETTINGS); | 78 | const settings = new Settings('app', DEFAULT_APP_SETTINGS); |
78 | const proxySettings = new Settings('proxy'); | 79 | const proxySettings = new Settings('proxy'); |
79 | 80 | ||
80 | if (settings.get('sentry')) { | 81 | const retrieveSettingValue = (key, defaultValue = true) => ifUndefinedBoolean(settings.get(key), defaultValue); |
82 | |||
83 | if (retrieveSettingValue('sentry')) { | ||
81 | // eslint-disable-next-line global-require | 84 | // eslint-disable-next-line global-require |
82 | require('./sentry'); | 85 | require('./sentry'); |
83 | } | 86 | } |
84 | 87 | ||
85 | // add `liftSingleInstanceLock` to settings.json to override the single instance lock | 88 | const liftSingleInstanceLock = retrieveSettingValue('liftSingleInstanceLock', false); |
86 | const liftSingleInstanceLock = settings.get('liftSingleInstanceLock') || false; | ||
87 | 89 | ||
88 | // Force single window | 90 | // Force single window |
89 | const gotTheLock = liftSingleInstanceLock | 91 | const gotTheLock = liftSingleInstanceLock |
@@ -147,7 +149,7 @@ if ( | |||
147 | } | 149 | } |
148 | 150 | ||
149 | // Disable GPU acceleration | 151 | // Disable GPU acceleration |
150 | if (!settings.get('enableGPUAcceleration')) { | 152 | if (!retrieveSettingValue('enableGPUAcceleration', false)) { |
151 | debug('Disable GPU Acceleration'); | 153 | debug('Disable GPU Acceleration'); |
152 | app.disableHardwareAcceleration(); | 154 | app.disableHardwareAcceleration(); |
153 | } | 155 | } |
@@ -176,7 +178,7 @@ const createWindow = () => { | |||
176 | } | 178 | } |
177 | 179 | ||
178 | // Create the browser window. | 180 | // Create the browser window. |
179 | const backgroundColor = settings.get('darkMode') | 181 | const backgroundColor = retrieveSettingValue('darkMode', false) |
180 | ? '#1E1E1E' | 182 | ? '#1E1E1E' |
181 | : settings.get('accentColor'); | 183 | : settings.get('accentColor'); |
182 | 184 | ||
@@ -190,7 +192,7 @@ const createWindow = () => { | |||
190 | show: false, | 192 | show: false, |
191 | titleBarStyle: isMac ? 'hidden' : '', | 193 | titleBarStyle: isMac ? 'hidden' : '', |
192 | frame: isLinux, | 194 | frame: isLinux, |
193 | spellcheck: settings.get('enableSpellchecking'), | 195 | spellcheck: retrieveSettingValue('enableSpellchecking'), |
194 | backgroundColor, | 196 | backgroundColor, |
195 | webPreferences: { | 197 | webPreferences: { |
196 | nodeIntegration: true, | 198 | nodeIntegration: true, |
@@ -268,15 +270,14 @@ const createWindow = () => { | |||
268 | // when you should delete the corresponding element. | 270 | // when you should delete the corresponding element. |
269 | if ( | 271 | if ( |
270 | !willQuitApp && | 272 | !willQuitApp && |
271 | (settings.get('runInBackground') === undefined || | 273 | retrieveSettingValue('runInBackground') |
272 | settings.get('runInBackground')) | ||
273 | ) { | 274 | ) { |
274 | e.preventDefault(); | 275 | e.preventDefault(); |
275 | if (isWindows) { | 276 | if (isWindows) { |
276 | debug('Window: minimize'); | 277 | debug('Window: minimize'); |
277 | mainWindow.minimize(); | 278 | mainWindow.minimize(); |
278 | 279 | ||
279 | if (settings.get('closeToSystemTray')) { | 280 | if (retrieveSettingValue('closeToSystemTray')) { |
280 | debug('Skip taskbar: true'); | 281 | debug('Skip taskbar: true'); |
281 | mainWindow.setSkipTaskbar(true); | 282 | mainWindow.setSkipTaskbar(true); |
282 | } | 283 | } |
@@ -300,7 +301,7 @@ const createWindow = () => { | |||
300 | mainWindow.on('minimize', () => { | 301 | mainWindow.on('minimize', () => { |
301 | app.wasMaximized = app.isMaximized; | 302 | app.wasMaximized = app.isMaximized; |
302 | 303 | ||
303 | if (settings.get('minimizeToSystemTray')) { | 304 | if (retrieveSettingValue('minimizeToSystemTray')) { |
304 | debug('Skip taskbar: true'); | 305 | debug('Skip taskbar: true'); |
305 | mainWindow.setSkipTaskbar(true); | 306 | mainWindow.setSkipTaskbar(true); |
306 | trayIcon.show(); | 307 | trayIcon.show(); |
@@ -326,7 +327,7 @@ const createWindow = () => { | |||
326 | mainWindow.maximize(); | 327 | mainWindow.maximize(); |
327 | } | 328 | } |
328 | 329 | ||
329 | if (!settings.get('enableSystemTray')) { | 330 | if (!retrieveSettingValue('enableSystemTray')) { |
330 | debug('Tray: hiding tray icon'); | 331 | debug('Tray: hiding tray icon'); |
331 | trayIcon.hide(); | 332 | trayIcon.hide(); |
332 | } | 333 | } |
@@ -351,7 +352,7 @@ const createWindow = () => { | |||
351 | openExternalUrl(url); | 352 | openExternalUrl(url); |
352 | }); | 353 | }); |
353 | 354 | ||
354 | if (settings.get('startMinimized')) { | 355 | if (retrieveSettingValue('startMinimized', false)) { |
355 | mainWindow.hide(); | 356 | mainWindow.hide(); |
356 | } else { | 357 | } else { |
357 | mainWindow.show(); | 358 | mainWindow.show(); |
@@ -551,10 +552,7 @@ ipcMain.on('stop-find-in-page', (e, action) => { | |||
551 | app.on('window-all-closed', () => { | 552 | app.on('window-all-closed', () => { |
552 | // On OS X it is common for applications and their menu bar | 553 | // On OS X it is common for applications and their menu bar |
553 | // to stay active until the user quits explicitly with Cmd + Q | 554 | // to stay active until the user quits explicitly with Cmd + Q |
554 | if ( | 555 | if (retrieveSettingValue('runInBackground')) { |
555 | settings.get('runInBackground') === undefined || | ||
556 | settings.get('runInBackground') | ||
557 | ) { | ||
558 | debug('Window: all windows closed, quit app'); | 556 | debug('Window: all windows closed, quit app'); |
559 | app.quit(); | 557 | app.quit(); |
560 | } else { | 558 | } else { |
@@ -562,8 +560,25 @@ app.on('window-all-closed', () => { | |||
562 | } | 560 | } |
563 | }); | 561 | }); |
564 | 562 | ||
565 | app.on('before-quit', () => { | 563 | app.on('before-quit', (event) => { |
566 | willQuitApp = true; | 564 | const yesButtonIndex = 0; |
565 | let selection = yesButtonIndex; | ||
566 | if (retrieveSettingValue('confirmOnQuit')) { | ||
567 | selection = dialog.showMessageBoxSync(app.mainWindow, { | ||
568 | type: 'question', | ||
569 | message: 'Quit', | ||
570 | detail: 'Do you really want to quit Ferdi?', | ||
571 | buttons: [ | ||
572 | 'Yes', | ||
573 | 'No', | ||
574 | ], | ||
575 | }); | ||
576 | } | ||
577 | if (selection === yesButtonIndex) { | ||
578 | willQuitApp = true; | ||
579 | } else { | ||
580 | event.preventDefault(); | ||
581 | } | ||
567 | }); | 582 | }); |
568 | 583 | ||
569 | app.on('activate', () => { | 584 | app.on('activate', () => { |