79481671

Date: 2025-03-03 17:21:38
Score: 0.5
Natty:
Report link

You can easily set and get any Electron state using the electron-store library.

First, install the library using npm i electron-store -D

main.js

import Store from 'electron-store';

function createWindow() {
  // Initialize electron store
  const store = new Store();
  
  // Create the browser window with previous store settings
  const mainWindow = new BrowserWindow({
    ...
    width: store.get('width') || 640,
    height: store.get('height') || 360
  });

  // Save window states for later
  function storeWindowState() {
    const [width, height] = mainWindow.getSize();
    store.set('width', width);
    store.set('height', height);
  }

  // Save window size after resizing
  mainWindow.on('resized', storeWindowState);
  mainWindow.on('maximize', storeWindowState);
  mainWindow.on('unmaximize', storeWindowState);
}

If needed, you can also save the window position using the moved event: https://www.electronjs.org/docs/latest/api/browser-window#event-moved-macos-windows

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: doppler