Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

A scroll event no longer puts back an offset the page wrote past (sdk/ui's DOM layer)

shinyobjectz6d8a15ac55c6
diff --git a/sdk/ui/src/dom/element.ts b/sdk/ui/src/dom/element.ts
index 85147e37..86732e12 100644
--- a/sdk/ui/src/dom/element.ts
+++ b/sdk/ui/src/dom/element.ts
@@ -19,6 +19,8 @@ export class Element_ extends Node_ {
_props: Record<string, unknown> = {};
/** props JS set on it (attributes, scroll offsets, a field's value): React's props are frozen, these sit over them */
_extra: Record<string, unknown> = {};
+ /** scrollTop and scrollLeft values written to the host and not yet seen back in its scroll event (setScroll) */
+ _ahead?: Record<string, number[]>;
_attrs = new Map<string, string>();
_style: Record<string, unknown> = {};
_styleObj: any;
@@ -443,6 +445,10 @@ function setScroll(el: Element_, box: number[], prop: string, at: number, v: num
const last = el._extra[prop];
const send = last === v ? v + 1e-4 : v;
el._extra[prop] = send;
+ // what the host hasn't told back yet (its scroll event, input.ts): an event that carries an older one is stale
+ const q = ((el._ahead ??= {})[prop] ??= []);
+ q.push(send);
+ if (q.length > 64) q.shift();
wire.set(el.__wid!, prop, send);
}
diff --git a/sdk/ui/src/dom/input.ts b/sdk/ui/src/dom/input.ts
index adb044a5..cae2f969 100644
--- a/sdk/ui/src/dom/input.ts
+++ b/sdk/ui/src/dom/input.ts
@@ -350,14 +350,29 @@ export function hostEvent(el: Node_, ev: Ev, payload: any) {
scrolling.set(el, S.frameNo);
const b = el._box();
const s = payload ?? {};
+ // The offset the host had when it committed. The page may have written past it since (each write is sent
+ // and shown at once, setScroll): then that newer one stands, as in Chrome, where a scroll handler reads the
+ // offset as it is now. Taken over, an older one put a scroller back a step, and two kept in step (a
+ // timeline's tracks and their heads) each put the other back, every frame, for good. One the page never
+ // wrote is the host's own (a wheel, a clamp): it stands, and what was written before it is over.
+ const take = (prop: "scrollTop" | "scrollLeft", i: 4 | 5) => {
+ const got = s[prop] as number | undefined;
+ if (got === undefined) return;
+ const q = el._ahead?.[prop];
+ const k = q ? q.findIndex((x) => Math.abs(x - got) < 1e-3) : -1;
+ if (q && k >= 0) {
+ q.splice(0, k + 1);
+ if (q.length) return;
+ } else if (q) q.length = 0;
+ if (b) b[i] = got;
+ el._extra[prop] = got;
+ };
+ take("scrollLeft", 4);
+ take("scrollTop", 5);
if (b) {
- b[4] = s.scrollLeft ?? b[4];
- b[5] = s.scrollTop ?? b[5];
if (s.scrollWidth) b[6] = s.scrollWidth;
if (s.scrollHeight) b[7] = s.scrollHeight;
}
- el._extra.scrollTop = s.scrollTop;
- el._extra.scrollLeft = s.scrollLeft;
dispatchDirect(el, new Event_("scroll", { bubbles: false }));
break;
}
diff --git a/sdk/ui/test/dom.test.tsx b/sdk/ui/test/dom.test.tsx
index c60fb575..35f213b2 100644
--- a/sdk/ui/test/dom.test.tsx
+++ b/sdk/ui/test/dom.test.tsx
@@ -6,27 +6,12 @@ import { beforeAll, describe, expect, test } from "bun:test";
import { useEffect, useRef, useState } from "react";
import { install, layout } from "../src/dom";
import { createPortal, flushSync, mount } from "../src/react";
-import { flush, restart } from "../src/wire";
-import { Host } from "./host";
+import { flush } from "../src/wire";
+import { fresh, snap } from "./host";
beforeAll(() => install());
const g = globalThis as any;
-/** A snapshot with these boxes (by data-testid): [x, y, w, h] and optional scroll [sl, st, sw, sh]. */
-function snap(host: Host, boxes: Record<string, number[]>) {
- const n: number[] = [];
- for (const [t, b] of Object.entries(boxes)) {
- const id = host.tid(t);
- const [x, y, w, h, sl = 0, st = 0, sw = w, sh = h] = b;
- n.push(id, x, y, w, h, sl, st, sw, sh, 0, 0, 0, 0, 0, 0, 0, 0, 0);
- }
- layout(JSON.stringify({ vw: 800, vh: 600, dpr: 2, n }));
-}
-function fresh() {
- restart();
- const host = new Host();
- return host;
-}
const press = (x: number, y: number, detail = 1) => ({ x: 0, y: 0, clientX: x, clientY: y, button: 0, detail });
describe("P18 DOM surface", () => {
@@ -72,33 +57,6 @@ describe("P18 DOM surface", () => {
g.__flushTest?.();
});
- test("geometry from the snapshot; scroll writes to the host", async () => {
- const host = fresh();
- let el: any;
- mount(
- <div data-testid="root">
- <div data-testid="box" className="overflow-y-auto" ref={(e: any) => (el = e)}>
- <div data-testid="inner" />
- </div>
- </div>,
- 0,
- { dom: true },
- );
- snap(host, { root: [0, 0, 800, 600], box: [10, 20, 200, 100, 0, 0, 200, 500], inner: [10, 20, 200, 500] });
- const r = el.getBoundingClientRect();
- expect([r.x, r.y, r.width, r.height, r.right, r.bottom]).toEqual([10, 20, 200, 100, 210, 120]);
- expect(el.clientHeight).toBe(100);
- expect(el.scrollHeight).toBe(500);
- expect(g.innerWidth).toBe(800);
- expect(g.devicePixelRatio).toBe(2);
- const from = host.batches.length;
- el.scrollTop = 1000;
- expect(el.scrollTop).toBe(400);
- await Promise.resolve();
- const ops = host.since(from);
- expect(ops.some((o: any) => o.op === "update" && o.set?.scrollTop === 400)).toBe(true);
- });
-
test("a box with its own z-index stays under the overflow clip of the box it's in; a fixed one doesn't", () => {
const host = fresh();
function App() {
diff --git a/sdk/ui/test/host.ts b/sdk/ui/test/host.ts
index 6a8aa2c1..60c66b0a 100644
--- a/sdk/ui/test/host.ts
+++ b/sdk/ui/test/host.ts
@@ -3,7 +3,8 @@
// strict where the host is lenient (an op on an id it doesn't know, a second create of an id, a listen twice, an
// update with nothing in it all throw), so a driver's mistake fails a test instead of passing quietly. Events go back
// through the wire's own `__ui_deliver`, as the host delivers them.
-import { OPS_VERSION, type Ev, type Op } from "../src/wire";
+import { layout } from "../src/dom";
+import { OPS_VERSION, restart, type Ev, type Op } from "../src/wire";
export type N = { kind: string; props: Record<string, unknown>; text?: string; on: Set<Ev>; children: number[]; parent?: number };
@@ -120,3 +121,20 @@ export class Host {
}
export const kinds = (ops: Op[]) => ops.reduce((a: Record<string, number>, o) => ((a[o.op] = (a[o.op] ?? 0) + 1), a), {});
+
+/** A wire started over and a new stand-in host for it. */
+export function fresh() {
+ restart();
+ return new Host();
+}
+
+/** A layout snapshot with these boxes (by data-testid): [x, y, w, h] and optional scroll [sl, st, sw, sh]. */
+export function snap(host: Host, boxes: Record<string, number[]>) {
+ const n: number[] = [];
+ for (const [t, b] of Object.entries(boxes)) {
+ const id = host.tid(t);
+ const [x, y, w, h, sl = 0, st = 0, sw = w, sh = h] = b;
+ n.push(id, x, y, w, h, sl, st, sw, sh, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+ }
+ layout(JSON.stringify({ vw: 800, vh: 600, dpr: 2, n }));
+}
diff --git a/sdk/ui/test/scroll.test.tsx b/sdk/ui/test/scroll.test.tsx
new file mode 100644
index 00000000..6b843562
--- /dev/null
+++ b/sdk/ui/test/scroll.test.tsx
@@ -0,0 +1,109 @@
+// Scrolling on the stand-in host (docs/ts-native.feature P18, src/dom): a scroll the page writes goes to the host and
+// shows at once, and the host's scroll event, which comes a frame late, never puts back an offset the page wrote past.
+import { beforeAll, describe, expect, test } from "bun:test";
+import { install } from "../src/dom";
+import { mount } from "../src/react";
+import { fresh, snap } from "./host";
+
+beforeAll(() => install());
+const g = globalThis as any;
+
+describe("P18 DOM surface: scrolling", () => {
+ test("geometry from the snapshot; scroll writes to the host", async () => {
+ const host = fresh();
+ let el: any;
+ mount(
+ <div data-testid="root">
+ <div data-testid="box" className="overflow-y-auto" ref={(e: any) => (el = e)}>
+ <div data-testid="inner" />
+ </div>
+ </div>,
+ 0,
+ { dom: true },
+ );
+ snap(host, { root: [0, 0, 800, 600], box: [10, 20, 200, 100, 0, 0, 200, 500], inner: [10, 20, 200, 500] });
+ const r = el.getBoundingClientRect();
+ expect([r.x, r.y, r.width, r.height, r.right, r.bottom]).toEqual([10, 20, 200, 100, 210, 120]);
+ expect(el.clientHeight).toBe(100);
+ expect(el.scrollHeight).toBe(500);
+ expect(g.innerWidth).toBe(800);
+ expect(g.devicePixelRatio).toBe(2);
+ const from = host.batches.length;
+ el.scrollTop = 1000;
+ expect(el.scrollTop).toBe(400);
+ await Promise.resolve();
+ const ops = host.since(from);
+ expect(ops.some((o: any) => o.op === "update" && o.set?.scrollTop === 400)).toBe(true);
+ });
+
+ test("a scroll event carrying an offset older than one the page wrote since leaves the newer one; the host's own stands", () => {
+ const host = fresh();
+ let el: any;
+ const heard: number[] = [];
+ mount(
+ <div data-testid="root">
+ <div data-testid="box" className="overflow-y-auto" ref={(e: any) => (el = e)} onScroll={(e: any) => heard.push(e.currentTarget.scrollTop)}>
+ <div data-testid="inner" />
+ </div>
+ </div>,
+ 0,
+ { dom: true },
+ );
+ snap(host, { root: [0, 0, 800, 600], box: [10, 20, 200, 100, 0, 0, 200, 500], inner: [10, 20, 200, 500] });
+ el.scrollTop = 50;
+ el.scrollTop = 120;
+ // the host's event for the first write, after the second: the page's 120 stands, and its handler reads 120
+ host.send("box", "scroll", { scrollTop: 50, scrollLeft: 0, scrollHeight: 500, scrollWidth: 200, clientHeight: 100, clientWidth: 200 });
+ expect(el.scrollTop).toBe(120);
+ host.send("box", "scroll", { scrollTop: 120, scrollLeft: 0, scrollHeight: 500, scrollWidth: 200, clientHeight: 100, clientWidth: 200 });
+ expect(el.scrollTop).toBe(120);
+ // a wheel the page never wrote: the host's offset stands
+ host.send("box", "scroll", { scrollTop: 300, scrollLeft: 0, scrollHeight: 500, scrollWidth: 200, clientHeight: 100, clientWidth: 200 });
+ expect(el.scrollTop).toBe(300);
+ expect(heard).toEqual([120, 120, 300]);
+ });
+
+ test("two scrollers kept in step both ways settle, though the host's scroll events come a frame late", async () => {
+ // (a timeline's tracks and heads: they went round for good in raea's window, a drag's offsets coming back in turn)
+ const host = fresh();
+ let a: any, b: any;
+ // each mirrors the other, as a timeline's tracks and heads did
+ mount(
+ <div data-testid="root">
+ <div data-testid="a" className="overflow-y-auto" ref={(e: any) => (a = e)} onScroll={() => b.scrollTop !== a.scrollTop && (b.scrollTop = a.scrollTop)}>
+ <div data-testid="ai" />
+ </div>
+ <div data-testid="b" className="overflow-hidden" ref={(e: any) => (b = e)} onScroll={() => a.scrollTop !== b.scrollTop && (a.scrollTop = b.scrollTop)}>
+ <div data-testid="bi" />
+ </div>
+ </div>,
+ 0,
+ { dom: true },
+ );
+ snap(host, { root: [0, 0, 800, 600], a: [0, 0, 200, 100, 0, 0, 200, 800], ai: [0, 0, 200, 800], b: [200, 0, 100, 100, 0, 0, 100, 800], bi: [200, 0, 100, 800] });
+ // a frame: what the page wrote is committed, and each scroller it moved gets its scroll event a frame later, with
+ // the offset the commit left it at (runtime/dom commit.rs)
+ let from = host.batches.length;
+ let late: [number, number][] = [];
+ const frame = async () => {
+ await Promise.resolve();
+ const moved = new Map<number, number>();
+ for (const o of host.since(from) as any[]) if (o.op === "update" && typeof o.set?.scrollTop === "number") moved.set(o.id, o.set.scrollTop);
+ from = host.batches.length;
+ const due = late;
+ late = [...moved];
+ for (const [id, top] of due) host.send(id, "scroll", { scrollTop: top, scrollLeft: 0, scrollHeight: 800, scrollWidth: 200, clientHeight: 100, clientWidth: 200 });
+ };
+ // a drag in five quick steps, a frame each, then the frames after it
+ for (const top of [50, 100, 150, 200, 250]) {
+ a.scrollTop = top;
+ await frame();
+ }
+ // (as in Chrome, whose scroll events also come a frame late to handlers that read the offsets as they are now,
+ // a sync both ways can lose a drag's last steps; it settles, where an older offset taken over kept it going round)
+ let f = 0;
+ while (late.length && f++ < 20) await frame();
+ expect(late).toEqual([]);
+ expect(a.scrollTop).toBe(b.scrollTop);
+ });
+});