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 {