diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index 67a3c21c4..29c4a0f1e 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -1554,7 +1554,90 @@ function routingPage(part) { const a = Math.max(0, Math.floor(s0 / 8) * 8 - 64), z = Math.ceil(s1 / 8) * 8 + 64; return a <= 0 && z >= len ? "" : `0 ${a} ${"3 5 ".repeat((z - a) / 8)}0 ${Math.ceil(len) + 64}`; }; - $("#view-routing").addEventListener("scroll", (e) => { scrolled = e.currentTarget.scrollTop; flowSoon(); }, { passive: true }); + $("#view-routing").addEventListener("scroll", (e) => { scrolled = e.currentTarget.scrollTop; flowSoon(); if (win) placeSoon(); }, { passive: true }); + + // with hundreds of keys open (#3164) only the keys near sight are in the + // list: a run of the others is one gap as tall as they are, and a key in + // it has the box it would have there, so its wire is drawn as before. + // Each update laid out, read and moved every key, in WebKit longer than + // a frame. Scrolled half a screen on, the list is made again around it. + const VIRT_AT = 200; + let gapOf = new Map(), win = null, laid = [], listAt = 0, viewH = 0, listGap = 7, placeAsked = 0; + const laidHs = new WeakMap(), usual = new Map(); // a li → { h, nobar } as last laid out; nobar → a key's height + const isKey = (e) => !e.classList.contains("rt-fold") && !e.classList.contains("rt-sub"); + const heightOf = (e) => { + const nobar = e.classList.contains("nobar"), m = laidHs.get(e); + return m && m.nobar === nobar ? m.h : usual.get(nobar) ?? usual.get(!nobar) ?? 38; + }; + function place(els) { + laid = els; + gapOf = new Map(); + win = null; + let n = 0; + for (const e of els) if (isKey(e)) n++; + if (n < VIRT_AT) return els; + const m = viewH || innerHeight, lo = scrolled - listAt - m, hi = scrolled - listAt + (viewH || innerHeight) + m; + win = { lo, hi }; + const out = []; + let y = 0, run = null; + // a gap is new each time, never one used for other rows: app.js's + // click hold may keep a clicked row's neighbour where it was, and a + // gap standing for other rows would take the view with it + const end = () => { + if (!run) return; + const g = el("li", "rt-gap"); + g.setAttribute("aria-hidden", "true"); + g.style.height = Math.max(0, run.h - listGap) + "px"; + run.g = g; + out.push(g); + run = null; + }; + for (const e of els) { + const h = heightOf(e); + if (isKey(e) && (y + h < lo || y > hi)) { + if (!run) run = { g: null, h: 0 }; + gapOf.set(e, { run, off: run.h, h }); + run.h += h + listGap; + } else { end(); out.push(e); } + y += h + listGap; + } + end(); + return out; + } + // put puts what is to be in the list there, moving only the nodes out of + // order: moving a node restarts what it plays, and the list made again + // around a scroll put every key back, a fold held where it was clicked + // moving again with them + function put(out) { + const mine = new Set(out); + for (const li of [...list.children]) if (!mine.has(li)) li.remove(); + let at = list.firstElementChild; + for (const e of out) { + if (e === at) at = at.nextElementSibling; + else list.insertBefore(e, at); + } + } + const placeSoon = () => { if (!placeAsked) placeAsked = requestAnimationFrame(() => { + placeAsked = 0; + if (!win || !list.isConnected) return; + // read again: a fold opened is held where it was by a scrollTop set + // after the layout that asked, and the scroll event of it can come a + // frame after this one, the list left around where the view was + scrolled = $("#view-routing").scrollTop; + const m = (viewH || innerHeight) / 2, lo = scrolled - listAt; + if (lo - m >= win.lo && lo + viewH + m <= win.hi) return; + // what is in sight stays where it is, the gaps above it as tall as + // they guess or not + const vt = $("#view-routing").getBoundingClientRect().top; + const anchor = [...list.children].find((e) => !e.classList.contains("rt-gap") && e.getBoundingClientRect().bottom > vt); + const was = anchor?.getBoundingClientRect().top; + put(place(laid)); + if (anchor?.isConnected) { + const d = anchor.getBoundingClientRect().top - was; + if (Math.abs(d) >= 1) $("#view-routing").scrollTop += d; + } + layout(); + }); }; function layout() { const r = stage.getBoundingClientRect(); @@ -1565,7 +1648,13 @@ function routingPage(part) { const boxes = new Map(), sets = []; const b = (e) => { let v = boxes.get(e); - if (!v) { const x = e.getBoundingClientRect(); boxes.set(e, v = { l: x.left - r.left, r: x.right - r.left, t: x.top - r.top, b: x.bottom - r.top, cx: (x.left + x.right) / 2 - r.left, cy: (x.top + x.bottom) / 2 - r.top }); } + if (v) return v; + const at = !e.isConnected && gapOf.get(e); + if (at) { // a key in a gap: where it would be in it + const g = b(at.run.g), l = g.l + (parseFloat(e.style.getPropertyValue("--depth")) || 0) * 18, t = g.t + at.off; + v = { l, r: g.r, t, b: t + at.h, cx: (l + g.r) / 2, cy: t + at.h / 2 }; + } else { const x = e.getBoundingClientRect(); v = { l: x.left - r.left, r: x.right - r.left, t: x.top - r.top, b: x.bottom - r.top, cx: (x.left + x.right) / 2 - r.left, cy: (x.top + x.bottom) / 2 - r.top }; } + boxes.set(e, v); return v; }; const h = b(hub), low = b(srcs).b; @@ -1604,7 +1693,7 @@ function routingPage(part) { } for (const row of rows.values()) { // a key folded away has no wire: its fold's is the way to it - if (!row.li.isConnected) { sets.push([row.wire, null]); continue; } + if (!row.li.isConnected && !gapOf.has(row.li)) { sets.push([row.wire, null]); continue; } const f = row.fold && folds.get(row.fold); const up = f?.open ? f : row.up && subs.get(row.up), a = b(row.li); sets.push([row.wire, up ? fromSub(up, a) : fromHub(a)]); @@ -1615,6 +1704,17 @@ function routingPage(part) { // read before the wires are set, which lays the stage out again const view = $("#view-routing"), v = view.getBoundingClientRect(); scrolled = view.scrollTop; + // what place() lays the next list out by: the list's top in the view's + // content, and each li's height as it is now + listAt = b(list).t + r.top - v.top + scrolled; + viewH = v.height; + listGap = parseFloat(getComputedStyle(list).rowGap) || 0; + for (const e of list.children) { + if (e.classList.contains("rt-gap") || e.classList.contains("idle")) continue; + const x = b(e), nobar = e.classList.contains("nobar"); + laidHs.set(e, { h: x.b - x.t, nobar }); + if (isKey(e) && e.firstElementChild?.checkVisibility?.({ contentVisibilityAuto: true })) usual.set(nobar, x.b - x.t); + } sight = { at: r.top, scroll: scrolled, lo: Math.max(0, v.top), hi: Math.min(innerHeight, v.bottom) }; wires.setAttribute("viewBox", `0 0 ${r.width} ${r.height}`); sky.setAttribute("viewBox", `0 0 ${r.width} ${r.height}`); @@ -1623,6 +1723,9 @@ function routingPage(part) { else if (p.getAttribute("d") !== d) p.setAttribute("d", d); } flowSoon(); + // the view moved by more than a scroll told of (a row held in place, + // the stage grown under it): the list made again around where it is + if (win) placeSoon(); holdAll(); } @@ -2027,12 +2130,7 @@ function routingPage(part) { } for (const [k, s] of subs) if (!want.has(k)) { s.li.remove(); s.wire.remove(); subs.delete(k); } for (const [k, f] of folds) if (!want.has("\u0001" + k)) { f.li.remove(); f.wire.remove(); folds.delete(k); } - const mine = new Set(els); - for (const li of [...list.children]) if (!mine.has(li)) li.remove(); - // moved only when the order changed: moving a node restarts what it plays - if (els.some((e, i) => list.children[i] !== e) || list.children.length !== els.length) { - for (const e of els) list.append(e); - } + put(place(els)); // FLIP: from where each was to its new place let moved = false; const placed = [...[...rows].map(([id, row]) => [id, row.li]), ...[...folds].map(([k, f]) => ["\u0001" + k, f.li])].filter(([, li]) => li.isConnected); @@ -4065,6 +4163,7 @@ function routingPage(part) { chip.hidden = true; headOf = null; hubText(); + win = null; laid = []; gapOf = new Map(); list.replaceChildren(el("li", "idle", t(purpose.length || day ? "No requests match these filters." : "No request yet"))); say(t("Every request an agent sends to magpie shows up here, routed for real.")); log.hidden = true; @@ -6954,7 +7053,12 @@ function routingPage(part) { return b; } - new ResizeObserver(() => layout()).observe(stage); + // laid out in the next frame, not inside this round of the observers: + // layout() reads the rows, and with hundreds of keys the rows coming into + // sight change #rt's height a fraction of a pixel, which WebKit reports + // as a ResizeObserver loop (#3164) + let stageAsked = 0; + new ResizeObserver(() => { if (!stageAsked) stageAsked = requestAnimationFrame(() => { stageAsked = 0; layout(); }); }).observe(stage); // the list is as tall as leaves the stage in sight above it: the reader // scrolls the list, not the page, and a request picked plays in view. // Its top stays where it is as it is sized, so a row just clicked does too diff --git a/internal/gui/assets/plugins/routing/routing.css b/internal/gui/assets/plugins/routing/routing.css index 4e7d5b430..ac0221e9a 100644 --- a/internal/gui/assets/plugins/routing/routing.css +++ b/internal/gui/assets/plugins/routing/routing.css @@ -59,6 +59,8 @@ layout). A row a 429 hit keeps its tag up over its top edge, which this would cut, so it is drawn whole */ .rt-accts li:not(.hit) { content-visibility: auto; contain-intrinsic-size: auto var(--rt-row, 38px); } +/* a run of keys far from sight, out of the list (client.js place, #3164) */ +.rt-accts li.rt-gap { display: block; padding: 0; border: 0; background: none; content-visibility: visible; contain: strict; pointer-events: none; } /* a group in the group: a heading its models hang from, set in under it */ .rt-accts li.rt-sub { display: flex; align-items: center; gap: 7px; padding: 5px 10px; font-size: 12px; color: var(--muted); background: var(--card-2); border-style: dashed; } .rt-accts li.rt-sub.on { border-style: solid; border-color: var(--fg-2); color: var(--fg); } diff --git a/internal/gui/tests/routing-many-keys.test.cjs b/internal/gui/tests/routing-many-keys.test.cjs index d7bbfb5a1..8dc42bcb2 100644 --- a/internal/gui/tests/routing-many-keys.test.cjs +++ b/internal/gui/tests/routing-many-keys.test.cjs @@ -14,7 +14,11 @@ // in the same pass. And no wire is one cubic thousands of pixels tall: WebKit // dashes a cubic at a cost that grows faster than its length, and the // flowing wire to a fold hundreds of rows from magpie took ~75 ms of each -// frame. English, Chinese and German; the API is faked here. +// frame. And only the keys near sight are in the list: each update laid +// out, read and moved all 800, longer than a frame in WebKit. The rest are +// gaps as tall as they are, their wires still drawn to where they would +// be, and scrolled to, the last key is there with its wire ending at it. +// English, Chinese and German; the API is faked here. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); const path = require("node:path"); @@ -113,7 +117,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", const folded = await look(); // both folds opened, each key a row of its own on the stage while (await page.evaluate(() => { const f = document.querySelector("#view-routing .rt-fold:not(.open)"); f?.click(); return !!f; })); - await page.waitForFunction(() => document.querySelectorAll("#view-routing .rt-accts > li:not(.rt-fold)").length >= 800); + await page.waitForFunction(() => document.querySelectorAll("#view-routing .rt-wires path[d]").length >= 800); const opened = await look(); clearInterval(pump); // the tallest wire, and the tallest cubic any wire is drawn with @@ -135,21 +139,66 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", } return { wire: Math.round(wire), cubic: Math.round(cubic) }; }); - // a key far below the fold of the window is not drawn, and is as tall - // as the keys drawn: drawn, WebKit painted all 800 again on each frame - // a magpie flew, ~40 frames a second - const rows = await page.evaluate(() => new Promise((done) => requestAnimationFrame(() => requestAnimationFrame(() => { - const keys = [...document.querySelectorAll("#view-routing .rt-accts > li:not(.rt-fold):not(.rt-sub):not(.idle):not(.hit)")]; + // a key far below the fold of the window is not in the list, and one + // in the list not in sight is not drawn and is as tall as the keys + // drawn: drawn, WebKit painted all 800 again on each frame a magpie + // flew, ~40 frames a second + const KEYS = "#view-routing .rt-accts > li:not(.rt-fold):not(.rt-sub):not(.rt-gap):not(.idle)"; + const rowsNow = () => page.evaluate((KEYS) => new Promise((done) => requestAnimationFrame(() => requestAnimationFrame(() => { + const all = [...document.querySelectorAll(KEYS)], keys = all.filter((li) => !li.classList.contains("hit")); const drawn = (li) => li.firstElementChild.checkVisibility({ contentVisibilityAuto: true }); const mid = (li) => { const r = li.getBoundingClientRect(); return Math.abs((r.top + r.bottom) / 2 - innerHeight / 2); }; const seen = keys.reduce((a, li) => (mid(li) < mid(a) ? li : a)); const h = (li) => Math.round(li.getBoundingClientRect().height); - done({ shown: mid(seen) < innerHeight / 2 && drawn(seen), last: drawn(keys[keys.length - 1]), heights: [...new Set(keys.map(h))] }); - })))); + // the wire to each key in sight ends at the middle of its left edge + const st = document.querySelector("#view-routing .rt-stage").getBoundingClientRect(); + const ends = [...document.querySelectorAll("#view-routing .rt-wires path[d]")].map((p) => { const n = p.getAttribute("d").match(/-?[\d.]+(?:e-?\d+)?/g).map(Number); return [n[n.length - 2] + st.left, n[n.length - 1] + st.top]; }); + const inSight = keys.filter((li) => { const r = li.getBoundingClientRect(); return r.bottom > 0 && r.top < innerHeight; }); + const unwired = inSight.filter((li) => { const r = li.getBoundingClientRect(); return !ends.some(([x, y]) => Math.abs(x - r.left) < 1.5 && Math.abs(y - (r.top + r.bottom) / 2) < 1.5); }).map((li) => li.title); + done({ shown: mid(seen) < innerHeight / 2 && drawn(seen), notDrawn: keys.filter((li) => !drawn(li)).length, listed: keys.length, inSight: inSight.length, unwired, wires: ends.length, + last: all.some((li) => li.title === "solpool2#k299" && drawn(li)), heights: [...new Set(keys.map(h))] }); + }))), KEYS); + const rows = await rowsNow(); t.diagnostic(`folded ${JSON.stringify(folded)}, open ${JSON.stringify(opened)}, wires ${JSON.stringify(tall)}, rows ${JSON.stringify(rows)}`); assert.equal(rows.shown, true, "a key in sight is drawn"); assert.equal(rows.last, false, "the last of 800 keys, out of sight, is not drawn"); + assert(rows.listed < 120, `${rows.listed} of 800 keys in the list`); assert.equal(rows.heights.length, 1, `keys drawn and not are one height: ${rows.heights}`); + assert(rows.wires >= 800, `${rows.wires} wires: each key has one, in the list or not`); + assert.deepEqual(rows.unwired, [], "a wire ends at each key in sight"); + // scrolled to the foot, the last key is there, drawn, with its wire + await page.mouse.move(700, 500); + // by the wheel, as a reader would: on until the last key is in the + // list, then to it + for (let k = 0; k < 80; k++) { + const at = await page.evaluate(() => [...document.querySelectorAll("#view-routing .rt-accts > li")].find((li) => li.title === "solpool2#k299")?.getBoundingClientRect().top); + if (at !== undefined && Math.abs(at - 400) < 150) break; + await page.mouse.wheel(0, at === undefined ? 3000 : Math.max(-3000, Math.min(3000, at - 400))); + await page.waitForTimeout(at === undefined ? 50 : 300); + } + await page.waitForTimeout(300); + const foot = await rowsNow(); + t.diagnostic(`at the foot ${JSON.stringify({ ...foot, unwired: foot.unwired.length })}`); + assert.equal(foot.last, true, "scrolled to, the last key is drawn"); + assert(foot.inSight > 5, `${foot.inSight} keys in sight at the foot`); + assert.deepEqual(foot.unwired, [], "a wire ends at each key in sight at the foot"); + // the second fold, hundreds of keys down, closed and opened again + // where it is clicked stays there, the list made again around it + const fold2 = () => page.evaluate(() => { const f = document.querySelectorAll("#view-routing .rt-fold")[1]; const r = f?.getBoundingClientRect(); return r && { top: r.top, x: r.left + 20, y: (r.top + r.bottom) / 2 }; }); + for (let k = 0; k < 80; k++) { + const f = await fold2(); + if (f && Math.abs(f.top - 300) < 150) break; + await page.mouse.wheel(0, !f ? -3000 : Math.max(-3000, Math.min(3000, f.top - 300))); + await page.waitForTimeout(!f ? 50 : 300); + } + await page.waitForTimeout(300); + for (const was of ["open", "closed"]) { + const f = await fold2(); + await page.mouse.click(f.x, f.y); + await page.waitForTimeout(800); + const after = await fold2(); + assert(after && Math.abs(after.top - f.top) < 2, `the second fold, ${was} and clicked, moved from ${f.top} to ${after?.top}`); + } for (const [name, n] of [["folded", folded], ["open", opened]]) { // a key's place is looked up in an index of its provider's list: // before, a sort of 800 keys looked through the 500 for each of @@ -163,7 +212,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", // dozen a second are the stage drawn anew as requests come. assert(n.thrash < 400, `${name}: the stage laid out again ${n.thrash} times for a wire drawn, in 3s`); } - assert(opened.boxes > 800, `the open stage was laid out (${opened.boxes} boxes read)`); + assert(opened.boxes > 100, `the open stage was laid out (${opened.boxes} boxes read)`); // a wire to a fold far from magpie runs thousands of pixels, in // pieces of its curve: before, it was one cubic as tall as itself assert(tall.wire > 5000, `the tallest wire is ${tall.wire}px`);