// Keyboard and focus: radio check marks, menu focus rings, hover against active, readable disabled controls, the file tree keys, and splitters. import assert from "node:assert/strict"; import { mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs"; await runSmoke("layout/keyboardFocus", async ({ page }) => { const output = await outputDir("layout/keyboardFocus"); await mockTauri(page, { commands: test => ({ ...test.workspace({ root: "C:\\keyboard", files: { "alpha.md": "# Alpha\n", "beta.md": "# Beta\n", "notes/first.md": "# First\n", "notes/second.md": "# Second\n", "notes/deep/third.md": "# Third\n", "zeta.txt": "Zeta\n", }, }), plugins_snapshot: () => ({ root: "C:/plugins", plugins: [] }), "plugin:window|is_maximized": () => false, }), }); await page.setViewportSize({ width: 1280, height: 760 }); await page.goto(smokeUrl); const tree = page.getByRole("tree", { name: "Workspace files" }); const row = rel => page.locator(`[data-tree-rel="${rel}"]`); const focused = () => page.evaluate(() => document.activeElement?.getAttribute("data-tree-rel") ?? document.activeElement?.getAttribute("aria-label")); await row("alpha.md").waitFor(); // The tree has one Tab stop. Arrow keys move, Right opens or enters a folder, Left closes it or goes up. assert.equal(await tree.locator('[role="treeitem"][tabindex="0"]').count(), 1); await row("alpha.md").click(); await page.getByRole("tab", { name: "alpha.md", exact: true, selected: true }).waitFor(); await row("alpha.md").focus(); await page.keyboard.press("ArrowDown"); assert.equal(await focused(), "beta.md"); assert.equal(await row("beta.md").getAttribute("aria-selected"), "true"); // Folders come first: notes, alpha.md, beta.md, zeta.txt. await page.keyboard.press("ArrowDown"); assert.equal(await focused(), "zeta.txt"); await page.keyboard.press("Home"); assert.equal(await focused(), "notes"); await page.keyboard.press("ArrowRight"); await row("notes/first.md").waitFor(); assert.equal(await row("notes").getAttribute("aria-expanded"), "true"); await page.keyboard.press("ArrowRight"); assert.equal(await focused(), "notes/deep"); await page.keyboard.press("ArrowLeft"); assert.equal(await focused(), "notes"); await page.keyboard.press("ArrowLeft"); assert.equal(await row("notes").getAttribute("aria-expanded"), "false"); await page.keyboard.press("End"); assert.equal(await focused(), "zeta.txt"); await page.keyboard.press("Home"); assert.equal(await focused(), "notes"); await page.keyboard.press("z"); assert.equal(await focused(), "zeta.txt"); await page.keyboard.press("Shift+ArrowUp"); assert.equal(await tree.locator('[aria-selected="true"]').count(), 2); assert.equal(await tree.locator('[role="treeitem"][tabindex="0"]').count(), 1); // Tab leaves the tree at once. await page.keyboard.press("Tab"); assert.notEqual(await page.evaluate(() => !!document.activeElement?.closest('[role="tree"]')), true); await page.keyboard.press("Shift+Tab"); assert.equal(await page.evaluate(() => !!document.activeElement?.closest('[role="tree"]')), true); // A splitter takes focus. The arrow keys move it by 16 pixels, and Home and End go to its limits. const splitter = page.getByRole("separator", { name: "Resize the side pane", exact: true }); const width = () => page.locator("aside.left").evaluate(node => Math.round(node.getBoundingClientRect().width)); const before = await width(); await splitter.focus(); await page.keyboard.press("ArrowRight"); assert.equal(await width(), before + 16); assert.equal(await splitter.getAttribute("aria-valuenow"), String(before + 16)); await page.keyboard.press("Home"); assert.equal(await width(), 160); await page.keyboard.press("End"); assert.equal(await width(), 480); assert.equal(await splitter.evaluate(node => getComputedStyle(node).outlineWidth), "2px"); const outline = page.getByRole("separator", { name: "Resize the outline", exact: true }); await outline.focus(); const right = () => page.locator("aside.right").evaluate(node => Math.round(node.getBoundingClientRect().width)); const rightBefore = await right(); await page.keyboard.press("ArrowLeft"); assert.equal(await right(), rightBefore + 16); await splitter.focus(); await page.keyboard.press("Home"); for (const theme of ["dark", "light"]) { await page.evaluate(async mode => { const { saveAppearance } = await import("/src/lib/theme/appearance.ts"); saveAppearance({ theme: "default", mode }); window.dispatchEvent(new Event("focus")); }, theme); await page.waitForFunction(mode => document.documentElement.dataset.theme === mode, theme); // An active toggle differs from a hovered button. A disabled button keeps full opacity and dim text. const files = page.getByRole("button", { name: "Files", exact: true }); const search = page.getByRole("button", { name: "Search", exact: true }); await search.hover(); const look = locator => locator.evaluate(node => { const style = getComputedStyle(node); return { fill: style.backgroundColor, color: style.color, bar: style.boxShadow }; }); const [hovered, active] = [await look(search), await look(files)]; assert.equal(hovered.bar, "none"); assert.match(active.bar, /inset/); assert.notEqual(hovered.color, active.color); const save = page.getByRole("button", { name: "Save", exact: true }); assert.equal(await save.isDisabled(), true); assert.equal(await save.evaluate(node => getComputedStyle(node).opacity), "1"); const dim = await page.evaluate(() => { const probe = document.createElement("span"); probe.style.color = "var(--rs-text-dim)"; document.body.append(probe); const color = getComputedStyle(probe).color; probe.remove(); return color; }); assert.equal(await save.evaluate(node => getComputedStyle(node).color), dim); await page.locator("header.header").screenshot({ path: `${output}/header-states-${theme}.png` }); // The current radio row shows a check mark and no fill. Keyboard focus adds a ring that hover does not have. await page.getByRole("tab", { name: "alpha.md", exact: true }).click({ button: "right" }); await page.getByRole("menuitem", { name: "View", exact: true }).click(); const write = page.getByRole("menuitemradio", { name: "Write", exact: true }); const source = page.getByRole("menuitemradio", { name: "Source", exact: true }); assert.equal(await write.getAttribute("aria-checked"), "true"); assert.equal(await write.locator("svg").count(), 1); assert.equal(await source.locator("svg").count(), 0); await source.hover(); assert.equal(await source.evaluate(node => getComputedStyle(node).outlineStyle), "none"); await write.focus(); await page.keyboard.press("ArrowDown"); const keyed = page.locator('[role="menu"] button:focus-visible'); assert.equal(await keyed.getAttribute("aria-checked"), "false"); assert.equal(await keyed.evaluate(node => getComputedStyle(node).outlineWidth), "2px"); // With focus and the pointer on other rows, the checked row has no fill. await page.mouse.move(0, 700); assert.equal(await write.evaluate(node => getComputedStyle(node).backgroundColor), "rgba(0, 0, 0, 0)"); await page.screenshot({ path: `${output}/menu-check-${theme}.png` }); await page.keyboard.press("Escape"); await page.keyboard.press("Escape"); await page.keyboard.press("Escape"); // Disabled menu rows keep full opacity and dim text. await page.getByRole("tab", { name: "alpha.md", exact: true }).click({ button: "right" }); const disabled = page.getByRole("menuitem", { name: "Close tabs after", exact: true }); assert.equal(await disabled.isDisabled(), true); assert.equal(await disabled.evaluate(node => getComputedStyle(node).opacity), "1"); assert.equal(await disabled.evaluate(node => getComputedStyle(node).color), dim); await page.keyboard.press("Escape"); } // Help names the key that leaves the text before Tab moves on. await page.keyboard.press("F1"); await page.getByRole("dialog", { name: "Help and about" }).getByText("Leave the text, then press Tab", { exact: true }).waitFor(); await page.getByRole("button", { name: "Close help", exact: true }).click(); assert.deepEqual(page.errors, []); console.log("PASS: one tree Tab stop, tree keys, type-ahead, range selection, keyboard splitters, radio check marks, menu focus ring, hover against active, readable disabled controls, Help, both themes."); });