From d5dc8eeccc8c5712916fec01863e409a68c6231a Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 2 Nov 2026 17:09:59 +0800 Subject: [PATCH] gui: Scroll to top and Scroll to bottom on a long page (emo172) #1090 (emo172) asked for back-to-top and back-to-bottom buttons on the main pages. Once a page is scrolled more than one window down, a small capsule with two buttons, Scroll to top and Scroll to bottom, fades in. It sits in the page's right margin when a wide window leaves one, and otherwise over the page's corner, above Providers' sticky Add buttons. It stays hidden near the top of the page, while a dialog is open, and (over the corner) at the page's foot, where the last row can't scroll out from under it, unless it has the pointer or keyboard focus. The page's own height never changes for it, because app.js's scroll guard measures that height. A press goes through scrollOnPurpose, so the window's scroll guard takes it as the reader's own scroll. It scrolls smoothly, or at once under reduced motion, and the button at the end it already reached is aria-disabled. Both buttons are real buttons, reachable by Tab, with a label and tooltip in en, zh, zh-TW, ja and de. They are drawn from the window's tokens in both themes, with no gradient and no coloured edge. scrollends.js is imported on the window's first scroll, so the core scripts carry one line in app.js, paid for by two comments cut from the scroll guard's. No budget was raised. old-webkit lists scrollends.js among the lazy scripts. docs/subsystems/gui-shell.md says where the capsule sits and when it shows. Run on mote at this head (on 2c90922b0, with the next commit): - view-ends.test.cjs, 32 of 32: Chromium and WebKit, light and dark, 420 and 1180 wide, every language. It checks that the capsule shows only past one window, that Scroll to bottom reaches the foot and stays there, and that the page is never taller under the capsule. It checks that the capsule is beside every row, or steps aside at the foot, and that Tab plus Enter on Scroll to top reaches 0 and stays. - click-scroll, i18n-keys and old-webkit: ok. - go test -run Budget ./internal/gui, gofmt -l, and go vet with GOOS=linux -tags nogui and with GOOS=windows: ok. - The whole GUI suite at the pre-rebase head (5f0a84a00 + this): 18,675 tests, 18,370 pass, 305 fail in 51 files. All 51 files were run again at 5f0a84a00 without this change, and 48 were red there too (297 fails). - The other three, import-row, provider-detect-models and routing-many-keys, and every test red at the head but not at the base, were run again alone at the head. They passed, or failed in a different engine or language than the first run, in the same tests that were red at the base. These are known load reds: #4389, #3488, #4585, #3415, #4127, #4126, #4244, #4082, #4619, #4431 and #4035. - old-webkit's red ("profilerename.js is not loaded by index.html") is on main since 1cb4d4301 and is fixed in the next commit. - go test ./internal/gui: red only at TestMainWindowCorners on mote's macOS 27 (#3949), the same as on main. Broken by hand: - At the old code, view-ends is red: the capsule's ::after room made the page taller ("the page grew under the capsule"). That room had also turned add-button, library-discover-jump and account-eye red in the whole suite, which is why the capsule steps aside instead. - Without the step-aside at the foot, view-ends is red. - Without the import hook in app.js, every case is red. - Without scrollOnPurpose, "the page stays at its foot" is red, because the guard puts the scroll back. Not tried: the real Wails app on macOS, Windows and Linux (WebKitGTK), and the tray panel, which has no long page to show it on. --- docs/subsystems/gui-shell.md | 1 + internal/gui/assets/app.css | 9 ++ internal/gui/assets/app.js | 5 +- internal/gui/assets/i18n.js | 8 + internal/gui/assets/scrollends.js | 74 +++++++++ internal/gui/core_budget_test.go | 2 +- internal/gui/tests/old-webkit.test.cjs | 5 +- internal/gui/tests/view-ends.test.cjs | 212 +++++++++++++++++++++++++ 8 files changed, 310 insertions(+), 6 deletions(-) create mode 100644 internal/gui/assets/scrollends.js create mode 100644 internal/gui/tests/view-ends.test.cjs diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index e4afd70ac..853917391 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -503,6 +503,7 @@ covers it in both engines, every language and a narrow window. - Settings' Search APIs add row has a base URL field under the key for every API (#1580): the vendor's own address is its placeholder, picking an API already added fills in its saved URL so a new key alone keeps it, and Add with no key changes only the URL. See [a search API's base URL](gateway-routing.md#a-search-apis-base-url), [`renderSearch`](../../internal/gui/assets/plugins/search/settings.js) and `search-api.test.cjs`. - Every user-visible string has zh, zh-TW, ja and de translations with the same placeholders. `gui-zh-tw.test.cjs`, `gui-ja.test.cjs` and `gui-de.test.cjs` fail on a missing one; `gui-zh-tw.test.cjs` also fails on a zh-TW string with a Simplified character left in it. - A click never moves the page. Code scrolls a view only with the reader's event in hand (`scrollOnPurpose`); `click-scroll.test.cjs` guards this. A click is taken where the reader left the view: a scroll not theirs made just before it, whose scroll event the browser sends only at the next frame, after the click, is put back first (`backToReader` in the click guard, #1554). What was clicked is held where it was (`hold`); a view at its top when clicked stays at its top, and what comes in above the control moves it down instead (#2134, `update-auto.test.cjs`), as long as it moves less than half its own height and so stays under the pointer. When what the click does leaves the page shorter under it (a list emptied), the view keeps room at its foot for it to stay (`.view-room`), room that goes as the reader scrolls back. A control under what it unrolls (`data-unrolls`, "Show 7 more") goes down with what it opens: what's held is the part it is in, so the rows open downwards. Anything larger above it, as the stage a day picked on Routing brings in above the bar, is scrolled past, and the control stays where it was clicked (`routing-days-fit.test.cjs`, `routing-failed-open.test.cjs`). +- A page scrolled a screen down shows Scroll to top and Scroll to bottom (emo172, #1090): a small capsule of two chevron buttons at the open page's lower right, in its right margin when a wide window leaves one, else over its corner; above Providers' sticky Add buttons. It goes again near the page's top and over a dialog, and, over the corner, at the page's foot, where the last row can't scroll out from under it, unless it has the pointer or focus. It never changes the page's height, which the guard below measures. A press is the reader's scroll (`scrollOnPurpose`, then `arrived` once the page stands still), smooth unless motion is reduced; Scroll to bottom is `aria-disabled` at the foot. Each button's name and tooltip are in every language. The code is [`scrollends.js`](../../internal/gui/assets/scrollends.js), imported on the window's first scroll so the core doesn't carry it; the tray panel has none. Home and End scroll a page as before. See `view-ends.test.cjs`. - A scroll not the reader's is put back, so every way the reader scrolls has to count as theirs: the wheel and a trackpad's coast after the lift (WebKit on the Mac plays the coast itself and the page hears its scrolls with no wheel for each, so the wheel starts the same chain a touch fling does, `flings`/`flingUntil`: each scroll keeps the next one the reader's till it rests, and a click ends it; issenn on Discord, `view-trackpad-coast.test.cjs`), a wheel or coast the page is never told of (macOS 13's WebKit, Safari 16.6's, scrolled a view under wheels it never dispatched, so every scroll was put back and only the bar's drag moved the page: three scrolls put back the same way within a second are taken as the reader's run and given back, and so is each one after it that goes on the same way within a second of the last, so a wheel turned slowly or on a loaded Mac loses no step; a scroll by code is one, not a run, and two at rest are still put back; issenn on Discord, `view-wheel-unheard.test.cjs`. Under a click's 4s hold the same holds: only a put-back that comes with the page redrawn since the hold last held (`redrawn`: the view's height or where the first held parts sit moved from `h.lay`) is the hold's and never counts toward a run, so a profile opened on the Agents page and wheeled through at once scrolls, issennknight on Discord, `view-wheel-after-click.test.cjs`. The parts `h.lay` measures are those still in the page that scroll with it: a part drawn again since (the Usage page's tabs, the agents' list under an agent opened or closed) is a redraw once, not at every scroll, and a sticky one (Settings' tabs at a narrow width) is left out, as it sits still while the page scrolls; `view-wheel-after-redraw.test.cjs`), a touch and its fling, the keys, a drag, the scrollbar's own drag (`barDrag`, #1089) and the middle button's autoscroll (`panning`, 多喝热水 on Discord). Chromium and WebView2 give the autoscroll a press of button 1 and then no event of the reader's while it scrolls; from that press the view follows it till Chromium ends it, on the next press (which reaches the page only as a pointerup), a key (a keyup), or the lift after the mouse moved while held. A field the guard brings back into view after putting a scroll back is one focused since the reader last scrolled that view, not one they scrolled away from (`scrolledFrom`): the middle press focuses the button under it. See `view-middle-autoscroll.test.cjs`. - A load that lands puts the view back where the reader left it (`show`'s `back` on the page's first reads, the Providers pages' reload after `render`): what it drew may have pulled the view up, the browser clamping it to a page shorter for a moment. While the reader scrolls, the place last heard is late (the page hears of a scroll a frame or more after it, WebKit scrolling on its own thread), and putting it back took their last steps and stopped their coast each time a load landed under the wheel, so Routing and Usage, scrolled as they opened, shook and snapped back (issennknight on Discord, macOS 13). `backAfterLoad` keeps a view that has gone on the way the reader's own scrolls go (`readerWay`, set from each scroll of theirs) as theirs, and puts back only a move against it, which is a redraw's clamp. The browser's scroll anchoring stays on: as data comes in above, it lowers `scrollTop` with what's on the screen still, which the reader never sees, so the test tells a put-back (the view up, what was under the pointer going down with it) from that. - Where the window's WebKit scrolls under wheels it never tells the page of (macOS 13's, 16.6), each gesture's first scrolls were put back until three the same way made a run (`puttingBack`), a shake at the start of every gesture, the first on a page just opened included. The first run given back is remembered (`deaf`, kept in localStorage as `magpie.unheardWheels` for the next start); from then on a scroll at rest waits 120ms before it is put back (`waitsBack`), and one more the same way within 80ms (a frame or a few; code's two scrolls in a row come further apart and are both put back, `view-wheel-unheard.test.cjs`), with the page's height the same (no redraw between them), makes it the reader's at once. A lone scroll (a redraw's, code's) is still put back, 120ms later; one under a click's hold is put back at once as before. See `view-open-wheel.test.cjs`, which starts the wheel before the first data, tells the page of each scroll 80ms late or never, and counts every write that moves the view up under the wheel. What the wait puts back is recorded as an at-once put-back is, so a wheel heard later than 120ms still gives it back (`givesBack`). diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index a3f52bb2e..6dece2c00 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -2225,6 +2225,15 @@ body.window .foot .status:not(.err):not(.warn) { pointer-events: none; } it shorter and move it */ .status.ok, .status.err, .status.warn { font-size: 12.5px; line-height: 16px; font-weight: 500; padding: 3px 10px; border-radius: 999px; background: color-mix(in srgb, currentColor 16%, var(--bg)); box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 30%, transparent); } .status.lift { position: fixed; z-index: 21; font-size: 12.5px; font-weight: 500; padding: 3px 10px; border-radius: 11px; background: color-mix(in srgb, currentColor 16%, var(--card)); left: 50%; bottom: 18px; transform: translateX(-50%); max-width: calc(100vw - 32px); box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 30%, transparent), 0 6px 20px rgba(0, 0, 0, .28); animation: fade-in .2s var(--ease); } +/* Scroll to top / Scroll to bottom (scrollends.js, #1090): a quiet capsule at + the open page's lower right, in when the page is a screen down */ +.view-ends { position: fixed; z-index: 9; right: 16px; bottom: 16px; display: flex; flex-direction: column; align-items: center; padding: 2px; border-radius: 9px; background: var(--pop-bg); -webkit-backdrop-filter: blur(20px); backdrop-filter: blur(20px); box-shadow: 0 4px 14px rgba(20, 22, 30, .12), 0 0 0 .5px var(--line-2); opacity: 0; visibility: hidden; pointer-events: none; transform: translateY(4px); transition: opacity .18s var(--ease), transform .18s var(--ease), visibility 0s .18s; } +.view-ends.on, .view-ends:focus-within { opacity: 1; visibility: visible; pointer-events: auto; transform: none; transition: opacity .18s var(--ease), transform .18s var(--ease), visibility 0s; } +.view-ends button { display: flex; align-items: center; justify-content: center; width: 26px; height: 24px; border-radius: 7px; color: var(--fg-2); } +.view-ends button:hover { background: var(--pill-hover); color: var(--fg); } +.view-ends button[aria-disabled="true"] { color: var(--faint); background: none; } +.view-ends .ve-sep { width: 14px; height: 1px; margin: 1px 0; background: var(--line-2); } +@media (prefers-reduced-motion: reduce) { .view-ends, .view-ends.on { transition: none; transform: none; } } /* a line longer than its place floats too (status), and is read whole (littleHong): wrapped, up to ten lines, the rest scrolled to; one line is still a pill (22px tall, 11px corners) */ diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 522ee7c36..f11d18d66 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -22708,14 +22708,12 @@ function savePrefs(body) { // // Code moves a view only in answer to a click that asks to go somewhere, and // shows that with the reader's event: scrollOnPurpose(e). Called without one -// (from a load, a timer, a helper that other clicks share) it is refused, -// and whatever scroll follows is put back. +// it is refused, and whatever scroll follows is put back. let purposeUntil = 0, held = null; // guardUntil is purposeUntil when the window is the click guard's own (a // field brought back into sight, backToReader): it lets that scroll stand, // but it isn't the reader's, so an app menu the click opens stays open. let guardUntil = 0; -// A new purposeful scroll supersedes any still-pending quota navigation. const readerScrolls = (ms) => { quotaFocus = ""; purposeUntil = Math.max(purposeUntil, performance.now() + ms); held = null; guardUntil = 0; }; function scrollOnPurpose(e, ms = 1000) { if (!e?.isTrusted || performance.now() - e.timeStamp > 1000) { @@ -23150,6 +23148,7 @@ for (const v of document.querySelectorAll(".view")) { puttingBack(v, from, h); }, { passive: true }); } +addEventListener("scroll", () => import("./scrollends.js"), { capture: true, passive: true, once: true }); let settingsLoad = Promise.resolve(); // show()'s read of Settings, for openSettingsAt async function show(v) { diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 0fd740249..f120a5c1a 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -4,6 +4,8 @@ // works. {name} slots are filled by t(str, vars). const I18N = { zh: { + "Scroll to top": "回到顶部", + "Scroll to bottom": "回到底部", "Claude Code runs as": "Claude Code 运行方式", "Interactive mode (TUI)": "交互模式(TUI)", "Last request {ago}: interactive mode": "上次请求 {ago}:交互模式", @@ -4427,6 +4429,8 @@ const I18N = { "This report is no longer kept: magpie keeps the last 5.": "这份报告已经不在了:magpie 只保留最近 5 份。", }, "zh-TW": { + "Scroll to top": "回到頂部", + "Scroll to bottom": "回到底部", "Claude Code runs as": "Claude Code 執行方式", "Interactive mode (TUI)": "互動模式(TUI)", "Last request {ago}: interactive mode": "上次請求 {ago}:互動模式", @@ -8835,6 +8839,8 @@ const I18N = { "This report is no longer kept: magpie keeps the last 5.": "這份報告已經不在了:magpie 只保留最近 5 份。", }, ja: { + "Scroll to top": "一番上へ", + "Scroll to bottom": "一番下へ", "Claude Code runs as": "Claude Code の実行方式", "Interactive mode (TUI)": "対話モード(TUI)", "Last request {ago}: interactive mode": "前回のリクエスト({ago}):対話モード", @@ -13245,6 +13251,8 @@ const I18N = { "This report is no longer kept: magpie keeps the last 5.": "このレポートはもう残っていません。magpie は最新の 5 件だけを保存します。", }, de: { + "Scroll to top": "Nach ganz oben", + "Scroll to bottom": "Nach ganz unten", "Claude Code runs as": "Claude Code läuft als", "Interactive mode (TUI)": "Interaktiver Modus (TUI)", "Last request {ago}: interactive mode": "Letzte Anfrage {ago}: interaktiver Modus", diff --git a/internal/gui/assets/scrollends.js b/internal/gui/assets/scrollends.js new file mode 100644 index 000000000..11bfdaa9f --- /dev/null +++ b/internal/gui/assets/scrollends.js @@ -0,0 +1,74 @@ +// Scroll to top and Scroll to bottom (#1090, emo172): a small capsule at the +// lower right of the open page, shown once the page is scrolled a screen +// down and gone again near its top, like a long page's in Safari. app.js +// imports this on the window's first scroll, so it weighs nothing on the +// core. A press is the reader's: it goes through scrollOnPurpose, and +// arrived() keeps the page where it ended. Home and End still scroll the +// page as they always did; the buttons are for a mouse, and Tab reaches +// them too. The tray panel has none. +(() => { + if (document.body.classList.contains("panel")) return; + const box = el("div", "view-ends"); + const up = el("button", "ve-up"), down = el("button", "ve-down"); + up.type = down.type = "button"; + up.append(svg("m4.5 9.5 3.5-3.5 3.5 3.5", 14, 1.7)); + down.append(svg("m4.5 6.5 3.5 3.5 3.5-3.5", 14, 1.7)); + box.append(up, el("span", "ve-sep"), down); + document.body.append(box); + let lang = null; + const open = () => [...document.querySelectorAll(".view")].find((v) => !v.hidden); + const atEnd = (v) => v.scrollTop + v.clientHeight >= v.scrollHeight - 2; + function paint() { + if (lang !== locale) { + lang = locale; + for (const [b, s] of [[up, "Scroll to top"], [down, "Scroll to bottom"]]) b.title = t(s), b.setAttribute("aria-label", t(s)); + } + const v = open(), modal = document.getElementById("modal"); + const held = box.contains(document.activeElement) || box.matches(":hover"); + let on = !!v && (!modal || modal.hidden) && v.scrollTop > v.clientHeight; + if (!v || !on && !held) { box.classList.remove("on"); return; } + const r = v.getBoundingClientRect(), bar = v.offsetWidth - v.clientWidth, pad = parseFloat(getComputedStyle(v).paddingRight) || 0; + // in the page's right margin when a wide window leaves one; else over the + // page's corner, and out of the way at the page's foot, where its last + // row can't be scrolled out from under it. The page's own height is + // never changed for it: app.js's "where the reader is" measures it. + const gutter = pad >= box.offsetWidth + 16; + if (!gutter && v.scrollHeight - v.clientHeight - v.scrollTop < box.offsetHeight + 24) on = false; + box.classList.toggle("on", on || held); + let bottom = innerHeight - r.bottom + 16; + // above a page's own sticky foot (Providers' Add buttons) + const foot = v.querySelector(":scope > .after-list:not(.away)"); + if (foot && getComputedStyle(foot).position === "sticky") bottom = Math.max(bottom, innerHeight - foot.getBoundingClientRect().top + 8); + box.style.right = Math.max(8, innerWidth - r.right + bar + (gutter ? (pad - box.offsetWidth) / 2 : 10)) + "px"; + box.style.bottom = bottom + "px"; + up.setAttribute("aria-disabled", v.scrollTop < 1); + down.setAttribute("aria-disabled", atEnd(v)); + } + let queued = false; + const later = () => { if (!queued) { queued = true; requestAnimationFrame(() => { queued = false; paint(); }); } }; + addEventListener("scroll", later, { capture: true, passive: true }); + addEventListener("resize", later); + box.addEventListener("focusout", later); + box.addEventListener("pointerleave", later); + new MutationObserver(later).observe(document.body, { subtree: true, attributes: true, attributeFilter: ["hidden"] }); + function go(e, top) { + const v = open(); + if (!v || e.currentTarget.getAttribute("aria-disabled") === "true") return; + if (!scrollOnPurpose(e, 2500)) return; + const to = top ? 0 : v.scrollHeight - v.clientHeight; + v.scrollTo({ top: to, behavior: calm() ? "auto" : "smooth" }); + // done when it has stood still a few frames (WebKit has no scrollend) + let last = -1, still = 0; + const until = performance.now() + 2400; + const settle = () => { + if (v.scrollTop === last) still++; else still = 0, last = v.scrollTop; + if (still < 3 && performance.now() < until) return requestAnimationFrame(settle); + arrived(v); + paint(); + }; + requestAnimationFrame(settle); + } + up.onclick = (e) => go(e, true); + down.onclick = (e) => go(e, false); + paint(); +})(); diff --git a/internal/gui/core_budget_test.go b/internal/gui/core_budget_test.go index a98c6345a..8967c572c 100644 --- a/internal/gui/core_budget_test.go +++ b/internal/gui/core_budget_test.go @@ -16,7 +16,7 @@ import ( // raises them and says why in its commit. const ( appScriptBudget = 1_316_874 // app.js: 1,198,239 after Usage left (P2); 1,218,098 with the 10-15 fixes to pages still in the core; 1,232,466 with the provider editor's Advanced fold and every dialog holding its place, the core's own (10-16); 1,250,533 with the quota refresh kept through redraws, the core's own since the tray panel draws it too, plus 1.5% (#2397); 1,269,083 with Free first's option in the provider editor's ROUTINGS, which the Routing page reads too, plus 1.5%; 1,272,726 with the Providers page drawing its last list at once (#1519), the core's page, and the Routing trace's account names read without Object.entries (#1481), plus 1.5%; a dialog growing out of the row that opened it and folding back into it, the core's own since every page's dialogs open through it (10-20); Codex's and Claude Code's own settings moved from Settings into the agent's row on the Agents page, the core's own since the tray panel and the window both draw them (10-20); 1,314,432 with Settings › Privacy's record of what was masked (its summary, the records view and the local model's controls), which belongs to Settings, the core's own, plus 1.5%; 1,336,481 with a provider's Names & levels saying Provider in model names is Settings' and opening it there (joe), and Settings' Image recognition saying where a custom provider's model is marked (nexnull), both in the core's own pages, with no room added (10-23); 1,337,633 with Settings › Models › Replies' Intent classifier timeout (#2934, reyoung), Settings' own row, drawn by waitRow, which the Image generation timeout's row now shares, with no room added; 1,337,735 with the provider editor's link naming a partner's page by its own host (siteHost) instead of usemagpie.ai's /go link, the core's own editor, with no room added (10-23); 1,338,084 with Check in now saying what the press came to (claimed +credits, already today, not available yet) where it said "refreshed" (#3326), the check-in row the tray panel draws too, the core's own, with no room added (10-23); 1,338,929 with a new partner's tile keeping a small dot until it is opened, in the add sheet, the core's own, with no room added (10-23); 1,343,324 with the sidebar's open-page row marked by one shape that moves on a spring (navTopGo), the window's own shell, which every page sits beside, with no room added (10-23); 1,343,465 with a dialog kept centred while its content comes in, until it is pressed (holdModal, every page's dialogs), the core's own, with no room added (10-24); 1,317,486 with what masking did moved out to Insights › Masking (plugins/redact), the budget lowered to it (10-24); 1,321,336 with Codex's one subagent model, its card's square saying plainly what each choice does and a row with a name in its sheet, where two squares read alike (the owner), the Agents page's, which the tray panel draws too, the core's own, with no room added (10-24); 1,321,424 with Settings › Models' Image generation MCP server row beside Image recognition's (JonahFu on X), the two drawn by one helper (mcpRow), Settings' own, with no room added (10-25); 1,321,994 with a provider of the user's set never to rest for a limit (zoecats), the provider editor's Advanced row, the core's own editor, with no room added (10-25); 1,322,289 with Settings › Privacy's Keep a key's form under Mask secrets (Charles Cao on X), Settings' own row, with no room added (10-25); 1,322,537 with no Codex squares beside its named settings rows (the owner), with no room added (10-25); 1,323,401 with the tray panel drawing its last allowances at once and asking for them beside the state (#3160, #3134), the tray panel's, the core's own, and the 4 bytes 84c50f032 added, with no room added (10-25); 1,326,105 with many releases' notes merged into one list by kind, each line with its version (the owner), in the update sheet and What's new, with no room added (10-25); 1,321,319 with Settings › Observability moved out to its plugin (plugins/otel/page.js) and its Langfuse setup there (an X user), the budget lowered to it (10-26); a quota card whose window is used but names no reset no longer saying "Not used yet" (windy on Discord) raised it to 1,321,347, and back to 1,321,319 with Settings' model pickers sharing one row helper (modelOpt) where six kept their own copy, no raise (10-26); 1,325,845 with every slow page's skeletons getting magpie's bird hopping bar to bar (skelBird, the owner after the Plugins page's README), one bird for the window over whatever page or dialog is loading, the core's own, with no room added (10-26); 1,310,129 with Settings › Network and sharing's gateway rows (its port, bare ids, local network, allowed addresses, web pages) moved out to the gateway plugin (plugins/gateway/settings.js), the budget lowered to it (10-27); 1,310,269 with Test models naming the first model that failed and its error in the status line, where it said only how many (ysm119 on Discord), the provider editor's, the core's own, with no room added (10-27); 1,310,460 with Settings › About's Website row linking usemagpie.ai (the owner), Settings' own row, with no room added (10-27); 1,309,669 with a pool's Stop at the caps and Check usage near a cap ticks under a provider's accounts (plugins#349), the core's own editor, its account rows' Remove and add rows each drawn by one helper (forgetOf, addRow), with no room added (10-28); 1,311,681 with Install another agent on the Agents page naming the agents Plugins has (More in Plugins) and opening Plugins at its Agents (the owner), the Agents page's, the core's own, with no room added (10-28); 1,311,735 with More in Plugins in sight while Install another agent is folded (the owner), with no room added (10-29); 1,313,704 with a window whose WebKit scrolls under wheels it never tells of (macOS 13) remembered once seen, a scroll at rest then waiting 120ms for a second the same way within 80ms before it is put back, kept for a late wheel to give back (waitsBack), and a load landing under the wheel keeping the reader's way (backAfterLoad, issennknight), the window's own scroll guard, with no room added (10-29); back to 1,311,735 with Settings › Models › Web search's rows (renderSearch, renderSearcher, the search APIs' keys) moved out to the search plugin (plugins/search/settings.js), app.js 1,292,527 after it, not lowered further since what is left plus 1.5% is over it (#4133, 10-29); 1,315,316 with the Agents row of a Claude Code or Codex not connected saying the account it is signed in to and switching it to another (Switch accounts only, the owner on X), in place of the failover line's ChatGPT accounts button, the Agents page's, the core's own, its comments cut down, with no room added (10-30); 1,317,705 with a Claude account added from a setup-token (sfss5362 on X): the sign-in's Use a setup-token instead…, its sheet, and its account row's magpie only, the provider editor's account rows, the core's own editor, its comments cut down, with no room added (10-30); back to 1,315,316 with the setup-token sheet and Copilot's GHE.com one moved out to signsheets.js, loaded when one is asked for (signAsk, 10-30); 1,315,479 with the provider editor lining its Advanced up with its fields (the owner: 高级这个折叠按钮为什么在一个前不着村后不着店的位置?) and a refresh on each of its accounts (在这里也要加上手动刷新用量的按钮), the core's own editor, with no room added (10-31); 1,315,603 with About's Log row, the path of the log the app writes and an Open for its folder (rayem1w on X), in app.js, its comment cut to one line, with no room added (10-31); 1,316,781 with a provider's model listed by an id of the user's (zeewell on X), the provider editor's Names & levels Listed as box beside Same as, the core's own editor, with no room added (10-31); 1,316,874 with a package manager's magpie (the AUR's magpie-bin) naming the command that updates it in Settings' Version row and lighting no Update pill (shuhai on Discord), the core's own update row, which the tray panel reads too, its comments cut to one line, with no room added (10-31); Settings › Sync's Kept on this computer row (hayden4608 on Discord) raised it to 1,317,497, which is no bound; the 1,316,874 it had came back once later trims left app.js at 1,308,363 (10-31) - coreScriptBudget = 1_533_802 // every core script: 1,358,034 after Usage left; 1,380,003 with them; 1,405,937 with the fold; 1,426,102 with 10-16's core work (the GitHub list loading, the move to plugins, Settings › Long conversations), plus 1.5% (#2612); 1,447,368 with loadBack, the window's one state read per return, which every page needs and so is the core's own (1,446,845 before it, 155 bytes under), plus 1.5%; OpenCode Zen's card saying OpenCode tells no key its balance adds ~390 bytes inside it; 1,472,589 with the dialog that grows out of its row and the agents' own settings in their rows, on main's 10-20 core work (the Providers page's last list, the Routing trace's names), plus 1.5% (10-20); 1,506,192 with Settings › Privacy's record of what was masked, plus 1.5%; 1,529,393 with the Intent classifier timeout row (#2934), with no room added; 1,529,495 with the editor's partner link host, with no room added; 1,529,844 with Check in now's press summary (#3326), with no room added; 1,530,689 with the new partner tile's dot, with no room added; 1,535,084 with the sidebar's spring, with no room added; 1,549,478 with the context window card's map (tiles by tokens, the default look, the block grid a switch away) and its text view showing an inline image, in context.js, which the Routing page draws as well as Usage, so the core's own, with no room added (10-24); 1,550,768 with a usage card's refresh opening after its text (#840's card, which the tray panel draws too), the Add key spinner, and a dialog kept centred while its content comes in, with no room added (10-24); 1,524,789 with what masking did moved out to Insights › Masking, lowered to it; 1,528,525 with Codex's one subagent model, with no room added (10-24); 1,528,972 with the Image generation MCP server row (JonahFu on X), with no room added (10-25); 1,529,542 with the limit-rest row (zoecats), with no room added (10-25); 1,529,837 with Keep a key's form (Charles Cao on X), with no room added (10-25); 1,530,050 with no Codex squares beside its named settings rows (the owner), with no room added (10-25); 1,530,631 with a plugin's page growing to its README on the dialog's spring instead of in one jump (the owner), the Plugins page's, in plugins.js, with no room added (10-25); 1,533,445 with the context map counting its tokens on as a live request grows, a tile pointed at keeping its tip, a tool's gathered results saying how many and the small files their folder (the owner, after flohoeller's map on X), in context.js, which the Routing page draws as well as Usage, and the 4 bytes 84c50f032 added to app.js (Settings' Version row) without a raise, with no room added (10-25); 1,534,312 with the tray panel's last allowances drawn at once (#3160, #3134), with no room added (10-25); 1,537,016 with many releases' notes merged into one list by kind (the owner), in app.js, with no room added (10-25); 1,542,244 with a plugin's README loading under magpie's bird bouncing down its lines instead of pulsing bars (the owner), the Plugins page's, in plugins.js, with no room added (10-26); 1,542,418 with a page plugin's sidebar tab coming and going as it is installed, switched on or off, or removed, instead of on the Plugins page's next load (the owner), in plugins.js, with no room added (10-26); 1,537,645 with Settings › Observability moved out to plugins/otel, lowered to it (10-26); 1,537,673 with a used window with no reset not "Not used yet" (windy on Discord), and back to 1,537,645 with Settings' model pickers sharing modelOpt (10-26); 1,541,967 with every slow page's skeletons getting magpie's bird (skelBird, the owner), in app.js, the README's bird sharing its drawing and styles, with no room added (10-26); 1,526,251 with Settings' gateway rows moved out to plugins/gateway/settings.js, lowered to it (10-27); 1,526,391 with Test models naming the first failed model and its error (ysm119 on Discord), in app.js, with no room added (10-27); 1,526,556 with About's Website row (the owner), in app.js, with no room added (10-27); 1,526,500 with a pool's cap ticks (plugins#349), in app.js, with no room added (10-28); 1,527,245 with About's Open-source licenses row and dialog (the owner), in app.js, with no room added (10-28); 1,528,076 with the Partners heading's light (the owner), in app.js, with no room added (10-28); 1,530,449 with Install another agent's More in Plugins row (the owner), in app.js, and Plugins opening at its Agents, in plugins.js, with no room added (10-28); 1,530,807 with layout.js measuring the scrollbar into --bar, which every page's right padding takes off so its sides match (the owner), the window's own shell, with no room added (10-28); 1,530,861 with More in Plugins in sight while Install another agent is folded (the owner), in app.js, with no room added (10-29); 1,532,830 with waitsBack and backAfterLoad (issennknight, macOS 13), the window's own scroll guard, in app.js, with no room added (10-29); the context map's tile wording (a4e929212) and Cindy's Add magpie (628ac36fc) had taken the core over it, and Web search's rows moved out to plugins/search/settings.js brought it back to 1,530,861 (#4133, 10-29); 1,532,459 with a provider editor's account rows on one line each, First or Make first at its right end and the settings that aren't set under ⋯ (the owner), the core's own editor, in app.js, which stays in its budget with its comments cut down, with no room added (10-29); 1,532,602 with a plugin's README waiting for its bird to land on three lines before it goes in, however soon it came (the owner, 10-30), in plugins.js, with no room added; 1,536,357 with Switch accounts only on the Agents row (the owner on X), in app.js, with no room added (10-30); 1,537,034 with a plugin's README read once shown at once, with no bird, when its page opens again and read again behind it, the last 20 kept (the owner), in plugins.js, with no room added (10-30); 1,539,423 with a Claude account added from a setup-token (sfss5362 on X), in app.js, with no room added (10-30); 1,538,975 with purposes.js written as compact JSON by genpurposes and the MCP tool purpose in it, 448 bytes less in all (ferock on Discord), lowered to it (10-30); 1,536,586 with the setup-token sheet and Copilot's GHE.com one moved out to signsheets.js, lowered by the 2,389 the setup-token raised it (10-30); 1,536,631 with the 45 bytes b62fa332f added to app.js (Send requests unmasked for subscriptions and plugin providers too) without a raise, with no room added (10-31); 1,537,356 with Settings › Models › Replies › Claude Code's interactive mode, a row beside Short Claude system prompt (the owner), in app.js, with no room added (10-31); Long conversations in Codex's row naming the config.toml keys Codex reads before it, with Take it out, and the window magpie tells Codex for the model it runs (zgjfjfl on Discord) raised it to 1,537,669, and it is back under 1,537,356 with the key list and the window's size written by the server (codexKeysText, tokensSize) and the row's words cut, no raise (10-31); 1,538,554 with the same two, with no room added (10-31); 1,538,678 with About's Log row (rayem1w on X), with no room added (10-31); 1,539,856 with a model's Listed as id (zeewell on X), in app.js, with no room added (10-31); Tuned for you's Codex row offering the advice to background compaction (herman_hong on Discord) and a package manager's magpie naming its command (shuhai on Discord) took it to 1,541,798, and the release notes' dialogs (What's new, the update's ask) moved out to whatsnew.js, loaded when one opens, brought it back under 1,539,856, no raise (10-31); 1,540,479 for Settings › Sync's Kept on this computer row (hayden4608 on Discord) was taken back to 1,539,856 once the core weighed 1,533,355, no raise (10-31); 1,532,958 with the context window card's head naming the agent and the model's lab (the owner), in context.js, after the card's part text viewer moved out to ctxtext.js (7,005 bytes), lowered to it (10-31); 1,533,105 with the provider editor's Claude Code runs as row moved above the accounts, its pill and hover card in claudemode.js, and Settings found at the row that draws anew (the owner), in app.js and i18n.js, with no room added (11-01); a Codex account's credits spent only after every other account, key and fallback (gokoshiki on Discord) fit in it with the Codex options' comments in app.js cut down, no raise (11-01); a plugin option's words in the window's language and the filtered-models note (plugins#590, anewuser6937 on Discord) fit in it with three of the provider editor's comments in app.js cut down, no raise (11-01); 1,533,802 with a closed Mac window telling its page it is out of sight (#4293, pipi-1997), in app.js, which every page needs, its comments cut to one line, with no room added (11-01); Warm up on a Codex or Claude account's Usage card (01huadalang on Discord), in plugins/usage/page.js, its one call in app.js paid for by two comments cut, no raise (11-01); Settings' Check in now for one account (xiaowu_95660), its row and press in checkinnow.js, loaded when Settings draws a check-in, its one call in app.js paid for by three check-in comments cut, no raise (11-02); a ChatGPT account's plan end under its row on Providers (danzi002 on Discord), its line in app.js paid for by two account-row comments cut, no raise (11-02) + coreScriptBudget = 1_533_802 // every core script: 1,358,034 after Usage left; 1,380,003 with them; 1,405,937 with the fold; 1,426,102 with 10-16's core work (the GitHub list loading, the move to plugins, Settings › Long conversations), plus 1.5% (#2612); 1,447,368 with loadBack, the window's one state read per return, which every page needs and so is the core's own (1,446,845 before it, 155 bytes under), plus 1.5%; OpenCode Zen's card saying OpenCode tells no key its balance adds ~390 bytes inside it; 1,472,589 with the dialog that grows out of its row and the agents' own settings in their rows, on main's 10-20 core work (the Providers page's last list, the Routing trace's names), plus 1.5% (10-20); 1,506,192 with Settings › Privacy's record of what was masked, plus 1.5%; 1,529,393 with the Intent classifier timeout row (#2934), with no room added; 1,529,495 with the editor's partner link host, with no room added; 1,529,844 with Check in now's press summary (#3326), with no room added; 1,530,689 with the new partner tile's dot, with no room added; 1,535,084 with the sidebar's spring, with no room added; 1,549,478 with the context window card's map (tiles by tokens, the default look, the block grid a switch away) and its text view showing an inline image, in context.js, which the Routing page draws as well as Usage, so the core's own, with no room added (10-24); 1,550,768 with a usage card's refresh opening after its text (#840's card, which the tray panel draws too), the Add key spinner, and a dialog kept centred while its content comes in, with no room added (10-24); 1,524,789 with what masking did moved out to Insights › Masking, lowered to it; 1,528,525 with Codex's one subagent model, with no room added (10-24); 1,528,972 with the Image generation MCP server row (JonahFu on X), with no room added (10-25); 1,529,542 with the limit-rest row (zoecats), with no room added (10-25); 1,529,837 with Keep a key's form (Charles Cao on X), with no room added (10-25); 1,530,050 with no Codex squares beside its named settings rows (the owner), with no room added (10-25); 1,530,631 with a plugin's page growing to its README on the dialog's spring instead of in one jump (the owner), the Plugins page's, in plugins.js, with no room added (10-25); 1,533,445 with the context map counting its tokens on as a live request grows, a tile pointed at keeping its tip, a tool's gathered results saying how many and the small files their folder (the owner, after flohoeller's map on X), in context.js, which the Routing page draws as well as Usage, and the 4 bytes 84c50f032 added to app.js (Settings' Version row) without a raise, with no room added (10-25); 1,534,312 with the tray panel's last allowances drawn at once (#3160, #3134), with no room added (10-25); 1,537,016 with many releases' notes merged into one list by kind (the owner), in app.js, with no room added (10-25); 1,542,244 with a plugin's README loading under magpie's bird bouncing down its lines instead of pulsing bars (the owner), the Plugins page's, in plugins.js, with no room added (10-26); 1,542,418 with a page plugin's sidebar tab coming and going as it is installed, switched on or off, or removed, instead of on the Plugins page's next load (the owner), in plugins.js, with no room added (10-26); 1,537,645 with Settings › Observability moved out to plugins/otel, lowered to it (10-26); 1,537,673 with a used window with no reset not "Not used yet" (windy on Discord), and back to 1,537,645 with Settings' model pickers sharing modelOpt (10-26); 1,541,967 with every slow page's skeletons getting magpie's bird (skelBird, the owner), in app.js, the README's bird sharing its drawing and styles, with no room added (10-26); 1,526,251 with Settings' gateway rows moved out to plugins/gateway/settings.js, lowered to it (10-27); 1,526,391 with Test models naming the first failed model and its error (ysm119 on Discord), in app.js, with no room added (10-27); 1,526,556 with About's Website row (the owner), in app.js, with no room added (10-27); 1,526,500 with a pool's cap ticks (plugins#349), in app.js, with no room added (10-28); 1,527,245 with About's Open-source licenses row and dialog (the owner), in app.js, with no room added (10-28); 1,528,076 with the Partners heading's light (the owner), in app.js, with no room added (10-28); 1,530,449 with Install another agent's More in Plugins row (the owner), in app.js, and Plugins opening at its Agents, in plugins.js, with no room added (10-28); 1,530,807 with layout.js measuring the scrollbar into --bar, which every page's right padding takes off so its sides match (the owner), the window's own shell, with no room added (10-28); 1,530,861 with More in Plugins in sight while Install another agent is folded (the owner), in app.js, with no room added (10-29); 1,532,830 with waitsBack and backAfterLoad (issennknight, macOS 13), the window's own scroll guard, in app.js, with no room added (10-29); the context map's tile wording (a4e929212) and Cindy's Add magpie (628ac36fc) had taken the core over it, and Web search's rows moved out to plugins/search/settings.js brought it back to 1,530,861 (#4133, 10-29); 1,532,459 with a provider editor's account rows on one line each, First or Make first at its right end and the settings that aren't set under ⋯ (the owner), the core's own editor, in app.js, which stays in its budget with its comments cut down, with no room added (10-29); 1,532,602 with a plugin's README waiting for its bird to land on three lines before it goes in, however soon it came (the owner, 10-30), in plugins.js, with no room added; 1,536,357 with Switch accounts only on the Agents row (the owner on X), in app.js, with no room added (10-30); 1,537,034 with a plugin's README read once shown at once, with no bird, when its page opens again and read again behind it, the last 20 kept (the owner), in plugins.js, with no room added (10-30); 1,539,423 with a Claude account added from a setup-token (sfss5362 on X), in app.js, with no room added (10-30); 1,538,975 with purposes.js written as compact JSON by genpurposes and the MCP tool purpose in it, 448 bytes less in all (ferock on Discord), lowered to it (10-30); 1,536,586 with the setup-token sheet and Copilot's GHE.com one moved out to signsheets.js, lowered by the 2,389 the setup-token raised it (10-30); 1,536,631 with the 45 bytes b62fa332f added to app.js (Send requests unmasked for subscriptions and plugin providers too) without a raise, with no room added (10-31); 1,537,356 with Settings › Models › Replies › Claude Code's interactive mode, a row beside Short Claude system prompt (the owner), in app.js, with no room added (10-31); Long conversations in Codex's row naming the config.toml keys Codex reads before it, with Take it out, and the window magpie tells Codex for the model it runs (zgjfjfl on Discord) raised it to 1,537,669, and it is back under 1,537,356 with the key list and the window's size written by the server (codexKeysText, tokensSize) and the row's words cut, no raise (10-31); 1,538,554 with the same two, with no room added (10-31); 1,538,678 with About's Log row (rayem1w on X), with no room added (10-31); 1,539,856 with a model's Listed as id (zeewell on X), in app.js, with no room added (10-31); Tuned for you's Codex row offering the advice to background compaction (herman_hong on Discord) and a package manager's magpie naming its command (shuhai on Discord) took it to 1,541,798, and the release notes' dialogs (What's new, the update's ask) moved out to whatsnew.js, loaded when one opens, brought it back under 1,539,856, no raise (10-31); 1,540,479 for Settings › Sync's Kept on this computer row (hayden4608 on Discord) was taken back to 1,539,856 once the core weighed 1,533,355, no raise (10-31); 1,532,958 with the context window card's head naming the agent and the model's lab (the owner), in context.js, after the card's part text viewer moved out to ctxtext.js (7,005 bytes), lowered to it (10-31); 1,533,105 with the provider editor's Claude Code runs as row moved above the accounts, its pill and hover card in claudemode.js, and Settings found at the row that draws anew (the owner), in app.js and i18n.js, with no room added (11-01); a Codex account's credits spent only after every other account, key and fallback (gokoshiki on Discord) fit in it with the Codex options' comments in app.js cut down, no raise (11-01); a plugin option's words in the window's language and the filtered-models note (plugins#590, anewuser6937 on Discord) fit in it with three of the provider editor's comments in app.js cut down, no raise (11-01); 1,533,802 with a closed Mac window telling its page it is out of sight (#4293, pipi-1997), in app.js, which every page needs, its comments cut to one line, with no room added (11-01); Warm up on a Codex or Claude account's Usage card (01huadalang on Discord), in plugins/usage/page.js, its one call in app.js paid for by two comments cut, no raise (11-01); Settings' Check in now for one account (xiaowu_95660), its row and press in checkinnow.js, loaded when Settings draws a check-in, its one call in app.js paid for by three check-in comments cut, no raise (11-02); a ChatGPT account's plan end under its row on Providers (danzi002 on Discord), its line in app.js paid for by two account-row comments cut, no raise (11-02); Scroll to top and Scroll to bottom (#1090, emo172), in scrollends.js, imported on the window's first scroll, its one line in app.js paid for by two comments cut, no raise (11-02) ) func TestCoreScriptBudget(t *testing.T) { diff --git a/internal/gui/tests/old-webkit.test.cjs b/internal/gui/tests/old-webkit.test.cjs index 2e927857a..d81d1343f 100644 --- a/internal/gui/tests/old-webkit.test.cjs +++ b/internal/gui/tests/old-webkit.test.cjs @@ -153,8 +153,9 @@ test("compat.js is the first of the page's own scripts", async () => { // Use is pressed, quotaall.js when a provider has two accounts, // quitask.js when a Quit asks first, checkinnow.js when Settings' Check // in now for one account is pressed, autobackup.js when Settings draws - // Sync; 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", "quitask.js", "checkinnow.js", "autobackup.js"].includes(f)) assert(all.includes(f), f + " is not loaded by index.html"); + // Sync, scrollends.js on the window's first scroll; 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", "quitask.js", "checkinnow.js", "autobackup.js", "scrollends.js"].includes(f)) assert(all.includes(f), f + " is not loaded by index.html"); }); test("the styles hold up in Safari 15.0", async () => { diff --git a/internal/gui/tests/view-ends.test.cjs b/internal/gui/tests/view-ends.test.cjs new file mode 100644 index 000000000..248f1109e --- /dev/null +++ b/internal/gui/tests/view-ends.test.cjs @@ -0,0 +1,212 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// Scroll to top and Scroll to bottom (emo172, #1090): a quiet capsule at the +// open page's lower right (scrollends.js), shown once the page is scrolled a +// screen down and not before. Each press is the reader's own scroll: the page +// goes to its top or its foot and stays there, not put back by the "where the +// reader is" guard. Reached by the keyboard too (Tab, then Enter), its label +// and tooltip in every language, in both themes, at a narrow window and a wide +// one, with the motion on and off. It never makes the page taller (app.js +// measures it to keep the reader's place), so over the page's corner it +// steps aside at the foot once the pointer and focus leave it. Chromium and +// WebKit. +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 now = new Date(); +// rows enough for a page several screens tall +const AGENTS = Array.from({ length: 40 }, (_, i) => ({ + id: "a" + i, name: "Agent " + i, path: "/t/a" + i + ".json", icon: "generic", wired: true, + fields: [{ key: "model", label: "model", value: "m", options: [{ value: "m", label: "M", ref: "a/m" }] }], +})); +const WORDS = { + en: ["Scroll to top", "Scroll to bottom"], + zh: ["回到顶部", "回到底部"], + "zh-TW": ["回到頂部", "回到底部"], + ja: ["一番上へ", "一番下へ"], + de: ["Nach ganz oben", "Nach ganz unten"], +}; +const SHAPES = [ + { viewport: { width: 440, height: 620 }, theme: "light", motion: "reduce" }, + { viewport: { width: 1180, height: 640 }, theme: "dark", motion: "no-preference" }, + // wide enough that the page keeps a margin at its right: the capsule sits in it + { viewport: { width: 1640, height: 700 }, theme: "light", motion: "no-preference", margin: true }, +]; + +function server(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:"${lang}",theme:"${theme}",web: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: AGENTS, profiles: [], settings: { lang, theme }, fx: { rate: 7.2, at: now.toISOString() } }); + if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } }); + 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)]; + await route.fulfill({ body: await fs.readFile(file), contentType }); + }; +} + +// the page at rest: the same scrollTop over a few reads +async function rested(view, p, ms = 4000) { + let last = NaN, same = 0; + const until = Date.now() + ms; + while (Date.now() < until) { + const t = await view.evaluate((v) => v.scrollTop); + if (t === last && ++same >= 3) return t; + if (t !== last) same = 0; + last = t; + await p.waitForTimeout(80); + } + return last; +} + +const shown = (p) => p.evaluate(() => { + const b = document.querySelector(".view-ends"); + return !!b && getComputedStyle(b).visibility === "visible" && b.classList.contains("on"); +}); + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(engine + ": a long page has Scroll to top and Scroll to bottom", { timeout: 300000 }, async (t) => { + assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit"); + const browser = await (engine === "webkit" ? webkit : chromium).launch(); + t.after(() => browser.close()); + for (const lang of Object.keys(WORDS)) { + for (const shape of SHAPES) { + await t.test(`${lang} ${shape.viewport.width} ${shape.theme}`, async () => { + const errors = []; + const ctx = await browser.newContext({ viewport: shape.viewport, reducedMotion: shape.motion, colorScheme: shape.theme }); + const p = await ctx.newPage(); + p.setDefaultTimeout(8000); + p.setDefaultNavigationTimeout(60000); + p.on("pageerror", (e) => { + if (!/^ResizeObserver loop completed with undelivered notifications/.test(e.message)) errors.push(e.message); + }); + await p.route("**/*", server(lang, shape.theme)); + const view = p.locator("#view-agents"); + await p.goto("http://magpie.test/?view=agents"); + await p.locator("#view-agents .ag-lead").first().waitFor(); + await p.waitForFunction(() => { const v = document.getElementById("view-agents"); return v.scrollHeight > v.clientHeight * 3; }).catch(() => {}); + const h = await view.evaluate((v) => ({ over: v.scrollHeight - v.clientHeight, h: v.clientHeight, tall: v.scrollHeight })); + assert(h.over > 2 * h.h, `the page must be several screens tall: ${JSON.stringify(h)}`); + + // at the top, nothing shows + assert.equal(await shown(p), false, "no capsule at the page's top"); + + // the reader's wheel, past a screen + const r = await view.boundingBox(); + await p.mouse.move(r.x + r.width / 2, r.y + r.height / 2); + for (let i = 0; i < 12 && (await view.evaluate((v) => v.scrollTop <= v.clientHeight * 1.2)); i++) { + await p.mouse.wheel(0, 300); + await p.waitForTimeout(120); + } + await rested(view, p); + await p.waitForFunction(() => document.querySelector(".view-ends.on")); + assert.equal(await shown(p), true, "the capsule shows a screen down"); + + const up = p.locator(".view-ends .ve-up"), down = p.locator(".view-ends .ve-down"); + // its words, as its name and its tooltip + for (const [b, w] of [[up, WORDS[lang][0]], [down, WORDS[lang][1]]]) { + assert.equal(await b.getAttribute("aria-label"), w); + assert.equal(await b.getAttribute("title"), w); + } + await p.getByRole("button", { name: WORDS[lang][1], exact: true }).waitFor(); + + // inside the window and over the page, quiet: no coloured left + // border, its colours the theme's + const look = await p.evaluate(() => { + const b = document.querySelector(".view-ends"), r = b.getBoundingClientRect(), v = document.getElementById("view-agents").getBoundingClientRect(); + const cs = getComputedStyle(b); + const rgb = cs.backgroundColor.match(/[\d.]+/g).map(Number); + return { + inside: r.left >= 0 && r.right <= innerWidth && r.top >= 0 && r.bottom <= innerHeight && r.right <= v.right && r.bottom <= v.bottom, + wide: document.documentElement.scrollWidth > innerWidth || document.body.scrollWidth > innerWidth, + left: parseFloat(cs.borderLeftWidth), + light: (rgb[0] + rgb[1] + rgb[2]) / 3, + image: cs.backgroundImage, + }; + }); + assert.ok(look.inside, "the capsule sits inside the window, over the page"); + assert.equal(look.wide, false, "nothing scrolls sideways"); + assert.equal(look.left, 0); + assert.equal(look.image, "none", "no gradient"); + if (shape.theme === "dark") assert.ok(look.light < 100, `dark in the dark theme: ${look.light}`); + else assert.ok(look.light > 200, `light in the light theme: ${look.light}`); + + if (process.env.ARTIFACT_DIR) await p.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-${shape.viewport.width}-view-ends.png`) }); + + // Scroll to bottom: to the page's foot, and it stays there + await down.click(); + const max = await view.evaluate((v) => v.scrollHeight - v.clientHeight); + await p.waitForFunction((m) => Math.abs(document.getElementById("view-agents").scrollTop - m) <= 1, max); + await p.waitForTimeout(700); + assert.ok(Math.abs((await view.evaluate((v) => v.scrollTop)) - max) <= 1, "the page stays at its foot"); + assert.equal(await down.getAttribute("aria-disabled"), "true", "at the foot, Scroll to bottom has nowhere to go"); + // the page's height is its own: nothing is added at its foot + assert.equal(await view.evaluate((v) => v.scrollHeight), h.tall, "the page grew under the capsule"); + // at the foot nothing that can be pressed is under the capsule once + // the pointer has left it: in a wide window's margin it is beside + // every row; over the page's corner it steps aside, as the last row + // can't be scrolled out from under it + await p.mouse.move(r.x + 40, r.y + 40); + await down.evaluate((b) => b.blur()); + // in sight in the margin; stepped aside, and gone, over the corner + await p.waitForFunction((m) => { + const b = document.querySelector(".view-ends"); + return m ? b.classList.contains("on") : getComputedStyle(b).visibility === "hidden"; + }, !!shape.margin); + const clear = await p.evaluate(() => { + const box = document.querySelector(".view-ends"), b = box.getBoundingClientRect(); + const under = new Set(); + if (getComputedStyle(box).visibility === "visible") for (let x = b.left + 2; x < b.right; x += 6) for (let y = b.top + 2; y < b.bottom; y += 6) { + for (const e of document.elementsFromPoint(x, y)) { + if (box.contains(e)) continue; + const c = e.closest("button, a, input, label, [role], [tabindex]:not(.view)"); + if (c) under.add(c.outerHTML.slice(0, 80)); + } + } + const rows = [...document.querySelectorAll("#view-agents #agents > .row")]; + return { rows: rows.length, under: [...under], beside: rows.every((r) => r.getBoundingClientRect().right <= b.left) }; + }); + assert.ok(clear.rows > 30, `the page's rows: ${clear.rows}`); + if (shape.margin) { + assert.equal(await shown(p), true, "in the margin it stays at the foot"); + assert.ok(clear.beside, "in the page's margin, beside every row"); + } else assert.equal(await shown(p), false, "over the corner it steps aside at the foot"); + assert.deepEqual(clear.under, [], "at the foot nothing to press is under the capsule"); + assert.ok(Math.abs((await view.evaluate((v) => v.scrollTop)) - max) <= 1, "the page stays at its foot"); + + // the keyboard: a button, so Tab reaches it; Enter takes the page to + // its top, and it stays there. A little up from the foot it is back. + await p.mouse.move(r.x + r.width / 2, r.y + r.height / 2); + await p.mouse.wheel(0, -300); + await p.waitForFunction(() => document.querySelector(".view-ends.on")); + await p.mouse.move(r.x + 40, r.y + 40); + await up.focus(); + await p.keyboard.press("Enter"); + await p.waitForFunction(() => document.getElementById("view-agents").scrollTop === 0); + await p.waitForTimeout(700); + assert.equal(await view.evaluate((v) => v.scrollTop), 0, "the page stays at its top"); + // focus keeps it in sight; once focus leaves, at the top it goes + assert.equal(await shown(p), true, "a focused capsule stays"); + await up.evaluate((b) => b.blur()); + await p.waitForFunction(() => !document.querySelector(".view-ends.on")); + + // code moving the page is still put back: only the reader's scrolls + // (and the capsule's presses) stand + await view.evaluate((v) => { v.scrollTop = v.clientHeight * 2; }); + await p.waitForTimeout(300); + assert.equal(await view.evaluate((v) => v.scrollTop), 0, "a scroll no one asked for is put back"); + assert.equal(await shown(p), false); + + assert.deepEqual(errors, []); + await ctx.close(); + }); + } + } + }); +}