From 8a0f7c6b8fe53da6cd03ccbbf268b4404b2c9631 Mon Sep 17 00:00:00 2001 From: Jarne Date: Wed, 11 Oct 2023 19:22:50 +0200 Subject: [PATCH 1/2] Save window maximized state --- packages/bruno-electron/src/index.js | 15 +++++++++++---- packages/bruno-electron/src/store/window-state.js | 10 ++++++++++ packages/bruno-electron/src/utils/window.js | 11 +++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/bruno-electron/src/index.js b/packages/bruno-electron/src/index.js index 3a38e21a..a57fadde 100644 --- a/packages/bruno-electron/src/index.js +++ b/packages/bruno-electron/src/index.js @@ -9,7 +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 { loadWindowState, saveBounds, saveMaximized } = require('./utils/window'); const lastOpenedCollections = new LastOpenedCollections(); @@ -32,7 +32,7 @@ let watcher; // Prepare the renderer once the app is ready app.on('ready', async () => { - const { x, y, width, height } = loadWindowState(); + const { maximized, x, y, width, height } = loadWindowState(); mainWindow = new BrowserWindow({ x, @@ -52,6 +52,10 @@ app.on('ready', async () => { // autoHideMenuBar: true }); + if (maximized) { + mainWindow.maximize(); + } + const url = isDev ? 'http://localhost:3000' : format({ @@ -63,8 +67,11 @@ app.on('ready', async () => { mainWindow.loadURL(url); watcher = new Watcher(); - mainWindow.on('resize', () => saveWindowState(mainWindow)); - mainWindow.on('move', () => saveWindowState(mainWindow)); + mainWindow.on('resize', () => saveBounds(mainWindow)); + mainWindow.on('move', () => saveBounds(mainWindow)); + + mainWindow.on('maximize', () => saveMaximized(true)); + mainWindow.on('unmaximize', () => saveMaximized(false)); mainWindow.webContents.on('new-window', function (e, url) { e.preventDefault(); diff --git a/packages/bruno-electron/src/store/window-state.js b/packages/bruno-electron/src/store/window-state.js index bb0a61b6..51c6060b 100644 --- a/packages/bruno-electron/src/store/window-state.js +++ b/packages/bruno-electron/src/store/window-state.js @@ -4,6 +4,8 @@ const Store = require('electron-store'); const DEFAULT_WINDOW_WIDTH = 1280; const DEFAULT_WINDOW_HEIGHT = 768; +const DEFAULT_MAXIMIZED = false; + class WindowStateStore { constructor() { this.store = new Store({ @@ -26,6 +28,14 @@ class WindowStateStore { setBounds(bounds) { this.store.set('window-bounds', bounds); } + + getMaximized() { + return this.store.get('maximized') || DEFAULT_MAXIMIZED; + } + + setMaximized(isMaximized) { + this.store.set('maximized', isMaximized); + } } module.exports = WindowStateStore; diff --git a/packages/bruno-electron/src/utils/window.js b/packages/bruno-electron/src/utils/window.js index d824141d..949a2aaa 100644 --- a/packages/bruno-electron/src/utils/window.js +++ b/packages/bruno-electron/src/utils/window.js @@ -7,12 +7,14 @@ const DEFAULT_WINDOW_WIDTH = 1280; const DEFAULT_WINDOW_HEIGHT = 768; const loadWindowState = () => { + const maximized = windowStateStore.getMaximized(); const bounds = windowStateStore.getBounds(); const positionValid = isPositionValid(bounds); const sizeValid = isSizeValid(bounds); return { + maximized, x: bounds.x && positionValid ? bounds.x : undefined, y: bounds.y && positionValid ? bounds.y : undefined, width: bounds.width && sizeValid ? bounds.width : DEFAULT_WINDOW_WIDTH, @@ -20,12 +22,16 @@ const loadWindowState = () => { }; }; -const saveWindowState = (window) => { +const saveBounds = (window) => { const bounds = window.getBounds(); windowStateStore.setBounds(bounds); }; +const saveMaximized = (isMaximized) => { + windowStateStore.setMaximized(isMaximized); +}; + const isPositionValid = (bounds) => { const area = getArea(bounds); @@ -49,5 +55,6 @@ const getArea = (bounds) => { module.exports = { loadWindowState, - saveWindowState + saveBounds, + saveMaximized }; From 0cf4f09608d44eda618470382c1e4c3d4eeddb9e Mon Sep 17 00:00:00 2001 From: Jarne Date: Tue, 17 Oct 2023 17:25:20 +0200 Subject: [PATCH 2/2] Save window bounds only if not maximized --- packages/bruno-electron/src/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/bruno-electron/src/index.js b/packages/bruno-electron/src/index.js index a57fadde..83e50783 100644 --- a/packages/bruno-electron/src/index.js +++ b/packages/bruno-electron/src/index.js @@ -67,8 +67,14 @@ app.on('ready', async () => { mainWindow.loadURL(url); watcher = new Watcher(); - mainWindow.on('resize', () => saveBounds(mainWindow)); - mainWindow.on('move', () => saveBounds(mainWindow)); + const handleBoundsChange = () => { + if (!mainWindow.isMaximized()) { + saveBounds(mainWindow); + } + }; + + mainWindow.on('resize', handleBoundsChange); + mainWindow.on('move', handleBoundsChange); mainWindow.on('maximize', () => saveMaximized(true)); mainWindow.on('unmaximize', () => saveMaximized(false));