Skip to content

Fix: save window maximized state #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/bruno-electron/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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,
Expand All @@ -52,6 +52,10 @@ app.on('ready', async () => {
// autoHideMenuBar: true
});

if (maximized) {
mainWindow.maximize();
}

const url = isDev
? 'http://localhost:3000'
: format({
Expand All @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do small enhancement here

  mainWindow.on('resize', () => {
    if (!mainWindow.isMaximized()) { 
      saveBounds(mainWindow)
    }
  });
  mainWindow.on('move', () => {
    if (!mainWindow.isMaximized()) {
      saveBounds(mainWindow)
    }
  });

(probably some code cleanup would be fine to minimize duplicity in my snippet)

So window bounds would be saved only in case window is not maximized. And why?
Imagine case that you resize and move window to x=0, y=0, width=100, height=100. This bounds are saved. Then you maximize window. Maximation flag is saved, but also bounds are replaced with some values which are not important when window is maximized. Then you close app. After starting app again I would expect it starts as maximized, but in case I unmaximize it then bounds will be restored to x=0, y=0, width=100, height=100. If you apply my change then It will happen. If not then after unmaximize action window position is restored to default value because it has negative x, y values on Windows and on Linux and Mac (in case there are no negative values after maximize action) window position and size I guess would be very similar if not the same as when window is maximized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your detailed explanation, makes absolutely sense, I'll change the implementation to work this way.


mainWindow.on('maximize', () => saveMaximized(true));
mainWindow.on('unmaximize', () => saveMaximized(false));

mainWindow.webContents.on('new-window', function (e, url) {
e.preventDefault();
Expand Down
10 changes: 10 additions & 0 deletions packages/bruno-electron/src/store/window-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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;
11 changes: 9 additions & 2 deletions packages/bruno-electron/src/utils/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@ 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,
height: bounds.height && sizeValid ? bounds.height : DEFAULT_WINDOW_HEIGHT
};
};

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);

Expand All @@ -49,5 +55,6 @@ const getArea = (bounds) => {

module.exports = {
loadWindowState,
saveWindowState
saveBounds,
saveMaximized
};