Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gui: the middle button's autoscroll scrolls a page instead of twitching (#2767)

yetonecba59b05e130
diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md
index 02388266b..593bad95a 100644
--- a/docs/subsystems/gui-shell.md
+++ b/docs/subsystems/gui-shell.md
@@ -356,6 +356,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/app.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. 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 scroll not the reader's is put back, so every way the reader scrolls has to count as theirs: the wheel, 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`.
- Typing never moves the page either. A textarea that grows with its text (the Library's `autosize` in plugins/library/client.js: a set's text, an agent's own text, a skill's description, a subagent's fields) is measured at height auto with what holds it kept at its height, so the page never goes short for a moment: a page gone short has its view's scroll clamped by the browser at every input, an input method's every composing step included, and the guard's putting it back pulled a textarea taller than the view to one of its edges (#2636, `library-ime-scroll.test.cjs`).
- There are no native `<select>` elements and no colored left-border stripes. State is shown with a dot or a swatch.
- What the window shows can be selected and copied, its controls can't, as in a Mac app (Outvi V on Discord: nothing could be copied into a bug report). The page is `user-select: none`, and two `:where()` rules after `body` in `app.css` make text content selectable (paragraphs, `pre` and `code`, a detail's values, hints and notes, every error class, a conversation, a request's story on Routing) and keep every control unselectable with what is in it (buttons, labels, tabs, menus, the navigation, grips). Both have no specificity, so a view's own rule still decides, as a list being sorted does; a field stays selectable. See `user-select.test.cjs`.
diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js
index f7d5dbc93..991647420 100644
--- a/internal/gui/assets/app.js
+++ b/internal/gui/assets/app.js
@@ -23783,20 +23783,41 @@ const barLifted = () => {
readerScrolls(250);
barDrag = null;
};
+// The middle button's autoscroll (Windows) is the reader's scroll: put back,
+// each of its scrolls twitched the page (#2767). From a trusted middle press,
+// the view it lands in follows it and lets go of what a click held, till it
+// ends as Chromium ends it: the next press (only its pointerup arrives), a
+// key, blur, or the lift after moving past 15px. More in gui-shell.md.
+let panning = null; // { v, from: [x, y], far, lifted }
+const panEnds = () => { panning = null; };
addEventListener("pointerdown", (e) => {
downAt = [e.clientX, e.clientY];
+ panEnds();
+ if (e.button === 1 && e.isTrusted) {
+ const pv = e.target instanceof Element ? e.target.closest(".view") : null;
+ if (pv && !pv.hidden && pv.scrollHeight > pv.clientHeight) { panning = { v: pv, from: downAt, far: false, lifted: false }; held = null; }
+ }
const v = e.target;
const bar = v instanceof Element && v.classList.contains("view") ? v.offsetWidth - v.clientWidth : 0;
barDrag = v instanceof Element && v.classList.contains("view") && !v.hidden && bar > 0 &&
e.clientX >= v.getBoundingClientRect().right - bar ? v : null;
if (barDrag) held = null;
}, { capture: true, passive: true });
-addEventListener("pointerup", barLifted, { capture: true, passive: true });
+addEventListener("pointerup", (e) => {
+ barLifted();
+ if (!panning) return;
+ if (panning.lifted || e.button !== 1 || panning.far || Math.hypot(e.clientX - panning.from[0], e.clientY - panning.from[1]) > 15) panEnds();
+ else panning.lifted = true;
+}, { capture: true, passive: true });
addEventListener("pointercancel", barLifted, { capture: true, passive: true });
addEventListener("pointermove", (e) => {
+ if (panning && !panning.lifted && Math.hypot(e.clientX - panning.from[0], e.clientY - panning.from[1]) > 15) panning.far = true;
if (e.buttons && downAt && Math.hypot(e.clientX - downAt[0], e.clientY - downAt[1]) > 6) readerScrolls(250);
}, { capture: true, passive: true });
+addEventListener("keyup", panEnds, true);
+addEventListener("blur", panEnds);
addEventListener("keydown", (e) => {
+ panEnds();
const typing = e.target.closest?.("input, textarea, select, [contenteditable]");
if (e.key === "Tab" || (!typing && SCROLL_KEYS.has(e.key))) readerScrolls(400);
if (e.isTrusted && e.key === "Enter") keyboardFocus(e);
@@ -23805,6 +23826,9 @@ addEventListener("keyup", (e) => {
if (e.isTrusted && e.key === " " && !e.target.closest?.("input, textarea, select, [contenteditable]")) keyboardFocus(e);
}, true);
const readerAt = new WeakMap();
+// what had the focus when the reader last scrolled a view themselves: they
+// took it out of sight, and it isn't brought back (backToReader)
+const scrolledFrom = new WeakMap();
// Where the reader is is a number, the view's scrollTop, unless the view
// names a part of itself to keep in place (keepInView): a part under others
// that redraw on their own (the Routing page's groups, under the live stage
@@ -23845,9 +23869,11 @@ function backToReader(v) {
if (pinned != null) readerAt.set(v, want);
if (Math.abs(v.scrollTop - want) < 0.5) return;
v.scrollTop = want;
- // a field focused out of sight still comes into view, no further than needed
+ // a field focused out of sight still comes into view, no further than needed,
+ // unless the reader scrolled it away (scrolledFrom): else a middle press's
+ // button, left behind by the autoscroll, pulled the page back to it.
const f = document.activeElement;
- if (f && f !== document.body && v.contains(f)) {
+ if (f && f !== document.body && f !== scrolledFrom.get(v) && v.contains(f)) {
const r = f.getBoundingClientRect(), b = v.getBoundingClientRect();
if (r.bottom > b.bottom || r.top < b.top) { readerScrolls(1000); guardUntil = purposeUntil; f.scrollIntoView({ block: "nearest" }); }
}
@@ -24028,7 +24054,8 @@ for (const v of document.querySelectorAll(".view")) {
v.tabIndex = -1;
v.addEventListener("scroll", () => {
if (v.hidden) return;
- if (performance.now() < purposeUntil || barDrag === v) { fitRoom(v); readerLeaves(v); return; }
+ if (panning?.v === v) readerScrolls(250);
+ if (performance.now() < purposeUntil || barDrag === v) { fitRoom(v); readerLeaves(v); scrolledFrom.set(v, document.activeElement); return; }
const from = v.scrollTop;
if (held?.v === v) hold(held);
else backToReader(v);
diff --git a/internal/gui/tests/view-middle-autoscroll.test.cjs b/internal/gui/tests/view-middle-autoscroll.test.cjs
new file mode 100644
index 000000000..88ee29dbd
--- /dev/null
+++ b/internal/gui/tests/view-middle-autoscroll.test.cjs
@@ -0,0 +1,167 @@
+// Run with Node's test runner and Playwright on the module path; see README.md.
+// A page scrolled by the middle button's autoscroll (多喝热水 on Discord,
+// Windows): pressing the wheel and moving the mouse scrolls the view with no
+// wheel, touch or drag of the reader's for the page to see. Chromium and
+// WebView2 send a pointerdown of button 1 and then only pointer moves with no
+// button held (the press is let go at once and the autoscroll runs on), or
+// none at all while the mouse is held still. The "where the reader is" rule
+// put each of those scrolls back as it came, and the page twitched between
+// where the autoscroll took it and where it was. A scroll the reader never
+// asked for (code setting scrollTop) is still put back once the autoscroll
+// has ended, by the next press of a button, as Chromium ends it. The middle
+// press focuses the button under it, and the autoscroll takes it out of
+// sight: the next scroll put back brought it back into view, and the page
+// jumped to it. So did a button a press focused and the wheel scrolled away.
+// Chromium only scrolls: WebKit has no middle-button autoscroll, and there the
+// press must leave the rule as it was. English and Chinese, 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 AGENTS = Array.from({ length: 14 }, (_, 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 VIEWPORT = { width: 440, height: 620 };
+
+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: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: "light" }, fx: { rate: 7.2, at: new Date().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's height once it has stopped changing
+async function settled(p, view) {
+ let last = -1;
+ for (let i = 0; i < 40; i++) {
+ const h = await view.evaluate((v) => v.scrollHeight);
+ if (h === last) break;
+ last = h;
+ await p.waitForTimeout(120);
+ }
+ return view.evaluate((v) => v.scrollHeight - v.clientHeight);
+}
+
+// Every scroll position the view passes through, read in the page each frame:
+// a scroll put back shows as a step up while the autoscroll goes down.
+async function record(view) {
+ await view.evaluate((v) => {
+ window.__tops = [];
+ const read = () => { window.__tops.push(v.scrollTop); window.__rec = requestAnimationFrame(read); };
+ v.addEventListener("scroll", () => window.__tops.push(v.scrollTop), { passive: true });
+ read();
+ });
+}
+const recorded = (view) => view.evaluate(() => { cancelAnimationFrame(window.__rec); return window.__tops.splice(0); });
+const top = (view) => view.evaluate((v) => v.scrollTop);
+const backs = (tops) => tops.slice(1).filter((y, i) => y < tops[i] - 0.5).length;
+
+// a scroll by code is put back, as it was before any middle press
+async function codePutBack(p, view, over, what) {
+ await p.waitForTimeout(400); // the reader's last scroll lets the next 250ms stand
+ const cur = await top(view);
+ const want = cur > over / 2 ? 0 : over;
+ await view.evaluate((v, y) => { v.scrollTop = y; }, want);
+ await p.waitForTimeout(250);
+ const now = await top(view);
+ assert.ok(Math.abs(now - cur) < 1, `${what}: a scroll by code is put back (was ${cur}, asked ${want}, at ${now})`);
+}
+
+for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
+ test(engine + ": the middle button's autoscroll scrolls a page", { timeout: 120000 }, async (t) => {
+ assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
+ // Chromium has the autoscroll only on Windows by default; WebView2 is that
+ const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium", args: ["--enable-blink-features=MiddleClickAutoscroll"] }));
+ t.after(() => browser.close());
+ for (const lang of ["en", "zh"]) {
+ await t.test(lang, async () => {
+ const errors = [];
+ const ctx = await browser.newContext({ viewport: VIEWPORT, reducedMotion: "reduce" });
+ const p = await ctx.newPage();
+ p.setDefaultTimeout(8000);
+ p.on("pageerror", (e) => errors.push(e.message));
+ await p.route("**/*", server(lang));
+ const view = p.locator("#view-agents");
+ await p.goto("http://magpie.test/?view=agents");
+ await p.locator("#view-agents .ag-lead").first().waitFor();
+ const over = await settled(p, view);
+ assert.ok(over > 150, `the page must overflow: ${over}`);
+ const r = await view.boundingBox();
+ // pressed on a button near the page's top, which the press focuses
+ const b = await view.locator("button.ag-start").first().boundingBox();
+ const x = b.x + b.width / 2, y = b.y + b.height / 2;
+
+ // pressed and let go, then the mouse moved below where it was pressed
+ await record(view);
+ await p.mouse.move(x, y);
+ await p.mouse.down({ button: "middle" });
+ await p.mouse.up({ button: "middle" });
+ for (let i = 1; i <= 6; i++) { await p.mouse.move(x, y + i * 12); await p.waitForTimeout(40); }
+ // held still: the autoscroll runs on with no event of the reader's
+ await p.waitForTimeout(600);
+ let tops = await recorded(view);
+ const ran = await top(view);
+ if (engine === "chromium") {
+ assert.ok(ran > 60, `the autoscroll scrolled the page: at ${ran}, passed ${tops.join(",")}`);
+ assert.equal(backs(tops), 0, `no scroll was put back under the autoscroll: ${tops.join(",")}`);
+ }
+ // the next press ends it, and with it the reader's scroll: it was a
+ // left press in the view's own padding, away from its scrollbar, which does nothing
+ await p.mouse.click(r.x + 3, r.y + r.height - 4);
+ await p.waitForTimeout(150);
+ const ended = await top(view);
+ await p.waitForTimeout(300);
+ assert.ok(Math.abs((await top(view)) - ended) < 1, "the autoscroll ended on the next press");
+ await codePutBack(p, view, over, "after the autoscroll");
+
+ // a button a press focused, then scrolled away by the wheel: a scroll
+ // put back leaves the page where the wheel took it
+ await p.mouse.wheel(0, -2000);
+ await p.waitForTimeout(400);
+ await p.mouse.move(x, y);
+ await p.mouse.down();
+ await p.mouse.move(r.x + 3, r.y + r.height - 4, { steps: 4 });
+ await p.mouse.up();
+ await p.mouse.move(r.x + r.width / 2, r.y + r.height / 2);
+ await p.mouse.wheel(0, 500);
+ let wheeled = NaN;
+ for (let i = 0; i < 40 && wheeled !== (wheeled = await top(view)); i++) await p.waitForTimeout(80);
+ assert.ok(wheeled > 100, `the wheel scrolled the page: ${wheeled}`);
+ await codePutBack(p, view, over, "after the wheel took a focused button away");
+
+ // pressed and held while the mouse moves, let go once it has gone on
+ if (engine === "chromium") {
+ // from where the wheel left the page, pressed low in the view
+ const before = await top(view);
+ await p.mouse.move(r.x + r.width / 2, r.y + r.height - 60);
+ await record(view);
+ await p.mouse.down({ button: "middle" });
+ for (let i = 1; i <= 6; i++) { await p.mouse.move(r.x + r.width / 2, r.y + r.height - 60 - i * 12); await p.waitForTimeout(40); }
+ await p.waitForTimeout(500);
+ tops = await recorded(view);
+ const now = await top(view);
+ assert.ok(now < before - 40, `the held autoscroll scrolled the page up: ${before} to ${now}`);
+ assert.equal(tops.slice(1).filter((v, i) => v > tops[i] + 0.5).length, 0, `no scroll was put back under the held autoscroll: ${tops.join(",")}`);
+ // the lift after a move ends a held one
+ await p.mouse.up({ button: "middle" });
+ await p.waitForTimeout(300);
+ await codePutBack(p, view, over, "after the held autoscroll");
+ }
+ assert.deepEqual(errors, []);
+ await ctx.close();
+ });
+ }
+ });
+}