Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window state not persisted anymore #64

Open
Christilut opened this issue Dec 2, 2020 · 5 comments
Open

Window state not persisted anymore #64

Christilut opened this issue Dec 2, 2020 · 5 comments

Comments

@Christilut
Copy link

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",
@ixaxaar
Copy link

ixaxaar commented Jan 26, 2021

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.

@Christilut
Copy link
Author

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

@codingedgar
Copy link

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.

@raine
Copy link

raine commented Oct 16, 2021

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.

@JaosnHsieh
Copy link

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.

linonetwo added a commit to tiddly-gittly/TidGi-Desktop that referenced this issue May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants