From 460021760657298376c3ee7412978f58d97b57aa Mon Sep 17 00:00:00 2001 From: Jarne Date: Sat, 7 Oct 2023 14:27:24 +0200 Subject: [PATCH 1/2] Add basic window state persistance --- packages/bruno-electron/src/index.js | 12 ++++++-- .../bruno-electron/src/store/window-state.js | 21 ++++++++++++++ packages/bruno-electron/src/utils/window.js | 28 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 packages/bruno-electron/src/store/window-state.js create mode 100644 packages/bruno-electron/src/utils/window.js diff --git a/packages/bruno-electron/src/index.js b/packages/bruno-electron/src/index.js index 3f67b8a96..5e9916ef0 100644 --- a/packages/bruno-electron/src/index.js +++ b/packages/bruno-electron/src/index.js @@ -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); diff --git a/packages/bruno-electron/src/store/window-state.js b/packages/bruno-electron/src/store/window-state.js new file mode 100644 index 000000000..4b2520380 --- /dev/null +++ b/packages/bruno-electron/src/store/window-state.js @@ -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; diff --git a/packages/bruno-electron/src/utils/window.js b/packages/bruno-electron/src/utils/window.js new file mode 100644 index 000000000..db1aff11d --- /dev/null +++ b/packages/bruno-electron/src/utils/window.js @@ -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 +}; From fab350a32dfed1466d6f884ca0777c248d0680ed Mon Sep 17 00:00:00 2001 From: Jarne Date: Sat, 7 Oct 2023 14:51:47 +0200 Subject: [PATCH 2/2] Add some checks if position/size is inside screen border --- packages/bruno-electron/src/utils/window.js | 33 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/bruno-electron/src/utils/window.js b/packages/bruno-electron/src/utils/window.js index db1aff11d..d824141d3 100644 --- a/packages/bruno-electron/src/utils/window.js +++ b/packages/bruno-electron/src/utils/window.js @@ -1,3 +1,4 @@ +const { screen } = require('electron'); const WindowStateStore = require('../store/window-state'); const windowStateStore = new WindowStateStore(); @@ -8,11 +9,14 @@ const DEFAULT_WINDOW_HEIGHT = 768; const loadWindowState = () => { const bounds = windowStateStore.getBounds(); + const positionValid = isPositionValid(bounds); + const sizeValid = isSizeValid(bounds); + return { - x: bounds.x || undefined, - y: bounds.y || undefined, - width: bounds.width || DEFAULT_WINDOW_WIDTH, - height: bounds.height || DEFAULT_WINDOW_HEIGHT + x: bounds.x && positionValid ? bounds.x : undefined, + y: bounds.y && positionValid ? bounds.y : undefined, + width: bounds.width && sizeValid ? bounds.width : DEFAULT_WINDOW_WIDTH, + height: bounds.height && sizeValid ? bounds.height : DEFAULT_WINDOW_HEIGHT }; }; @@ -22,6 +26,27 @@ const saveWindowState = (window) => { windowStateStore.setBounds(bounds); }; +const isPositionValid = (bounds) => { + const area = getArea(bounds); + + return ( + bounds.x >= area.x && + bounds.y >= area.y && + bounds.x + bounds.width <= area.x + area.width && + bounds.y + bounds.height <= area.y + area.height + ); +}; + +const isSizeValid = (bounds) => { + const area = getArea(bounds); + + return bounds.width <= area.width && bounds.height <= area.height; +}; + +const getArea = (bounds) => { + return screen.getDisplayMatching(bounds).workArea; +}; + module.exports = { loadWindowState, saveWindowState