diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index 1e398742e..3c22075d3 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -558,8 +558,19 @@ body.side-nav #nav button.has-dot.can-fold::after { right: 28px; } 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; } + /* the row's icon takes the light too, from its own ink: as bright as the + word would be at its middle (--iw, navlamp.js), and with no lag of its + own, since the light already moves */ + :is(.nav-s, #nav button, #prefs) > :is(svg, .nav-ic, .nav-s-ic).lit { + --lo: var(--muted); + --hi: var(--fg); + --hot: color-mix(in oklab, var(--hi) calc(var(--si) * 100%), var(--lo)); + --far: color-mix(in oklab, var(--fg) calc(var(--si) * 15%), var(--lo)); + color: color-mix(in oklab, var(--hot) calc(var(--iw) * 100%), var(--far)); + transition: none; + } } -@supports (color: oklch(from red round(l) c h)) { :is(.nav-s, #nav button, #prefs, .side-gw) > span.lit { --hi: oklch(from var(--fg) round(l) 0 h); } } +@supports (color: oklch(from red round(l) c h)) { :is(.nav-s, #nav button, #prefs, .side-gw) > span.lit, :is(.nav-s, #nav button, #prefs) > :is(svg, .nav-ic, .nav-s-ic).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/navlamp.js b/internal/gui/assets/navlamp.js index 43591de2f..3c7a5e0d8 100644 --- a/internal/gui/assets/navlamp.js +++ b/internal/gui/assets/navlamp.js @@ -7,9 +7,19 @@ // reduced motion it is where the pointer is at once. Imported by app.js // when the pointer first comes over the sidebar (layoutNav), not at start, // where it held up the open page's row; a touch never lights a row. +// A row's icon takes the same light as its word (app.css, .lit icons): it +// is as bright as the word's ink would be at the icon's middle, so it +// lights fully under the pointer and dims as the pointer goes along the +// word. The gateway's status dot keeps its colour. 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; +// a row's icon: a sub-row's, a page's, Settings' gear +const ICON = ":scope > :is(svg, .nav-ic, .nav-s-ic)"; +// how much of the torch's heart is in the word's ink at a distance from the +// pointer, as a share of the light's 36px reach: the gradient's stops in +// app.css (all of it to 12%, 45% of it at 45%, none at the edge) +const heat = (d) => d <= 0.12 ? 1 : d <= 0.45 ? 1 - (d - 0.12) / 0.33 * 0.55 : Math.max(0, 0.45 * (1 - (d - 0.45) / 0.55)); function frame() { raf = 0; @@ -19,11 +29,15 @@ function frame() { 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); + const ic = s.parentElement && s.parentElement.querySelector(ICON); if (!to && (now < 0.01 || !s.isConnected)) { lit.delete(s); - s.classList.remove("lit"); - s.style.removeProperty("--si"); - s.style.removeProperty("--lx"); + for (const e of [s, ic]) if (e) { + e.classList.remove("lit"); + e.style.removeProperty("--si"); + e.style.removeProperty("--lx"); + e.style.removeProperty("--iw"); + } continue; } if (Math.abs(to - now) > 0.002) busy = true; @@ -31,6 +45,12 @@ function frame() { s.classList.add("lit"); s.style.setProperty("--si", now.toFixed(3)); s.style.setProperty("--lx", (x - s.getBoundingClientRect().left).toFixed(1) + "px"); + if (ic) { + const b = ic.getBoundingClientRect(); + ic.classList.add("lit"); + ic.style.setProperty("--si", now.toFixed(3)); + ic.style.setProperty("--iw", heat(Math.abs(x - (b.left + b.width / 2)) / 36).toFixed(3)); + } } if (busy) raf = requestAnimationFrame(frame); } diff --git a/internal/gui/tests/nav-lamp-icon.test.cjs b/internal/gui/tests/nav-lamp-icon.test.cjs new file mode 100644 index 000000000..5e788aef6 --- /dev/null +++ b/internal/gui/tests/nav-lamp-icon.test.cjs @@ -0,0 +1,130 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// The sidebar's torch (navlamp.js) lights a row's icon as well as its word: +// a sub-list row's icon (Settings' General, Appearance…) and a page row's +// (Providers), and Settings' gear. Under the pointer the icon is lit as +// the word's heart is, near the text's own ink and well off its resting +// --muted; with the pointer at the word's far end it is lit less; and once +// the pointer leaves it is back to --muted. The selected row's icon is +// never lit. Both engines, every language, light and dark, at the +// narrowest width the sidebar shows and a wide one. +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,sideGateway:{url:"http://127.0.0.1:3425",running: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 } }); + 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: "" }); } + }; +} + +// an icon's state: its box, its colour as sRGB, whether lit, and the +// colours it is judged against (--muted, its resting ink; --fg, the text's) +const look = (page, sel) => page.$eval(sel, (ic) => { + const c = document.createElement("canvas").getContext("2d", { willReadFrequently: true }); + const rgb = (v) => { c.clearRect(0, 0, 1, 1); c.fillStyle = "#010203"; c.fillStyle = v; c.fillRect(0, 0, 1, 1); return [...c.getImageData(0, 0, 1, 1).data.slice(0, 3)]; }; + const root = getComputedStyle(document.documentElement), b = ic.getBoundingClientRect(); + return { x: b.left + b.width / 2, y: b.top + b.height / 2, lit: ic.classList.contains("lit"), raw: getComputedStyle(ic).color, color: rgb(getComputedStyle(ic).color), muted: rgb(root.getPropertyValue("--muted").trim()), fg: rgb(root.getPropertyValue("--fg").trim()) }; +}); +const far = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); + +async function lights(page, sel, word, label) { + await page.mouse.move(700, 600); + await page.waitForFunction((s) => !document.querySelector(s).classList.contains("lit"), sel); + await page.waitForTimeout(300); // its own colour transition settling + const rest = await look(page, sel); + assert(far(rest.color, rest.muted) < 6, `${label} rests in --muted: ${JSON.stringify(rest)}`); + const span = far(rest.muted, rest.fg); + // under the pointer: lit as the word's heart + await page.mouse.move(rest.x, rest.y); + await page.waitForFunction((s) => { const e = document.querySelector(s); return e.classList.contains("lit") && +e.style.getPropertyValue("--si") > 0.99; }, sel); + await page.waitForTimeout(400); // the light catching the pointer up + const on = await look(page, sel); + assert(far(on.color, on.muted) > span * 0.6 && far(on.color, on.fg) < span * 0.6, `${label} lit under the pointer: ${JSON.stringify(on)}`); + // at the word's far end: lit less + const w = await page.$eval(word, (s) => { const b = s.getBoundingClientRect(); return { x: b.right - 2, y: b.top + b.height / 2 }; }); + await page.mouse.move(w.x, w.y, { steps: 6 }); + await page.waitForTimeout(600); + const end = await look(page, sel); + assert(far(end.color, end.muted) + 8 < far(on.color, on.muted), `${label} lit less with the pointer at the word's end: ${JSON.stringify({ on: on.color, end: end.color })}`); + // gone: back to --muted + await page.mouse.move(700, 600); + await page.waitForFunction((s) => !document.querySelector(s).classList.contains("lit"), sel); + await page.waitForTimeout(300); + const back = await look(page, sel); + assert(far(back.color, back.muted) < 6, `${label} back in --muted: ${JSON.stringify(back)}`); +} + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: the torch lights a sidebar row's icon too`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + const langs = ["en", "zh", "zh-TW", "ja", "de"]; + for (const [i, lang] of langs.entries()) for (const width of [1024, 1440]) { + const theme = (i + width) % 2 ? "dark" : "light"; + await t.test(`${lang} ${width}px ${theme}`, async () => { + const context = await browser.newContext({ viewport: { width, 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("body.side-nav .nav-sub > .nav-s + .nav-s"); + await page.mouse.move(40, 300); // over the sidebar, which loads the lamp + await page.waitForFunction(() => document.documentElement.classList.contains("navlamp")); + await page.waitForTimeout(600); // the list's own unrolling + + // a sub-list row with an icon, not the selected one + const n = await page.$$eval(".nav-sub > .nav-s", (all) => all.findIndex((b) => !b.classList.contains("cur") && b.querySelector(":scope > svg") && (b.dataset.lampTest = "1"))); + assert(n >= 0, "a sub-list row with an icon"); + const row = `.nav-sub > .nav-s[data-lamp-test]`; + await lights(page, `${row} > svg`, `${row} > span`, "a sub-list row's icon"); + // a page row's (Providers); Settings' gear is open here, so it is + // tried from another page below + await lights(page, `#nav > button[data-view="providers"] > .nav-ic`, `#nav > button[data-view="providers"] > span`, "a page row's icon"); + + // the selected sub-row's icon is never lit + const cur = await page.$(".nav-sub > .nav-s.cur > svg"); + if (cur) { + const b = await cur.boundingBox(); + await page.mouse.move(b.x + b.width / 2, b.y + b.height / 2); + await page.waitForTimeout(200); + assert(!(await cur.evaluate((e) => e.classList.contains("lit"))), "the selected row's icon keeps its own ink"); + } + + await page.mouse.move(700, 600); + await page.goto("http://magpie.test/?view=agents"); + await page.waitForSelector("body.side-nav #prefs > svg"); + await page.mouse.move(40, 300); + await page.waitForFunction(() => document.documentElement.classList.contains("navlamp")); + await lights(page, "#prefs > svg", "#prefs > span", "Settings' gear"); + // the open page's icon is never lit + const open = await page.$("#nav > button.on > .nav-ic"); + const b = await open.boundingBox(); + await page.mouse.move(b.x + b.width / 2, b.y + b.height / 2); + await page.waitForTimeout(200); + assert(!(await open.evaluate((e) => e.classList.contains("lit"))), "the open page's icon keeps its own ink"); + assert.deepEqual(errors, []); + }); + } + }); +}