From ecb28abf2eb4a322eb07060a65958eeafd7232eb Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 16 Oct 2026 06:08:53 +0800 Subject: [PATCH] gui: a menu redrawn under a still pointer leaves the typing in its search (#2439) The font menu, session combo and row menus focus whichever item the pointer moves onto. When the search narrowed the list under a pointer that hadn't moved, the redrawn item under it got a mouseenter too, took the focus out of the search, and the next letters went nowhere. fonts.test.cjs went red whenever the list landed while the pointer sat over the menu. hoverFocus now remembers where the last mousemove was. A mouseenter there is a redraw's (UI Events fires mouseenter before its mousemove), so it leaves the focus where it is. A real move still focuses the item. The new fonts test hovers an item, then types "HarmonyOS" into the search. The focus has to stay in the field, and Enter has to pick HarmonyOS Sans SC. - Broken by hand: with the old `b.onmouseenter = () => b.focus()`, the test is red in chromium and webkit (the focus is on the item, and the typed text is cut short). - With the fix: green in both engines, 3 runs. Full suite: node --test --test-concurrency=4 internal/gui/tests/*.test.cjs (chromium and webkit) on a61a6f954 with all three of these commits' changes: 10655 tests, 10616 pass, 39 fail, all in 16 top-level tests. Each of their files was then run alone: - green alone on origin/main 008571462 and on this tree (load): import-link-gemini, market-have, plugin-page, readable-focus, text-size. - red alone on main too, filed or commented: - quota-count-wrap and quota-name-translated: #2228 - s3-sync: #2448 - usage-agents-hide: #2276 - usage-checkin: #2142 - usage-fold: #2163 - tray-usages: #2453, new After rebasing onto 008571462, these were run again on this tree, alone: - green: fonts, key-balance-card, plugin-move, i18n-keys. - still red, the same as main: quota-name-translated, quota-count-wrap, tray-usages. The full GUI run was not repeated after the rebase. go test ./internal/gui on 008571462 plus these changes, sandbox HOME: FAIL. The only failure is TestPanelJoinsFullScreenSpaces, which is #2450 and is red on main too. TestCoreScriptBudget passes. --- internal/gui/assets/app.js | 15 ++++++++++++--- internal/gui/tests/fonts.test.cjs | 32 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index ea9a83593..b8a7b5fa2 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -3584,7 +3584,7 @@ function openRowMenu(anchor, acts) { // the click goes along, so an item that opens something below can // bring it into view (scrollOnPurpose) b.onclick = (e) => { e.stopPropagation(); closeAgentMenu(); o.run(e); }; - b.onmouseenter = () => b.focus({ preventScroll: true }); + hoverFocus(b); box.append(b); if (!o.off) items.push(b); } @@ -14149,6 +14149,15 @@ function protoPicker(p, value, onChange) { return pill; } +// hoverFocus: an item the pointer moves onto takes the focus. A list +// redrawn under a still pointer (a search narrowing it) gets a mouseenter +// too, which took the focus out of the search mid-word (#2439); one where +// the last mousemove was is a redraw's (mouseenter comes before its move). +let pointerAt = ""; +addEventListener("mousemove", (e) => { pointerAt = e.screenX + "," + e.screenY; }, true); +function hoverFocus(b) { + b.onmouseenter = (e) => { if (e.screenX + "," + e.screenY !== pointerAt) b.focus({ preventScroll: true }); }; +} let protoMenu = null; function closeProtoMenu() { if (!protoMenu) return; @@ -14247,7 +14256,7 @@ function openProtoMenu(anchor, opts, value, choose, head = "Protocol this key sp b.focus({ preventScroll: true }); } }; - b.onmouseenter = () => b.focus({ preventScroll: true }); + hoverFocus(b); box.append(b); return b; }); @@ -18054,7 +18063,7 @@ function openSessCombo(anchor, all, opts, value, choose, head) { b.title = o.v; b.append(tick, words); b.onclick = (e) => { e.stopPropagation(); closeProtoMenu(); choose(o.v); }; - b.onmouseenter = () => b.focus({ preventScroll: true }); + hoverFocus(b); return b; }; const draw = () => { diff --git a/internal/gui/tests/fonts.test.cjs b/internal/gui/tests/fonts.test.cjs index 1ece5b7be..86ea16f85 100644 --- a/internal/gui/tests/fonts.test.cjs +++ b/internal/gui/tests/fonts.test.cjs @@ -185,6 +185,38 @@ for (const [engine, runtime] of engines) { } finally { await browser.close(); } }); + // #2439: the pointer resting on the menu while a search narrows it got a + // mouseenter from the redraw, which took the focus out of the field, so + // Enter chose "Refresh fonts" (the item now under the pointer). + test(`${engine}: a search narrowing the menu under a still pointer keeps the typing in its field`, async () => { + const browser = await runtime.launch(); + try { + const { page, api, errors } = await open(browser, "zh", { viewport: { width: 560, height: 620 } }); + await page.locator("#uiFontRow .font-family").click(); + const second = await page.locator(".font-menu .pm-item").nth(1).boundingBox(); + await page.mouse.move(second.x + second.width / 2, second.y + second.height / 2); + await page.locator(".font-menu .sc-q").fill("HarmonyOS"); + // the item now under the pointer is Refresh fonts. The redraw's + // mouseenter comes on the engine's own hover timer (WebKit's is + // later than a frame), and what is checked is that nothing came, so + // there is no state to wait for: 300ms is past both engines' timers. + await page.waitForTimeout(300); + assert.equal(await page.evaluate(() => document.activeElement.className), "sc-q", "the search field keeps the focus"); + const reads = api.reads; + await page.keyboard.press("Enter"); + await settled(page); + assert.equal(await page.locator("#uiFontRow .font-family span").textContent(), "HarmonyOS Sans SC"); + assert.equal(api.reads, reads, "Enter didn't refresh the font list"); + // a pointer that moves still takes an item's focus + await page.locator("#uiFontRow .font-family").click(); + const item = page.locator(".font-menu .pm-item").nth(2); + const box = await item.boundingBox(); + await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2 + 1); + assert.equal(await item.evaluate(e => e === document.activeElement), true, "a moved pointer focuses the item it's on"); + assert.deepEqual(errors, []); + } finally { await browser.close(); } + }); + test(`${engine}: font menu search and keys fit narrow windows without scrolling the page`, async () => { const browser = await runtime.launch(); try {