From b3b79eaa902020367654271b8faf3005f9f13880 Mon Sep 17 00:00:00 2001 From: meriadec Date: Thu, 8 Feb 2018 18:05:21 +0100 Subject: [PATCH] Save and restore window dimensions --- src/helpers/db.js | 16 ++++++++++++++++ src/main/app.js | 16 ++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/helpers/db.js b/src/helpers/db.js index dfe8da71..4bb05ade 100644 --- a/src/helpers/db.js +++ b/src/helpers/db.js @@ -1,4 +1,6 @@ import Store from 'electron-store' +import set from 'lodash/set' +import get from 'lodash/get' const encryptionKey = {} @@ -35,4 +37,18 @@ export default { db.set('data', val) return db.get('data') }, + + getIn: (key, path, defaultValue) => { + const db = store(key) + const data = db.get('data') + return get(data, path, defaultValue) + }, + + setIn: (key, path, val) => { + const db = store(key) + const data = db.get('data') + set(data, path, val) + db.set('data', data) + return db.get('data') + }, } diff --git a/src/main/app.js b/src/main/app.js index 5887a1e6..f294c18b 100644 --- a/src/main/app.js +++ b/src/main/app.js @@ -1,8 +1,10 @@ // @flow import { app, BrowserWindow, Menu, ipcMain, screen } from 'electron' +import debounce from 'lodash/debounce' import menu from 'main/menu' +import db from 'helpers/db' // necessary to prevent win from being garbage collected let mainWindow @@ -32,9 +34,10 @@ const defaultWindowOptions = ({ height, width }) => ({ function createMainWindow() { const MIN_HEIGHT = 768 const MIN_WIDTH = 1024 + const savedDimensions = db.getIn('settings', 'window.dimensions', {}) - const height = MIN_HEIGHT - const width = MIN_WIDTH + const width = savedDimensions.width || MIN_WIDTH + const height = savedDimensions.height || MIN_HEIGHT const windowOptions = { ...defaultWindowOptions({ height, width }), @@ -44,11 +47,11 @@ function createMainWindow() { titleBarStyle: 'hiddenInset', } : {}), + width, height, - minHeight: MIN_HEIGHT, minWidth: MIN_WIDTH, + minHeight: MIN_HEIGHT, show: false, - width, webPreferences: { ...defaultWindowOptions.webPreferences, // Enable, among other things, the ResizeObserver @@ -78,6 +81,11 @@ function createMainWindow() { } }) + window.on('resize', debounce(() => { + const [width, height] = window.getSize() + db.setIn('settings', 'window.dimensions', { width, height }) + }, 100)) + window.webContents.on('devtools-opened', () => { window.focus() setImmediate(() => {