Window commands

Three namespaces: window for a window's life and its chrome, window-state for a frame that survives a relaunch, positioner for one of nine placements on the main screen. All three are registered by the macOS runtime with the platform's own adapter, so window.crust.invoke("window", "list") works in an app that names no plugin.

Every command answers a value, never a rejected invoke: { ok: true, … } or { ok: false, code, message }. Without an adapter the answer is not-implemented with requires: "NSWindow", never an empty list that reads like a real one.

window

commandargumentsanswer
create{ label, title?, width?, height?, url?, activate? }{ ok: true, label, created: true }
close{ label }{ ok: true, label, closed: true }
focus{ label }{ ok: true, label, focused: true }
setTitle{ label, title }{ ok: true, label, title }
listnone{ ok: true, labels }
isOpen{ label }{ ok: true, label, open }
labelnone{ ok: true, label }
setSize{ label, width, height }{ ok: true, label, width, height }
getSize{ label }{ ok: true, label, width, height }
setPosition{ label, x, y }{ ok: true, label, x, y }
getPosition{ label }{ ok: true, label, x, y }
setAlwaysOnTop{ label, value }{ ok: true, label, alwaysOnTop }
isAlwaysOnTop{ label }{ ok: true, label, alwaysOnTop }
setFullscreen{ label, value }{ ok: true, label, fullscreen }
isFullscreen{ label }{ ok: true, label, fullscreen }
minimize{ label }{ ok: true, label, minimized: true }
unminimize{ label }{ ok: true, label, minimized: false }
isMinimized{ label }{ ok: true, label, minimized }
maximize{ label }{ ok: true, label, maximized: true }
isMaximized{ label }{ ok: true, label, maximized }
setDecorations{ label, value }{ ok: true, label, decorations }
isDecorated{ label }{ ok: true, label, decorations }
setOpacity{ label, value }{ ok: true, label, opacity }
opacity{ label }{ ok: true, label, opacity }
setCursor{ cursor }{ ok: true, cursor }
setCursorVisible{ value }{ ok: true, visible }
capture{ label, ignoreOcclusion?, prime? }{ ok: true, label, width, height, scale, png }

label is the one command with no adapter behind it: the bridge reads the window identity off the calling request (?window=<label> in the URL crust opened the page with, sent by the page client on every invoke) and hands it over as ctx.window. It answers the label that request carried — never the first open window, the focused one, or the only one. A page the bridge did not open that way carries no identity, and the answer is not-implemented with requires: "the calling page's window identity on the invoke request". A client-supplied label is a routing hint, not a credential: the bearer token is the only one, so a page that claims another label can mislabel itself and nothing else.

create refuses a label that is already open rather than replacing it, and refuses width/height that are not positive integers instead of clamping them. A created bridge-served document — a relative URL or an absolute URL on the bridge's own origin — carries ?window=<label> for the command's label plus the seed token, so the minted document token holds that window's grants; a window or token the page put in the URL is replaced. A bridge-origin URL whose path begins // is refused: the native navigation would follow it as protocol-relative and carry the token to a foreign host. activate: false is the passive create: the window is shown (orderFrontRegardless on macOS, SW_SHOWNOACTIVATE on Windows, the plain show on Linux) and never becomes key or brings the app forward, whatever app.activate or testing.activation says. A non-boolean activate is invalid-value rather than being coerced, because a truthy "false" would order the window forward and steal the front — the exact behaviour the flag exists to avoid. Absent is the platform's ordinary create. setSize measures the window frame, so the title bar is inside its height. The webview is four edge pins on the content view with no size of its own, so the page's own viewport follows the frame on every resize — a drag, setSize, maximize or a restore — and bun run e2e:window asserts that in both directions. setCursor and setCursorVisible take no label: macOS has one cursor for the app, not one per window. setCursor accepts default, pointer, crosshair, text and grab.

Failure codes: invalid-label (a missing or empty label), invalid-title (a create or setTitle title that is not a string), unknown-window (no open window carries that label), duplicate-label (create on a label already in the platform's map), invalid-size (a size that is not a positive integer), invalid-position (an x or y that is not an integer; negative is legitimate), invalid-value (a value that is not a boolean, an activate that is not a boolean, or an opacity outside 0–1, or a cursor outside the list), invalid-url (create with an empty or non-string url, or a bridge-origin URL with an authority-absolute // path), unknown-adapter (the adapter has no such member, checked before the arguments), handler-error (the adapter threw, or a getter answered nothing).

capture

commandargumentsanswer
capture{ label, ignoreOcclusion?, prime? }{ ok: true, label, width, height, scale, png }

capture renders the page as the webview draws it — -[WKWebView takeSnapshotWithConfiguration:completionHandler:] on macOS, webkit_web_view_get_snapshot on Linux — never the display, so it needs no Screen Recording grant and never reads a screen buffer. width and height are the bitmap in page pixels (the point size times scale), scale is the pixels-per-point the platform measured (2 on a Retina display, 1 under Xvfb), and png is base64 of the PNG bytes with no data: prefix, ready for an <img src> once a prefix is added. The answer is asynchronous: WebKit hands the picture to a completion handler after the invoke returns, so a second capture that starts while one is pending fails the first rather than queueing behind it, and a handler that does not answer within 5000 ms fails naming the timeout.

ignoreOcclusion: true is a probe-only opt-in for a window that may be off the active Space. WebKit suspends rendering for a window it considers occluded, and takeSnapshot then never calls its completion handler; the option holds a disable for one capture and releases it before the command answers. The disable is ref-counted per webview by one shared owner that the perf harness also uses, so a capture inside a profiling run never turns detection back on under the profiler: WebKit's default is restored only when crust's last holder on that view lets go. It never activates the app, orders the window, changes its collection behaviour or touches another app's Space. Absent or false leaves WebKit's behaviour untouched, so an ordinary app takes exactly the same picture it did before.

This is unsupported WebKit private SPI — _setWindowOcclusionDetectionEnabled: — not a public API. A future WebKit update may remove the selector. When it does, the capture answers a named handler-error ("unsupported WebKit _setWindowOcclusionDetectionEnabled: is absent on this macOS; no capture was started") rather than crashing or hanging; no picture is sent with a stale flag. The flag prevents WebKit from suspending a view; it does not wake one WebKit has already suspended, so a probe must keep its run loop pumping while it waits for the page. Use it only in an automated probe whose window is deliberately off-Space and that needs a screenshot without disturbing the human. Do not use it in a shipped app for casual user-facing screenshots: a disabled detection flag changes how WebKit renders the whole view, and the long-lived hold belongs to the perf harness (which shares the same owner), not to a screenshot command. A non-boolean ignoreOcclusion is invalid-value rather than being coerced, because a truthy "false" would silently opt a shipped app in.

prime: true is a second, harness-only opt-in for the harder case: a view whose window has never been composited at all. It calls unsupported private AppKit SPI — -[NSWindow _setOcclusionStateIsVisible:] — to set the window's composited bit for one capture, then restores it in a finally on every terminal path: success, refusal, timeout, supersede or throw. The hold is ref-counted by one owner, so two probes in one process cannot clear each other's bit. It never activates, never orders or moves the window and never changes a Space.

The private call exists because there is no public route. Measured on macOS 27: an accessory app that never activates leaves occlusionState at 8192 — the composited bit (2) is absent — and takeSnapshot never completes. Four public wake-ups all left that bit at 8192 and all timed out: orderBack: at an on-screen corner, alpha 0.01, a 1×1 window at alpha 0, and -[NSWindow display] with -[NSView displayIfNeeded]. Even a positive control — an opaque 120×90 window at (20,20), ordered front — stayed at 8192 with no PNG. Detaching the webview from its superview and reattaching it also produced no PNG in 2500 ms, and dropped firstResponder from 1 to 0. Setting the bit directly answers in 17 ms with a real 960×640 PNG.

That is why prime is not a default and not for a shipped app. A production bundle, or a macOS where the selector is absent, answers handler-error naming prime-unavailable before anything is sent. A shipped app that needs a screenshot should show or activate its window instead of reaching for this; the private selector can change or disappear without notice. A non-boolean prime is invalid-value rather than being coerced. Absent or false leaves the path byte-identical to today's ordinary capture.

Without the option, a never-composited window answers handler-error naming no-completion after the 5000 ms budget rather than a bare timeout, and an answer without a bitmap names no-picture. An automated probe while a fullscreen app owns the active Space is the caller that needs prime most: its window never draws there, and — as measured below — it need not answer no-completion at all.

Measured on this machine with a fullscreen Ghostty frontmost and the probe window off its Space — the reported condition, confirmed:

capture bare:                 1373ms ok blank  width=960 height=640 scale=2 bytes=12214 pixels=614400 opaque=614400 colours=1 differing=0
capture ignoreOcclusion=true:    40ms ok blank  same numbers
capture prime=true:              34ms ok real   bytes=12248 pixels=614400 opaque=614400 colours=4 differing=460800

prime is confirmed in that state: bare and ignoreOcclusion are useless there — both return a blank picture, and ignoreOcclusion only cuts the 1373 ms blank answer to 40 ms — while prime returns a real four-colour picture in 34 ms.

The dangerous part is the blank answer. In that state the capture answers ok: true with a valid PNG — 12214 bytes, 614400 opaque pixels, one colour, zero differing pixels — instead of the no-completion error. ok promises a picture, not that the page drew anything. That image is indistinguishable from a genuinely blank page by looking at it alone, so a caller that treats ok as "content captured" saves a solid-colour screenshot and reports success. A probe that must have real content uses prime and checks the pixels for itself; ok alone is not that check.

A platform whose webview cannot render a picture is not a stale adapter, so it answers not-implemented instead of unknown-adapter and never falls back to a screenshot of the screen: requires: "-[WKWebView takeSnapshotWithConfiguration:completionHandler:]", with the message "window.capture renders the page with the webview itself, which this platform does not expose" and the adapter that supplies it on macOS named. That is the answer on Windows today; Linux supplies it through WebKitGTK's webkit_web_view_get_snapshot and refuses with a named handler-error when the view has not been realised, rather than returning an empty PNG. A label with no webview is unknown-window (the darwin adapter names it "no open window labelled … to capture"), and handler-error also covers a platform answer without all four fields, an empty PNG, and WebKit reporting it rendered nothing.

window-state

commandargumentsanswer
save{ label }{ ok: true, label, frame }
restore{ label }{ ok: true, label, restored, frame? }
forget{ label }{ ok: true, label, removed }
saveAllnone{ ok: true, saved, skipped }
restoreAllnone{ ok: true, restored, missing }
forgetAllnone{ ok: true, removed }

frame is { x, y, width, height }. A missing or corrupt state file is not an error: restore answers restored: false and the window opens at its configured size. saveAll names every window whose frame the platform could not read in skipped instead of aborting the rest; restoreAll names every label with no stored entry in missing; forgetAll answers the labels it dropped.

Failure codes: invalid-label, unknown-window, unknown-adapter (a partial frame adapter), handler-error; not-implemented with requires: "NSWindow" when the plugin was built without a frame adapter.

positioner

Nine commands, one per placement: center, topLeft, topRight, bottomLeft, bottomRight, topCenter, bottomCenter, leftCenter, rightCenter.

Each takes { label } and answers { ok: true, label, x, y, placed: true }. The screen frame is the main screen's visible frame, so top is the largest y and a topLeft window sits under the menu bar. A window larger than the screen keeps its negative offset: clamping would report a success the user cannot see. To choose a display before placing, screen.primary answers the display the platform marks and its workArea; those rectangles are top-left page pixels rather than the y-up frame the window calls take, so read the screen guide before feeding one to setPosition.

Failure codes: invalid-label, unknown-window, unknown-adapter (no getSize/setPosition, or no usable screen frame), handler-error; not-implemented with requires: "NSWindow" without an adapter.