Browse Source

Save and restore window dimensions

master
meriadec 7 years ago
parent
commit
b3b79eaa90
  1. 16
      src/helpers/db.js
  2. 16
      src/main/app.js

16
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')
},
}

16
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(() => {

Loading…
Cancel
Save