Skip to content

helper: windows that land on the Edge come back - #7

Merged
fdussert merged 1 commit into
fdussert:mainfrom
mcouzinet:window-fence
Nov 19, 2026
Merged

fdussert merged 1 commit into
fdussert:mainfrom
mcouzinet:window-fence

Conversation

@mcouzinet

Copy link
Copy Markdown
Contributor

The mouse fence keeps the pointer off the Edge, but nothing stops an application from placing a window there, which is how it actually happens. It happened to me twice today: a Safari window and a Finder window, both restored to a frame saved before the Edge was plugged in. The kiosk covers that display above the status-bar level, so the window was focused, held the keyboard, and was invisible. Nothing on screen says it exists, the pointer cannot reach it, and Mission Control shows the kiosk. I got the Finder one back with tell application "Finder" to set bounds of window 1 to ..., which is not a thing to ask of anybody.

Same trap AdminPlacement was written for, from the other side, so this reuses it rather than growing new geometry.

How it works. WindowFence sweeps every two seconds: one CGWindowListCopyWindowInfo call, no round trip to any application, no permission. Anything of somebody else's on layer 0 overlapping the Edge goes to AdminPlacement.rescue, and only then is the Accessibility API reached, for that one application, to set kAXPosition (and kAXSize when the window is wider than the display it is moved to). So the steady-state cost is one list read every two seconds, and AX traffic only when there is something to rescue, which is almost never.

Choices worth a look.

  • It rides on the existing fence config flag and toggle rather than adding a second switch: the promise is the same for the pointer and for windows. The menu entry is renamed Mouse fence to Edge fence. Easy to split if you would rather have two.
  • Three moves per window number, then it is left alone: an application that puts its window straight back would otherwise be fought forever, twice a second, with the window flickering between two displays.
  • Any overlap counts, not just a window entirely on the Edge, for the reason AdminPlacement.isUsable already gives: a straddling window has the part you read on the side you cannot see.
  • The window list and AX name the same window in two ways with no shared identifier, so the match is on the frame, falling back to whichever window of that application still overlaps the Edge.

Checked. scripts/test-helper.sh green (85 tests, 9 new on WindowSweep, listed in the shim registry) and scripts/build-helper.sh builds the bundle. The geometry was verified by hand on my Edge (a Finder window at x -1740 came back to the main display keeping its size). What I could not exercise here is the AX move from inside a running helper: this machine has no "Fremkit Helper Dev" identity in the keychain, so a local install would be ad-hoc signed and would drop the Accessibility and Input Monitoring grants of the helper that is running. Worth a try on a machine set up for it before merging.

🤖 Generated with Claude Code

@fdussert fdussert left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks — this is a real trap and reusing AdminPlacement.rescue is the right shape. Reviewed merged onto current main together with #8 and #9: scripts/test-helper.sh 89/89, the helper builds clean. Mutations caught by WindowSweepTests (intersects → contains, dropping the ownPid filter, widening the layer filter). Our own windows are never touched, no Edge → sweep() returns at once, no Accessibility → one log line.

Two things I'd like fixed before merging:

  1. The 3-try cap is per window lifetime, not per episode (WindowFence.swift:79,96). attempts is only cleared when the window closes, so a long-lived Safari or Finder window that lands on the Edge again after a few sleep/wake or replug cycles is never rescued after its third time — the very case this PR is for. Dropping the counts of windows that are no longer stray on each sweep would do it, e.g. attempts = attempts.filter { strayIDs.contains($0.key) }.
  2. A window being dragged is moved mid-drag. Any overlap counts, and the fence stops the pointer, not the window: drag a window toward the Edge, part of it crosses, and the next sweep snaps it to the centre of the main display while the button is still down. Skipping the sweep while a button is held (CGEventSource.buttonState(.combinedSessionState, button: .left)) would avoid it.

Smaller, take or leave:

  • Several windows rescued in one sweep all land centred on top of each other; a small cascade offset would help.
  • Giving up after the last try logs nothing — one line would make it diagnosable.
  • The 2 s timer could carry a tolerance.
  • The retry/rescue logic in the helper has no test; only WindowSweep's picking rules do. If the reset in (1) can live in FremkitCore, it can be tested there.
  • README: a stray line break around line 39 ("in a kiosk window and" alone on its line), and line ~217 runs long.

@fdussert

Copy link
Copy Markdown
Owner

Hi Mickael! A gentle nudge on this one, plus a heads-up on what changed around it.

#15 (by @FlorianBruniaux) landed today: the helper menu can now hide the dashboard, so the Edge can be "borrowed" as a normal display. While it's hidden, the fences pause, and that goes through a generic hook in AppDelegate: fenceUpdates plus syncFences(). Since #15 is merged, this branch now conflicts in AppDelegate.swift, README.md and CHANGELOG.md. When you rebase, WindowFence should register its start/stop in fenceUpdates rather than being started directly. That's one append, and it makes the window sweep pause while the dashboard is hidden. Otherwise the sweep would move every window off the Edge the moment the user hides the dashboard to reach them.

The two points from my earlier review still stand: the try counter should reset per episode, not per window lifetime, and the sweep should skip while a mouse button is held so a window being dragged isn't moved. The smaller ones are optional.

No rush, and thanks again for this. It's a real trap and the AdminPlacement.rescue reuse is the right shape. #9 conflicts with main too, if you get a moment.

@fdussert fdussert mentioned this pull request Oct 15, 2026
@mcouzinet

Copy link
Copy Markdown
Contributor Author

Rebased on 0.3.0. One thing changed on the way: AppDelegate grew fenceUpdates and syncFences(), with a comment inviting a future window fence to register there, so the window fence now registers alongside the mouse one instead of starting and stopping itself in three places. It therefore follows the same rule as the pointer fence, including the new one: it stands down while the dashboard is hidden.

scripts/test-helper.sh green (88 tests), bundle builds.

@fdussert fdussert left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks Mickael, this is much nicer now: plugging into fenceUpdates/syncFences() gives the window fence the hide/show and toggle behaviour for free, and keeping the logic in WindowSweep makes it testable. Built and tested locally (88/88). A few things before merging:

  1. WindowFence.swift:120: the 1 s messaging timeout is set on the application element only. As far as I know, the window elements copied from it don't inherit it, so frame(of:) and the position/size setters use the global default (~6 s). A hung app with a window on the Edge could then stall the main run loop, where the mouse fence tap also runs. Could you set the timeout on each window element too, or on AXUIElementCreateSystemWide()?
  2. WindowFence.swift:71: a window being dragged toward the Edge overhangs it while the pointer is held back at the boundary, and the sweep then moves it mid-drag, using up one of its three attempts. Could the sweep skip while a mouse button is down (CGEventSource.buttonState(.combinedSessionState, button:))?
  3. WindowSweep.swift:38: please also skip windows whose kCGWindowAlpha is 0. Some apps keep invisible layer-0 windows around, and moving those can upset them.
  4. WindowSweep.swift:45 (open question): for a window that only overlaps the boundary, centring it on the main display is a big jump. Would nudging it just off the Edge be enough there? Happy to hear your view.
  5. Nits: the comment at WindowFence.swift:25 says "two rounds a second" while the interval is 2 s, and the README wrap at lines 40 and 218 needs reflowing.

Heads-up: this and #9 conflict only in CHANGELOG.md and docs/troubleshooting.md; keeping both sides resolves it. I'd suggest #9 lands first, as it is closer.

The mouse fence keeps the pointer off the Edge, but nothing stopped an
application from placing a window there: Safari or the Finder restoring
a frame saved before the Edge was plugged in, or a display rearranged
under windows that were where they belonged. The kiosk covers that
display above the status-bar level, so a window that arrives there is
focused, holds the keyboard, and is invisible. Nothing on screen says
it exists, and the pointer cannot reach it, which is the same trap
AdminPlacement was written for, from the other side.

A two-second sweep reads the window list, which costs one call and no
round trip to any application, and hands anything of somebody else's
that overlaps the Edge to AdminPlacement, the geometry the admin window
already uses, then moves it there. The Accessibility API is reached
only for an application that actually has a window on the Edge, which
is almost never. Three moves per window is the limit: an application
that puts its window straight back would otherwise be fought forever,
twice a second, with the window flickering between two displays.

It rides on the existing fence switch rather than adding one, since it
is the same promise for the pointer and for windows, and the menu entry
is renamed from "Mouse fence" to "Edge fence" to say so.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fdussert

Copy link
Copy Markdown
Owner

Hi Mickael, thanks a lot for this one, and sorry for the silence since your rebase. Rather than send you a third round, we finished the remaining points on top of your commit, in the feature/edge-fences branch:

  • the Accessibility calls now run off the main thread, with the 1 s timeout on the window elements too, so a half-hung app can no longer stall the pointer;
  • no move while a mouse button is down, so a window being dragged is left alone;
  • fully transparent windows are skipped, and floating and modal panels are brought back too (a mail reminder stayed on the Edge on the first real run);
  • the three attempts now count per episode and are forgotten after 30 s off the Edge;
  • windows rescued together are staggered instead of stacked;
  • one log line when a window is given up, and a few nits.

Your commit stays yours in the history, and this PR will show as merged once main is pushed.

fdussert added a commit that referenced this pull request Nov 19, 2026
… Edge

mcouzinet's window fence (#7) and pointer patrol (#9), with the review's remaining points finished on top of his commits: Accessibility calls off the main thread, no move during a drag, attempts counted per episode, a stale last position dropped after a display rearrangement, an atomic cursor restore.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@fdussert
fdussert merged commit 6b80ef5 into fdussert:main Nov 19, 2026
2 checks passed
fdussert added a commit that referenced this pull request Nov 19, 2026
The Edge fence for windows and the pointer patrol (mcouzinet, #7 and #9), the Claude limits widget with one row per account, and pinned Claude sessions.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

2 participants