From 876d477d7325d7ebf08a6c691eb98acb3fe574ed Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 31 Oct 2026 07:53:03 +0800 Subject: [PATCH] sidebar: a sub-list's row under the pointer is lit as by a torch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The owner asked for the sidebar's sub-lists (Settings' General, Allowances, Network and sharing…) to light the way a torch does, not a whole row at once, and picked this version from a mock-up of four: only the row under the pointer lights, brightest where the pointer is and falling off along the word, the rest of that row a little over the others. The light follows the pointer with a small lag and fades as it moves to the next row or off the list. - navlamp.js (new, 2KB): one pointermove listener on the document eases the light's place and each row's --si; a touch lights nothing, the selected row is never lit, reduced motion puts it there at once. It is imported by app.js when a sub-list is first drawn, as signsheets.js and the others are, so it is outside the core's scripts. - app.css: .nav-s > span.lit paints the word with a radial gradient clipped to its text (36px wide, its heart --fg taken to white in dark and black in light by relative colour, the rest of the row 15% toward --fg). All under @supports color-mix, so Safari 15 keeps the plain hover; while the lamp is on that plain hover stands down so the row doesn't flash to full ink first. - app.js: the one import line (+46 bytes), with a clause of the sub-row click comment cut that said again what the line above says, so app.js and the core stay in their budgets with no raise (app.js 1,313,980, core 1,537,347). - old-webkit.test.cjs lists navlamp.js among the scripts loaded later. Tests: nav-lamp.test.cjs, both engines, light and dark, en and zh. It reads the word's pixels: with the pointer at a word's start the start's ink stands off the ground more than the end's, and the other way round once the pointer is at its end; one row lit at a time, the next row taking it and the first going out, all out off the list, the selection and a touch never lit, lit in one frame with reduced motion. Run at this head: - the whole GUI suite on xiaomi-gpu (Windows), at 03d21ab8e plus this: 16131 tests, 15613 pass, 486 fail in 91 files, nav-lamp ok. Those 91 files again at concurrency 2, at 11fc687c0 with this (6d6715370 before this message): 416 fail; at origin/main 11fc687c0 alone: 499 fail. The reds are the same tests on both sides (#4124, numbers there). - nav-lamp.test.cjs + old-webkit.test.cjs: chromium on mote 4/4; webkit on xiaomi-gpu (Windows) 4/4 and old-webkit 7/7. On mote webkit timed out at launch (load 21, another session's full GUI suite), so it ran on Windows instead. - go test -tags nogui -run Budget ./internal/gui on mote: ok. Broken by hand, each red, then put back: - the gradient flattened to one colour: "brightest where the pointer is" red in every case; - touch let through: "a touch lights no row" red; - every row kept lit: the first row never goes out, timed out red. Not tried: the real app on Linux (WebKitGTK) and Windows (WebView2), and macOS 13's WebKit, where relative colour is missing and the heart is --fg itself. --- internal/gui/assets/app.css | 16 +++ internal/gui/assets/app.js | 8 +- internal/gui/assets/navlamp.js | 44 ++++++++ internal/gui/tests/nav-lamp.test.cjs | 135 +++++++++++++++++++++++++ internal/gui/tests/old-webkit.test.cjs | 6 +- 5 files changed, 202 insertions(+), 7 deletions(-) create mode 100644 internal/gui/assets/navlamp.js create mode 100644 internal/gui/tests/nav-lamp.test.cjs diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index f9450c411..ea568b212 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -433,6 +433,22 @@ body.side-nav #nav button.has-dot::after { top: 50%; right: 10px; margin-top: -3 before the selection slides there */ .nav-s:hover:not(.cur) { color: var(--fg); } .nav-s.cur { color: var(--fg); font-weight: 500; } +/* the row under the pointer is lit as by a torch (navlamp.js): --lx is the + pointer's place in the word, --si how lit the row is. Its heart is a step + past the text's ink (--fg taken to white in dark, black in light), the + rest of the row 15% of the way to it, and the plain hover above stands + down. Where color-mix is missing (Safari 15) the plain hover stays. */ +@supports (color: color-mix(in oklab, red, blue)) { + .navlamp .nav-s:hover:not(.cur) { color: var(--ctl-fg); } + .nav-s > span.lit { + --hi: var(--fg); + --hot: color-mix(in oklab, var(--hi) calc(var(--si) * 100%), var(--ctl-fg)); + --far: color-mix(in oklab, var(--fg) calc(var(--si) * 15%), var(--ctl-fg)); + background: radial-gradient(36px 400px at var(--lx) 50%, var(--hot) 0%, var(--hot) 12%, color-mix(in oklab, var(--hot) 45%, var(--far)) 45%, var(--far) 100%); + -webkit-background-clip: text; background-clip: text; color: transparent; + } +} +@supports (color: oklch(from red round(l) c h)) { .nav-s > span.lit { --hi: oklch(from var(--fg) round(l) 0 h); } } /* a row pressed darkens, as a Mac sidebar's does: it never shrinks the way a button does (.seg button:active, .icon:active below) */ body.side-nav #side button:active { transform: none; } diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index e7d8a384a..90a5a045f 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -23888,6 +23888,7 @@ function navSubs() { const fresh = !box; if (fresh) { box = el("div", "nav-sub"); + import("./navlamp.js").catch(() => {}); box.setAttribute("role", "tablist"); box.dataset.view = view; box.append(el("i", "nav-hl")); @@ -23924,10 +23925,9 @@ function navSubs() { s.onpointercancel = () => navSubGlide(box, box.querySelector(".nav-s.cur")); s.onpointerleave = (e) => { if (e.buttons & 1) navSubGlide(box, box.querySelector(".nav-s.cur")); }; // the page draws its strip again as it likes: the one there now is - // clicked at once, so what is typed next goes to the tab picked (the - // press has set the selection off already). Another tab opens at its - // top: the page's tabs share its one scroll, which would otherwise keep - // the place reached in the tab before. + // clicked at once, so what is typed next goes to the tab picked. + // Another tab opens at its top: the page's tabs share its one scroll, + // which would otherwise keep the place reached in the tab before. s.onclick = (e) => { navSubGlide(box, s); for (const o of box.querySelectorAll(".nav-s")) o.classList.toggle("cur", o === s); diff --git a/internal/gui/assets/navlamp.js b/internal/gui/assets/navlamp.js new file mode 100644 index 000000000..0ed69bb6d --- /dev/null +++ b/internal/gui/assets/navlamp.js @@ -0,0 +1,44 @@ +// The sidebar's sub-lists light the row under the pointer as a torch would: +// brightest where it points, the rest of that row a step up from the +// others, which keep their own ink (app.css, .nav-s > span.lit). The light +// follows the pointer with a little lag and fades out as it moves on; with +// reduced motion it is where the pointer is at once. Imported by app.js +// when a sub-list is first drawn; a touch never lights a row. +document.documentElement.classList.add("navlamp"); +const lit = new Map(); // a row's word -> how lit it is, 0 to 1 +let at = null, tx = 0, x = 0, raf = 0; + +function frame() { + raf = 0; + const still = calm(), row = at && at.isConnected && !at.parentElement.classList.contains("cur") ? at : null; + if (row && !lit.has(row)) lit.set(row, 0); + x = still ? tx : x + (tx - x) * 0.22; + let busy = Math.abs(tx - x) > 0.3; + for (const [s, v] of lit) { + const to = s === row ? 1 : 0, now = still ? to : v + (to - v) * (to > v ? 0.22 : 0.1); + if (!to && (now < 0.01 || !s.isConnected)) { + lit.delete(s); + s.classList.remove("lit"); + s.style.removeProperty("--si"); + s.style.removeProperty("--lx"); + continue; + } + if (Math.abs(to - now) > 0.002) busy = true; + lit.set(s, now); + s.classList.add("lit"); + s.style.setProperty("--si", now.toFixed(3)); + s.style.setProperty("--lx", (x - s.getBoundingClientRect().left).toFixed(1) + "px"); + } + if (busy) raf = requestAnimationFrame(frame); +} +const go = () => { if (!raf) raf = requestAnimationFrame(frame); }; + +document.addEventListener("pointermove", (e) => { + const row = e.pointerType === "touch" || !e.target.closest ? null : e.target.closest(".nav-sub > .nav-s"); + const word = row && row.querySelector("span"); + if (word && !at) x = e.clientX; + tx = e.clientX; + if (word !== at) at = word; + if (at || lit.size) go(); +}, { passive: true }); +document.documentElement.addEventListener("pointerleave", () => { at = null; go(); }); diff --git a/internal/gui/tests/nav-lamp.test.cjs b/internal/gui/tests/nav-lamp.test.cjs new file mode 100644 index 000000000..463298a6e --- /dev/null +++ b/internal/gui/tests/nav-lamp.test.cjs @@ -0,0 +1,135 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// The sidebar's sub-list lights the row under the pointer as a torch would +// (navlamp.js, app.css .nav-s > span.lit): the word is brightest where the +// pointer is and falls off along the row, the rest of the row a little +// over the others; only that row is lit, the next row takes the light as +// the pointer moves to it, and it goes out when the pointer leaves. The +// selected row is never lit, a touch lights nothing, and with reduced +// motion the light is there at once. Read from the pixels: the ink near +// the pointer stands further off the ground than the word's far end, and +// both ends turn when the pointer goes to the other end. Both engines, +// light and dark, English and Chinese. +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"); + +function serve(lang, theme) { + 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:${JSON.stringify(theme)},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 } }); + if (url.pathname === "/api/settings") return json({ lang, theme, tray: "panel", version: "0.1.400", dir: "~/.config/magpie", gateway: "http://127.0.0.1:3425", lanURLs: [], visionModels: [], imageGenModels: [], workbuddyCheckins: [], redactWords: [] }); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/usage/quotas") return json([]); + if (url.pathname === "/api/groups") return json({ groups: [], pools: [] }); + 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: "" }); } + }; +} + +// each sub-row's word: its box, how lit it is, and whether it is the selection +const words = (page) => page.$$eval(".nav-sub > .nav-s > span", (all) => all.map((s) => { + const b = s.getBoundingClientRect(); + return { x: b.left, y: b.top, w: b.width, h: b.height, lit: s.classList.contains("lit"), si: +(s.style.getPropertyValue("--si") || 0), cur: s.parentElement.classList.contains("cur") }; +})); +// how far the ink stands off the ground in a word's first and last fifth: +// the most any pixel's luminance differs from the corner's +async function ink(page, w) { + const png = await page.screenshot({ clip: { x: w.x, y: w.y, width: w.w, height: w.h } }); + return page.evaluate(async (src) => { + const img = new Image(); + img.src = "data:image/png;base64," + src; + await img.decode(); + const c = document.createElement("canvas"); + c.width = img.width; c.height = img.height; + const g = c.getContext("2d"); + g.drawImage(img, 0, 0); + const d = g.getImageData(0, 0, c.width, c.height).data; + const lum = (i) => 0.2126 * d[i] + 0.7152 * d[i + 1] + 0.0722 * d[i + 2]; + const ground = lum(0), fifth = Math.max(1, Math.floor(c.width / 5)); + const part = (from) => { let m = 0; for (let y = 0; y < c.height; y++) for (let x = from; x < from + fifth; x++) m = Math.max(m, Math.abs(lum((y * c.width + x) * 4) - ground)); return m; }; + return { start: part(0), end: part(c.width - fifth) }; + }, png.toString("base64")); +} + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: the sub-list's row under the pointer is lit as by a torch`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const theme of ["dark", "light"]) for (const lang of ["en", "zh"]) await t.test(`${theme} ${lang}`, async () => { + const context = await browser.newContext({ viewport: { width: 1024, height: 800 }, colorScheme: theme }); + t.after(() => context.close()); + 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, theme)); + await page.goto("http://magpie.test/?view=settings"); + await page.waitForSelector(".nav-sub > .nav-s + .nav-s"); + await page.waitForFunction(() => document.documentElement.classList.contains("navlamp")); + await page.waitForTimeout(600); // the list's own unrolling + let all = await words(page); + assert(all.every((w) => !w.lit), "nothing lit before the pointer comes"); + // the longest word not selected, and another + const free = all.map((w, i) => ({ ...w, i })).filter((w) => !w.cur); + const long = free.reduce((a, b) => (b.w > a.w ? b : a)); + const other = free.find((w) => w.i !== long.i); + const lit = (i) => page.waitForFunction((i) => { const s = document.querySelectorAll(".nav-sub > .nav-s > span")[i]; return s.classList.contains("lit") && +s.style.getPropertyValue("--si") > 0.99; }, i); + const out = (i) => page.waitForFunction((i) => !document.querySelectorAll(".nav-sub > .nav-s > span")[i].classList.contains("lit"), i); + + // at the word's start, the start stands off the ground more than its end + await page.mouse.move(long.x + 2, long.y + long.h / 2); + await lit(long.i); + await page.waitForTimeout(400); // the light catching the pointer up + const atStart = await ink(page, long); + assert(atStart.start > atStart.end + 12, `brightest where the pointer is: ${JSON.stringify(atStart)}`); + all = await words(page); + assert.deepEqual(all.filter((w) => w.lit).length, 1, "only the row under the pointer is lit"); + // moved to its end, the light follows + await page.mouse.move(long.x + long.w - 2, long.y + long.h / 2, { steps: 8 }); + await page.waitForTimeout(600); + const atEnd = await ink(page, long); + assert(atEnd.end > atEnd.start + 12, `the light follows the pointer: ${JSON.stringify(atEnd)}`); + + // to another row: it takes the light and the first goes out + await page.mouse.move(other.x + 4, other.y + other.h / 2, { steps: 4 }); + await lit(other.i); + await out(long.i); + // off the list: all out + await page.mouse.move(600, 500); + await out(other.i); + + // the selection is never lit + const cur = all.find((w) => w.cur); + if (cur) { + await page.mouse.move(cur.x + 4, cur.y + cur.h / 2); + await page.waitForTimeout(200); + assert((await words(page)).every((w) => !w.lit), "the selected row keeps its own ink"); + } + + // a touch lights nothing + await page.mouse.move(600, 500); + await page.evaluate(({ x, y }) => document.elementFromPoint(x, y).dispatchEvent(new PointerEvent("pointermove", { bubbles: true, clientX: x, clientY: y, pointerType: "touch" })), { x: long.x + 4, y: long.y + long.h / 2 }); + await page.waitForTimeout(200); + assert((await words(page)).every((w) => !w.lit), "a touch lights no row"); + + // reduced motion: lit in the first frame + await page.emulateMedia({ reducedMotion: "reduce", colorScheme: theme }); + await page.mouse.move(long.x + 4, long.y + long.h / 2); + await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))); + const now = (await words(page))[long.i]; + assert(now.lit && now.si === 1, `lit at once with reduced motion: ${JSON.stringify(now)}`); + assert.deepEqual(errors, []); + }); + }); +} diff --git a/internal/gui/tests/old-webkit.test.cjs b/internal/gui/tests/old-webkit.test.cjs index e0f9497be..1dc06013a 100644 --- a/internal/gui/tests/old-webkit.test.cjs +++ b/internal/gui/tests/old-webkit.test.cjs @@ -146,9 +146,9 @@ test("compat.js is the first of the page's own scripts", async () => { // value first, ruleedit.js on a masking rule's Edit, ratewindows.js when // a provider editor draws its rate windows, traywindows.js when the menu // bar shows a card, autoresetown.js when Settings draws Codex's auto-use - // rows, registry.js when Plugins draws its npm registry; the checks above - // still read them - for (const f of await scripts()) if (!["webfonts.js", "cfgblocked.js", "signsheets.js", "ruleedit.js", "ratewindows.js", "traywindows.js", "autoresetown.js", "registry.js"].includes(f)) assert(all.includes(f), f + " is not loaded by index.html"); + // rows, registry.js when Plugins draws its npm registry, navlamp.js when + // the sidebar draws a sub-list; the checks above still read them + for (const f of await scripts()) if (!["webfonts.js", "cfgblocked.js", "signsheets.js", "ruleedit.js", "ratewindows.js", "traywindows.js", "autoresetown.js", "registry.js", "navlamp.js"].includes(f)) assert(all.includes(f), f + " is not loaded by index.html"); }); test("the styles hold up in Safari 15.0", async () => {