Files
magpie/internal/gui/tests/form-align.cjs
T
yetone 1f04ed2193 gui: a form's name and its control share a line in every editor
The owner (11-12, the second time): in a provider editor, Free for me and
Redaction sat 4-5px under their ticks. "这个事情发生过不止一次了,要彻底避免。"

Cause: each label carried a padding-top guessed for one control, 5px for
a 29px input. Any other kind of control, such as a tick, a switch, the
icon box, a row of options, a list or a JSON box, sat off its name.

Fix, at the shared row layout instead of per row:
- Every editor grid sets its rows on their first baseline: .editor, a
  dialog's .ebody, Advanced's .more .inner and Connect. The per-label
  padding and label.by-list are gone.
- Controls with no text of their own carry a text line at their middle:
  the icon box (line-height 30px) and the library switch (a ZWSP
  ::before).
- Exception: a row whose control starts with a text field is set at its
  top, its name padded to the field's line (--field-h, 44px on the narrow
  web). WebKit lays a baseline row out again on every layout and reveals
  a focused field each time, so the page jumped back to a new group's
  name (group-from-model-add). app.js's settleRows marks those rows
  .at-top (and a JSON box's name .by-json), only in grids a mutation
  touched. A CSS :has(+ ...) version held WebKit ~9s on Codex's editor
  (9209ms vs 249ms on base, provider-pass-errors).
- Connect's options wrap at <=520px instead of scrolling: WebKit grew a
  baseline row holding a scroller 6px each time its bar came and went
  (connect-snippet-shell).
- The rate-windows Add button sits at its row's top.
- provider-at-once compares the label's text height, not its padded box.
  The label is still one line; a name beside a 44px field is padded down
  to its line.

Guard: form-align.test.cjs opens the provider editors (a key's, Codex,
with Advanced open), the group editors and Connect. For every name and
control row it asserts the label's first text line and the control's
first line share a vertical centre within 2px. It runs in chromium and
webkit, en/zh/zh-TW/ja/de, at 1100, 420, a web 390 and a panel width.
LESSONS.md gets the rule.

Run (mica, through mica-run):
- At the pushed tree (49274f690, the same tree before this message): form-align, provider-pass-errors,
  provider-at-once, provider-count-free, provider-unmasked,
  provider-rate-windows, provider-proxy, connect-snippet-shell,
  group-from-model-add, icon-url, library-mcp-hub, settings-lines-wrap,
  settings-update-proxy, editor-roomy and i18n-keys: 499 pass, 3 fail at load 75. The 3: settings-lines-wrap chromium de 440 is red the same on origin/main 2bc25a2d6, 2 of 2 (#6353). connect-snippet-shell chromium en/de 390 passed when its file ran again alone (#4128 is its sibling).
- go test -run Budget ./internal/gui: ok.
- Earlier, at 648cd3fdb: the whole GUI suite (473 files). The new reds in
  it were the :has slowdown above (fixed). The 64 files it reached were
  then run again on mote, rebased on 343920464; the reds left are listed below.
- Broken by hand: every first baseline put back to start, form-align
  0/10. On the old CSS it was 0/10.

Reds that are the same on origin/main (343920464, 2bc25a2d6):
- connect-address-kept at 1100: #6755, filed now.
- provider-mask "Hide accounts": #5109.
- settings-groups warm-ups: #6597.
- editor-advanced-align webkit: #4615 / #6421.
- library-bili webkit: the tab click is lost about 1 run in 4, 2/8 on
  main, near #6015.

Not tried: the real app in WKWebView, WebKitGTK and WebView2, and Windows.
2026-11-15 19:53:10 +08:00

44 lines
2.5 KiB
JavaScript

// formRows: every label of a form grid that sits beside its control, with
// how far the middle of its first line is from the middle of the
// control's first line. The first line of the control is its first
// visible field, button, switch row, tick or line of text, in order.
async function formRows(page, root = ".editor") {
return page.evaluate((root) => {
const shown = (e) => { const r = e.getBoundingClientRect(); return r.width > 0 && r.height > 0 && getComputedStyle(e).visibility !== "hidden"; };
const lineOf = (n) => { const r = document.createRange(); r.selectNodeContents(n); return [...r.getClientRects()].find((x) => x.width > 0 && x.height > 0); };
const LEAF = "input:not([type=hidden]), textarea, select, button, [role=switch], label.tick, .sess-pick, .icon-now";
const first = (box) => {
const w = document.createTreeWalker(box, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT);
for (let n = w.currentNode; n; n = w.nextNode()) {
if (n.nodeType === 3) { if (n.data.trim() && n.parentElement && shown(n.parentElement)) { const r = lineOf(n); if (r) return { r, what: "text " + n.data.trim().slice(0, 24) }; } continue; }
if (n !== box && n.matches(LEAF) && shown(n)) {
let r = n.getBoundingClientRect();
// a box of several lines: its first line, inside its border and padding
if (n.matches("textarea")) {
const s = getComputedStyle(n), lh = parseFloat(s.lineHeight) || parseFloat(s.fontSize) * 1.2;
const top = r.top + parseFloat(s.borderTopWidth) + parseFloat(s.paddingTop);
r = { top, height: lh };
}
return { r, what: n.tagName.toLowerCase() + "." + [...n.classList].join(".") };
}
}
return null;
};
const out = [];
for (const ed of document.querySelectorAll(root)) {
for (const l of ed.querySelectorAll("label")) {
const p = l.parentElement, c = l.nextElementSibling;
if (!p || getComputedStyle(p).display !== "grid" || !c || l.matches(".tick") || !shown(l) || !shown(c)) continue;
const lr = l.getBoundingClientRect(), cr = c.getBoundingClientRect();
if (lr.right > cr.left + 1) continue; // stacked (a phone's one column)
const t = lineOf(l), f = first(c);
if (!t || !f) continue;
const mid = (r) => r.top + r.height / 2;
out.push({ label: l.textContent.trim().slice(0, 30), what: f.what, off: Math.round((mid(t) - mid(f.r)) * 10) / 10 });
}
}
return out;
}, root);
}
module.exports = { formRows };