Skip to content

Add shortcuts to cycle through stacked windows - #1883

Merged
rxhanson merged 1 commit into
rxhanson:mainfrom
harshpatel333:feat/cycle-windows
Oct 2, 2026
Merged

rxhanson merged 1 commit into
rxhanson:mainfrom
harshpatel333:feat/cycle-windows

Conversation

@harshpatel333

@harshpatel333 harshpatel333 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Discussion: #1884

What this adds

Two new actions, Cycle Stacked Windows and Cycle Stacked Windows Backward. Each press brings forward one window from the focused window's stack, so you can reach windows hidden behind others in the same spot without the mouse.

  • Settings: set under Behavior → Stacked Windows, below "Toggle window list". There are no default shortcuts.
  • URL scheme: cycle-stacked-windows and cycle-stacked-windows-backward.
  • Menu: both actions are left out of the menu.

How it behaves

  • What counts as a stack: stacks are found with the hover list's cascade rule: windows at the same position, or offset from it by Offset window position on overlap.
    • Size: a new Only stack windows of the same size setting in Stacked Windows applies to both the hover list and the cycle shortcuts, so the two stay in sync. It's off by default, which keeps the hover list exactly as it behaves today.
    • With it on: windows must also match in size (within 24pt, so terminals that snap to whole character cells still count). A maximized window sharing a half's top-left corner is then not stacked with the half, and the hover list shows the stack the shortcuts would cycle from its front window.
    • The Todo window is left out of cycling, as in the other multi-window actions.
  • Order: Cycle first brings forward the window at the back of the stack. Backward first brings forward the window just behind the front one. Repeated presses visit every window in either direction.
  • Keeping its place: the order is remembered while the focus stays in the stack, so pressing faster than an app can raise its window still advances. A window whose app refuses to raise it is skipped rather than retried. Pressing either shortcut while a window outside the stack is focused starts over from that window's stack.
  • Raising: windows are raised off the main thread, with AX timeouts. When presses come in faster than an app responds, only the latest raise runs. Its app is only activated if the user is still in one of the cycle's apps.
  • Nothing stacked: Rectangle beeps.

Other changes

  • The cascade math moves into StackBadgeGeometry, so the hover list and cycling share it. With the new setting off, the hover list behaves exactly as before.
  • Shortcut recorders: the two recorders on the Behavior tab are validated like the Shortcuts tab's and follow "Remove keyboard shortcut restrictions". While one records, the other shortcuts are paused through ShortcutRecordingObserver. MASShortcutViewRepresentable can now take that observer; the view is let go of when SwiftUI removes it, so collapsing the section mid-recording doesn't leave a hidden recorder active.
  • Adds AccessibilityElement.raise() (AXRaise) and TodoManager.getTodoWindowId().
  • README: a "Cycle stacked windows" section, plus the new URL action names.

Testing

  • Unit tests: StackCycleTests covers stack membership (cascades, maximized and quarter windows at the same corner, terminal-sized windows) and cycle order in both directions. It also covers presses made before a raise lands, raises that land partway through a run of presses, refused raises, and switching between stacks. A new ShortcutRecordingObserverTests case covers letting go of a recording view.
  • Same-size setting: additional tests cover it, including cases where a neighboring window or a denser group of other-sized windows shares the corner.
  • Full suite: 484 tests, 0 failures, with Xcode 27.0 on macOS 26.6.2.
  • Heads-up on main: the test target doesn't compile at the moment. DisplayTransferTests.transfer returns DisplayTransfer.transferredRect(...), which now returns a tuple, so it needs a .rect. I patched that locally to run the suite; it isn't part of this PR, but I can open a separate one.
  • Manual testing: cycled stacked windows forward and backward on macOS 26.6.2.

@rxhanson

Copy link
Copy Markdown
Owner

Thanks for contributing! This makes sense to me, and I'll test/review it soon.

Unlike the hover list, size matters. A maximized window sharing a half's top-left corner is not cycled with the half.

This will likely result in a bug filed due to confusion about the discrepancy. My first thought for resolving it is providing the option for both the hover list & cycle shortcuts to operate on either the same size or any size to keep them in sync. Resolving this isn't necessarily a requirement, but it would improve the concept in my opinion.

The stacked windows implementation was worked on by @MyronKoch (feel free to chime in if you have any thoughts/comments).

As for that test, I broke it, thanks for the heads up.

Windows snapped to the same area sit on top of each other, and the
hover list is the only way to reach the covered ones. Add two actions,
Cycle Stacked Windows and Cycle Stacked Windows Backward, that bring
the focused window's stack forward one window at a time. Their
shortcuts are set under Behavior > Stacked Windows, next to the hover
list's.

Stacks are found with the hover list's cascade rule. A new "Only stack
windows of the same size" setting, off by default so the hover list
behaves as before, limits both the hover list and the cycle shortcuts
to windows of one size (within 24pt, so terminals that snap to whole
character cells still count). With it on, a maximized window sharing a
half's corner is not stacked with the half, and the hover list shows
the stack the shortcuts would cycle from its front window. The Todo
window is left out of cycling, as in the other multi-window actions.

A cycling session keeps its anchor, ring order and a logical cursor for
as long as the focus stays in the stack. That way both directions visit
every window, rapid presses each advance, and a window whose app
refuses to raise it is stepped past rather than retried. Raises run off
the main thread with AX timeouts. Only the latest one runs, and its app
is activated only while the user is still among the cycle's apps.

The cascade math moves into StackBadgeGeometry so the hover list and
cycling share it.
@MyronKoch

Copy link
Copy Markdown
Contributor

Nice work - this fills the one gap the hover list leaves: once the list is open you can walk it with the arrows and Return, but there's no way to open it without the mouse. Cycling covers that nicely.

On size: the hover list is deliberately size-agnostic because it moves nothing. It only lists what's pegged to that corner, so a maximized window over a half belongs in the list. Keeping "same size" as an opt-in, off by default, preserves that, and having one setting drive both makes sense to me.

One idea for later, not for this PR: a shortcut that opens the hover list at the focused window's stack would give keyboard users the whole list, not just next/previous.

@harshpatel333

Copy link
Copy Markdown
Contributor Author

Thanks both!

@rxhanson, good call on the discrepancy. I've pushed an Only stack windows of the same size setting under Stacked Windows that the hover list and the cycle shortcuts both follow. It's off by default, so the hover list behaves exactly as it does today, and cycling then includes every window pegged to that corner (for example, a maximized window over a half). With it on, both only stack windows of the same size, and the hover list shows the same stack the shortcuts would cycle from its front window. The PR description has the details. Happy to change the naming, default or placement. And no worries about the test!

@MyronKoch, thanks for the context. That's exactly why I kept same-size opt-in and off by default. A shortcut that opens the hover list at the focused window's stack is a great idea, and I'm happy to take it on in a separate PR once this one lands.

One more thing I'd be open to implementing: support for mapping actions to mouse buttons, like the side buttons on many mice. It would pair nicely with cycling, for example stepping through a stack with the thumb buttons.

@rxhanson

rxhanson commented Oct 1, 2026

Copy link
Copy Markdown
Owner

I tested this out and did a quick walk through the code. Looks good! I'm going to give it a little more thorough walk through the code first thing tomorrow.

@rxhanson
rxhanson merged commit 6ba1002 into rxhanson:main Oct 2, 2026
@harshpatel333

Copy link
Copy Markdown
Contributor Author

Thanks! I love Rectangle and use it every single day. It makes my 49" Ultrawide monitor infinitely better. I would love to keep contributing.

@rxhanson

rxhanson commented Oct 2, 2026

Copy link
Copy Markdown
Owner

Thanks for contributing! I forgot to mention that the mouse button idea sounds good to me if you end up working on 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.

3 participants