diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index d625d942a..66319fedd 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -433,22 +433,25 @@ 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. */ +/* the row under the pointer is lit as by a torch (navlamp.js), a page's + row and a sub-list's alike: --lx is the pointer's place in the word, --si + how lit the row is, --lo the row's own ink. 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 { + .navlamp body.side-nav :is(#nav button[data-view], #prefs):hover:not(.on) { color: var(--fg-2); } + .navlamp body.side-nav :is(#nav button[data-view], #prefs) > span { --lo: var(--fg-2); } + :is(.nav-s, #nav button, #prefs) > 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)); + --hot: color-mix(in oklab, var(--hi) calc(var(--si) * 100%), var(--lo, var(--ctl-fg))); + --far: color-mix(in oklab, var(--fg) calc(var(--si) * 15%), var(--lo, 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); } } +@supports (color: oklch(from red round(l) c h)) { :is(.nav-s, #nav button, #prefs) > 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 909ff87ba..499c3ae2c 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -23471,8 +23471,7 @@ document.fonts?.addEventListener("loadingdone", fitTop); // ("Work / Library / MCP servers"). Narrower, and magpie web on a phone, // the pages go back into the header as tabs and each page shows its own // tabs again, as iPadOS's sidebar turns into a tab bar when it has no room. -// It is the same #nav, moved between the two. (app.css's side-nav rules -// hold at the same width: narrower than Usage's requests, 768 points) +// It is the same #nav, moved between the two. var SIDE_AT = 1024; var sideQuery = matchMedia(`(min-width: ${SIDE_AT}px)`); function sideNav() { return document.body.classList.contains("side-nav"); } @@ -23500,6 +23499,7 @@ function layoutNav() { side.hidden = !on; $("#crumb").hidden = !on; if (on) { + side.addEventListener("pointerover", () => import("./navlamp.js").catch(() => {}), { once: true }); side.querySelector(".side-top").append(btn, brand); side.querySelector(".side-scroll").append(nav); side.querySelector(".side-foot").prepend(prefs); @@ -23717,7 +23717,6 @@ 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")); diff --git a/internal/gui/assets/navlamp.js b/internal/gui/assets/navlamp.js index 0ed69bb6d..394c26525 100644 --- a/internal/gui/assets/navlamp.js +++ b/internal/gui/assets/navlamp.js @@ -1,16 +1,18 @@ -// The sidebar's sub-lists light the row under the pointer as a torch would: +// The sidebar lights the row under the pointer as a torch would, in its +// pages (Providers, Routing…, Settings) and their sub-lists alike: // 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 +// others, which keep their own ink (app.css, 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. +// 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. 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; + const still = calm(), row = at && at.isConnected && !at.parentElement.matches(".cur, .on") ? 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; @@ -34,8 +36,8 @@ function 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"); + const row = e.pointerType === "touch" || !e.target.closest ? null : e.target.closest(".nav-sub > .nav-s, body.side-nav :is(#nav > button[data-view], #prefs)"); + const word = row && row.querySelector(":scope > span"); if (word && !at) x = e.clientX; tx = e.clientX; if (word !== at) at = word; diff --git a/internal/gui/tests/nav-lamp.test.cjs b/internal/gui/tests/nav-lamp.test.cjs index 463298a6e..ed64f8bab 100644 --- a/internal/gui/tests/nav-lamp.test.cjs +++ b/internal/gui/tests/nav-lamp.test.cjs @@ -8,7 +8,8 @@ // 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. +// light and dark, English and Chinese. The sidebar's page rows (Providers, +// Routing, Gateway…) and Settings are lit the same way. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); const path = require("node:path"); @@ -76,7 +77,9 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", 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.mouse.move(40, 300); // over the sidebar, which loads the lamp await page.waitForFunction(() => document.documentElement.classList.contains("navlamp")); + await page.mouse.move(700, 500); 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"); @@ -133,3 +136,70 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", }); }); } + +// a page row's word (Providers, Routing…, Settings): its box, whether lit, +// whether its page is open +const tops = (page) => page.$$eval("#side :is(#nav > button[data-view], #prefs) > span", (all) => all.filter((s) => s.offsetWidth).map((s) => { + const b = s.getBoundingClientRect(); + return { x: b.left, y: b.top, w: b.width, h: b.height, lit: s.classList.contains("lit"), on: s.parentElement.classList.contains("on"), id: s.parentElement.dataset.view || s.parentElement.id }; +})); + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: a page row in the sidebar 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=agents"); + await page.waitForSelector("body.side-nav #side #nav > button[data-view]"); + await page.mouse.move(40, 300); // over the sidebar, which loads the lamp + await page.waitForFunction(() => document.documentElement.classList.contains("navlamp")); + await page.mouse.move(700, 500); + await page.waitForTimeout(400); + let all = await tops(page); + assert(all.length >= 4 && all.some((w) => w.id === "prefs"), `the page rows and Settings: ${JSON.stringify(all.map((w) => w.id))}`); + assert(all.every((w) => !w.lit), "nothing lit before the pointer comes"); + const at = (id) => all.findIndex((w) => w.id === id); + const lit = (i) => page.waitForFunction((i) => { const s = [...document.querySelectorAll("#side :is(#nav > button[data-view], #prefs) > span")].filter((s) => s.offsetWidth)[i]; return s.classList.contains("lit") && +s.style.getPropertyValue("--si") > 0.99; }, i); + const out = (i) => page.waitForFunction((i) => ![...document.querySelectorAll("#side :is(#nav > button[data-view], #prefs) > span")].filter((s) => s.offsetWidth)[i].classList.contains("lit"), i); + const pv = at("providers"), gw = at("gateway"), prefs = at("prefs"), word = all[pv]; + + // at the word's start, the start stands off the ground more than its end + await page.mouse.move(word.x + 2, word.y + word.h / 2); + await lit(pv); + await page.waitForTimeout(400); + const atStart = await ink(page, word); + assert(atStart.start > atStart.end + 12, `brightest where the pointer is: ${JSON.stringify(atStart)}`); + assert.equal((await tops(page)).filter((w) => w.lit).length, 1, "only the row under the pointer is lit"); + await page.mouse.move(word.x + word.w - 2, word.y + word.h / 2, { steps: 8 }); + await page.waitForTimeout(600); + const atEnd = await ink(page, word); + assert(atEnd.end > atEnd.start + 12, `the light follows the pointer: ${JSON.stringify(atEnd)}`); + + // to Gateway, then Settings: each takes the light from the one before + await page.mouse.move(all[gw].x + 4, all[gw].y + all[gw].h / 2, { steps: 4 }); + await lit(gw); + await out(pv); + await page.mouse.move(all[prefs].x + 4, all[prefs].y + all[prefs].h / 2, { steps: 4 }); + await lit(prefs); + await out(gw); + await page.mouse.move(700, 500); + await out(prefs); + + // the open page's row keeps its own ink + const cur = all.find((w) => w.on); + assert(cur, "a page is open"); + await page.mouse.move(cur.x + 4, cur.y + cur.h / 2); + await page.waitForTimeout(200); + assert((await tops(page)).every((w) => !w.lit), "the open page's row is never lit"); + assert.deepEqual(errors, []); + }); + }); +} diff --git a/internal/gui/tests/old-webkit.test.cjs b/internal/gui/tests/old-webkit.test.cjs index 0bb9bc295..7ec7a0084 100644 --- a/internal/gui/tests/old-webkit.test.cjs +++ b/internal/gui/tests/old-webkit.test.cjs @@ -147,8 +147,8 @@ test("compat.js is the first of the page's own scripts", async () => { // 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, navlamp.js when - // the sidebar draws a sub-list, whatsnew.js when release notes open; the - // checks above still read them + // the pointer comes over the sidebar, whatsnew.js when release notes + // open; 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", "whatsnew.js"].includes(f)) assert(all.includes(f), f + " is not loaded by index.html"); });