Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions apps/desktop/src/preview/PickPreload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
121 changes: 121 additions & 0 deletions apps/desktop/src/preview/PickTarget.test.ts
Original file line number Diff line number Diff line change
@@ -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<FakeElement>();
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<FakeElement>();
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");
});
});
22 changes: 22 additions & 0 deletions apps/desktop/src/preview/PickTarget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
interface PickScope<E> {
elementsFromPoint(x: number, y: number): ReadonlyArray<E>;
contains(node: NoInfer<E>): 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<E extends { readonly shadowRoot: PickScope<E> | null }>(
scope: PickScope<E>,
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;
}
Loading