diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index ad1d8e6f1..f3063b456 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -8632,8 +8632,11 @@ $("#modal").addEventListener("keydown", (e) => { e.preventDefault(); (e.shiftKey ? last : first).focus(); }); -// A dialog opens centred, then stays put while what it holds grows or -// shrinks (a fold opening, an error line, a list filling): it holds the +// A dialog opens centred, and stays centred while what it holds fills in +// before the reader has done anything in it (a plugin's README loading +// under a dialog opened short read as a dialog sat low, the owner 10-24). +// From the reader's first press or key in it, it stays put while what it +// holds grows or shrinks (a fold opening, an error line, a list filling): it holds the // place of whatever the reader last pressed or focused in it, and grows // and shrinks round that, down to the window's foot, where it stays and // its fields scroll as a full-height dialog's do. Where there is no @@ -8645,7 +8648,7 @@ $("#modal").addEventListener("keydown", (e) => { // a cap on its height. A change made in the page is held before it is // drawn (a mutation's microtask); one that only resizes (an animation, the // window) a frame later, as an observer can't resize what it observes. -const modalHold = { off: 0, at: null, y: 0, cap: null, capSet: "" }; +const modalHold = { off: 0, at: null, y: 0, cap: null, capSet: "", touched: false }; // how far down the dialog an element sits, to the fraction of a pixel, // what moves it in an animation (a row coming in) left out. An element // that scales (a Copy button's pop, a segment pressed) is measured by its @@ -8692,6 +8695,20 @@ function holdModal(later) { const want = modalHold.off + (y == null ? 0 : modalHold.y - y); const c = getComputedStyle(m), h = d.getBoundingClientRect().height; const space = m.clientHeight - parseFloat(c.paddingTop) - parseFloat(c.paddingBottom); + if (!modalHold.touched) { + // untouched: centred, as it opened, over what it holds now; measured + // by its layout, as content that comes while it opens is measured + // under the open's scale + modalHold.off = Math.max(0, (space - Math.min(d.offsetHeight, space)) / 2); + modalHold.cap = null; + // no cap from its place: capped by it, a dialog growing from short would + // take a frame a step to reach the middle + d.style.top = modalHold.off + "px"; + d.style.maxHeight = "100%"; + modalHold.capSet = ""; + if (y != null) modalHold.y = modalDepth(modalHold.at, d) ?? y; + return; + } // grown to the window's foot it stays, capped there, its fields scrolling; // it moves up only for a window grown too short to show half of it modalHold.off = Math.max(0, Math.min(want, Math.max(space - h, modalHold.off), space / 2)); @@ -8714,7 +8731,9 @@ function holdModal(later) { new ResizeObserver(later).observe(d); new ResizeObserver(later).observe(m); new MutationObserver(() => holdModal()).observe(d, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ["class", "hidden", "open"] }); - m.addEventListener("pointerdown", (e) => modalMark(e.target), true); + // the reader's own press or key: from here the dialog holds its place + m.addEventListener("pointerdown", (e) => { modalHold.touched = true; modalMark(e.target); }, true); + m.addEventListener("keydown", () => { modalHold.touched = true; }, true); m.addEventListener("focusin", (e) => modalMark(e.target), true); // a scroll moves the marked element, not the dialog m.addEventListener("scroll", () => modalMark(modalHold.at), true); @@ -8761,6 +8780,7 @@ function openModal(content) { d.style.top = d.style.maxHeight = ""; modalHold.off = d.offsetTop - parseFloat(getComputedStyle(m).paddingTop); modalHold.at = modalHold.cap = null; + modalHold.touched = false; m.classList.add("held"); d.style.top = modalHold.off + "px"; d.style.maxHeight = modalHold.capSet = modalCap(); diff --git a/internal/gui/core_budget_test.go b/internal/gui/core_budget_test.go index f813e67be..184a3d3fd 100644 --- a/internal/gui/core_budget_test.go +++ b/internal/gui/core_budget_test.go @@ -15,8 +15,8 @@ import ( // so a page can't quietly grow back into the core; a change that needs more // raises them and says why in its commit. const ( - appScriptBudget = 1_343_324 // app.js: 1,198,239 after Usage left (P2); 1,218,098 with the 10-15 fixes to pages still in the core; 1,232,466 with the provider editor's Advanced fold and every dialog holding its place, the core's own (10-16); 1,250,533 with the quota refresh kept through redraws, the core's own since the tray panel draws it too, plus 1.5% (#2397); 1,269,083 with Free first's option in the provider editor's ROUTINGS, which the Routing page reads too, plus 1.5%; 1,272,726 with the Providers page drawing its last list at once (#1519), the core's page, and the Routing trace's account names read without Object.entries (#1481), plus 1.5%; a dialog growing out of the row that opened it and folding back into it, the core's own since every page's dialogs open through it (10-20); Codex's and Claude Code's own settings moved from Settings into the agent's row on the Agents page, the core's own since the tray panel and the window both draw them (10-20); 1,314,432 with Settings › Privacy's record of what was masked (its summary, the records view and the local model's controls), which belongs to Settings, the core's own, plus 1.5%; 1,336,481 with a provider's Names & levels saying Provider in model names is Settings' and opening it there (joe), and Settings' Image recognition saying where a custom provider's model is marked (nexnull), both in the core's own pages, with no room added (10-23); 1,337,633 with Settings › Models › Replies' Intent classifier timeout (#2934, reyoung), Settings' own row, drawn by waitRow, which the Image generation timeout's row now shares, with no room added; 1,337,735 with the provider editor's link naming a partner's page by its own host (siteHost) instead of usemagpie.ai's /go link, the core's own editor, with no room added (10-23); 1,338,084 with Check in now saying what the press came to (claimed +credits, already today, not available yet) where it said "refreshed" (#3326), the check-in row the tray panel draws too, the core's own, with no room added (10-23); 1,338,929 with a new partner's tile keeping a small dot until it is opened, in the add sheet, the core's own, with no room added (10-23); 1,343,324 with the sidebar's open-page row marked by one shape that moves on a spring (navTopGo), the window's own shell, which every page sits beside, with no room added (10-23) - coreScriptBudget = 1_549_478 // every core script: 1,358,034 after Usage left; 1,380,003 with them; 1,405,937 with the fold; 1,426,102 with 10-16's core work (the GitHub list loading, the move to plugins, Settings › Long conversations), plus 1.5% (#2612); 1,447,368 with loadBack, the window's one state read per return, which every page needs and so is the core's own (1,446,845 before it, 155 bytes under), plus 1.5%; OpenCode Zen's card saying OpenCode tells no key its balance adds ~390 bytes inside it; 1,472,589 with the dialog that grows out of its row and the agents' own settings in their rows, on main's 10-20 core work (the Providers page's last list, the Routing trace's names), plus 1.5% (10-20); 1,506,192 with Settings › Privacy's record of what was masked, plus 1.5%; 1,529,393 with the Intent classifier timeout row (#2934), with no room added; 1,529,495 with the editor's partner link host, with no room added; 1,529,844 with Check in now's press summary (#3326), with no room added; 1,530,689 with the new partner tile's dot, with no room added; 1,535,084 with the sidebar's spring, with no room added; 1,549,478 with the context window card's map (tiles by tokens, the default look, the block grid a switch away) and its text view showing an inline image, in context.js, which the Routing page draws as well as Usage, so the core's own, with no room added (10-24) + appScriptBudget = 1_343_465 // app.js: 1,198,239 after Usage left (P2); 1,218,098 with the 10-15 fixes to pages still in the core; 1,232,466 with the provider editor's Advanced fold and every dialog holding its place, the core's own (10-16); 1,250,533 with the quota refresh kept through redraws, the core's own since the tray panel draws it too, plus 1.5% (#2397); 1,269,083 with Free first's option in the provider editor's ROUTINGS, which the Routing page reads too, plus 1.5%; 1,272,726 with the Providers page drawing its last list at once (#1519), the core's page, and the Routing trace's account names read without Object.entries (#1481), plus 1.5%; a dialog growing out of the row that opened it and folding back into it, the core's own since every page's dialogs open through it (10-20); Codex's and Claude Code's own settings moved from Settings into the agent's row on the Agents page, the core's own since the tray panel and the window both draw them (10-20); 1,314,432 with Settings › Privacy's record of what was masked (its summary, the records view and the local model's controls), which belongs to Settings, the core's own, plus 1.5%; 1,336,481 with a provider's Names & levels saying Provider in model names is Settings' and opening it there (joe), and Settings' Image recognition saying where a custom provider's model is marked (nexnull), both in the core's own pages, with no room added (10-23); 1,337,633 with Settings › Models › Replies' Intent classifier timeout (#2934, reyoung), Settings' own row, drawn by waitRow, which the Image generation timeout's row now shares, with no room added; 1,337,735 with the provider editor's link naming a partner's page by its own host (siteHost) instead of usemagpie.ai's /go link, the core's own editor, with no room added (10-23); 1,338,084 with Check in now saying what the press came to (claimed +credits, already today, not available yet) where it said "refreshed" (#3326), the check-in row the tray panel draws too, the core's own, with no room added (10-23); 1,338,929 with a new partner's tile keeping a small dot until it is opened, in the add sheet, the core's own, with no room added (10-23); 1,343,324 with the sidebar's open-page row marked by one shape that moves on a spring (navTopGo), the window's own shell, which every page sits beside, with no room added (10-23); 1,343,465 with a dialog kept centred while its content comes in, until it is pressed (holdModal, every page's dialogs), the core's own, with no room added (10-24) + coreScriptBudget = 1_550_768 // every core script: 1,358,034 after Usage left; 1,380,003 with them; 1,405,937 with the fold; 1,426,102 with 10-16's core work (the GitHub list loading, the move to plugins, Settings › Long conversations), plus 1.5% (#2612); 1,447,368 with loadBack, the window's one state read per return, which every page needs and so is the core's own (1,446,845 before it, 155 bytes under), plus 1.5%; OpenCode Zen's card saying OpenCode tells no key its balance adds ~390 bytes inside it; 1,472,589 with the dialog that grows out of its row and the agents' own settings in their rows, on main's 10-20 core work (the Providers page's last list, the Routing trace's names), plus 1.5% (10-20); 1,506,192 with Settings › Privacy's record of what was masked, plus 1.5%; 1,529,393 with the Intent classifier timeout row (#2934), with no room added; 1,529,495 with the editor's partner link host, with no room added; 1,529,844 with Check in now's press summary (#3326), with no room added; 1,530,689 with the new partner tile's dot, with no room added; 1,535,084 with the sidebar's spring, with no room added; 1,549,478 with the context window card's map (tiles by tokens, the default look, the block grid a switch away) and its text view showing an inline image, in context.js, which the Routing page draws as well as Usage, so the core's own, with no room added (10-24); 1,550,768 with a usage card's refresh opening after its text (#840's card, which the tray panel draws too), the Add key spinner, and a dialog kept centred while its content comes in, with no room added (10-24) ) func TestCoreScriptBudget(t *testing.T) { diff --git a/internal/gui/tests/modal-centred-late.test.cjs b/internal/gui/tests/modal-centred-late.test.cjs new file mode 100644 index 000000000..6a15ec429 --- /dev/null +++ b/internal/gui/tests/modal-centred-late.test.cjs @@ -0,0 +1,94 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// A dialog whose content arrives after it opens stays centred: a plugin's +// page opens with a short skeleton, and its README comes later. Until the +// reader presses in it, the dialog keeps the middle as it grows (it sat low +// before, its top held where the skeleton had put it). After a press, it +// holds its place, so what was under the pointer doesn't move away. +const assert = require("node:assert/strict"); +const fs = require("node:fs/promises"); +const path = require("node:path"); +const { test } = require("node:test"); +const { chromium, webkit } = require("playwright"); + +const assets = path.resolve(__dirname, "../assets"); +const listings = [{ package: "opencode-copilot-auth", name: "GitHub Copilot", icon: "githubcopilot", providers: ["github-copilot"], + summary: { en: "Your GitHub Copilot plan." }, npm: { version: "0.0.9", weekly: 48213, publisher: "thdxr" } }]; +const readme = (n) => ["# opencode-copilot-auth", ...Array.from({ length: n }, (_, i) => "\nParagraph " + i + ".")].join("\n"); + +function server(reply) { + return async (route) => { + const url = new URL(route.request().url()); + const json = (data) => route.fulfill({ json: data }); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"en",theme:"light",web:false};` }); + if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang: "en", theme: "light" } }); + if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true }, plugins: [] }); + if (url.pathname === "/api/groups") return json({ groups: [], pools: [] }); + const state = { bun: true, bunVersion: "1.3.0", plugins: [] }; + if (url.pathname === "/api/plugins/market") return json({ listings, state }); + if (url.pathname === "/api/plugins") return json(state); + if (url.pathname === "/api/plugins/listings") return json({ listings }); + if (url.pathname === "/api/plugins/search") return json({ hits: [{ package: "opencode-copilot-auth", version: "0.0.9" }] }); + if (url.pathname === "/api/plugins/page") return json(await reply()); + if (url.pathname.startsWith("/api/")) return json({}); + if (!/^\/[\w./-]*$/.test(url.pathname) || url.host !== "magpie.test") return route.fulfill({ status: 404, body: "" }); + const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname); + const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)]; + try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); } + }; +} + +// the dialog's gaps above and below it, inside the modal's padding (which +// differs top and foot in the window) +const gaps = (page) => page.evaluate(() => { + const el = document.querySelector("#modal"), c = getComputedStyle(el), m = el.getBoundingClientRect(); + const d = document.querySelector("#modal .dialog").getBoundingClientRect(); + return { top: d.top - m.top - parseFloat(c.paddingTop), bottom: m.bottom - parseFloat(c.paddingBottom) - d.bottom, height: d.height }; +}); + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(engine + ": a dialog whose content comes late stays centred until it is pressed", async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const height of [820, 560]) { + await t.test(height + "px", async () => { + const page = await (await browser.newContext({ viewport: { width: 980, height } })).newPage(); + page.setDefaultTimeout(5000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + let release; + const gate = () => new Promise((r) => { release = r; }); + let next = gate(); + await page.route("**/*", server(() => next)); + + await page.goto("http://magpie.test/?view=plugins"); + await page.locator("#view-plugins .pm-find input").fill("copilot"); + const card = page.locator('.pm-card[data-pkg="opencode-copilot-auth"]'); + // the search's hits and npm's facts draw the card again: pressed + // before they are in, the press is lost + await card.locator(".pm-dl", { hasText: "48k/week" }).waitFor(); + await page.waitForLoadState("networkidle"); + await card.locator(".pm-sum").click(); + await page.locator("#modal .pm-detail").waitFor(); + const before = await gaps(page); + // the README is short enough to fit at 820, and taller than the window at 560 + release({ readme: readme(height > 600 ? 12 : 60), updated: "2026-09-01T00:00:00Z" }); + await page.locator("#modal .pm-md p", { hasText: "Paragraph 11." }).waitFor(); + await page.waitForFunction((h) => document.querySelector("#modal .dialog").getBoundingClientRect().height > h + 40, before.height); + // measured when the open's morph is over + await page.waitForFunction(() => document.querySelector("#modal .dialog").getAnimations().length === 0); + const after = await gaps(page); + assert(Math.abs(after.top - after.bottom) <= 1, `centred after the README came: ${JSON.stringify(after)}`); + + // pressed in, it holds its top while the content changes under it + await page.locator("#modal .pm-detail .pm-md h4, #modal .pm-detail .pm-md h1, #modal .pm-detail .pm-md p").first().dispatchEvent("pointerdown"); + await page.evaluate(() => { const p = document.querySelector("#modal .pm-md p:last-of-type"); for (let i = 0; i < 3; i++) p.after(p.cloneNode(true)); }); + await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))); + const held = await gaps(page); + assert(Math.abs(held.top - after.top) <= 1, `held its top after a press: ${JSON.stringify({ after, held })}`); + assert.deepEqual(errors, []); + await page.context().close(); + }); + } + }); +}