From eb985cda28dab32090248a72e9af0a352042e3d2 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Fri, 20 Nov 2026 22:12:42 +0800 Subject: [PATCH] fix(project-view): keep the tree from scrolling sideways while dragging (#4828) WebKit autoscrolls the pressed tree toward a pointer right of it, so dragging a file to the editor, terminal or out of the window scrolled long rows out of view. The drag now holds the horizontal scroll position for its whole length, as it already held the vertical one outside the tree. --- src/features/project-tree/ProjectView.test.tsx | 16 ++++++++++++++++ src/features/project-tree/ProjectView.tsx | 5 ++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/features/project-tree/ProjectView.test.tsx b/src/features/project-tree/ProjectView.test.tsx index f668a1078..de863baeb 100644 --- a/src/features/project-tree/ProjectView.test.tsx +++ b/src/features/project-tree/ProjectView.test.tsx @@ -398,6 +398,22 @@ describe("ProjectView", () => { expect(ghost()).toBeNull(); }); + it("keeps the tree from scrolling sideways during a drag", async () => { + const { tree, row, rows } = setup(); + await waitFor(() => expect(rows()).toContain("README.md")); + document.elementFromPoint = () => null; + fireEvent( + row("README.md").querySelector("[data-project-path]")!, + new MouseEvent("pointerdown", { bubbles: true, button: 0 }), + ); + act(() => void window.dispatchEvent(new MouseEvent("pointermove", { buttons: 1, clientX: 40, clientY: 0 }))); + // WebKit's autoscroll toward a pointer right of the tree. + tree.scrollLeft = 600; + fireEvent.scroll(tree); + expect(tree.scrollLeft).toBe(0); + act(() => void window.dispatchEvent(new MouseEvent("pointerup", { bubbles: true }))); + }); + it("hands the selected files to an OS drag once the pointer leaves the window", async () => { const { row, rows } = setup(); await waitFor(() => expect(rows()).toContain("README.md")); diff --git a/src/features/project-tree/ProjectView.tsx b/src/features/project-tree/ProjectView.tsx index c926f29b9..656b18bd0 100644 --- a/src/features/project-tree/ProjectView.tsx +++ b/src/features/project-tree/ProjectView.tsx @@ -672,9 +672,11 @@ export function ProjectView() { // While dragging, the tree scrolls only when the pointer is near its top or bottom edge // (IDEA's DnD autoscroll). Leaving the tree toward a drop target elsewhere (the terminal, // an editor) keeps it where it is: WebKit's own autoscroll of the pressed scroll container - // would otherwise keep scrolling it toward the pointer. + // would otherwise keep scrolling it toward the pointer. It never scrolls sideways: a pointer + // right of the tree would scroll its long rows out of view. const scroller = containerRef.current?.querySelector('[role="tree"]') ?? null; let held = scroller?.scrollTop ?? 0; + const heldLeft = scroller?.scrollLeft ?? 0; let pointer: { x: number; y: number } | null = null; let frame = 0; const inside = (p: { x: number; y: number }) => { @@ -685,6 +687,7 @@ export function ProjectView() { if (!scroller) return; if (pointer && !inside(pointer)) scroller.scrollTop = held; else held = scroller.scrollTop; + if (scroller.scrollLeft !== heldLeft) scroller.scrollLeft = heldLeft; }; const edgeScroll = () => { frame = 0;