Remember where app was located on close

This commit is contained in:
Preston 2020-09-20 14:54:23 -04:00
parent b0d1ddf1ac
commit b7013be907
2 changed files with 80 additions and 3 deletions

View File

@ -1,5 +1,6 @@
import { app, BrowserWindow, Menu } from 'electron'
import { app, BrowserWindow, Menu, ipcMain, screen } from 'electron'
import { productName } from '../../package.json'
import Datastore from 'nedb'
require('electron-context-menu')({
showSearchWithGoogle: false,
@ -8,6 +9,13 @@ require('electron-context-menu')({
prepend: (params, browserWindow) => []
})
const localDataStorage = app.getPath('userData') // Grabs the userdata directory based on the user's OS
const settingsDb = new Datastore({
filename: localDataStorage + '/settings.db',
autoload: true
})
// set app name
app.setName(productName)
@ -63,8 +71,6 @@ function createWindow () {
*/
mainWindow = new BrowserWindow({
backgroundColor: '#fff',
width: 960,
height: 540,
icon: isDev
? path.join(__dirname, '../../_icons/iconColor.png')
: `${__dirname}/_icons/iconColor.png`,
@ -80,6 +86,40 @@ function createWindow () {
show: false
})
mainWindow.setBounds({
width: 1200,
height: 800
})
settingsDb.findOne({
_id: 'bounds'
}, function (err, doc) {
if (doc === null || err) {
return
}
if (typeof doc !== 'object' || typeof doc.value !== 'object') {
return
}
const { maximized, ...bounds } = doc.value
const allDisplaysSummaryWidth = screen
.getAllDisplays()
.reduce((accumulator, { size: { width } }) => accumulator + width, 0)
if (allDisplaysSummaryWidth >= bounds.x) {
mainWindow.setBounds({
x: bounds.x,
y: bounds.y,
width: bounds.width,
height: bounds.height
})
}
if (maximized) {
mainWindow.maximize()
}
})
// eslint-disable-next-line
setMenu()
@ -103,6 +143,35 @@ function createWindow () {
mainWindow.on('closed', () => {
console.log('closed')
})
ipcMain.on('setBounds', (_e, data) => {
const value = {
...mainWindow.getBounds(),
maximized: mainWindow.isMaximized()
}
settingsDb.findOne({
_id: 'bounds'
}, function (err, doc) {
if (err) {
return
}
if (doc !== null) {
settingsDb.update({
_id: 'bounds'
}, {
$set: {
value
}
}, {})
} else {
settingsDb.insert({
_id: 'bounds',
value
})
}
})
})
}
app.on('ready', () => {

View File

@ -79,6 +79,7 @@ export default Vue.extend({
console.log('User is using Electron')
this.activateKeyboardShortcuts()
this.openAllLinksExternally()
this.setBoundsOnClose()
}
setTimeout(() => {
@ -244,6 +245,13 @@ export default Vue.extend({
shell.openExternal(el.href)
}
})
},
setBoundsOnClose: function () {
window.onbeforeunload = (e) => {
const electron = require('electron')
electron.ipcRenderer.send('setBounds')
}
}
}
})