diff --git a/src/components/Changes.svelte b/src/components/Changes.svelte index e3b18c7cd..08a269932 100644 --- a/src/components/Changes.svelte +++ b/src/components/Changes.svelte @@ -407,7 +407,13 @@ // auto-selects its only commit — so an effect could not tell "the reader // picked a commit" from "the revision changed underneath them", and scrolled // on both. + // + // The tick comes first so that the selection has reached the effect that + // loads the diff: `diffReady` is read the moment it is awaited, and reading + // it any earlier gets the promise for the diff being navigated away from. async function scrollToDiff() { + await tick(); + await diffReady; await tick(); diffView?.scrollToFilesTop(); } diff --git a/src/components/PierreDiff.svelte b/src/components/PierreDiff.svelte index 6fcf446be..48993bc2a 100644 --- a/src/components/PierreDiff.svelte +++ b/src/components/PierreDiff.svelte @@ -373,7 +373,7 @@ // Scroll to a logical position in the scroll content, where 0 is the top of // the `header`. Pierre reuses a paged scroll scaffold, so the logical position // is not the container's `scrollTop` and has to go through `scrollTo`. - export function scrollToPosition( + function scrollToPosition( position: number, behavior: "smooth" | "instant" = "smooth", ): void { @@ -392,12 +392,24 @@ return headerEl.getBoundingClientRect().height - reserve; } - // Scroll the chrome out of the way. + // Bring the top of the files column into view, and only ever come back up to + // it: a caller asking for the files top wants the diff's first lines on + // screen, and they already are whenever the port sits above that point. + // Scrolling down from there would pull the chrome off screen on the reader's + // behalf, which nothing about picking a commit asks for. Instantly, too — an + // animated correction runs for half a second against a diff that may still be + // loading underneath it, and each adjustment it makes on the way reads as the + // view jumping again. export function scrollToFilesTop(): void { const position = filesTopPosition(); - if (position !== undefined) { - scrollToPosition(position); + if (position === undefined) { + return; } + const current = view?.getScrollTop(); + if (current !== undefined && current <= position) { + return; + } + scrollToPosition(position, "instant"); } // Nothing that targets a file should scroll the chrome back into view: the