Skip to content

Preserve relative window position when moving to another display - #1809

Merged
rxhanson merged 1 commit into
rxhanson:mainfrom
YuriNachos:YuriNachos/w8-rectangle
Aug 14, 2026
Merged

rxhanson merged 1 commit into
rxhanson:mainfrom
YuriNachos:YuriNachos/w8-rectangle

Conversation

@YuriNachos

Copy link
Copy Markdown
Contributor

Summary

  • Preserve a window's relative position (not just its size) when moving to another display with the attemptMatchOnNextPrevDisplay opt-in enabled and no Rectangle snap action to replay.
  • A window at the right-third / centered / etc. now keeps that relative spot on the destination screen instead of jumping to the center. The window's rect is mapped proportionally between the source and destination visibleFrames and clamped inside the destination.
  • Applies identically to next/previous-display and specific-display (display 1/2/3) moves.
  • Opt-in only: users who never enabled attemptMatchOnNextPrevDisplay see no change.

Root cause

NextPrevDisplayCalculation.calculate (and SpecificDisplayCalculation.calculate) only preserved position by replaying lastAction. With the opt-in ON but no replayable lastAction (a manually positioned window), execution fell through to calculateRect, whose override returns centerCalculation — so the window was centered regardless of the opt-in.

Fix

In the fall-through case (opt-in ON, no replayable snap), map the window's current rect proportionally from the source screen's adjustedVisibleFrame to the destination screen's adjustedVisibleFrame, preserving its relative position and size as fractions of the source frame, then clamp inside the destination. Shared by both call sites via a single NextPrevDisplayCalculation.relativePositionedRect(window:source:destination:) helper. Degenerate source frames return the window unchanged.

Testing

xcodebuild test -project Rectangle.xcodeproj -scheme Rectangle -destination 'platform=macOS'

  • NextPrevDisplayMappingTests: 4 pure-geometry tests covering right-third mapping, centered-quarter mapping, destination-overflow clamp, and identity. All pass.
  • Full suite: 209 tests, 21 pre-existing failures in ActiveSideSplitRatiosCooperativeTests / HalfSplitCornerCalculationTests (unchanged baseline — these pre-date this PR, in-flight cooperative-resize / corner-calc work). No new failures.
  • Multi-display end-to-end could not be exercised on the single-display build host; the proportional mapping is covered exhaustively by the unit tests above, and the two call sites are minimal and structurally identical.

AI assistance

This PR was prepared with AI assistance; the approach, tests, and verification were reviewed by the contributor.

Closes #1723

When the hidden attemptMatchOnNextPrevDisplay opt-in is ON and a window is
moved to the next/previous or a specific display with no Rectangle snap
action to replay, map the window's rect proportionally from the source
screen's adjustedVisibleFrame to the destination's, preserving its relative
position and size, and clamp it inside the destination. Previously the window
jumped to the center in this case. Applies identically to next/prev and
specific-display moves. No change when the opt-in is off/unset.
@YuriNachos

Copy link
Copy Markdown
Contributor Author

One practical note on this PR: the Build workflow is still marked 'waiting for approval to run', so the checks haven't actually executed on this branch yet. Whenever you're working through the queue, approving that run would let it build — and if anything else would save you time, whether that's a specific display setup to test the relative-position behavior on or further changes to the diff, I'm glad to handle that part. No rush from my side; I just don't want it sitting invisible.

@rxhanson
rxhanson merged commit 2111f76 into rxhanson:main Aug 14, 2026
@rxhanson

Copy link
Copy Markdown
Owner

Great, thanks! Good point on the workflow approval, and thanks for your patience :)

anandghegde added a commit to anandghegde/Rectangle that referenced this pull request Sep 24, 2026
A window against three screen edges - spanning one axis and against one
edge of the other, like a left half or a top half - was stretched along
the whole of the spanning axis when it moved between a landscape and a
portrait display, turning a left half into a tall sliver. It now keeps
its size and is centered along the middle one of its three edges.
Picking a corner instead would mean guessing from how the displays are
arranged; centering makes no assumption.

Keeping a window's position is now a setting, "Keep window position
when moving across displays" in the Behavior tab, backed by
keepWindowPositionOnDisplayChange. It is on by default. Turned off,
display moves behave as they did before this branch: the window is
centered, or with attemptMatchOnNextPrevDisplay on and no action to
replay, mapped proportionally as in rxhanson#1809. relativePositionedRect and
its tests are restored for that path, and the centerOnDisplayChange
terminal command is gone, since the setting replaces it.
rxhanson pushed a commit that referenced this pull request Sep 24, 2026
* Keep a window's position when moving it to another display

Moving a window to the next, previous or a specific display centered it
on the destination, which loses the spot the user had put it in.

DisplayTransfer works out each axis on its own from whether the window
was against a screen edge: against both edges it follows both and spans
the destination, against one it keeps its size and stays against that
edge, against neither it keeps its size and its center keeps the same
relative spot. The distance from an edge is carried over rather than
flattened, so a window snapped with gaps arrives with the same gaps.

This is now the default for next/previous and specific display moves.
The old centering is available as centerOnDisplayChange, and
attemptMatchOnNextPrevDisplay still replays the last Rectangle action
where there is one.

Closes #1666

* RectangleTests: match the current setFrame signature

The test double overrides setFrame(_:adjustSizeFirst:), but
AccessibilityElement.setFrame gained an adjustPosition parameter in
"Add optional snapping animations and a blurred snap preview" (#1849),
so the override no longer matches and the test target does not compile:

  RectangleTests.swift:6153:23: error: method does not override any
  method from its superclass

This is not related to the rest of the branch - it reproduces on a clean
checkout - but the test target has to build before any of the tests can
run.

* Center three-edge windows along the middle edge across orientations

A window against three screen edges - spanning one axis and against one
edge of the other, like a left half or a top half - was stretched along
the whole of the spanning axis when it moved between a landscape and a
portrait display, turning a left half into a tall sliver. It now keeps
its size and is centered along the middle one of its three edges.
Picking a corner instead would mean guessing from how the displays are
arranged; centering makes no assumption.

Keeping a window's position is now a setting, "Keep window position
when moving across displays" in the Behavior tab, backed by
keepWindowPositionOnDisplayChange. It is on by default. Turned off,
display moves behave as they did before this branch: the window is
centered, or with attemptMatchOnNextPrevDisplay on and no action to
replay, mapped proportionally as in #1809. relativePositionedRect and
its tests are restored for that path, and the centerOnDisplayChange
terminal command is gone, since the setting replaces it.
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.

Window position resets to center when moving to next/previous display

2 participants