gui: a segment pressed in a dialog leaves the dialog where it was

The owner (10-23, a screenshot of Kimi Code's add dialog): every click
on a Plan segment moved the dialog a few pixels down.

The dialog's hold (modalMark at pointerdown, holdModal on the redraw)
keeps what was pressed where it was. A pressed .opt scales (.opt:active,
a .14s transition) and is still scaled when its click redraws; an element
that scales was measured by its parent, the .segs strip, 2px (its
padding) above the button's own top marked at the press. The dialog
went 2px down at every click. modalDepth now measures a scaled element
by its own top as drawn unscaled, from its matrix and transform-origin;
one that turns or skews is measured by its parent as before.

Run at this tree (base eee9ece29):
- Reproduced with presses held 120ms as a hand does, Kimi's four plans,
  eight presses: 262→264→…→278 in Chromium and WebKit; with the fix
  262.1 throughout. Instant Playwright clicks never showed it.
- segment-press-dialog.test.cjs (new): 4/4 green; on the old app.js 4/4
  red ("press 1: the dialog moved from 262.09 to 264.09").
- Every GUI file that opens a dialog (109 files, grep '#modal'): 1467
  pass, 3 red. plugin-github-filter passed rerun. modal-morph chromium
  420 ("the strip's top on the row's", 401 vs 404-406) is red on the old
  app.js too, 2 of 2: it measures the row before the page settles. Fixed
  in its own commit next.
- gofmt -l clean.
This commit is contained in:
yetone
2026-10-23 16:48:00 +08:00
parent 56418ac5cb
commit db48697d67
2 changed files with 86 additions and 3 deletions
+7 -3
View File
@@ -8606,17 +8606,21 @@ $("#modal").addEventListener("keydown", (e) => {
const modalHold = { off: 0, at: null, y: 0, cap: null, capSet: "" };
// 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) is measured by what holds it, as its
// top moves round its middle by more than the matrix says.
// that scales (a Copy button's pop, a segment pressed) is measured by its
// own top as drawn unscaled, round its transform-origin: measured by what
// holds it, a pressed segment read 2px above where it was marked, and the
// dialog went 2px down at every click (the owner, 10-23). One that turns
// or skews is measured by what holds it.
function modalDepth(a, d) {
if (!a?.isConnected || !d.contains(a) || a === d || !a.offsetParent) return null;
if (getComputedStyle(d).transform === "none") {
const moves = [];
for (let x = a; x && x !== d; x = x.parentElement) {
const t = getComputedStyle(x).transform;
const c = getComputedStyle(x), t = c.transform;
if (t === "none") continue;
const m = new DOMMatrixReadOnly(t);
if (m.m11 === 1 && m.m22 === 1 && !m.m12 && !m.m21) moves.push(m.m42);
else if (!m.m12 && !m.m21) { a = x; moves.length = 0; moves.push(m.m42 + parseFloat(c.transformOrigin.split(" ")[1]) * (1 - m.m22)); }
else { a = x.parentElement; moves.length = 0; }
}
if (a === d) return null;
@@ -0,0 +1,79 @@
// Run with Node's test runner and Playwright on the module path; see README.md.
// A segment pressed in a dialog leaves the dialog where it was (the owner,
// 10-23: each click on Kimi Code's Plan moved the dialog a few pixels
// down). A pressed segment scales (.opt:active) and is still scaling when
// its click redraws; the dialog's hold measured it by the segments' strip
// then, 2px above where the press had marked it, and went down by that at
// every click. Pressed as a hand does, down and up a beat apart, eight
// times round the four plans. English and Chinese, Chromium and WebKit; no
// backend, the API is faked here.
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 kimi = {
id: "kimi", name: "Kimi Code", icon: "generic", kind: "partner", sponsored: true, regionLabel: "Plan",
notes: { en: "One subscription, one K3 quota shared by every agent", zh: "一份订阅,所有 agent 共享 K3 额度" },
website: "https://usemagpie.ai/go/kimi/site", keysUrl: "https://usemagpie.ai/go/kimi/keys",
chat: "https://api.kimi.example/coding/v1", anthropic: "https://api.kimi.example/coding",
regions: [
{ id: "cn", name: "China", chat: "https://api.kimi.example/coding/v1", anthropic: "https://api.kimi.example/coding" },
{ id: "global", name: "Global", chat: "https://api.kimi.example/global/v1", anthropic: "https://api.kimi.example/global" },
{ id: "api-cn", name: "Pay as you go · China", chat: "https://api.moonshot.example/cn/v1", anthropic: "https://api.moonshot.example/cn/anthropic" },
{ id: "api", name: "Pay as you go · Global", chat: "https://api.moonshot.example/v1", anthropic: "https://api.moonshot.example/anthropic" },
],
};
function serve(lang) {
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:"${lang}",theme:"dark",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, theme: "dark" } });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [kimi], excluded: [], gateway: { running: true, window: true } });
if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
if (url.pathname === "/api/gateway/trace") return json({ routes: [] });
if (url.pathname.startsWith("/api/")) return json({});
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: "" }); }
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
test(`${engine} ${lang}: a plan pressed leaves its dialog where it was`, async (t) => {
const browser = await (engine === "webkit" ? webkit : chromium).launch();
t.after(() => browser.close());
const page = await browser.newPage({ viewport: { width: 1100, height: 800 } });
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang));
await page.goto("http://magpie.test/?view=providers");
await page.locator("#addProvider").click();
await page.locator("#addSheet .tile", { hasText: "Kimi Code" }).click();
const d = page.locator("#modal .editor");
const opts = d.locator(".segs.regions .opt");
await opts.nth(3).waitFor();
// the dialog come in and still
await page.waitForFunction(() => !document.querySelector("#modal").getAnimations({ subtree: true }).length);
const top = () => d.evaluate((e) => e.getBoundingClientRect().top);
const at = await top();
for (let i = 0; i < 8; i++) {
const b = await opts.nth((i + 1) % 4).boundingBox();
await page.mouse.move(b.x + b.width / 2, b.y + b.height / 2);
await page.mouse.down();
await page.waitForTimeout(120);
await page.mouse.up();
await page.waitForFunction((n) => document.querySelectorAll("#modal .segs.regions .opt")[n].classList.contains("on"), (i + 1) % 4);
await page.waitForTimeout(300);
assert(Math.abs(await top() - at) < 0.5, `press ${i + 1}: the dialog moved from ${at} to ${await top()}`);
}
assert.deepEqual(errors, []);
});
}
}