From b7013be907dac47e48b01d6aa169248356ccb48e Mon Sep 17 00:00:00 2001 From: Preston Date: Sun, 20 Sep 2020 14:54:23 -0400 Subject: [PATCH] Remember where app was located on close --- src/main/index.js | 75 +++++++++++++++++++++++++++++++++++++++++++-- src/renderer/App.js | 8 +++++ 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 1227a4a32..ea2772aa5 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -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', () => { diff --git a/src/renderer/App.js b/src/renderer/App.js index fbfbe4d7f..bb8c00c31 100644 --- a/src/renderer/App.js +++ b/src/renderer/App.js @@ -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') + } } } })