gui: Settings stays where it was clicked in the sidebar (jen_celeus on X)

In the sidebar layout Settings sits at the sidebar's foot, and opening it
listed its eight tabs under the button. The foot is anchored at the
window's bottom, so it grew upward and the button rose about 223 points,
out from under the pointer, and dropped back when another page folded the
list.

Settings' tabs now open above its button and grow up from it, so the button
and the gateway's line stay where they were. The foot is laid from its end
(`.side-foot` is `column-reverse`, its rows ordered tabs, Settings, gateway
and kept whole). A window too short for the list scrolls the foot from its
end, so the button stays in sight, and the foot may take 80% of the sidebar
while the list shows instead of 55%. The list's DOM order is the order
shown, so Tab reads tabs, Settings, gateway. The rows rise from the button,
nearest first. The overlay bar's thumb math takes the reversed scroll
origin. Nothing else in the layout changes: pages' own tabs still open
under their rows. The core-script budget is paid by cutting two sidebar
comments, with no raise.

Run (mote, at this head after rebasing on 86825005e unless said):
- sidebar-settings-steady.test.cjs (new), chromium and webkit, wide 1200,
  narrow 760 (sidebar picked), short 460 and tiny 340 high, all five
  languages: 42 of 42 pass. It samples the button every frame while the
  list opens, after a tab is picked and while it folds.
- Broken by hand: the old code is red in all 20 cases per engine ("the
  button moved 223px", 185.8px in the short window). With the foot back to
  `column`, red (223px). With the list put under the button in the DOM,
  red on the reading order. Before the rows were kept whole, the tiny
  window was red at 21.7px.
- sidebar, sidebar-glide, sidebar-tab-top, sidebar-overlay-scrollbar,
  nav-lamp, nav-sub-unroll, side-toggle, side-width, side-gateway-lan,
  settings-tabs-stick, text-size, click-scroll, readable-focus,
  page-width, i18n-keys at 3f27eab03 (this change before the rebase, no
  GUI files changed under it except the one new test): 322 pass, 1 red,
  sidebar-overlay-scrollbar chromium en "dragged to the end: 224 of 253",
  the known #1863. Alone it passed 3 of 3. Commented on #1863.
- go test ./internal/gui/... at 3f27eab03: ok.
- At this head: sidebar-settings-steady, sidebar, side-toggle and
  i18n-keys, chromium and webkit, 90 of 90 pass; TestCoreScriptBudget ok
  (220 bytes under the core budget).

Not run: the whole GUI suite, the full Go suite (started after the tag),
Windows and WebKitGTK by hand. No Go code changed, so no gofmt or vet.
This commit is contained in:
yetone
2026-11-10 15:45:35 +08:00
parent 8417ae6da1
commit d374168d04
4 changed files with 146 additions and 15 deletions
File diff suppressed because one or more lines are too long
+15 -1
View File
@@ -394,7 +394,16 @@ body.side-drag, body.side-drag * { cursor: col-resize !important; user-select: n
the button and the magpie beside them, level with them (see .top above) */
body.window.mac .side-top { height: calc(50px / var(--zoom, 1)); padding: calc(2px / var(--zoom, 1)) 0 0 calc(96px / var(--zoom, 1)); }
.side-scroll { flex: 1; min-height: 0; overflow-y: auto; padding: 0 10px 10px; }
.side-foot { flex: none; max-height: 55%; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; padding: 8px 10px 10px; border-top: .5px solid var(--line-2); }
/* laid from its end, Settings' button and the gateway's line kept where
they are as Settings' tabs open over the button and the foot grows up
(jen_celeus on X), and a foot too short for them scrolls from its end.
The order is the reading order, top down: tabs, Settings, the gateway */
.side-foot { flex: none; max-height: 55%; overflow-y: auto; display: flex; flex-direction: column-reverse; gap: 1px; padding: 8px 10px 10px; border-top: .5px solid var(--line-2); }
/* a foot too short for them scrolls, its rows kept whole */
.side-foot > * { flex: none; }
.side-foot > .side-gw { order: 0; }
.side-foot > #prefs { order: 1; }
.side-foot > .nav-sub { order: 2; }
/* a short window's sidebar keeps no bar beside its pages: a thin one is laid
over them while they scroll and fades once they stop, as a Mac's overlay
scroll bar does (app.js sideBars), the same in WebKit, WebView2 and
@@ -437,6 +446,11 @@ body.side-nav #nav button.has-dot::after { top: 50%; right: 10px; margin-top: -3
/* the open page's tabs, under it on a thin line */
.nav-sub { position: relative; display: flex; flex-direction: column; gap: 1px; margin: 2px 0 6px 16px; padding-left: 8px; }
.nav-sub.moving { overflow: hidden; }
/* Settings' tabs, over its button at the sidebar's foot (app.js navSubs),
close to the button they come from; the foot takes more of a short
sidebar while they show */
.side-foot > .nav-sub { margin: 6px 0 2px 16px; }
.side-foot:has(> .nav-sub) { max-height: 80%; }
.nav-sub::before { content: ""; position: absolute; left: 0; top: 3px; bottom: 3px; width: 1px; background: var(--line); }
/* rows keep their size while the list unrolls or folds: squeezed, a strip
drawn again then (Installed gets its count) marked the selection a third
+16 -13
View File
@@ -23668,10 +23668,8 @@ function layoutNav() {
renderSideToggle();
fitTop();
}
// the sidebar's width, the reader's own once they drag its edge (sideDrag):
// kept for the window, as a Mac app keeps it; none set, app.css's. A narrow
// window has it no wider than a third of itself and a little, so the page
// keeps the room
// the sidebar's width, the reader's own once dragged (sideDrag), else
// app.css's; at most ~a third of a narrow window
var SIDE_MIN = 168, SIDE_MAX = 320;
var sideW = 0;
try { sideW = Math.round(+localStorage.getItem("magpie.sideWidth")) || 0; } catch { /* app.css's, then */ }
@@ -23731,10 +23729,7 @@ function sideDrag(edge) {
});
addEventListener("resize", () => { if (sideW) sideWidth(); });
}
// a box of the sidebar that runs longer than the window has no bar of its
// own (app.css .side-bar): a thin one is laid over its edge while it
// scrolls and fades once it stops, as a Mac's overlay bar does; held under
// the pointer it stays, and dragged it scrolls the box
// a sidebar box's overlay bar, as a Mac's (app.css .side-bar)
function sideBars() {
const side = $("#side");
if (!side) return;
@@ -23754,7 +23749,9 @@ function sideBars() {
bar.style.top = box.offsetTop + "px";
bar.style.height = ch + "px";
const h = Math.max(24, Math.round(ch * ch / sh));
const y = Math.round((ch - h) * box.scrollTop / (sh - ch));
// the foot is laid from its end (app.css): scrollTop 0 there
const from = getComputedStyle(box).flexDirection === "column-reverse" ? sh - ch : 0;
const y = Math.round((ch - h) * (box.scrollTop + from) / (sh - ch));
thumb.style.height = h + "px";
thumb.style.transform = `translateY(${y}px)`;
};
@@ -23853,8 +23850,10 @@ function navSubs() {
navSubsAt = sig;
let box = document.querySelector(".nav-sub:not(.leaving)");
const want = side && at && items.length > 1;
// Settings' list is over its button, which stays put (jen_celeus on X)
const above = at?.id === "prefs";
// another page's list folds away as this one's opens, both at once
if (box && (!want || box.dataset.view !== view || box.previousElementSibling !== at)) { navSubLeave(box); box = null; }
if (box && (!want || box.dataset.view !== view || (above ? box.nextElementSibling : box.previousElementSibling) !== at)) { navSubLeave(box); box = null; }
if (want) {
const fresh = !box;
if (fresh) {
@@ -23869,7 +23868,7 @@ function navSubs() {
e.preventDefault();
all[(to + all.length) % all.length].focus();
};
at.after(box);
if (above) at.before(box); else at.after(box);
}
box.setAttribute("aria-label", (at.querySelector("span[data-t]") || at.querySelector("span"))?.textContent || "");
for (const s of box.querySelectorAll(".nav-s")) s.remove();
@@ -24025,8 +24024,12 @@ function navSubEnter(box) {
box.classList.add("moving");
box.animate([{ height: "0px", marginTop: "0px", marginBottom: "0px", opacity: 0 }, to], { duration: 300, easing: NAV_EASE })
.finished.then(() => box.classList.remove("moving"), () => {});
[...box.querySelectorAll(".nav-s")].forEach((s, i) => s.animate(
[{ opacity: 0, transform: "translateY(-6px)" }, { opacity: 1, transform: "none" }],
// Settings' rises from its button, the nearest row first
const up = !!box.closest(".side-foot");
const rows = [...box.querySelectorAll(".nav-s")];
if (up) rows.reverse();
rows.forEach((s, i) => s.animate(
[{ opacity: 0, transform: `translateY(${up ? 6 : -6}px)` }, { opacity: 1, transform: "none" }],
{ duration: 260, delay: 40 + Math.min(i, 12) * 20, easing: NAV_EASE, fill: "backwards" }));
box.querySelector(".nav-hl").animate([{ opacity: 0 }, { opacity: 1 }], { duration: 200, delay: 80, fill: "backwards" });
}
@@ -0,0 +1,114 @@
// Run with Node's test runner and Playwright on the module path; see README.md.
// Settings at the sidebar's foot stays where it was clicked (jen_celeus on
// X): its tabs used to be listed under it, so the foot grew upward and the
// button rose with it, out from under the pointer. The list now opens
// above the button, growing from it, and folds back the same way; the
// button never moves, while the list unrolls or after, in a wide window, a
// narrow one, a short one and one too short for the whole list.
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_ASSETS || path.resolve(__dirname, "../assets"));
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:${JSON.stringify(lang)},theme:"light",web:false,sideGateway:{url:"http://127.0.0.1:3425",running:true,boot:true}};` });
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: [], presets: [], excluded: [], gateway: { url: "http://127.0.0.1:3425", running: true, window: true } });
if (url.pathname === "/api/groups") return json({ groups: [] });
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)];
await route.fulfill({ body: await fs.readFile(file), contentType });
};
}
// every frame for a while, the furthest Settings' button got from where it was
const drift = (page, ms) => page.evaluate((ms) => new Promise((done) => {
const b = document.querySelector("#prefs"), at = b.getBoundingClientRect();
let most = 0;
const end = performance.now() + ms;
const look = () => {
const r = b.getBoundingClientRect();
most = Math.max(most, Math.abs(r.top - at.top), Math.abs(r.bottom - at.bottom), Math.abs(r.left - at.left), Math.abs(r.right - at.right));
if (performance.now() < end) requestAnimationFrame(look); else done(most);
};
requestAnimationFrame(look);
}), ms);
const sizes = [
{ name: "wide", width: 1200, height: 760 },
{ name: "narrow", width: 760, height: 760, side: true },
{ name: "short", width: 1200, height: 460 },
// too short for the whole list: the foot scrolls, its end in sight
{ name: "tiny", width: 1200, height: 340, over: true },
];
const langs = ["en", "zh", "zh-TW", "ja", "de"];
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(`${engine}: Settings stays where it was clicked`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const size of sizes) for (const lang of langs) await t.test(`${size.name} ${lang}`, async () => {
const context = await browser.newContext({ viewport: { width: size.width, height: size.height } });
if (size.side) await context.addInitScript(() => { try { localStorage.setItem("magpie.navLayout", "side"); } catch {} });
const page = await context.newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang));
await page.goto("http://magpie.test/");
await page.waitForSelector("#nav button.on");
assert.ok(await page.evaluate(() => document.body.classList.contains("side-nav")), "the sidebar shows");
const prefs = page.locator("#side .side-foot #prefs");
const before = await prefs.boundingBox();
// clicked, the list unrolls and the button stays under the pointer
await page.mouse.click(before.x + before.width / 2, before.y + before.height / 2);
const opening = await drift(page, 700);
const subs = page.locator("#side .nav-sub:not(.leaving) .nav-s");
await subs.first().waitFor();
assert.equal(await subs.count(), 8, "Settings' tabs are listed");
const after = await prefs.boundingBox();
assert.ok(opening < 1, `the button moved ${opening}px while the list opened`);
assert.ok(Math.abs(after.y - before.y) < 1 && Math.abs(after.x - before.x) < 1, `the button stays put: ${JSON.stringify(before)} then ${JSON.stringify(after)}`);
assert.ok(await page.locator("#prefs").evaluate((b) => b.classList.contains("on")), "Settings is open");
// the list grows from the button: it sits right above it, the open tab in sight
const [listBottom, btnTop] = await page.evaluate(() => [document.querySelector(".nav-sub:not(.leaving)").getBoundingClientRect().bottom, document.querySelector("#prefs").getBoundingClientRect().top]);
assert.ok(listBottom <= btnTop + 0.5 && btnTop - listBottom < 12, `the list is just above the button: ${listBottom} ${btnTop}`);
// and is read in the order shown, tabs then Settings then the gateway
assert.deepEqual(await page.locator(".side-foot").evaluate((f) => [...f.children].map((c) => c.id || c.className)), ["nav-sub", "prefs", "sideGw"], "read top down");
const inSight = (sel) => page.locator(sel).evaluate((s) => {
const r = s.getBoundingClientRect(), f = s.closest(".side-foot").getBoundingClientRect();
return r.top >= f.top - 0.5 && r.bottom <= f.bottom + 0.5;
});
assert.ok(await inSight("#prefs"), "the button is in sight");
const over = await page.locator(".side-foot").evaluate((f) => f.scrollHeight > f.clientHeight + 1);
assert.equal(over, !!size.over, "the foot scrolls only when the list can't fit");
if (!size.over) assert.ok(await inSight(".nav-s.cur"), "the open tab is in sight");
// a tab picked there: the button still where it was
await page.locator(".nav-s[data-of=setTab-network]").click();
await page.waitForFunction(() => document.querySelector("#setTab-network").classList.contains("on"));
assert.ok((await drift(page, 300)) < 1, "picking a tab leaves the button put");
assert.ok(Math.abs((await prefs.boundingBox()).y - before.y) < 1);
// another page: the list folds away and the button never moved
await page.locator('#nav button[data-view="agents"]').click();
const closing = await drift(page, 600);
await page.waitForFunction(() => !document.querySelector(".nav-sub"));
assert.ok(closing < 1, `the button moved ${closing}px while the list folded`);
assert.ok(Math.abs((await prefs.boundingBox()).y - before.y) < 1, "the button is where it began");
assert.deepEqual(errors, []);
await context.close();
});
});
}