feat: Use settings for feature control

Change-Type: minor
This commit is contained in:
Jonas Hermsmeier 2018-05-25 22:44:41 +02:00
parent 7c3f104d1b
commit 6728382141
No known key found for this signature in database
GPG Key ID: 1B870F801A0CEE9F
2 changed files with 61 additions and 22 deletions

View File

@ -36,10 +36,14 @@ const JSON_INDENT = 2
* - `$XDG_CONFIG_HOME/etcher` or `~/.config/etcher` on Linux * - `$XDG_CONFIG_HOME/etcher` or `~/.config/etcher` on Linux
* - `~/Library/Application Support/etcher` on macOS * - `~/Library/Application Support/etcher` on macOS
* See https://electronjs.org/docs/api/app#appgetpathname * 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 * @constant
* @type {String} * @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 * @summary Configuration file path
@ -64,15 +68,21 @@ const CONFIG_PATH = path.join(USER_DATA_DIR, 'config.json')
*/ */
const readConfigFile = (filename) => { const readConfigFile = (filename) => {
return new Bluebird((resolve, reject) => { return new Bluebird((resolve, reject) => {
fs.readFile(filename, (error, buffer) => { fs.readFile(filename, { encoding: 'utf8' }, (error, contents) => {
let data = {}
if (error) { if (error) {
if (error.code === 'ENOENT') { if (error.code === 'ENOENT') {
resolve({}) resolve(data)
} else { } else {
reject(error) reject(error)
} }
} else { } else {
resolve(JSON.parse(buffer.toString())) try {
data = JSON.parse(contents)
} catch(e) {
// ignore (?)
}
resolve(data)
} }
}) })
}) })

View File

@ -20,31 +20,29 @@ const electron = require('electron')
const path = require('path') const path = require('path')
const EXIT_CODES = require('../shared/exit-codes') const EXIT_CODES = require('../shared/exit-codes')
const buildWindowMenu = require('./menu') const buildWindowMenu = require('./menu')
const settings = require('./app/models/settings')
/* eslint-disable lodash/prefer-lodash-method */
const config = settings.getDefaults()
let mainWindow = null let mainWindow = null
electron.app.on('window-all-closed', electron.app.quit) /**
* @summary Create Etcher's main window
// Sending a `SIGINT` (e.g: Ctrl-C) to an Electron app that registers * @example
// a `beforeunload` window event handler results in a disconnected white * electron.app.on('ready', createMainWindow)
// browser window in GNU/Linux and macOS. */
// The `before-quit` Electron event is triggered in `SIGINT`, so we can const createMainWindow = () => {
// 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', () => {
mainWindow = new electron.BrowserWindow({ mainWindow = new electron.BrowserWindow({
width: 800, width: 800,
height: 380, height: 380,
useContentSize: true, useContentSize: true,
show: false, show: false,
resizable: Boolean(process.env.ETCHER_FULLSCREEN), resizable: Boolean(config.fullscreen),
maximizable: false, maximizable: false,
fullscreen: Boolean(process.env.ETCHER_FULLSCREEN), fullscreen: Boolean(config.fullscreen),
fullscreenable: Boolean(process.env.ETCHER_FULLSCREEN), fullscreenable: Boolean(config.fullscreen),
kiosk: Boolean(process.env.ETCHER_FULLSCREEN), kiosk: Boolean(config.fullscreen),
autoHideMenuBar: true, autoHideMenuBar: true,
titleBarStyle: 'hidden-inset', titleBarStyle: 'hidden-inset',
icon: path.join(__dirname, '..', '..', 'assets', 'icon.png'), icon: path.join(__dirname, '..', '..', 'assets', 'icon.png'),
@ -57,7 +55,10 @@ electron.app.on('ready', () => {
buildWindowMenu(mainWindow) buildWindowMenu(mainWindow)
// Prevent flash of white when starting the application // 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.on('closed', () => {
mainWindow = null mainWindow = null
@ -81,4 +82,32 @@ electron.app.on('ready', () => {
}) })
mainWindow.loadURL(`file://${path.join(__dirname, 'app', 'index.html')}`) 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')