How to restore window size


#1

Hi there. I’m super new to Electron and haven’t been able to find a way to restore the window size each time my app launches. I’ve tried various solutions I’ve found on the interwebs, but I haven’t gotten any to work. Maybe someone could explain how to do it in dumb-speak so that I could understand it?

Here’s the repository: https://github.com/sdale28/Habitica-App

Any help would be greatly appreciated!

Thanks!


#2

Have a look at the usage for electron-window-state package:

const windowStateKeeper = require('electron-window-state');
let win;

app.on('ready', function () {
  // Load the previous state with fallback to defaults
  let mainWindowState = windowStateKeeper({
    defaultWidth: 1000,
    defaultHeight: 800
  });

  // Create the window using the state information
  win = new BrowserWindow({
    'x': mainWindowState.x,
    'y': mainWindowState.y,
    'width': mainWindowState.width,
    'height': mainWindowState.height
  });

  // Let us register listeners on the window, so we can update the state
  // automatically (the listeners will be removed when the window is closed)
  // and restore the maximized or full screen state
  mainWindowState.manage(win);
});

That is added in main.js where you initialize your BrowserWindow


#3

Thanks! It took a little trial and error, but that worked perfectly (once I finally figured out how to get it to work).