# Restore macOS windows to their original Space after restart \(\#58886\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/33c6212b055fa8a97b17c8653311c9ccab7fb3b6)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 33c6212b055fa8a97b17c8653311c9ccab7fb3b6

Requested commit: 33c6212b055fa8a97b17c8653311c9ccab7fb3b6

Commit: 33c6212b055fa8a97b17c8653311c9ccab7fb3b6

Tree: 22673d53958809989fc82abc68377816e0681f31

Author: Toru Nayuki

Committer: GitHub

## Message

```
Restore macOS windows to their original Space after restart (#58886)

Addresses #9640 (the Spaces part).

On macOS, Zed already restores each window's size, position, and
display, but not its Space (virtual desktop). So after a restart, all
windows open on whatever Space is active at launch, not the Space they
were on before. This is the most reported problem in #9640.

A window's Space is not part of the window bounds we save. It is handled
by AppKit's own window state restoration. So instead of tracking the
Space myself, this PR uses that AppKit feature:

- I mark `GPUIWindow` as restorable and encode its native state (frame +
Space) by calling `encodeRestorableStateWithCoder:` with an
`NSKeyedArchiver`. A delegate skips the `NSView`/`NSWindow` objects that
do not support `NSSecureCoding`.
- I save the blob per workspace in a new `restorable_window_states`
table.
- When a saved workspace is opened again, I create the window and then
replay the blob with `restoreStateWithCoder:`. AppKit then moves the
window back to its Space.

On other platforms, and when there is no saved blob, this does nothing.

Verification:

- Added a round-trip test for the database part (save / overwrite /
read, and `ON DELETE CASCADE`).
- The macOS encode/restore part calls into AppKit, so I could not
unit-test it. I checked it by hand: I put several windows on different
Spaces, quit with Cmd+Q, started Zed again, and each window came back on
the Space it was closed on. I also checked that the saved blob is a real
AppKit archive (it starts with `bplist00`).

For now this only works for local workspaces. Remote workspaces could
use the same way later.

Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Release Notes:

- macOS: Restored windows now reopen on the Space (virtual desktop) they
were on, instead of all landing on the currently active Space.

---------

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
```

## Parents

- [002161d5ba8dda2e6f6551be9439ebb893eb35c3](https://git.cafe/gitcafe/zed/commit/002161d5ba8dda2e6f6551be9439ebb893eb35c3?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/33c6212b055fa8a97b17c8653311c9ccab7fb3b6?format=markdown)
