Snug is a menu bar app that tiles every window the moment it opens. Open a window and it drops into the next free tile; close one and the rest reflow with no holes. There's no config file, and it never asks for Screen Recording permission: it reads each window's position, size and owning app, and nothing else.
macOS has no window manager API, so everything goes through the Accessibility API ( AXUIElement ). Here's what that taught me.
1. Give every AX call one thread
Each AX call is a synchronous message to another app, so a busy or hung app can stall the caller. Snug runs one background thread that owns every AX call, every observer and the tiler's state. The main thread only draws UI.
A detail worth knowing: a thread's run loop exits immediately if it has no sources, so the AX thread adds a dummy port to keep it alive before any observer exists.
RunLoop . current . add ( NSMachPort ( ) , forMode : . default ) // keeps the run loop alive with no sources
Enter fullscreen mode
Exit fullscreen mode
2.…