Skip to content

feat: restore windows to their original macOS Spaces on restart - #308301

Open
Toru Nayuki (tnayuki) wants to merge 4 commits into
microsoft:mainfrom
tnayuki:feat/macos-spaces-restoration
Open

Toru Nayuki (tnayuki) wants to merge 4 commits into
microsoft:mainfrom
tnayuki:feat/macos-spaces-restoration

Conversation

@tnayuki

Copy link
Copy Markdown

Fixes #123517

Summary

  • Add electron-osx-spaces as a dependency to save and restore the macOS Space (virtual desktop) each window belongs to
  • Use AppKit's native NSWindow state restoration APIs (encodeRestorableStateWithCoder: / restoreStateWithCoder:) via the electron-osx-spaces package
  • The restorable state is persisted as base64-encoded data in IWindowState.macOSRestorableState
  • No-op on Windows and Linux

Changes

File Change
package.json Add electron-osx-spaces to dependencies
window.ts Add macOSRestorableState field to IWindowState
windowImpl.ts Encode Space state on quit, restore on window creation

How to test

  1. Build and launch Code - OSS on macOS
  2. Open multiple windows (Cmd+Shift+N)
  3. Drag each window to a different Space (virtual desktop)
  4. Quit (Cmd+Q)
  5. Relaunch — windows should restore to their original Spaces

Compatibility

  • Backwards compatible: macOSRestorableState is optional; old state data works without it
  • Cross-platform: electron-osx-spaces returns no-op on non-macOS platforms
  • macOS 15+: The package includes a workaround for NSWindowRestoresWorkspaceAtLaunch being broken (FB15644170)

Add electron-osx-spaces dependency to save and restore the Space
(virtual desktop) each window belongs to. This uses AppKit's native
NSWindow state restoration APIs via the electron-osx-spaces package.

Fixes microsoft#123517
@vs-code-engineering

vs-code-engineering Bot commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Benjamin Pasero (@bpasero)

Matched files:

  • src/vs/platform/window/electron-main/window.ts
  • src/vs/platform/windows/electron-main/windowImpl.ts

@tnayuki

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@cnaccio

Copy link
Copy Markdown

Looking forward to this!

@coreyfeder

Copy link
Copy Markdown

You are a hero to the people. 👏

Could you document what happens when different Spaces are closed or moved between app close and app restart?

@0xdevalias

Copy link
Copy Markdown

Use AppKit's native NSWindow state restoration APIs (encodeRestorableStateWithCoder: / restoreStateWithCoder:) via the electron-osx-spaces package

macOS 15+: The package includes a workaround for NSWindowRestoresWorkspaceAtLaunch being broken (FB15644170)

I'm not deeply across all the nuance of things in this space, but from some recent deep reading through related Chromium bug fixes / etc; I think the 'correct' Apple API's to use are actually NSWindowRestoration restoreWindow(withIdentifier:state:completionHandler:); with the macOS 15+ workaround (a custom NSKeyedUnarchiver: "Returning a default-initialized NSWindowRestorationOptions from _windowRestorationOptions is enough to get windows to restore on the spaces whence they came.")) only being used by Chromium as the native API's weren't flexible enough for their needs (which may not be an issue for vscode):

Specifically this comment:

  • This bug is about switching to the NSWindowRestoration API.

    It turns out that that API is not flexible enough for Chromium to use it (FB22128526)

    The key problem is that the NSWindowRestoration API assumes that there exists exactly one set of windows that needs to be saved and restored. Chrome, in contrast, has the concept of “profiles”, and each profile has its own set of windows that needs to, independently of the other profiles, be saved and restored.

    The specific list of issues:

    • It has no concept of sets of windows; it assumes every window needs to be saved/restored
    • It automatically triggers at launch, and at no other time, whereas Chrome needs to be able to say, at any point, “please restore this set of windows”
    • When it automatically triggers at launch, any display of UI is blocked, so that Chrome can’t show UI to ask the user which profile they want to use.

I raised an upstream issue in electron-osx-spaces about this as well, but wanted to share the info on this PR as well so it wasn't missed/lost:

@tnayuki

Toru Nayuki (tnayuki) commented Jul 8, 2026 •

Copy link
Copy Markdown
Author

Glenn 'devalias' Grant (@0xdevalias) Thanks for the research! Switching to NSWindowRestoration would require a significant rewrite of VSCode's window lifecycle. This PR takes the minimal approach: hook into the existing window state serialization to add Space restoration without touching the rest of the window management.

@horner

Copy link
Copy Markdown

Benjamin Pasero (@bpasero) can you evaluate the integration point
Robo (@deepak1556) can you evaluate the native/Electron implementation.?

In particular, it would help to know which concern is the principal blocker:

the manual AppKit state-restoration mechanism;
introducing electron-osx-spaces as a native dependency;
requiring this capability to be implemented upstream in Electron; or
missing compatibility and failure-mode testing.

I would be happy to help coordinate testing across macOS versions, Apple Silicon/Intel, multiple monitors, update restarts, deleted or reordered Spaces, and external-monitor disconnection. A brief architectural direction would help contributors focus on the evidence or changes needed for an Insiders trial.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

Relaunching VSCode forgets what macOS Desktop each window belongs to

7 participants