diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index 3dfed98e9..e4214917d 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -396,6 +396,9 @@ body:not(.side-nav) #nav button[data-view="groups"], body.side-nav .rt-togroups tabs under it, Settings and the gateway at the foot; the same #nav that is the header's tabs otherwise */ .nav-gl, .nav-ic, .side-l, .crumb { display: none; } +.nav-pic { width: 16px; height: 16px; } +.nav-pic > * { display: block; width: 100%; height: 100%; object-fit: contain; } +.nav-pic > .mask { background: currentColor; -webkit-mask: var(--i) center / contain no-repeat; mask: var(--i) center / contain no-repeat; } /* at any width the reader picks it (app.js layoutNav); in a narrow window it gives the page more of the room */ body.side-nav { --side-w: 212px; padding-left: var(--side-w); } diff --git a/internal/gui/assets/pluginpages.js b/internal/gui/assets/pluginpages.js index 37993b02a..aa30e17b8 100644 --- a/internal/gui/assets/pluginpages.js +++ b/internal/gui/assets/pluginpages.js @@ -190,12 +190,16 @@ b.dataset.view = e.key; b.dataset.pluginPage = ""; if (e.icon) { - const img = document.createElement("img"); - img.className = "nav-ic"; + // a mark in one near-black or near-white tone, as a currentColor + // SVG is in an (black), is drawn in the tab's own ink, as the + // built-in pages' marks are (shannnnnks on X: Day card's was black + // on the dark sidebar); a coloured picture is drawn as it is + const box = el("i", "nav-ic nav-pic"), img = el("img"); img.src = e.icon; img.alt = ""; - img.width = img.height = 16; - b.append(img); + box.append(img); + b.append(box); + toneCheck(img); } else { const s = svg(PUZZLE, 16, 1.5); s.classList.add("nav-ic"); diff --git a/internal/gui/tests/fixtures/day-card-icon.svg b/internal/gui/tests/fixtures/day-card-icon.svg new file mode 100644 index 000000000..fa8c56d15 --- /dev/null +++ b/internal/gui/tests/fixtures/day-card-icon.svg @@ -0,0 +1 @@ + diff --git a/internal/gui/tests/plugin-page-icon-tone.test.cjs b/internal/gui/tests/plugin-page-icon-tone.test.cjs new file mode 100644 index 000000000..de9df6a26 --- /dev/null +++ b/internal/gui/tests/plugin-page-icon-tone.test.cjs @@ -0,0 +1,141 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// shannnnnks on X (magpie in Docker, v0.1.4059): Day card's icon in the +// sidebar was black on the dark sidebar. Its icon.svg (saved here as it is) +// draws with currentColor, and a plugin page's tab drew it as an , +// where currentColor is black whatever the theme. A plugin page's mark in +// one near-black or near-white tone is now drawn as the built-in pages' +// marks are, in the tab's own ink (toneCheck, a mask), and a coloured +// picture as it is. Both themes, the desktop and the web window, every +// language, the phone's More list, Chromium and WebKit; the ink that stands +// apart from the sidebar's ground is measured on a screenshot. +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"); +const DAY = "a".repeat(32), WHITE = "b".repeat(32), GLANCE = "c".repeat(32); +const icons = { + [DAY]: () => fs.readFile(path.join(__dirname, "fixtures/day-card-icon.svg")), + [WHITE]: async () => ``, + [GLANCE]: () => fs.readFile(path.resolve(__dirname, "../testdata/page-plugin/page/icon.svg")), +}; +const entry = (tok, name, title) => ({ spec: "/x/" + name, name, key: "plugin-" + name, title, reads: [], url: `/_magpie/plugin/${tok}/index.html`, icon: `/_magpie/plugin/${tok}/icon.svg` }); +const pages = [entry(DAY, "day-card", "Day card"), entry(WHITE, "white", "White mark"), entry(GLANCE, "glance", "Usage glance")]; + +function server(lang, theme, web) { + 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:"${theme}",web:${web}};` }); + 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, web }); + if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true }, plugins: [] }); + if (url.pathname === "/api/groups") return json({ groups: [], pools: [] }); + if (url.pathname === "/api/plugin-pages") return json(pages); + if (url.pathname.startsWith("/api/")) return json({}); + const m = url.pathname.match(/^\/_magpie\/plugin\/([0-9a-f]{32})\/icon\.svg$/); + if (m && icons[m[1]]) return route.fulfill({ contentType: "image/svg+xml", body: await icons[m[1]]() }); + if (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: "" }); } + }; +} + +// The share of each box's pixels that stand apart from the ground (a +// colour distance over 48 of 441), read from one screenshot of the page; +// the ground is read just left of the box, in its tab. +async function inked(page, sel) { + const shot = (await page.screenshot()).toString("base64"); + return page.evaluate(async ([shot, sel]) => { + const img = new Image(); + img.src = "data:image/png;base64," + shot; + await img.decode(); + const c = document.createElement("canvas"); + c.width = img.naturalWidth; c.height = img.naturalHeight; + const x = c.getContext("2d", { willReadFrequently: true }); + x.drawImage(img, 0, 0); + const k = img.naturalWidth / innerWidth, out = {}; + for (const e of document.querySelectorAll(sel)) { + const b = e.getBoundingClientRect(); + const L = Math.round(b.left * k), T = Math.round(b.top * k), W = Math.round(b.width * k), H = Math.round(b.height * k); + const d = x.getImageData(L, T, W, H).data; + const g = x.getImageData(L - 3, T + (H >> 1), 1, 1).data; + let n = 0; + for (let i = 0; i < d.length; i += 4) if (Math.hypot(d[i] - g[0], d[i + 1] - g[1], d[i + 2] - g[2]) > 48) n++; + out[e.closest("button").dataset.view] = n / (W * H); + } + return out; + }, [shot, sel]); +} + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: a plugin page's one-tone mark is drawn in the sidebar's ink`, 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 web of [false, true]) for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { + await t.test(`${theme} ${web ? "web" : "window"} ${lang}`, async () => { + const context = await browser.newContext({ viewport: { width: 1100, height: 760 }, reducedMotion: "reduce" }); + const page = await context.newPage(); + page.setDefaultTimeout(8000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", server(lang, theme, web)); + await page.goto("http://magpie.test/"); + assert.ok(await page.evaluate(() => document.body.classList.contains("side-nav")), "the sidebar"); + const tab = (k) => page.locator(`#nav button[data-view="plugin-${k}"]`); + await tab("day-card").locator(".nav-pic .mask").waitFor(); + await tab("white").locator(".nav-pic .mask").waitFor(); + await tab("glance").locator(".nav-pic img").waitFor(); + await page.waitForFunction(() => [...document.querySelectorAll("#nav .nav-pic img")].every((i) => i.complete)); + assert.equal(await tab("day-card").locator("img").count(), 0, "Day card's picture gave way to its mask"); + assert.equal(await tab("glance").locator(".mask").count(), 0, "a coloured picture is drawn as it is"); + for (const k of ["day-card", "white"]) { + const [paint, ink, w] = await tab(k).locator(".nav-pic .mask").evaluate((m) => [getComputedStyle(m).backgroundColor, getComputedStyle(document.querySelector('#nav button[data-view="usage"] .nav-ic')).color, m.getBoundingClientRect().width]); + assert.equal(paint, ink, `${k}: in the ink a built-in page's mark is`); + assert.equal(w, 16, `${k}: the size the built-in pages' marks are`); + } + // the tab's list read again (a plugin turned on, a language picked) + // retitles the tab and keeps its mark + await page.evaluate(() => window.loadPluginPages()); + await page.waitForTimeout(300); + assert.equal(await tab("day-card").locator(":scope > span").textContent(), "Day card", "the title in its own span"); + assert.equal(await tab("day-card").locator(".nav-pic .mask").count(), 1, "the mark kept on a second read"); + await page.waitForTimeout(100); + const ink = await inked(page, "#nav button[data-plugin-page] .nav-pic"); + for (const k of ["plugin-day-card", "plugin-white", "plugin-glance"]) assert.ok(ink[k] > 0.08, `${k} shows on the ${theme} sidebar: ${(ink[k] * 100).toFixed(1)}% inked`); + // the open page's mark in the text's own colour, as a built-in's is + await page.evaluate(() => show("plugin-day-card")); + await page.waitForFunction(() => document.querySelector('#nav button[data-view="plugin-day-card"]').classList.contains("on")); + // the tab's ink eases over .22s (#nav.hl-on): wait it out + await page.waitForFunction(() => { const m = document.querySelector('#nav button[data-view="plugin-day-card"] .mask'); return getComputedStyle(m).backgroundColor === getComputedStyle(document.body).color; }, null, { timeout: 2000 }).catch(() => {}); + const [on, fg] = await page.evaluate(() => [getComputedStyle(document.querySelector('#nav button[data-view="plugin-day-card"] .mask')).backgroundColor, getComputedStyle(document.body).color]); + assert.equal(on, fg, "the open page's mark is the text's colour"); + if (process.env.ARTIFACT_DIR && lang === "zh") await page.locator("#nav").screenshot({ path: path.join(process.env.ARTIFACT_DIR, `nav-ic-${engine}-${theme}-${web ? "web" : "window"}.png`) }); + assert.deepEqual(errors, []); + await context.close(); + }); + } + // on a phone the pages past the tab bar are in More's list, each with + // its tab's mark: Day card's there in the list's ink too + for (const theme of ["dark", "light"]) await t.test(`${theme} phone More`, async () => { + const context = await browser.newContext({ viewport: { width: 390, height: 760 }, reducedMotion: "reduce", hasTouch: true, isMobile: engine === "chromium" }); + const page = await context.newPage(); + page.setDefaultTimeout(8000); + await page.route("**/*", server("en", theme, true)); + await page.goto("http://magpie.test/"); + await page.locator('#nav button[data-view="plugin-day-card"] .nav-pic .mask').waitFor({ state: "attached" }); + await page.locator("#nav .nav-more").tap(); + const menu = page.locator(".nav-more-menu"); + await menu.waitFor({ state: "visible" }); + const item = menu.locator(".pm-item", { hasText: "Day card" }); + const [paint, ink, w] = await item.locator(".nav-pic .mask").evaluate((m) => [getComputedStyle(m).backgroundColor, getComputedStyle(m).color, m.getBoundingClientRect().width]); + assert.equal(paint, ink, "in the list's ink"); + assert.equal(w, 20, "the size More's marks are"); + await context.close(); + }); + }); +} diff --git a/internal/gui/tests/plugin-page.test.cjs b/internal/gui/tests/plugin-page.test.cjs index 122aba6ae..39d7b604d 100644 --- a/internal/gui/tests/plugin-page.test.cjs +++ b/internal/gui/tests/plugin-page.test.cjs @@ -175,7 +175,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", const tabs = page.locator("#nav button[data-plugin-page]"); assert.equal(await tabs.count(), 4, where); assert.equal(await page.evaluate(() => { const h = document.querySelector("#nav .nav-gl[data-plugin-pages]"); return [...h.parentElement.children].indexOf(h) > [...h.parentElement.querySelectorAll("button[data-view='plugins']")].map((b) => [...b.parentElement.children].indexOf(b))[0]; }), true, `${where}: after the built-in pages`); - assert.equal(await tabs.first().locator("img.nav-ic").getAttribute("src"), `/_magpie/plugin/${GLANCE}/icon.svg`, where); + assert.equal(await tabs.first().locator(".nav-ic img").getAttribute("src"), `/_magpie/plugin/${GLANCE}/icon.svg`, where); // titled in the window's language, else by its title assert.equal(await tabs.first().locator("span").textContent(), TITLES[lang] || "Usage glance", `${where}: the tab's title`); if (lang === "de") {