The Electron Window State Manager is a small package that gives you the ability to save the state of a
BrowserWindow and retreive the saved data of the state later.
npm install electron-window-state-manager
The Window State Manager can store the dimensions (width and height), the position (x and y coordinates) and the current state (maximized or not) of a
BrowserWindow and save it to a json file.
The saved state can than later be retreived when using the same window name at the instance creation. The saved state's data will be automatically retreived when creating a
WindowStateManagerinstance with an already saved window name.
To be able to use this package in your project you need to require it:
const WindowStateManager = ;
It creates a new
WindowStateManager with a
name and default properties as set by the
nameString - Name of the window.
defaultWidthInteger - Default window width in pixels.
defaultHeightInteger - Default window height in pixels.
The value of
name is used to reference a saved state in the json file. If you create a new instance of
WindowStateManager with a name which was already saved previously, the data of this state will be loaded.
If a state with the value of
name cannot be found in the json file or a saved state has wrong data, the default values assigned in the
options Object will be used.
WindowStateManager class has the following methods:
Saves the state of the passed
BrowserWindow and returns
false depending on whether the state was successfully saved to the json file.
In case the state of a window in fullscreen is saved, the saving process will not succeed because we don't want to save the dimensions of a fullscreen window.
The method returns
false and will not save anything if a window in fullscreen is saved, because we don't want to save the dimensions of a fullscreen window.
If a maximized window is saved, the dimensions and position of the window will not be stored, only the previously saved values or the default values will be saved. However the maximized state of the window will be saved, so that you can open the window in a maximized state again later if the window was closed in a maximized state.
const app = app;const BrowserWindow = BrowserWindow;const WindowStateManager = ;const mainWindow;// Create a new instance of the WindowStateManager// and pass it the name and the default propertiesconst mainWindowState = 'mainWindow'defaultWidth: 1024defaultHeight: 768;app;