From 67283821414583a13613d28456704266e50025a7 Mon Sep 17 00:00:00 2001 From: Jonas Hermsmeier Date: Fri, 25 May 2018 22:44:41 +0200 Subject: [PATCH] feat: Use settings for feature control Change-Type: minor --- lib/gui/app/models/local-settings.js | 18 ++++++-- lib/gui/etcher.js | 65 ++++++++++++++++++++-------- 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/lib/gui/app/models/local-settings.js b/lib/gui/app/models/local-settings.js index 3405b5fc..bd485f47 100644 --- a/lib/gui/app/models/local-settings.js +++ b/lib/gui/app/models/local-settings.js @@ -36,10 +36,14 @@ const JSON_INDENT = 2 * - `$XDG_CONFIG_HOME/etcher` or `~/.config/etcher` on Linux * - `~/Library/Application Support/etcher` on macOS * See https://electronjs.org/docs/api/app#appgetpathname + * NOTE: The ternary is due to this module being loaded both, + * in Electron's main and renderer processes * @constant * @type {String} */ -const USER_DATA_DIR = electron.remote.app.getPath('userData') +const USER_DATA_DIR = electron.app + ? electron.app.getPath('userData') + : electron.remote.app.getPath('userData') /** * @summary Configuration file path @@ -64,15 +68,21 @@ const CONFIG_PATH = path.join(USER_DATA_DIR, 'config.json') */ const readConfigFile = (filename) => { return new Bluebird((resolve, reject) => { - fs.readFile(filename, (error, buffer) => { + fs.readFile(filename, { encoding: 'utf8' }, (error, contents) => { + let data = {} if (error) { if (error.code === 'ENOENT') { - resolve({}) + resolve(data) } else { reject(error) } } else { - resolve(JSON.parse(buffer.toString())) + try { + data = JSON.parse(contents) + } catch(e) { + // ignore (?) + } + resolve(data) } }) }) diff --git a/lib/gui/etcher.js b/lib/gui/etcher.js index 235000be..9a73fe9b 100644 --- a/lib/gui/etcher.js +++ b/lib/gui/etcher.js @@ -20,31 +20,29 @@ const electron = require('electron') const path = require('path') const EXIT_CODES = require('../shared/exit-codes') const buildWindowMenu = require('./menu') +const settings = require('./app/models/settings') + +/* eslint-disable lodash/prefer-lodash-method */ + +const config = settings.getDefaults() let mainWindow = null -electron.app.on('window-all-closed', electron.app.quit) - -// Sending a `SIGINT` (e.g: Ctrl-C) to an Electron app that registers -// a `beforeunload` window event handler results in a disconnected white -// browser window in GNU/Linux and macOS. -// The `before-quit` Electron event is triggered in `SIGINT`, so we can -// make use of it to ensure the browser window is completely destroyed. -// See https://github.com/electron/electron/issues/5273 -electron.app.on('before-quit', () => { - process.exit(EXIT_CODES.SUCCESS) -}) - -electron.app.on('ready', () => { +/** + * @summary Create Etcher's main window + * @example + * electron.app.on('ready', createMainWindow) + */ +const createMainWindow = () => { mainWindow = new electron.BrowserWindow({ width: 800, height: 380, useContentSize: true, show: false, - resizable: Boolean(process.env.ETCHER_FULLSCREEN), + resizable: Boolean(config.fullscreen), maximizable: false, - fullscreen: Boolean(process.env.ETCHER_FULLSCREEN), - fullscreenable: Boolean(process.env.ETCHER_FULLSCREEN), - kiosk: Boolean(process.env.ETCHER_FULLSCREEN), + fullscreen: Boolean(config.fullscreen), + fullscreenable: Boolean(config.fullscreen), + kiosk: Boolean(config.fullscreen), autoHideMenuBar: true, titleBarStyle: 'hidden-inset', icon: path.join(__dirname, '..', '..', 'assets', 'icon.png'), @@ -57,7 +55,10 @@ electron.app.on('ready', () => { buildWindowMenu(mainWindow) // Prevent flash of white when starting the application - mainWindow.on('ready-to-show', mainWindow.show) + mainWindow.on('ready-to-show', () => { + console.timeEnd('ready-to-show') + mainWindow.show() + }) mainWindow.on('closed', () => { mainWindow = null @@ -81,4 +82,32 @@ electron.app.on('ready', () => { }) mainWindow.loadURL(`file://${path.join(__dirname, 'app', 'index.html')}`) +} + +electron.app.on('window-all-closed', electron.app.quit) + +// Sending a `SIGINT` (e.g: Ctrl-C) to an Electron app that registers +// a `beforeunload` window event handler results in a disconnected white +// browser window in GNU/Linux and macOS. +// The `before-quit` Electron event is triggered in `SIGINT`, so we can +// make use of it to ensure the browser window is completely destroyed. +// See https://github.com/electron/electron/issues/5273 +electron.app.on('before-quit', () => { + process.exit(EXIT_CODES.SUCCESS) }) + +settings.load().then((localSettings) => { + Object.assign(config, localSettings) +}).catch((error) => { + // TODO: What do if loading the config fails? + console.error('Error loading settings:') + console.error(error) +}).finally(() => { + if (electron.app.isReady()) { + createMainWindow() + } else { + electron.app.on('ready', createMainWindow) + } +}) + +console.time('ready-to-show')