diff --git a/apps/desktop/src/preview/PickPreload.ts b/apps/desktop/src/preview/PickPreload.ts index dcb16b851dc23..759838c521b9d 100644 --- a/apps/desktop/src/preview/PickPreload.ts +++ b/apps/desktop/src/preview/PickPreload.ts @@ -21,6 +21,7 @@ import type { import { createAnnotationIdSource } from "./AnnotationIds.ts"; import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts"; import { previewAnnotationStyles } from "./AnnotationStyles.generated.ts"; +import { pickElementAtPoint } from "./PickTarget.ts"; import { installRecordingCursor } from "./RecordingCursor.ts"; import { DEFAULT_RECORDING_INPUT_OPTIONS } from "./RecordingInput.ts"; import { @@ -279,13 +280,16 @@ function isAnnotationNode(element: Element): boolean { } function pickFromPoint(clientX: number, clientY: number): Element | null { - for (const candidate of document.elementsFromPoint(clientX, clientY)) { - if (!(candidate instanceof Element)) continue; - if (isAnnotationNode(candidate) || candidate.hasAttribute(INPUT_LAYER_ATTRIBUTE)) continue; - if (candidate === document.documentElement || candidate === document.body) continue; - return candidate; - } - return null; + return pickElementAtPoint( + document, + clientX, + clientY, + (element) => + isAnnotationNode(element) || + element.hasAttribute(INPUT_LAYER_ATTRIBUTE) || + element === document.documentElement || + element === document.body, + ); } function describeRawElement(element: Element): string { diff --git a/apps/desktop/src/preview/PickTarget.test.ts b/apps/desktop/src/preview/PickTarget.test.ts new file mode 100644 index 0000000000000..cb245e9565a1c --- /dev/null +++ b/apps/desktop/src/preview/PickTarget.test.ts @@ -0,0 +1,121 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { pickElementAtPoint } from "./PickTarget.ts"; + +// Models the DOM contract the picker relies on: hit-testing covers the whole page, and each +// `elementsFromPoint` retargets the hits against the scope it is called on, like Blink does. +class FakeElement { + shadowRoot: FakeScope | null = null; + readonly label: string; + readonly scope: FakeScope; + + constructor(label: string, scope: FakeScope) { + this.label = label; + this.scope = scope; + scope.members.add(this); + } +} + +class FakeScope { + readonly members = new Set(); + readonly host: FakeElement | null; + private readonly page: FakePage; + + constructor(page: FakePage, host: FakeElement | null) { + this.page = page; + this.host = host; + } + + contains(element: FakeElement): boolean { + return this.members.has(element); + } + + elementsFromPoint(): FakeElement[] { + const elements: FakeElement[] = []; + for (const hit of this.page.hits) { + const element = this.retarget(hit); + if (elements.at(-1) !== element) elements.push(element); + } + if (elements.at(-1) !== this.page.html) elements.push(this.page.html); + return elements; + } + + private retarget(target: FakeElement): FakeElement { + let element = target; + while (element.scope.host && !element.scope.encloses(this)) element = element.scope.host; + return element; + } + + private encloses(scope: FakeScope): boolean { + for (let current: FakeScope | null = scope; current; current = current.host?.scope ?? null) { + if (current === this) return true; + } + return false; + } +} + +class FakePage { + hits: FakeElement[] = []; + readonly overlays = new Set(); + readonly document = new FakeScope(this, null); + readonly html = new FakeElement("html", this.document); + readonly body = new FakeElement("body", this.document); + + attachShadow(host: FakeElement, mode: "open" | "closed"): FakeScope { + const root = new FakeScope(this, host); + if (mode === "open") host.shadowRoot = root; + return root; + } + + pick(): FakeElement | null { + return pickElementAtPoint( + this.document, + 0, + 0, + (element) => this.overlays.has(element) || element === this.html || element === this.body, + ); + } +} + +describe("pickElementAtPoint", () => { + it("picks the element inside an open shadow root instead of its host", () => { + const page = new FakePage(); + const host = new FakeElement("shadow-demo", page.document); + const button = new FakeElement("button", page.attachShadow(host, "open")); + page.hits = [button, host, page.body, page.html]; + + expect(page.pick()?.label).toBe("button"); + }); + + it("falls back to the host when the point misses its shadow content", () => { + const page = new FakePage(); + const host = new FakeElement("shadow-demo", page.document); + page.attachShadow(host, "open"); + page.hits = [host, page.body, page.html]; + + expect(page.pick()?.label).toBe("shadow-demo"); + }); + + it("keeps a closed shadow root opaque", () => { + const page = new FakePage(); + const outerHost = new FakeElement("outer-host", page.document); + const outerRoot = page.attachShadow(outerHost, "open"); + const closedHost = new FakeElement("closed-host", outerRoot); + const secret = new FakeElement("secret", page.attachShadow(closedHost, "closed")); + page.hits = [secret, closedHost, outerHost, page.body, page.html]; + + expect(page.pick()?.label).toBe("closed-host"); + }); + + it("picks the page element beneath the picker overlay, never the overlay", () => { + const page = new FakePage(); + const host = new FakeElement("shadow-demo", page.document); + const button = new FakeElement("button", page.attachShadow(host, "open")); + const overlay = new FakeElement("overlay", page.document); + page.overlays.add(overlay); + const toolbar = new FakeElement("toolbar", page.attachShadow(overlay, "closed")); + page.hits = [toolbar, button, host, page.body, page.html]; + + expect(page.pick()?.label).toBe("button"); + }); +}); diff --git a/apps/desktop/src/preview/PickTarget.ts b/apps/desktop/src/preview/PickTarget.ts new file mode 100644 index 0000000000000..af689a07bedda --- /dev/null +++ b/apps/desktop/src/preview/PickTarget.ts @@ -0,0 +1,22 @@ +interface PickScope { + elementsFromPoint(x: number, y: number): ReadonlyArray; + contains(node: NoInfer): boolean; +} + +// Document hit-testing retargets shadow content to its host, so descend into open roots. +// ShadowRoot.elementsFromPoint still lists the host and the rest of the page (the overlay +// too), so only the root's own elements count. Closed roots expose no shadowRoot. +export function pickElementAtPoint | null }>( + scope: PickScope, + x: number, + y: number, + isExcluded: (element: E) => boolean, +): E | null { + for (const candidate of scope.elementsFromPoint(x, y)) { + if (!scope.contains(candidate) || isExcluded(candidate)) continue; + const inner = + candidate.shadowRoot && pickElementAtPoint(candidate.shadowRoot, x, y, isExcluded); + return inner ?? candidate; + } + return null; +}