diff --git a/internal/gui/assets/addfloat.js b/internal/gui/assets/addfloat.js new file mode 100644 index 000000000..0a201c14a --- /dev/null +++ b/internal/gui/assets/addfloat.js @@ -0,0 +1,24 @@ +// addToolsFloat: the add sheet's tools (search, Custom provider, Import…) +// scrolled out of sight show in the title bar's empty right, as Library's, +// Plugins' and Settings' tools do beside the sidebar (app.css +// .add-tools.afloat); scrolled back they are in their row again. The group +// itself is pinned, never a copy, so there is one search, its words and +// focus kept. A narrow window has no room in its bar: the sheet's head is +// held at the top of the page while the sheet is under it, as Settings' +// is (app.css). Each change fades in; a fade only, so reduced motion keeps it. +// app.js renderAdd imports this as the sheet first opens. +window.addToolsFloat = function (drawn) { + const v = $("#view-providers"), g = v.querySelector("#addSheet:not([hidden]) .add-tools"); + if (!g) return; + const head = g.parentElement, sheet = head.parentElement, top = v.getBoundingClientRect().top; + const side = document.body.classList.contains("side-nav"); + const h = head.getBoundingClientRect(); + const up = side && !v.hidden && h.bottom <= top + 2; + head.classList.toggle("stuck", !side && h.top - sheet.getBoundingClientRect().top > 6.5); + if (up === g.classList.contains("afloat")) return; + g.classList.toggle("afloat", up); + self.sideTools?.(); + if (!drawn) g.animate([{ opacity: 0 }, { opacity: 1 }], { duration: up ? 180 : 140, easing: "cubic-bezier(.23, 1, .32, 1)" }); +}; +$("#view-providers").addEventListener("scroll", () => addToolsFloat(), { passive: true }); +addEventListener("resize", () => addToolsFloat()); diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index 691f9322e..11c1ca4b6 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -3471,13 +3471,37 @@ body.window .ag-sk-icon { width: 22px; height: 22px; border-radius: 6px; } .tile .cnt { flex: none; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; } .sheet .row-head .custom { display: inline-flex; align-items: center; gap: 4px; color: var(--accent); } .sheet .row-head .custom:hover, .sheet .row-head .custom.on { color: var(--accent); background: var(--accent-soft); } -/* narrow: the search and Custom provider on a line of their own */ +.sheet .add-tools { display: flex; align-items: center; gap: 6px; min-width: 0; } +/* narrow: the tools on a line of their own */ @media (max-width: 760px) { .sheet .row-head { flex-wrap: wrap; height: auto; row-gap: 8px; } .sheet .row-head::after { content: ""; order: 1; flex-basis: 100%; } - .sheet .row-head .find { order: 2; flex: 1; width: auto; min-width: 0; } - .sheet .row-head .custom { order: 2; } + .sheet .add-tools { order: 2; flex: 1 1 100%; } + .sheet .row-head .find { flex: 1; width: auto; min-width: 0; } } +/* scrolled out of sight beside the sidebar, the tools show in the title + bar's empty right, where Settings' search is (app.js addToolsFloat) */ +body.side-nav #addSheet .add-tools.afloat { + position: fixed; z-index: 11; top: 0; left: var(--tools-l, 50%); right: var(--tools-r, 64px); + height: var(--top-h, 46px); margin: 0; justify-content: flex-end; gap: 10px; + pointer-events: none; --wails-draggable: drag; +} +body.side-nav.mac #addSheet .add-tools.afloat { padding-top: calc(2px / var(--zoom, 1)); } +body.side-nav #addSheet .add-tools.afloat > * { pointer-events: auto; --wails-draggable: no-drag; flex: none; } +body.side-nav #addSheet .add-tools.afloat > .find { flex: 0 1 180px; min-width: 120px; } +/* a narrow window has no room in its bar: the sheet's head stays at the + top while the sheet is under it, as Settings' does */ +body:not(.side-nav) #addSheet > .row-head { position: sticky; top: 0; z-index: 5; --bleed-l: 12px; --bleed-r: 12px; } +body.window:not(.side-nav) #addSheet > .row-head { top: -2px; --bleed-l: 16px; --bleed-r: 16px; } +body:not(.side-nav) #addSheet > .row-head::before { + content: ""; position: absolute; z-index: -1; pointer-events: none; + top: -6px; bottom: -6px; left: calc(-1 * var(--bleed-l)); right: calc(var(--bar, 0px) - var(--bleed-r)); + background: color-mix(in srgb, var(--bg) 92%, transparent); + -webkit-backdrop-filter: saturate(1.6) blur(16px); backdrop-filter: saturate(1.6) blur(16px); + box-shadow: 0 1px 0 transparent; transition: box-shadow .18s var(--ease); +} +body:not(.side-nav) #addSheet > .row-head.stuck::before { box-shadow: 0 1px 0 var(--line-2); } +@supports not (color: color-mix(in srgb, red 50%, blue)) { body:not(.side-nav) #addSheet > .row-head::before { background: var(--bg); } } /* the provider editor, in a dialog */ /* the label column is as wide as its longest label, so that label starts at the padding and the fields end the same way on the right (the diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 334dfbd1f..cb576098a 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -7147,11 +7147,15 @@ function renderAdd() { c.dataset.pick = "custom"; c.title = t("any OpenAI or Anthropic compatible URL"); c.onclick = () => { editing = { custom: true }; draft = null; renderProviders(); }; - head.append(q, c); const imp = el("button", "text", t("Import…")); imp.title = t("Bring over providers set up in other apps"); imp.onclick = openImportApps; - head.append(imp); + // the sheet's tools, one group: scrolled out of sight beside the sidebar + // they show in the title bar (addToolsFloat), the same nodes, so the + // search keeps its words and the focus + const tools = el("span", "add-tools"); + tools.append(q, c, imp); + head.append(tools); if (providers.providers.length) { const x = el("button", "text", t("Close")); x.onclick = closeAddSheet; @@ -7276,6 +7280,8 @@ function renderAdd() { } }; drawTiles(); + // its tools shown in the title bar once out of sight (addfloat.js) + if (self.addToolsFloat) addToolsFloat(true); else import("./addfloat.js").then(() => addToolsFloat(true), () => {}); if (hadFocus && $("#modal").hidden) { (focusPick && sheet.querySelector(`[data-pick="${CSS.escape(focusPick)}"]`) || q).focus(NO_SCROLL); if (selection) q.setSelectionRange(...selection); @@ -23693,6 +23699,7 @@ function layoutNav() { sideWidth(); renderSideToggle(); fitTop(); + self.addToolsFloat?.(true); } // the sidebar's width, the reader's own once dragged (sideDrag), else // app.css's; at most ~a third of a narrow window @@ -24166,7 +24173,7 @@ if (mode === "window") { set("--crumb-max", "none"); return; } - const head = $(":is(#view-library .lib-head, #view-plugins .pm-head, #view-settings .set-head):not([hidden] *)"); + const head = $(":is(#view-library .lib-head, #view-plugins .pm-head, #view-settings .set-head, #addSheet .add-tools.afloat):not([hidden] *)"); const c = crumb.getBoundingClientRect(), gap = parseFloat(getComputedStyle(crumb).columnGap) || 0; const wide = kids.map((e) => (e.tagName === "B" ? e.scrollWidth : e.getBoundingClientRect().width) + gap); const whole = Math.ceil(wide.reduce((w, x) => w + x, 0) - (kids.length ? gap : 0)); @@ -24198,6 +24205,7 @@ if (mode === "window") { set("--tools-l", Math.ceil(c.left + max + 16) + "px"); }); }; + self.sideTools = tools; const ro = new ResizeObserver(tools); for (const e of [$(".top"), $("#crumb"), $(".actions")]) ro.observe(e); addEventListener("resize", tools); diff --git a/internal/gui/tests/add-sheet-float.test.cjs b/internal/gui/tests/add-sheet-float.test.cjs new file mode 100644 index 000000000..21fc1e4dc --- /dev/null +++ b/internal/gui/tests/add-sheet-float.test.cjs @@ -0,0 +1,163 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// The add sheet's tools follow the reader down (the owner, 11-12: 向上滑动, +// 然后这些区域不在可视区域的时候,这些区域应该自动浮现在Header处). Beside +// the sidebar, the sheet's search, Custom provider and Import… scrolled out +// of sight show in the title bar's empty right, left of its buttons, where +// Settings' search is; scrolled back they are in their row again. They are +// the same nodes, so there is one search: what was typed and the focus go +// with it. A narrow window has no room in its bar, so the sheet's head is +// held at the top of the page while the sheet is under it. The page is +// scrolled with the wheel, as a reader does (the app puts back any other +// scroll), and nothing scrolls on a click. Every language, both engines, +// wide and narrow; no backend, the API is faked here. +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 preset = (id, name, kind) => ({ id, name, icon: "openai", kind, chat: `https://api.${id}.example.com/v1`, added: false }); +// enough rows that the sheet runs far past the window +const presets = [ + ...Array.from({ length: 90 }, (_, i) => preset("vendor-" + i, "Vendor " + i, "vendor")), + ...Array.from({ length: 30 }, (_, i) => preset("relay-" + i, "Relay " + i, "relay")), +]; +const providers = [{ id: "openai", name: "OpenAI", icon: "openai", preset: "", models: [], agents: [], key: { set: true, masked: "sk-…ab12" } }]; + +function server(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:"${lang}",theme:"light",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: "light" } }); + if (url.pathname === "/api/providers") return json({ providers, presets, excluded: [], gateway: { running: true, window: true } }); + if (url.pathname === "/api/groups") return json({ groups: [], pools: [] }); + if (url.pathname === "/api/gateway/trace") return json({ routes: [] }); + 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)]; + try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); } + }; +} + +// a wheel step over the view, landed before the next look +async function wheel(page, dy) { + const was = await page.evaluate(() => document.getElementById("view-providers").scrollTop); + const v = await page.locator("#view-providers").boundingBox(); + await page.mouse.move(v.x + v.width / 2, v.y + v.height / 2); + await page.mouse.wheel(0, dy); + return page.waitForFunction((was) => document.getElementById("view-providers").scrollTop !== was, was, { timeout: 1000 }).then(() => true, () => false); +} + +// where the tools are: in the title bar, or in the sheet's head +const where = (page) => page.evaluate(() => { + const r = (e) => e.getBoundingClientRect(); + const head = document.querySelector("#addSheet > .row-head"), find = document.querySelector("#addSheet .find"); + const g = find?.parentElement, view = document.getElementById("view-providers"), bar = r(document.querySelector(".top")); + const f = find && r(find), hit = f && document.elementFromPoint(f.left + f.width / 2, f.top + f.height / 2); + return { + // in the bar: drawn over the title bar, wholly + afloat: !!f && f.top >= bar.top - 1 && f.bottom <= bar.bottom + 1, finds: document.querySelectorAll("#addSheet .find").length, + tools: g && r(g), find: f, head: head && r(head), view: r(view), top: r(document.querySelector(".top")), + actions: r(document.querySelector(".actions")), crumb: r(document.getElementById("crumb")), + clear: !!hit && (hit === find || find.contains(hit)), scroll: view.scrollTop, + focused: document.activeElement === find, value: find?.value, + }; +}); + +const shown = (page) => page.locator("#addSheet .tile .n").evaluateAll((ns) => ns.filter((n) => n.getClientRects().length).map((n) => n.textContent)); + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(engine + ": the add sheet's tools follow the reader down", async (t) => { + assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit"); + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { + for (const width of [1100, 420]) { + await t.test(`${lang} ${width}px`, async () => { + const context = await browser.newContext({ viewport: { width, height: 700 } }); + const page = await context.newPage(); + page.setDefaultTimeout(8000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", server(lang)); + await page.goto("http://magpie.test/?view=providers"); + await page.locator("#addProvider").click(); + const sheet = page.locator("#addSheet"); + await sheet.locator(".tile").first().waitFor(); + await sheet.evaluate((el) => Promise.all(el.getAnimations({ subtree: true }).map((a) => a.finished))); + const side = await page.evaluate(() => document.body.classList.contains("side-nav")); + assert.equal(side, width >= 1024, "the layout"); + + // in sight: in their row, not in the bar + let w = await where(page); + assert(!w.afloat, "in their row while in sight"); + assert(w.find.top >= w.head.top - 1 && w.find.bottom <= w.head.bottom + 1, "in the sheet's head: " + JSON.stringify([w.find, w.head])); + + // a little down, the head still in sight: still in their row + await wheel(page, 40); + w = await where(page); + if (w.head.bottom > w.view.top + 2) assert(!w.afloat, "not in the bar while the row shows"); + + // down with the wheel till the head's row is out of sight + for (let i = 0; i < 60; i++) { + const h = await page.evaluate(() => { + const s = document.querySelector("#addSheet"), v = document.getElementById("view-providers"); + return s.getBoundingClientRect().top + 60 - v.getBoundingClientRect().top; + }); + if (h < 0) break; + if (!(await wheel(page, 100))) break; + } + await page.waitForTimeout(250); + w = await where(page); + assert.equal(w.finds, 1, "one search"); + if (side) { + assert(w.afloat, "in the bar once out of sight"); + assert(w.find.top >= w.top.top - 1 && w.find.bottom <= w.top.bottom + 1, "in the title bar: " + JSON.stringify([w.find, w.top])); + assert(w.tools.right <= w.actions.left + 0.5, "left of the bar's buttons: " + JSON.stringify([w.tools, w.actions])); + assert(w.tools.left >= w.crumb.left, "right of where the reader is"); + // Custom provider and Import… with it (the two with a title); + // Close stays in the sheet + const bs = await page.locator("#addSheet .row-head button[title]").evaluateAll((es) => es.map((e) => e.getBoundingClientRect().toJSON())); + assert.equal(bs.length, 2); + for (const b of bs) assert(b.top >= w.top.top - 1 && b.bottom <= w.top.bottom + 1, "Custom provider and Import… in the bar too: " + JSON.stringify(b)); + } else { + assert(!w.afloat, "no room in a narrow bar"); + assert(Math.abs(w.head.top - w.view.top) <= 4, "the head held at the page's top: " + JSON.stringify([w.head, w.view])); + } + assert(w.clear, "the search is clear to press"); + + // a click on it moves nothing, and typing there finds + const before = w.scroll; + await sheet.locator(".find").click(); + await page.waitForTimeout(150); + w = await where(page); + assert.equal(w.scroll, before, "nothing scrolled on the click"); + assert(w.focused, "the search has the focus"); + await page.keyboard.type("Relay 2"); + await page.waitForTimeout(250); + const names = await shown(page); + assert(names.length > 0 && names.every((n) => n.includes("Relay 2")), "the list found: " + names.join(", ")); + w = await where(page); + assert.equal(w.finds, 1, "still one search"); + assert(w.focused && w.value === "Relay 2", "the focus and the words kept: " + JSON.stringify([w.focused, w.value])); + + // back up: in their row again, the words and the focus with them + for (let i = 0; i < 80 && (await page.evaluate(() => document.getElementById("view-providers").scrollTop)) > 0; i++) { + if (!(await wheel(page, -200))) break; + } + await page.waitForTimeout(250); + w = await where(page); + assert(!w.afloat, "in their row once scrolled back"); + assert(w.find.top >= w.head.top - 1 && w.find.bottom <= w.head.bottom + 1, "back in the sheet's head"); + assert(w.focused && w.value === "Relay 2", "the words and focus kept going back"); + assert.deepEqual(errors, []); + await context.close(); + }); + } + } + }); +} diff --git a/internal/gui/tests/old-webkit.test.cjs b/internal/gui/tests/old-webkit.test.cjs index 25ff4bce9..254a176e6 100644 --- a/internal/gui/tests/old-webkit.test.cjs +++ b/internal/gui/tests/old-webkit.test.cjs @@ -176,9 +176,10 @@ test("compat.js is the first of the page's own scripts", async () => { // autoswitchtip.js when a provider's accounts draw their switch; // codexprojects.js when the open Codex app shows fewer projects; // pipecheck.js when Settings draws Models; + // addfloat.js when the Providers page's add sheet first opens; // 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", "claudemode.js", "ctxtext.js", "resetask.js", "quotaall.js", "quotatable.js", "quitask.js", "checkinnow.js", "proxies.js", "autobackup.js", "scrollends.js", "profilerename.js", "settingshints.js", "keyspend.js", "syncfiles.js", "agentcompact.js", "agentwindow.js", "balfound.js", "passerrors.js", "tlscerts.js", "keybalance.js", "modellimits.js", "notices.js", "videogen.js", "provlinks.js", "setfeats.js", "pageshare.js", "panelfirst.js", "panelarrange.js", "modelfind.js", "agentsmanager.js", "agentsuffix.js", "phonetabs.js", "pluginmd.js", "bark.js", "acctexport.js", "plugincounts.js", "navicons.js", "autoswitchtip.js", "codexprojects.js", "pipecheck.js"].includes(f)) assert(all.includes(f), f + " is not loaded by index.html"); + 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", "claudemode.js", "ctxtext.js", "resetask.js", "quotaall.js", "quotatable.js", "quitask.js", "checkinnow.js", "proxies.js", "autobackup.js", "scrollends.js", "profilerename.js", "settingshints.js", "keyspend.js", "syncfiles.js", "agentcompact.js", "agentwindow.js", "balfound.js", "passerrors.js", "tlscerts.js", "keybalance.js", "modellimits.js", "notices.js", "videogen.js", "provlinks.js", "setfeats.js", "pageshare.js", "panelfirst.js", "panelarrange.js", "modelfind.js", "agentsmanager.js", "agentsuffix.js", "phonetabs.js", "pluginmd.js", "bark.js", "acctexport.js", "plugincounts.js", "navicons.js", "autoswitchtip.js", "codexprojects.js", "pipecheck.js", "addfloat.js"].includes(f)) assert(all.includes(f), f + " is not loaded by index.html"); }); test("the styles hold up in Safari 15.0", async () => {