Skip to content

The agent that grows with you

fix(desktop): read "is the cursor over the HUD" off the tree, not off a list

Brooklyn Nicholson717b49c08469
diff --git a/apps/desktop/src/app/hud/click-through.test.ts b/apps/desktop/src/app/hud/click-through.test.ts
new file mode 100644
index 00000000000..249286b1319
--- /dev/null
+++ b/apps/desktop/src/app/hud/click-through.test.ts
@@ -0,0 +1,71 @@
+import { beforeEach, describe, expect, it } from 'vitest'
+
+import { hudIgnoresMouse } from './click-through'
+
+/** The HUD's real shape: a React mount wrapping the shell, the bar inside it,
+ * and a portalled overlay as a sibling of the mount under <body>. */
+function hud() {
+ document.body.innerHTML = ''
+
+ const mount = document.createElement('div')
+ mount.id = 'root'
+
+ const shell = document.createElement('div')
+ shell.setAttribute('data-hud-shell', '')
+
+ const bar = document.createElement('input')
+
+ const overlay = document.createElement('div')
+ overlay.setAttribute('role', 'dialog')
+
+ shell.append(bar)
+ mount.append(shell)
+ document.body.append(mount, overlay)
+
+ return { bar, mount, overlay, shell }
+}
+
+describe('hudIgnoresMouse', () => {
+ beforeEach(() => {
+ document.body.innerHTML = ''
+ })
+
+ it('hands the mouse back over the scaffolding the HUD hangs in', () => {
+ const { mount, shell } = hud()
+
+ for (const around of [mount, document.body, document.documentElement, shell]) {
+ expect(hudIgnoresMouse(shell, around, null, true)).toBe(true)
+ }
+ })
+
+ it('keeps the window solid over the HUD and over portalled overlays', () => {
+ const { bar, overlay, shell } = hud()
+
+ expect(hudIgnoresMouse(shell, bar, null, true)).toBe(false)
+ expect(hudIgnoresMouse(shell, overlay, null, true)).toBe(false)
+ })
+
+ it('does not pin the window just because the composer holds the caret', () => {
+ const { bar, mount, shell } = hud()
+
+ expect(hudIgnoresMouse(shell, mount, bar, true)).toBe(true)
+ })
+
+ it('pins the window while a portalled overlay holds focus, so an outside click can dismiss it', () => {
+ const { mount, overlay, shell } = hud()
+
+ expect(hudIgnoresMouse(shell, mount, overlay, true)).toBe(false)
+ })
+
+ it('lets a stale focus go once the window is no longer the active one', () => {
+ const { mount, overlay, shell } = hud()
+
+ expect(hudIgnoresMouse(shell, mount, overlay, false)).toBe(true)
+ })
+
+ it('treats an unfocused document body as nothing holding focus', () => {
+ const { mount, shell } = hud()
+
+ expect(hudIgnoresMouse(shell, mount, document.body, true)).toBe(true)
+ })
+})
diff --git a/apps/desktop/src/app/hud/click-through.ts b/apps/desktop/src/app/hud/click-through.ts
index d833529b8b9..76b61069bd2 100644
--- a/apps/desktop/src/app/hud/click-through.ts
+++ b/apps/desktop/src/app/hud/click-through.ts
@@ -1,12 +1,46 @@
import { type RefObject, useEffect } from 'react'
+/**
+ * Whether the OS window should hand the mouse to whatever is behind it.
+ *
+ * `hit` is what the document reports under the cursor, `active` what holds
+ * focus; both are answered against the shell by where they sit in the tree.
+ *
+ * - Anything that CONTAINS the shell is the scaffolding the HUD hangs in — the
+ * React mount, `<body>`, the document. Those are full-window and
+ * hit-testable, so a hit on one means the cursor is over nothing. Naming them
+ * was the earlier mistake: the list had `<body>` and `<html>` and missed
+ * `#root`, so every point in the window reported something and the window
+ * never went transparent at all.
+ * - Focus BESIDE the shell is a portalled dialog, popover or menu, and it owns
+ * the next click — including the one outside itself that dismisses it, which
+ * the hit test cannot see coming. That pins the window solid.
+ * - Focus INSIDE the shell does not. The composer holding the caret is the
+ * HUD's resting state rather than a claim on the whole rectangle, and reading
+ * it as one is what made an engaged HUD eat every click in its own empty
+ * space — on a fresh thread, the entire window.
+ */
+export function hudIgnoresMouse(
+ root: Element,
+ hit: Element | null,
+ active: Element | null,
+ windowFocused: boolean
+): boolean {
+ const overSomething = hit !== null && !hit.contains(root)
+ // `windowFocused` is what stops a stale `active` — the composer keeps focus
+ // after you click away to another app — pinning the HUD solid forever.
+ const overlayFocused = windowFocused && active !== null && !root.contains(active) && !active.contains(root)
+
+ return !overSomething && !overlayFocused
+}
+
/**
* Let clicks fall through the HUD everywhere it isn't really there.
*
* The one thing about HUD mode that CSS cannot express, because it is a
- * property of the OS WINDOW rather than of the page. It reads the engaged state
- * off the DOM (`:focus-within`) rather than keeping a second copy, so there is
- * one answer to "is the HUD in use" and the stylesheet owns it.
+ * property of the OS WINDOW rather than of the page. It asks the document what
+ * is under the cursor rather than keeping a second copy of the answer, so the
+ * stylesheet stays the one place that decides what the HUD is made of.
*
* An always-on-top window eats every click inside its rectangle, visible or
* not — and most of the HUD's rectangle is a faded-out band over whatever the
@@ -14,9 +48,10 @@ import { type RefObject, useEffect } from 'react'
* page-level property, and the click never reaches the page.
*
* So the window itself is made mouse-transparent except where it is genuinely
- * interactive: wherever the cursor is over something, and whenever anything in
- * the window holds focus. `forward: true` keeps mousemove flowing while
- * ignoring, which is what lets it re-arm when the cursor comes back to the bar.
+ * interactive: wherever the cursor is over something the HUD paints, and
+ * whenever a portalled overlay holds focus. `forward: true` keeps mousemove
+ * flowing while ignoring, which is what lets it re-arm when the cursor comes
+ * back to the bar.
*/
export function useHudClickThrough(rootRef: RefObject<HTMLElement | null>): void {
useEffect(() => {
@@ -34,33 +69,14 @@ export function useHudClickThrough(rootRef: RefObject<HTMLElement | null>): void
let point: { x: number; y: number } | null = null
// Hit-test rather than enumerate. Everything the HUD doesn't want to catch
- // — the shell's dead space, the sheet, the faded band — is already
- // `pointer-events: none`, so anything the document hands back at this point
- // is something real: the bar, a control, the exit chip, a popover, a dialog.
- // Listing those instead is how links and dialogs ended up unclickable, since
- // portalled overlays live outside the shell and moving focus into one takes
- // `:focus-within` with it.
- const overSomething = () => {
- if (!point) {
- return false
- }
-
- const hit = document.elementFromPoint(point.x, point.y)
-
- return Boolean(hit) && hit !== root && hit !== document.body && hit !== document.documentElement
- }
-
- // Focus is asked of the document, not of the shell. A dialog or popover is
- // portalled to `document.body`, so focus entering one leaves the shell's
- // `:focus-within` — and a HUD that decides it is unused the moment it opens
- // a dialog goes mouse-transparent underneath it. Nothing but the HUD lives
- // in this window, so any focus at all is the HUD in use. `hasFocus` gates it
- // so a stale `activeElement` — the composer keeps it after you click away to
- // another app — can't pin the HUD solid forever.
- const focused = () => document.hasFocus() && document.activeElement !== document.body
-
+ // — the shell's dead space, the sheet, the faded band — is
+ // `pointer-events: none` in the stylesheet, so whatever the document hands
+ // back is something real: the bar, a control, the exit chip, a popover, a
+ // dialog. Listing those instead is how links and dialogs ended up
+ // unclickable, since portalled overlays live outside the shell.
const apply = () => {
- const next = !focused() && !overSomething()
+ const hit = point ? document.elementFromPoint(point.x, point.y) : null
+ const next = hudIgnoresMouse(root, hit, document.activeElement, document.hasFocus())
if (ignoring !== next) {
ignoring = next