Add basic window state persistance

This commit is contained in:
Jarne 2023-10-07 14:27:24 +02:00
parent 1e27427d09
commit 4600217606
3 changed files with 59 additions and 2 deletions

View File

@ -9,6 +9,7 @@ const LastOpenedCollections = require('./store/last-opened-collections');
const registerNetworkIpc = require('./ipc/network');
const registerCollectionsIpc = require('./ipc/collection');
const Watcher = require('./app/watcher');
const { loadWindowState, saveWindowState } = require('./utils/window');
const lastOpenedCollections = new LastOpenedCollections();
@ -27,9 +28,13 @@ let watcher;
// Prepare the renderer once the app is ready
app.on('ready', async () => {
const { x, y, width, height } = loadWindowState();
mainWindow = new BrowserWindow({
width: 1280,
height: 768,
x,
y,
width,
height,
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
@ -54,6 +59,9 @@ app.on('ready', async () => {
mainWindow.loadURL(url);
watcher = new Watcher();
mainWindow.on('resize', () => saveWindowState(mainWindow));
mainWindow.on('move', () => saveWindowState(mainWindow));
mainWindow.webContents.on('new-window', function (e, url) {
e.preventDefault();
require('electron').shell.openExternal(url);

View File

@ -0,0 +1,21 @@
const _ = require('lodash');
const Store = require('electron-store');
class WindowStateStore {
constructor() {
this.store = new Store({
name: 'window-state',
clearInvalidConfig: true
});
}
getBounds() {
return this.store.get('window-bounds') || {};
}
setBounds(bounds) {
this.store.set('window-bounds', bounds);
}
}
module.exports = WindowStateStore;

View File

@ -0,0 +1,28 @@
const WindowStateStore = require('../store/window-state');
const windowStateStore = new WindowStateStore();
const DEFAULT_WINDOW_WIDTH = 1280;
const DEFAULT_WINDOW_HEIGHT = 768;
const loadWindowState = () => {
const bounds = windowStateStore.getBounds();
return {
x: bounds.x || undefined,
y: bounds.y || undefined,
width: bounds.width || DEFAULT_WINDOW_WIDTH,
height: bounds.height || DEFAULT_WINDOW_HEIGHT
};
};
const saveWindowState = (window) => {
const bounds = window.getBounds();
windowStateStore.setBounds(bounds);
};
module.exports = {
loadWindowState,
saveWindowState
};