/electron-window-keeper

Great alternative to electron-window-state

Primary LanguageTypeScriptMIT LicenseMIT

Electron Window Keeper

One day, this module will be the easiest way to manage complicated window state persistence.

It should be a maintained alternative to electron-window-state.

Available to use in main process only.

API

Usage

The usage as simple as it should be:

import ElectronWindowKeeper from "electron-window-keeper";
import { app, BrowserWindow } from "electron";

app.on("ready", () => {
    const windowState = new ElectronWindowKeeper();
    const window = new BrowserWindow({
        width: 1280,
        height: 720,
        // always position defaults (x, y, width, height, fullscreen) above
        ...windowState.restoredState
    });
    windowState.manage(window);
});

If you don't understand ... (spread syntax) you can read javascript.info guide.

To disable tracking of maximized state pass trackMaximized: false.

To disable tracking of fullscreen state just pass fullscreen: false right after restoredState:

new BrowserWindow({
    // default props
    ...windowState.restoredState,
    fullscreen: false // always override restored fullscreen value
});

Also note, that as stated in BrowserWindow docs, setting minimum or maximum window size doesn't prevent setting it from restored state.

TODO

  • tests. again, I'm too lazy to setup it now
  • describe behavior when multiple app instances are launched (last resized window would override the state)

Notes

Electron memento was really close to finalize the idea, but this module is meant to be more modern and comprehensive.

Also note, that to bring the window to top without gaining the focus you can specify alwaysOnTop: true when creating new BrowserWindow and on just created window call browserWindow.setAlwaysOnTop(false). But never use it on production! It would be annoying to the user.