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 <[email protected]>
Цей коміт міститься в:
Orca authored and GitHub committed 2026-12-03 17:57:49 +08:00
1 джерело 69c9521e85
коміт c3726daed6
3 files changed
+67 -3

No files matched your search

+28
Переглянути файл
@@ -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" });
+27 -2
Переглянути файл
@@ -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<TreeHandle>();
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<TreeHandle>();
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<TreeHandle>();
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");
+12 -1
Переглянути файл
@@ -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<T>(props: TreeProps<T>) {
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;
},