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