electron-window-state
electron-window-state copied to clipboard
Window state not persisted anymore
Since a while (maybe since Electron 11), the window state does not persist anymore. The json file is not created anymore so my window state resets every time. I tried giving it a different path but does not seem to work either.
const defaultWidth = Math.min(1920, screen.getPrimaryDisplay().size.width)
const defaultHeight = Math.min(1080, screen.getPrimaryDisplay().size.height)
const mainWindowState = WindowStateKeeper({
defaultWidth,
defaultHeight,
maximize: true // Doesn't seem to work, but does set isMaximized to undefined when window isnt maximized
})
// Create the browser window.
win = new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
frame: false,
resizable: true,
fullscreenable: true,
maximizable: true,
minimizable: true,
minHeight: 800,
minWidth: 1024,
closable: true,
titleBarStyle: 'hidden',
backgroundColor: '#222',
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
devTools: ALLOW_DEVTOOLS,
enableRemoteModule: true
}
})
mainWindowState.manage(win)
"electron-window-state": "^5.0.3",
"electron": "11.0.2",
Same here, specifying custom path or file prevents saving state. Also state is only saved on closing the app,
To fix this issue I took this code and modified it to my linking and it works well (for me).
I also added a win.on('resize', saveState); to save state on every resize event.
Thanks, setting it manually does seem to work:
win.on('resize', debounce(mainWindowState.saveState, 500))
win.on('move', debounce(mainWindowState.saveState, 500))
Leaving the issue open because seems the default way with .manage() is broken
In my case the state is not persisted between updates of the app, I'll try the resize | move and see if it works better.
I had to do:
const debouncedSaveWindowState = debounce(
(event: any) => mainWindowState.saveState(event.sender),
500
)
mainWindow.on('resize', debouncedSaveWindowState)
mainWindow.on('move', debouncedSaveWindowState)
@Christilut's code assumes that the window is sent as argument to mainWindowState.saveState, but in my electron, it would get an object where sender property is the actual window.
It works persistently on Windows and Mac on my side after I changed the defaultHeight to 600.
On my windows laptop this condition state.y + state.height <= bounds.y + bounds.height would always be false if I set defaultHeight to 768.