diff --git a/src/ui/Tree.test.tsx b/src/ui/Tree.test.tsx index 39f001729..c1282f642 100644 --- a/src/ui/Tree.test.tsx +++ b/src/ui/Tree.test.tsx @@ -535,3 +535,33 @@ describe("Tree indents", () => { expect(placeholder?.style.paddingLeft).toBe("calc(24px + 1 * var(--tree-indent, 20px))"); }); }); + +describe("Tree double-click", () => { + it("selects on the first press without cancelling its default", () => { + const { tree } = setup(); + const row = within(tree).getByRole("treeitem", { name: "README.md" }); + expect(fireEvent.mouseDown(row, { button: 0, detail: 1 })).toBe(true); + expect(row).toHaveAttribute("aria-selected", "true"); + }); + + it("cancels the second press so double-clicking cannot select label text, and still activates", () => { + const { tree, onActivate } = setup(); + const row = within(tree).getByRole("treeitem", { name: "README.md" }); + fireEvent.mouseDown(row, { button: 0, detail: 1 }); + // The browser starts its word selection on this press; cancelling it keeps + // the labels unselected (jsdom reports cancellation through dispatchEvent). + expect(fireEvent.mouseDown(row, { button: 0, detail: 2 })).toBe(false); + fireEvent.doubleClick(row); + expect(onActivate).toHaveBeenCalledWith(expect.objectContaining({ id: "README.md" }), expect.anything()); + expect(row).toHaveAttribute("aria-selected", "true"); + }); + + it("still expands a folder on double-click", () => { + const { tree } = setup(); + const row = within(tree).getByRole("treeitem", { name: "src" }); + fireEvent.mouseDown(row, { button: 0, detail: 1 }); + fireEvent.mouseDown(row, { button: 0, detail: 2 }); + fireEvent.doubleClick(row); + expect(labels(tree)).toEqual(["src", "main", "util.ts", "README.md"]); + }); +}); diff --git a/src/ui/Tree.tsx b/src/ui/Tree.tsx index 1064a3fef..b92bbcb88 100644 --- a/src/ui/Tree.tsx +++ b/src/ui/Tree.tsx @@ -590,6 +590,17 @@ export function Tree(props: TreeProps) { return; } if (e.button !== 0) return; + // The second press of a double-click starts the browser's word selection, + // which in a virtualized tree spills onto the rows an expanding folder just + // inserted and highlights neighbouring labels. Prevent it: rows are + // controls, not text (the shared List does the same via `user-select`), the + // first press already selected, and the dblclick below still fires, so + // activation keeps working. Rows can host editors (inline rename), so this + // is scoped to repeated clicks instead of unselecting all row content. + if (e.detail >= 2) { + e.preventDefault(); + return; + } setQuery(""); if (e.shiftKey) selectIndex(i, "range"); else if (e.metaKey || e.ctrlKey) selectIndex(i, "toggle");