Skip to content

fix(window): restore geometry at its save-time scale - #4112

Open
baxen wants to merge 8 commits into
mainfrom
mbot/window-scale-restore
Open

baxen wants to merge 8 commits into
mainfrom
mbot/window-scale-restore

Conversation

@baxen

@baxen baxen commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Why

Fixes #3083. On a Mac with a 2x laptop and a 1x external, the window reopens at double or half size when Buzz quits on one screen and starts on the other. Both monitors stay attached. The window then saves that wrong size for the next launch.

tauri-plugin-window-state saves physical pixels with no scale. When the saved rect is reachable, restore_geometry_from sends those pixels as physical, and macOS converts them at the scale of the screen the new window starts on, not the one it was saved on.

Supersedes #2732.

What

Buzz records the scale factors of the plugin's saved size and position every time the plugin saves, and restores at those scales. The plugin stays the only writer of .window-state.json. Buzz writes {"main": {"scale": 2.0, "position": 1.0, "prev": 2.0}} to its own .window-scale.json beside it: scale is the size's, position is the saved x/y's, and prev is prev_x/prev_y's (the normal origin of a maximized window). A file with only scale reads the positions at that scale.

How

  • Save. Buzz keeps the scales of the plugin's cached frame in memory and writes them only where the plugin writes its file.
    • The kept scales start from .window-scale.json at launch, as the plugin's cache starts from its file.
    • They follow every update of the plugin's cache (window-state 2.5.0), under the plugin's own conditions. Moved (unless minimized) sets the position's scale, and the old one becomes prev, as the plugin moves x/y into prev_x/prev_y. Resized (unless minimized or maximized, with the plugin's macOS decorated/resizable quirk) sets the size's. CloseRequested and Exit (unless minimized or maximized) set both.
    • window_state::window_event maps window events to those updates; Kept::apply holds the rules.
    • On RunEvent::Exit, after the plugin's save, and in onboarding_preserve_window, Buzz writes the kept scales if it has the size's, and nothing otherwise. The onboarding snapshot gets a copy of the scale file, onboarding-window-scale.json. The onboarding restore uses that copy, then removes it.
  • Restore, scale recorded (macOS and Linux). restore_input picks the onboarding snapshot and its scale copy while one is pending, else the plugin's file and .window-scale.json. restore_plan converts each saved position from its own scale to the size's pixels (at_size_scale), then restore_request measures every monitor's work area, the fallback, the 48-point header and the client offset in the saved scale's pixels. It then runs the existing reachability check and sends the result as logical units at that scale, whether the rect is reachable or relocated. Measuring the monitors in the saved scale matters too: otherwise a rect saved on a 1x external that's now unplugged can fall inside a 2x laptop's physical rect, count as reachable, and open off-screen. The rule that treats a size below the minimum as saved at a lower scale is skipped here, because the scale is known.
  • Restore, no scale (state from before this change). The request is exactly what it is on main.
  • Windows ignores the recorded scale and is unchanged.

The issue's option 1 (guessing the scale from the monitor that contains the header) isn't included. In the reporter's layout the laptop's and the external's physical rects overlap, so that guess can be wrong.

Risk

Testing

  • cargo test -p Buzz --lib window_state: 25 pass. New tests:
    • In the [Bug] macOS: window reopens at double or half size when Buzz starts on a screen with another scale #3083 layout, a reachable rect saved at 2x comes back at 1200x750 points when the startup scale is 1, and a 1x rect comes back exact when the startup scale is 2.
    • A sub-minimum 1x size is kept when the scale is recorded.
    • Reachability is measured in points after unplugging.
    • With no scale file, the request matches main.
    • Size and position restore each at their own scale, read from disk with restore_input and planned with restore_plan as restore does: a 2x size with its position moved to (2212, -200) points on the 1x external comes back there at 1200x750 points, and a maximized window's prev origin uses prev's scale.
    • The cache rules: a drag to 1x with no Resized, then a minimized quit, keeps a 2x size and a 1x position; maximizing moves the normal origin's scale into prev; saves never touch prev.
    • Window events drive the kept scales through window_event (other windows and other events are ignored), events alone never write the file, and Exit writes the scales of the plugin's save.
    • restore_input reads the onboarding snapshot with its own scale copy while it's pending, else the live pair.
    • The onboarding path writes both scale files, and restore removes the snapshot's copy.
    • With no kept scale, nothing is written.
    • A geometry file overwritten without its scales (downgrade, or an exit cut short) ignores the stale scale file: 1200x750 pixels saved at 1x come back as 1200x750 physical pixels, not 600x375 points, and the kept scales start empty.
    • While the plugin's restore_state holds its restore lock, Buzz ignores Moved and Resized too.
  • Each of these changes makes a test fail: dropping the geometry check, starting the kept scales from an unchecked file, writing scales without their geometry, ignoring the restore lock, ignoring Moved, skipping the position conversion in the restore path, ignoring the recorded scale in the restore path, onboarding reading the live scale file, Close not reading the position, and Moved updating while minimized. Removing the one-line window_event call in lib.rs isn't caught by a test.
  • cargo clippy -p Buzz --all-targets -- -D warnings is clean.
  • cargo test -p Buzz: 1261 pass. 2-3 host_command process tests fail locally, a different set each run. They don't touch window code.
  • No in-app run yet.

In-app check

This needs a 2x laptop with a 1x external still attached (Josh Friend has this setup).

  1. Put Buzz on the laptop and size it to about 1200x750 points. Quit with Cmd-Q.
  2. Focus a window on the external (for example, Finder), then launch Buzz.
  3. The window opens on the laptop at the same size and position, not doubled or clamped.
  4. Move Buzz to the external and resize it. Quit, focus a window on the laptop, and relaunch.
  5. The window opens on the external at the same size, not halved.
  6. Quit and relaunch twice more without moving the window. Its size stays the same.

🤖 Generated with Claude Code

@baxen
baxen marked this pull request as ready for review November 20, 2026 20:24
@baxen
baxen requested a review from a team as a code owner November 20, 2026 20:24
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T20:26:21.649732Z 3772ca1 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3772ca1bbe

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src-tauri/src/window_state.rs Resolved

@wesbillman wesbillman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Star Lord reviewed 3772ca1. Please fix the Close/Force Quit issue below and the existing finding on cached normal geometry. This was a source review, and both paths still need verification on mixed-DPI hardware.

Comment thread src-tauri/src/lib.rs Resolved

@kalvinnchau kalvinnchau left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Two remaining concerns: position/size scale pairing and regression coverage of persistence wiring.

Comment thread src-tauri/src/lib.rs Resolved
Comment thread src-tauri/src/lib.rs Resolved
@baxen
baxen force-pushed the mbot/window-scale-restore branch from 2142c2d to 558c0f9 Compare November 20, 2026 21:39

@kalvinnchau kalvinnchau left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 The earlier position-scale reproduction now passes. Two P3 follow-ups remain: production restore-path coverage and the documented persistence contract. Native mixed-DPI behavior remains unverified.

Comment thread src-tauri/src/window_state.rs Resolved
Comment thread src-tauri/src/window_state.rs Resolved
@baxen
baxen force-pushed the mbot/window-scale-restore branch from 558c0f9 to 2895c15 Compare November 20, 2026 22:35

@kalvinnchau kalvinnchau left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 The prior restore-conversion coverage and documentation findings are addressed. One P3 remains: the mixed-scale test assumes logical units on Windows.

Comment thread src-tauri/src/window_state.rs Resolved

@kalvinnchau kalvinnchau left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Approved at 2895c15.

The per-field scale tracking addresses the earlier cached position/size mismatch in source and model checks. The persisted-state reproduction now restores the expected position and size. Restore conversion runs through the tested production planner, and removing that conversion now fails the regression test. The persistence documentation also matches the implementation. No actionable security issue was identified.

The 25 window_state tests pass on macOS. One test-only P3 remains: the mixed-scale test expects logical units even on Windows, where restore deliberately uses physical units (#4112 (comment)). Gate that assertion or check the Windows physical result.

Native mixed-DPI quit/onboarding behavior and AppKit event ordering remain unverified; the full-package suite and native Linux/Windows runs were not performed. The possible dispatch-time scale mismatch remains a hypothesis, not a demonstrated runtime defect.

@jedwards27 jedwards27 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 :bot: Jude’s code review agent

Verdict: COMMENT — recommend request changes at base 6ba83fa6a2bb73c6cf03c25a3ef3a74f883fc4c8, head 85ddde07e739c8130012abaf6c4e737af7960c65. Risk: high — native window geometry and paired persistence across launches.

Blocking finding (P2): stale scale metadata can corrupt restored geometry. src-tauri/src/lib.rs:1391–1394 saves the plugin's geometry file before Buzz writes .window-scale.json (src-tauri/src/window_state.rs:181–199). But restore_input accepts any syntactically valid scale file alongside any saved geometry (window_state.rs:300–315), and restore_plan uses that scale to convert size and position (window_state.rs:389–425). After a previous app version overwrites only the geometry file on a 1× monitor, or the process exits between these two writes, a surviving 2× scale is applied to fresh 1× pixels. A 1200×750-pixel saved window is requested as 600×375 logical points on re-upgrade—precisely the size-loss class this PR fixes. The PR description acknowledges the downgrade path. Author action: bind the sidecar to the exact geometry snapshot and fall back to legacy restore when the pair does not match; add a regression that overwrites only the geometry file and verifies both downgrade/re-upgrade and interrupted-save recovery. A digest of the geometry is one possible binding; ensure the check covers the file actually consumed on restore.

Other paths reviewed: per-field scale provenance for size/current/previous positions, onboarding snapshot selection, legacy fallback, and reachable-header relocation. No additional actionable UI/accessibility defect found. The pinned plugin ignores move/resize events while its restore lock is held, whereas Buzz's scale listener does not (window_state.rs:137–169); whether this creates a real AppKit ordering mismatch is unproven, not a second blocker. Inspect with a native maximized/reopen journey if possible.

Verification owner: author for stale-pair fix and fail-before/pass-after regression; reviewer/tooling for exact-head native mixed-DPI verification and the restore-event-order question. Validation: clean detached checkout at 85ddde07e739c8130012abaf6c4e737af7960c65; git diff --check 6ba83fa6a2bb73c6cf03c25a3ef3a74f883fc4c8...HEAD passed. CI required at this head passed on attempt 2, including its Linux Rust tests; Windows native validation was skipped. An independent macOS cargo test -p Buzz --lib window_state attempt timed out during cold compilation before tests ran. Manual/native evidence: no exact-head 2×/1× app journey performed. Residual risk: source/model checks and CI do not establish AppKit monitor-coordinate behavior. Run the 2×→1× and reverse quit/relaunch journey, close/hide→force-quit, disconnected display, and maximize/unmaximize with isolated app identity before merge.

@baxen
baxen force-pushed the mbot/window-scale-restore branch from 85ddde0 to 91252a0 Compare November 21, 2026 12:07
@baxen

baxen commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator Author

🤖 Thanks for the review. The P2 is fixed in 91252a0: .window-scale.json now records the geometry it was saved with (geometry: width, height, x, y, prev_x, prev_y from the plugin's file it was written beside). restore_input uses the scales only when that matches the geometry file it restores, the live file or the onboarding snapshot, and the scales Buzz keeps at launch go through the same check. A mismatch restores as on main. Regression: a_geometry_save_without_its_scales_restores_as_before writes a bound 2x pair, then overwrites only the geometry with 1200x750 pixels saved at 1x. That covers both the downgrade and the interrupted save, since each leaves a scale file naming an older geometry. The test checks it restores at 1200x750 physical pixels, not 600x375 points, and that the kept scales start empty. Dropping the check, or starting the kept scales from the unchecked file, fails it.

On the restore lock: Buzz now ignores Moved and Resized while the plugin's restore_state runs, the same window in which the plugin holds its lock and skips them. Covered in window_events_update_the_kept_scales_and_only_saves_write_them. The native 2x/1x journeys still need real hardware; they're listed in the PR description.

baxen and others added 8 commits November 22, 2026 11:21
tauri-plugin-window-state saves physical pixels with no scale, and the
reachable path sent them as physical, so macOS converted them at the
startup screen's scale: a window saved on a 2x laptop reopened at double
size when Buzz started on a 1x external, and the reverse halved it.

Buzz now records the main window's scale in .window-scale.json next to
the plugin's file at every plugin save (Exit, CloseRequested, and the
onboarding preserve path, whose snapshot gets its own copy). On macOS
and Linux, a recorded scale measures the monitors in the saved pixels and
sends the result in logical units. State saved without a scale restores
exactly as before. Windows is unchanged.

Fixes #3083

Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
While the window is maximized or minimized, tauri-plugin-window-state
keeps the normal frame it cached earlier instead of saving the current
one. Writing the current scale then paired that frame with another
screen's scale, for example a window maximized on a 2x laptop, dragged
to a 1x external and quit. Skip the scale write under the plugin's own
condition, and give the onboarding snapshot a copy of the scale file
paired with the plugin file it copies.

Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On CloseRequested the window-state plugin only updates its in-memory
frame; its file is written at Exit or by our onboarding save. Writing the
scale at close let a close followed by a crash or Force Quit pair the old
geometry on disk with the new scale. Close now keeps the scale in memory,
under the same maximized/minimized rule. Exit writes the window's current
scale, or the kept one when the window is gone or its frame is kept, and
nothing when neither exists.

Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rame

tauri-plugin-window-state also updates its cached size on every Resized
event while the window isn't maximized or minimized. Dragging a window
from a 2x screen to a 1x one, maximizing and quitting left 1x pixels in
its file beside the earlier 2x scale. The kept scale now starts from the
scale file, as the plugin's cache starts from its file, and follows each
cache update under the plugin's own condition: Resized (with its macOS
decorated/resizable quirk), CloseRequested and Exit. Exit and onboarding
write the kept scale if there is one, and nothing otherwise.

Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The window-state plugin updates its cached position on Moved and its size
on Resized, so a drag to another scale without a Resized leaves the two at
different scales. Buzz now keeps a scale for the size, the position and the
previous position (prev_x/prev_y), following the plugin's rules for each
event, and restore converts each position from its own scale.

The event mapping, the cache rules and the choice between the onboarding
snapshot and the live files are now small functions with tests.

Co-authored-by: Orbit Helper <0f7edbd528cce2e9ea41a9310dfea0c84e0f5f04c60551b7c3e657f8033a4a39@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… scales

Restore now plans through one entry point, `restore_plan`, which converts
the saved positions to the size's scale and builds the request. The
mixed-scale tests read the files with `restore_input` and plan with
`restore_plan`, so dropping the conversion from the restore path fails them.

docs/shell-design.md now describes the scale file, the onboarding copy and
the behaviour without a scale file.

Co-authored-by: Orbit Helper <0f7edbd528cce2e9ea41a9310dfea0c84e0f5f04c60551b7c3e657f8033a4a39@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Windows restores in physical pixels and ignores recorded scales, so
restore_plan returns no logical scale there and the test's expect would
panic on the windows-native job.

Co-authored-by: Orbit Helper <0f7edbd528cce2e9ea41a9310dfea0c84e0f5f04c60551b7c3e657f8033a4a39@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The scale file now records the geometry it belongs to. Restore, and the
scales Buzz keeps at launch, use it only when that matches the geometry
file being restored, so a geometry file written without its scales (by an
older build, or an exit cut short between the two writes) restores as it
did before scales, instead of taking a stale scale.

Buzz also ignores Moved and Resized while the plugin's restore_state runs,
as the plugin does under its restore lock.

Co-authored-by: Orbit Helper <0f7edbd528cce2e9ea41a9310dfea0c84e0f5f04c60551b7c3e657f8033a4a39@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@baxen
baxen force-pushed the mbot/window-scale-restore branch from 91252a0 to 06d1c0a Compare November 22, 2026 19:27

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.

[Bug] macOS: window reopens at double or half size when Buzz starts on a screen with another scale

4 participants