r/SwiftUI • u/rokoodev • 7d ago
Pane: open-source trackpad window snapping for macOS, built with CGEventTap and the Accessibility API
I built Pane, a free and open-source macOS menu bar app that snaps windows when you two-finger swipe on the active window’s title bar (halves, quarters, fill, minimize, next monitor). It’s pure Swift with no dependencies: AppKit, Core Graphics and the Accessibility API.
A few problems that turned out more interesting than I expected:
1. Listening to every scroll without ever slowing scrolling down
Pane uses a CGEventTap on scrollWheel events. The tap sits in front of all scrolling on the system, so the hot path has to be cheap. On .began it finds the window under the cursor with NSWindow.windowNumber(at:belowWindowWithWindowNumber:) and CGWindowListCopyWindowInfo, which only asks the window server. The Accessibility API, which goes to the app and can block on a hung one, is only touched once the swipe is actually on a title bar, and every AX call has a 150 ms messaging timeout.
2. Swipe vs scroll, and natural scrolling
Deltas are normalized with isDirectionInvertedFromDevice, so finger direction is the same whether natural scrolling is on or off. Once a swipe is claimed, its momentum events are swallowed too, otherwise the window below keeps scrolling after the snap. State resets on every .began, so a lost .ended event can’t leave Pane eating normal scrolls.
3. Moving windows that don’t want to move
Setting the frame through AX is size → position → size again, because macOS clamps a resize that runs past the Dock or a screen edge, but never a move. Right after a window changes screens, some apps still clamp to the old one, so Pane checks the result and retries a few times. Chrome and Electron apps animate AX moves slowly while AXEnhancedUserInterface is on, so it’s switched off for the move and restored afterwards. All of this runs on a background queue so a slow app never stalls the event tap or the preview animation.
Repo
Layout picking and geometry are covered by tests, CI lints with swift-format and builds a universal binary, and releases are built by GitHub Actions from the tagged source.
MIT licensed: github.com/rokoov/pane
Happy to answer questions about any of it, and I’d love feedback on the code from people who’ve worked with event taps or AX before.

1
u/bleducnx 6d ago
Where can one download this app? The GitHub page is in 404 mode.
1
u/builtinswift 7d ago
the chrome animation workaround is such a sneaky find