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.
This commit is contained in:
l0ng-ai
2026-11-20 22:12:42 +08:00
committed by GitHub
parent 212ee03d71
commit eb985cda28
2 changed files with 20 additions and 1 deletions
@@ -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"));
+4 -1
View File
@@ -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<HTMLElement>('[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;