From ba970b1b494faea4da25eda01ada4c865e7bd90c Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 31 Oct 2026 09:58:00 +0800 Subject: [PATCH] gui: a provider editor's Advanced starts where its fields do MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The owner: 高级这个折叠按钮为什么在一个前不着村后不着店的位置? Advanced's summary was indented a fixed 86px, the 72px label column plus its 14px gap from before the column became fit-content(132px), so it lined up with nothing. A ResizeObserver (edAligned) now reads the editor grid's first column and gap into --ed-lab/--ed-at for Advanced, More endpoints and the import's soft warning; open, Advanced's fields take at least the same label column. With the labels stacked (a phone's browser) it starts at the edge. The chevron is 11px in --muted, not --faint. Run: editor-advanced-align.test.cjs (new), both engines, en/zh/zh-TW/ ja/de at 1000, 560 and web 390, on wsl-run (mote's WebKit is broken, #4125): ok. With account-refresh, window-cap, account-models and account-row-line: 90/90. 25 editor GUI files compared with the base 8ee75adbf at concurrency 2 on the WSL box: same reds on both (WSL WebKit under load from other sessions), none new. Not tried: the real Linux app (WebKitGTK). --- internal/gui/assets/app.css | 8 +- internal/gui/assets/app.js | 17 +++- .../gui/tests/editor-advanced-align.test.cjs | 89 +++++++++++++++++++ 3 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 internal/gui/tests/editor-advanced-align.test.cjs diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index 99d19f67f..1ad0690a8 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -3244,18 +3244,18 @@ body.window .ag-sk-icon { width: 22px; height: 22px; border-radius: 6px; } .link { font-size: 12px; color: var(--accent); padding: 3px 4px; white-space: nowrap; } .link:hover { text-decoration: underline; text-underline-offset: 2px; } .editor .more { grid-column: 1 / -1; } -.editor .more summary { font-size: 12px; color: var(--muted); list-style: none; display: inline-flex; align-items: center; gap: 4px; padding: 2px 0; margin-left: 86px; } +.editor .more summary { font-size: 12px; color: var(--muted); list-style: none; display: inline-flex; align-items: center; gap: 4px; padding: 2px 0; margin-left: var(--ed-at, 86px); } .editor .more summary::-webkit-details-marker { display: none; } -.editor .more summary::before { content: "▸"; font-size: 10px; color: var(--faint); display: inline-block; transition: transform .42s cubic-bezier(.32, .72, 0, 1); } +.editor .more summary::before { content: "▸"; font-size: 11px; color: var(--muted); display: inline-block; transition: transform .42s cubic-bezier(.32, .72, 0, 1); } .editor .more[open]:not(.closing) summary::before { transform: rotate(90deg); } @media (prefers-reduced-motion: reduce) { .editor .more summary::before { transition: none; } } .editor .more summary:hover { color: var(--fg); } /* its labels are longer (Anthropic 地址, Responses-URL): their column takes the longest, on one line, rather than wrap a last 址 under it (#875) */ -.editor .more .inner { display: grid; grid-template-columns: minmax(72px, max-content) minmax(0, 1fr); gap: 10px 14px; align-items: start; padding-top: 8px; } +.editor .more .inner { display: grid; grid-template-columns: minmax(var(--ed-lab, 72px), max-content) minmax(0, 1fr); gap: 10px 14px; align-items: start; padding-top: 8px; } .editor .more .inner label { white-space: nowrap; } .editor .warnbox { grid-column: 1 / -1; font-size: 12px; line-height: 1.5; color: var(--fg); background: color-mix(in srgb, var(--amber) 12%, transparent); border: 1px solid color-mix(in srgb, var(--amber) 35%, transparent); border-radius: 8px; padding: 8px 10px; } -.editor .warnbox.soft { background: none; color: var(--amber); border: 0; padding: 0 0 0 86px; } +.editor .warnbox.soft { background: none; color: var(--amber); border: 0; padding: 0 0 0 var(--ed-at, 86px); } .editor.import .mchips .mchip { cursor: default; } .editor .bar { grid-column: 1 / -1; display: flex; align-items: center; gap: 4px; padding-top: 10px; border-top: 1px solid var(--line-2); margin-top: 2px; } .editor .bar .danger { margin-left: -7px; } diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 90a5a045f..0b9485acf 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -8997,6 +8997,17 @@ function openModal(content) { const stop = () => follow.disconnect(); grow.finished.then(stop, stop); } +// edAligned: a row across an editor starts where its fields do (--ed-at) +const edAlign = new ResizeObserver((es) => { + for (const { target: n } of es) { + if (!n.isConnected) { edAlign.unobserve(n); continue; } + const cs = getComputedStyle(n.parentElement), cols = cs.gridTemplateColumns.split(" "); + const lab = cols.length > 1 ? parseFloat(cols[0]) || 0 : 0; + n.style.setProperty("--ed-lab", lab + "px"); + n.style.setProperty("--ed-at", (lab && lab + parseFloat(cs.columnGap)) + "px"); + } +}); +const edAligned = (n) => (edAlign.observe(n), n); // frame holds an editor's head and its buttons still while the fields // between them scroll. function frame(ed) { @@ -9479,7 +9490,7 @@ function drawEditor(p, presetID) { // the settings few need — headers, proxy, limits, windows, fallback — // folded under Advanced, out of the way of the key and the models: open // when one of them is set, or as it was left in this editor - const adv = el("details", "more advanced"); + const adv = edAligned(el("details", "more advanced")); adv.open = advanced && advanced.of === editing ? advanced.open : advancedSet(draft, p); const of = editing; adv.onfold = (open) => { advanced = { of, open }; }; @@ -10051,7 +10062,7 @@ function drawEditor(p, presetID) { ed.append(adv); if (custom) { - const more = el("details", "more"); + const more = edAligned(el("details", "more")); more.append(el("summary", "", t("More endpoints"))); const inner = el("div", "inner"); // the other protocols' URLs, drawn again when the base URL's changes @@ -10776,7 +10787,7 @@ function renderImport(im) { } ed.append(...field(t("Models"), chips, "")); } - if (im.replaces) ed.append(el("div", "warnbox soft", t("Replaces your {name}, key and all.", { name: im.replaces }))); + if (im.replaces) ed.append(edAligned(el("div", "warnbox soft", t("Replaces your {name}, key and all.", { name: im.replaces })))); // The link may name the vendor's own logo — an explicit icon= wins over // whatever the catalog or preset gave. magpie fetches it here (the dialog diff --git a/internal/gui/tests/editor-advanced-align.test.cjs b/internal/gui/tests/editor-advanced-align.test.cjs new file mode 100644 index 000000000..1cb76cf23 --- /dev/null +++ b/internal/gui/tests/editor-advanced-align.test.cjs @@ -0,0 +1,89 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// The owner, on a provider editor's Advanced: 高级这个折叠按钮为什么在一个前不着村后不着店的位置? +// It was indented a fixed 86px, from when the label column was 72px; the +// column is now as wide as its labels, so it lined up with nothing. Its +// chevron starts where the fields do, and open, its fields start there +// too unless their own labels are longer (#875 keeps those on one line). +// With the labels above the fields (a phone's browser) it starts at the +// edge. In every language, wide, narrow and on a phone, both engines. +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(process.env.MAGPIE_GUI_ASSETS || path.resolve(__dirname, "../assets")); +const claude = () => ({ + id: "claude", name: "Claude Code", icon: "claude-color", chat: "", responses: "", anthropic: "", catalog: "", routing: "", + models: [{ id: "claude-opus-5-5", name: "Claude Opus 5.5", on: true }], agents: [], fallback: [], headers: {}, keyList: [], + account: { agent: "claude", agentName: "Claude Code", user: "first@example.com", plan: "team", logins: [{ user: "first@example.com", plan: "team", active: true, on: true }] }, +}); +const serve = (lang, web) => 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:${JSON.stringify(lang)},theme:"light",web:${web}};` }); + 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: "light" } }); + if (url.pathname === "/api/providers") return json({ providers: [claude()], presets: [], excluded: [], gateway: { running: true, window: true } }); + if (url.pathname === "/api/groups") return json({ groups: [] }); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/login/usage") return json({}); + 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: "" }); } +}; + +// where the editor's fields start, and its Advanced's chevron, its open +// fields and how long their labels are +const measure = (page) => page.evaluate(() => { + const adv = document.querySelector(".editor details.advanced"); + const g = adv.parentElement, cs = getComputedStyle(g); + const label = [...g.children].find((c) => c.tagName === "LABEL" && c.offsetWidth); + const field = label.nextElementSibling.getBoundingClientRect(); + const sum = adv.querySelector("summary").getBoundingClientRect(); + const out = { fields: field.left, edge: g.getBoundingClientRect().left + parseFloat(cs.paddingLeft), stacked: cs.gridTemplateColumns.split(" ").length === 1, summary: sum.left, labelCol: label.getBoundingClientRect().width }; + if (adv.open) { + const ls = [...adv.querySelectorAll(":scope > .inner > label")].filter((l) => l.offsetWidth); + out.innerFields = ls.length ? ls[0].nextElementSibling.getBoundingClientRect().left : null; + out.innerLabels = Math.max(0, ...ls.map((l) => { const r = document.createRange(); r.selectNodeContents(l); return r.getBoundingClientRect().width; })); + } + return out; +}); + +for (const [engine, browserType] of [["chromium", chromium], ["webkit", webkit]]) { + test(engine + ": a provider editor's Advanced starts where its fields do", async (t) => { + const browser = await browserType.launch(); + t.after(() => browser.close()); + for (const [web, width] of [[false, 1000], [false, 560], [true, 390]]) for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { + const at = `${lang} ${web ? "web " : ""}${width}`; + const context = await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" }); + const page = await context.newPage(); + page.setDefaultTimeout(5000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, web)); + await page.goto("http://magpie.test/?view=providers"); + await page.locator(".row.provider", { hasText: "Claude Code" }).first().click(); + const adv = page.locator(".editor details.advanced"); + await adv.locator("summary").waitFor(); + await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))); + let m = await measure(page); + const start = m.stacked ? m.edge : m.fields; + assert.ok(Math.abs(m.summary - start) <= 1, `${at}: Advanced starts at ${m.summary}, its fields at ${start}`); + + await adv.locator("summary").click(); + await page.waitForFunction(() => document.querySelector(".editor details.advanced")?.open); + await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))); + m = await measure(page); + if (m.innerFields != null && !m.stacked) { + assert.ok(m.innerFields >= m.fields - 1, `${at}: Advanced's fields start left of the others (${m.innerFields} < ${m.fields})`); + if (m.innerLabels <= m.labelCol) assert.ok(Math.abs(m.innerFields - m.fields) <= 1, `${at}: Advanced's fields at ${m.innerFields}, the others at ${m.fields}`); + } + assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth + 1), true, `${at}: the page scrolls sideways`); + await page.screenshot({ path: path.join(process.env.SHOTS || "/tmp", `editor-advanced-${engine}-${lang}-${width}.png`) }).catch(() => {}); + assert.deepEqual(errors, []); + await context.close(); + } + }); +}