Skip to content

Commit

Permalink
#278 save window settings, #267 close app when close window on mac
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcb777 committed May 22, 2019
1 parent c62a487 commit 1cc1268
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
35 changes: 35 additions & 0 deletions src/helpers/window-settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import storage from 'electron-json-storage'
import { promisify } from 'es6-promisify'
import { get, set } from 'lodash'

const storageGet = promisify(storage.get)
const storageSet = promisify(storage.set)

export const saveWindowSettings = async window => {
if (!window) return

const [width, height] = window.getSize()
const [x, y] = window.getPosition()
const settings = await storageGet('settings')

if (!settings) return

set(settings, 'windowParams', {
width,
height,
x,
y,
})

return storageSet('settings', settings)
}

export const getWindowSettings = async () => {
try {
const settings = await storageGet('settings')
return settings.windowParams || {}
} catch (e) {
console.log(e)
return {}
}
}
26 changes: 20 additions & 6 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path'
import { format as formatUrl } from 'url'
import { autoUpdater } from 'electron-updater'

import { saveWindowSettings, getWindowSettings } from 'helpers/window-settings'
import buildMenu from 'menu'

const isProduction = process.env.NODE_ENV === 'production'
Expand All @@ -27,13 +28,16 @@ const installExtensions = async () => {
).catch(console.log) // eslint-disable-line
}

function createMainWindow() {
async function createMainWindow() {
const windowParams = await getWindowSettings()

const w = new BrowserWindow({
webPreferences: {
webSecurity: false,
},
backgroundColor: '#2A2A35',
show: false,
...windowParams,
})

w.once('ready-to-show', () => {
Expand Down Expand Up @@ -86,23 +90,33 @@ function createMainWindow() {
return w
}

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
let beforeQuitDone = false

app.on('before-quit', async event => {
if (!beforeQuitDone) {
event.preventDefault()
await saveWindowSettings(mainWindow)

beforeQuitDone = true
app.quit()
}
})

app.on('activate', () => {
app.on('window-all-closed', async () => {
app.quit()
})

app.on('activate', async () => {
if (mainWindow === null) {
mainWindow = createMainWindow()
mainWindow = await createMainWindow()
}
})

app.on('ready', async () => {
if (isDevelopment) {
await installExtensions()
}
mainWindow = createMainWindow()
mainWindow = await createMainWindow()
if (isProduction) {
autoUpdater.checkForUpdatesAndNotify()
}
Expand Down

0 comments on commit 1cc1268

Please sign in to comment.