From c3726daed673b18b4ef53fbce2f4daae84588857 Mon Sep 17 00:00:00 2001 From: Orca <301901286+og-orca@users.noreply.github.com> Date: Thu, 3 Dec 2026 10:57:49 +0100 Subject: [PATCH] fix(project-tree): keep the clicked row in place between the clicks of a double-click (#7741) With Always Select Opened File on, the first click of a double-click opened the preview tab and TreeHandle.reveal centered the row even though it was already visible, so the second click landed on another row. reveal now leaves a row that is in view where it is (1 px tolerance for fractional display scales) and centers only rows out of view. This applies to every tree that calls reveal. Closes #7712 Co-authored-by: og-orca <301901286+og-orca@users.noreply.github.com> --- e2e/projectTree.spec.ts | 28 ++++++++++++++++++++++++++++ src/ui/Tree.test.tsx | 29 +++++++++++++++++++++++++++-- src/ui/Tree.tsx | 13 ++++++++++++- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/e2e/projectTree.spec.ts b/e2e/projectTree.spec.ts index f8e1b864d..24baf6a68 100644 --- a/e2e/projectTree.spec.ts +++ b/e2e/projectTree.spec.ts @@ -46,6 +46,34 @@ test("a single click shows the file in the preview tab, which the next one reuse await expect(tabs.getByRole("tab", { name: "README.md" })).toHaveCount(0); }); +test("Always Select Opened File keeps the clicked row in place for the second click", async ({ workbench: page }) => { + await page.setViewportSize({ width: 1200, height: 420 }); + const tree = page.getByRole("tree", { name: "Project" }); + const tabs = page.getByRole("tablist", { name: "Editor tabs" }); + await page.evaluate(async () => { + const storeModule = "/src/features/project-tree/store.ts"; + const { expandAll, setViewOption } = await import(/* @vite-ignore */ storeModule); + setViewOption("autoscrollFromSource", true); + await expandAll(); + }); + const file = tree.getByRole("treeitem", { name: "OrderDto.kt", exact: true }); + await expect(file).toBeVisible(); + await tree.evaluate((el) => (el.scrollTop = 60)); + await settle(page); + const open = tree.locator('[aria-expanded="true"]'); + const folders = await open.count(); + + const label = (await file.getByText("OrderDto.kt").boundingBox())!; + const at = [label.x + label.width / 2, label.y + label.height / 2] as const; + await page.mouse.click(...at); + await expect(tabs.getByRole("tab", { name: "OrderDto.kt" })).toHaveAttribute("aria-selected", "true"); + await page.mouse.down({ clickCount: 2 }); + await page.mouse.up({ clickCount: 2 }); + await expect(file).toHaveAttribute("aria-selected", "true"); + await expect(open).toHaveCount(folders); + expect(await tree.evaluate((el) => el.scrollTop)).toBe(60); +}); + test("Rename targets a file or folder directly from its context menu", async ({ workbench: page }) => { await page.setViewportSize({ width: 720, height: 480 }); const menu = page.getByRole("menu", { name: "Context menu" }); diff --git a/src/ui/Tree.test.tsx b/src/ui/Tree.test.tsx index 444750dc1..bdc1051cd 100644 --- a/src/ui/Tree.test.tsx +++ b/src/ui/Tree.test.tsx @@ -653,14 +653,39 @@ describe("Tree scroll following", () => { expect(scrollTo).not.toHaveBeenCalled(); }); + const tall: N[] = Array.from({ length: 100 }, (_, i) => ({ id: `row ${i}` })); + it("explicitly reveals an unchanged lead", () => { const handle = createRef(); - setup({ handleRef: handle, defaultSelected: ["README.md"] }); + setup({ roots: tall, handleRef: handle, defaultSelected: ["row 60"] }); scrollTo.mockClear(); - act(() => handle.current!.reveal("README.md")); + act(() => handle.current!.reveal("row 60")); expect(scrollTo).toHaveBeenCalledTimes(1); }); + it("reveals a row already in view without scrolling", () => { + const handle = createRef(); + const { lead } = setup({ roots: tall, handleRef: handle }); + fireEvent.mouseDown(screen.getByRole("treeitem", { name: "row 20" }), { button: 0 }); + expect(lead()).toBe("row 20"); + scrollTo.mockClear(); + act(() => handle.current!.reveal("row 20")); + expect(scrollTo).not.toHaveBeenCalled(); + expect(lead()).toBe("row 20"); + }); + + it("reveals a row at the bottom edge without centering it when the offset is rounded to a device pixel", () => { + const handle = createRef(); + const { tree } = setup({ roots: tall, handleRef: handle }); + const row = screen.getByRole("treeitem", { name: "row 30" }); + const end = Number(/translateY\(([\d.]+)px\)/.exec(row.style.transform)![1]) + parseFloat(row.style.height); + tree.scrollTop = end - tree.clientHeight - 0.4; + fireEvent.mouseDown(row, { button: 0 }); + scrollTo.mockClear(); + act(() => handle.current!.reveal("row 30")); + expect(scrollTo).not.toHaveBeenCalled(); + }); + it("centers the focused tree's selection without changing it", () => { const { tree, key, lead } = setup({ defaultSelected: ["README.md"] }); key("End"); diff --git a/src/ui/Tree.tsx b/src/ui/Tree.tsx index b9e430ec1..4b39ea2c1 100644 --- a/src/ui/Tree.tsx +++ b/src/ui/Tree.tsx @@ -72,7 +72,7 @@ export interface TreeOccurrence { export interface TreeHandle { focus(): void; - /** Selects `key` (must be visible, i.e. ancestors expanded) and scrolls to it; false when no row has `key`. */ + /** Selects `key` (must be visible, i.e. ancestors expanded) and centers it unless it is in view; false when no row has `key`. */ reveal(key: string): boolean; /** Scrolls the row with `key` into view without changing the selection. */ scrollIntoView(key: string): void; @@ -560,6 +560,17 @@ export function Tree(props: TreeProps) { if (i === undefined) return false; setAnchor(key); commitSelection(new Set([key]), key); + // Centering a row in view would move the rows under the pointer between the clicks of a double-click. + const viewport = scrollRef.current; + const row = virtualizer.getVirtualItems().find((item) => item.index === i); + // At a fractional display scale the row can stick out by less than a pixel after it was scrolled into view. + if ( + viewport && + row && + row.start >= viewport.scrollTop - 1 && + row.end <= viewport.scrollTop + viewport.clientHeight + 1 + ) + return true; virtualizer.scrollToIndex(i, { align: "center" }); return true; },