diff --git a/tests/browser/interaction.spec.js b/tests/browser/interaction.spec.js index 491bd55..50c10e3 100644 --- a/tests/browser/interaction.spec.js +++ b/tests/browser/interaction.spec.js @@ -1316,38 +1316,94 @@ test('arrow keys and Enter select a net from the search list', async ({ page }) expect((await state(page)).selectedNet).toBe(net); }); -test('an arrow key onto a pinned part keeps its pin', async ({ page }) => { +test('arrow keys in the part list only hover, Enter pins and selects', async ({ page }) => { await load(page); await page.locator('#tab-components').click(); const rows = page.locator('#comp-tbody .comp-row'); const a = Number(await rows.nth(0).getAttribute('data-idx')); const b = Number(await rows.nth(1).getAttribute('data-idx')); - await rows.nth(1).click(); - await settle(page); await rows.nth(0).click(); await settle(page); + await page.mouse.move(0, 0); await page.keyboard.press('ArrowDown'); await settle(page); - expect((await state(page)).selectedFootprintIdx).toBe(b); + await expect(page.locator('#comp-tbody .comp-row.focused')).toHaveAttribute('data-idx', String(b)); + let s = await state(page); + expect(s.selectedFootprintIdx).toBe(a); + expect(s.highlightedFootprints).toEqual([b]); + expect(await page.evaluate((b) => !!pinnedComponents[b], b)).toBe(false); + + await page.keyboard.press('Enter'); + await settle(page); + s = await state(page); + expect(s.selectedFootprintIdx).toBe(b); + expect(s.highlightedFootprints).toEqual([]); expect(await page.evaluate(([a, b]) => [!!pinnedComponents[a], !!pinnedComponents[b]], [a, b])) .toEqual([true, true]); }); -test('an arrow key past the last part keeps its pin', async ({ page }) => { +test('an arrow key past the last part changes nothing but the hover', async ({ page }) => { await load(page); await page.locator('#tab-components').click(); const last = page.locator('#comp-tbody .comp-row').last(); const z = Number(await last.getAttribute('data-idx')); await last.click(); await settle(page); + await page.mouse.move(0, 0); await page.keyboard.press('ArrowDown'); await settle(page); - expect((await state(page)).selectedFootprintIdx).toBe(z); + const s = await state(page); + expect(s.selectedFootprintIdx).toBe(z); + expect(s.highlightedFootprints).toEqual([z]); expect(await page.evaluate((z) => !!pinnedComponents[z], z)).toBe(true); }); +test('a selection from outside the part list drops the arrow-key focus', async ({ page }) => { + await load(page); + await page.locator('#tab-components').click(); + const rows = page.locator('#comp-tbody .comp-row'); + const b = Number(await rows.nth(1).getAttribute('data-idx')); + const c = Number(await rows.nth(2).getAttribute('data-idx')); + await rows.nth(0).click(); + await settle(page); + await page.mouse.move(0, 0); + + await page.keyboard.press('ArrowDown'); + await settle(page); + // A breadcrumb click selects without a rebuild of the list. + await page.evaluate((c) => selectFootprint(c, false), c); + await settle(page); + await expect(page.locator('#comp-tbody .comp-row.focused')).toHaveCount(0); + expect((await state(page)).highlightedFootprints).toEqual([]); + + await page.keyboard.press('Enter'); + await settle(page); + expect((await state(page)).selectedFootprintIdx).toBe(c); + expect(await page.evaluate((b) => !!pinnedComponents[b], b)).toBe(false); +}); + +test('a mouse hover after the arrow keys takes over the hover', async ({ page }) => { + await load(page); + await page.locator('#tab-components').click(); + const rows = page.locator('#comp-tbody .comp-row'); + const c = Number(await rows.nth(2).getAttribute('data-idx')); + await rows.nth(0).click(); + await settle(page); + await page.mouse.move(0, 0); + + await page.keyboard.press('ArrowDown'); + await settle(page); + await rows.nth(2).hover(); + await waitIdle(page); + await expect(page.locator('#comp-tbody .comp-row.focused')).toHaveCount(0); + expect((await state(page)).highlightedFootprints).toEqual([c]); + + await settle(page); + expect((await state(page)).highlightedFootprints).toEqual([]); +}); + test('Escape deselects', async ({ page }) => { await load(page); await selectNetFromList(page, frontNets[0]); diff --git a/web/app.js b/web/app.js index d02d56d..ccd9a92 100644 --- a/web/app.js +++ b/web/app.js @@ -668,17 +668,27 @@ function setHover(nets, footprints) { scheduleRedrawAll(); } +// The last used input owns the hover. The arrow keys call setHover directly; +// the functions below drop the arrow-key focus row in the part list. +function dropCompFocus() { + var row = document.querySelector("#comp-tbody .comp-row.focused"); + if (row) row.classList.remove("focused"); +} + function hoverFootprint(fpIdx) { + dropCompFocus(); setHover([], [fpIdx]); } function hoverNet(netName) { + dropCompFocus(); setHover([netName], []); } // Clicks call this too: a click can rebuild the list under the pointer, and // then no mouseleave fires. function hoverClear() { + dropCompFocus(); if (hoverNets.length === 0 && highlightedFootprints.length === 0) return; setHover([], []); } @@ -1080,6 +1090,8 @@ function deselectFootprint(fpIdx) { } function updateCompListSelection(fpIdx) { + // A new selection ends an arrow-key walk: drop its focus row and hover. + if (document.querySelector("#comp-tbody .comp-row.focused")) hoverClear(); document.querySelectorAll(".comp-row").forEach(function(row) { row.classList.toggle("selected", parseInt(row.dataset.idx) === fpIdx); }); @@ -1902,15 +1914,17 @@ function getCurrentTab() { // Navigate up/down in the comp or net search results. delta = +1 (down) or -1 (up). function navigateList(tabType, delta) { if (tabType === "comp") { + // An arrow key only moves the focus and hovers the part. Enter pins and + // selects it. var rows = Array.from(document.querySelectorAll("#comp-tbody .comp-row")); if (rows.length === 0) return; - var cur = rows.findIndex(function(r) { return r.classList.contains("selected"); }); + var cur = rows.findIndex(function(r) { return r.classList.contains("focused"); }); + if (cur < 0) cur = rows.findIndex(function(r) { return r.classList.contains("selected"); }); var next = Math.max(0, Math.min(rows.length - 1, cur < 0 ? (delta > 0 ? 0 : rows.length - 1) : cur + delta)); - var row = rows[next]; - var idx = parseInt(row.dataset.idx); - // An arrow key is not a click: it pins the part it lands on, never unpins. - if (!pinnedComponents[idx]) togglePinComponent(idx); - selectFootprint(idx, false); + rows.forEach(function(r) { r.classList.remove("focused"); }); + rows[next].classList.add("focused"); + rows[next].scrollIntoView({ block: "nearest" }); + setHover([], [parseInt(rows[next].dataset.idx)]); } else { // Net panel var netDetail = document.getElementById("net-detail-panel"); @@ -1937,7 +1951,8 @@ function navigateList(tabType, delta) { function activateListSelection(tabType) { if (tabType === "comp") { - var row = document.querySelector("#comp-tbody .comp-row.selected") || + var row = document.querySelector("#comp-tbody .comp-row.focused") || + document.querySelector("#comp-tbody .comp-row.selected") || document.querySelector("#comp-tbody .comp-row"); if (row) { var idx = parseInt(row.dataset.idx); @@ -2055,7 +2070,7 @@ window.addEventListener("load", async function() { // Keyboard nav in component list document.getElementById("comp-search-input").addEventListener("keydown", function(e) { - if (e.key === "Escape") { e.stopPropagation(); this.value = ""; updateCompFilter(""); this.blur(); } + if (e.key === "Escape") { e.stopPropagation(); this.value = ""; updateCompFilter(""); hoverClear(); this.blur(); } else if (e.key === "ArrowDown") { e.preventDefault(); navigateList("comp", 1); } else if (e.key === "ArrowUp") { e.preventDefault(); navigateList("comp", -1); } else if (e.key === "Enter") { e.preventDefault(); activateListSelection("comp"); this.blur(); } diff --git a/web/viewer.css b/web/viewer.css index 5e8160f..33e66f7 100644 --- a/web/viewer.css +++ b/web/viewer.css @@ -1008,11 +1008,13 @@ canvas { /* ---- Keyboard navigation focus ---- */ .net-search-row.focused, -.net-comp-row.focused { +.net-comp-row.focused, +.comp-row.focused { background: var(--surface2); outline: 1px solid var(--accent); outline-offset: -1px; } +.comp-row.focused td { background: var(--surface2); } /* ---- Help overlay ---- */