1616 * the viewport alone. A hunk in a file that has not mounted yet is scrolled to by estimate
1717 * and corrected once the file draws. An agent's reveal (`hunk session navigate`, a comment
1818 * added with `--focus`) may name an exact line or note; the hunk reveal then lands on that
19- * line or note card instead of the hunk's first row. While a long review is still loading,
20- * files landing above a revealed target change height, so the target is held in place (and
21- * keeps the selection) until the reader's own wheel, touch, pointer, or key input.
19+ * line or note card instead of the hunk's first row. Files drawing or landing around a
20+ * revealed target change height, so the target is held in place (and keeps the selection)
21+ * for a moment after the reveal, and for as long as a long review is still loading, until
22+ * the reader's own wheel, touch, pointer, or key input, or a scroll command.
2223 * - Viewport anchoring. When the reader scrolls by hand — wheel, scrollbar, keys, or the
2324 * page's own scroll commands — and the scroll settles, the hunk nearest the viewport's
2425 * middle becomes the selection through `selection/anchor`, which never moves a viewport
2526 * (`REVIEW_VIEWPORT_ANCHOR_REVEAL`, audit B11). A target the page just revealed holds the
2627 * selection for as long as it stays where the reveal put it, so mounting files nearby
2728 * cannot steal it.
28- * - Scroll commands, through the `controller` this component fills in.
29+ * - Scroll commands, through the `controller` this component fills in. The page settles the
30+ * viewport before every command (`controller.settle`), so a key pressed before the scroll's
31+ * quiet period ends starts from the hunk that scroll brought to the middle, as the terminal
32+ * adopts it before reading its next key.
2933 * - Reloads. A new generation swaps the document under the reader; the stream keeps the file
3034 * at the top of the viewport, and the reader's offset inside it, where they were.
3135 */
@@ -56,7 +60,7 @@ import {
5660} from "./FileSection" ;
5761import type { ReviewViewedStatus } from "../viewed/viewedFiles" ;
5862import { planReviewFileRender , reviewDrawnRows , type ReviewFileRenderPlan } from "./fileProjection" ;
59- import type { ReviewFileGeometry } from "./hunkGeometry" ;
63+ import { estimateReviewFileGeometry , type ReviewFileGeometry } from "./hunkGeometry" ;
6064import { StreamMeasurements } from "./streamMeasurements" ;
6165
6266const NO_HIGHLIGHTS : readonly ReviewAgentHighlight [ ] = [ ] ;
@@ -78,8 +82,12 @@ const EDGE_HOLD_MAX_MS = 5_000;
7882/** How long scrolling must pause before the viewport counts as settled. */
7983const SCROLL_SETTLE_MS = 120 ;
8084
81- /** How long a reveal into a file that has not drawn yet waits to be corrected. */
82- const PENDING_REVEAL_MS = 3_000 ;
85+ /**
86+ * How long a reveal keeps its target in place after a load has finished: the target's own
87+ * file drawing (a hunk placed by estimate), or files around it drawing taller or shorter than
88+ * they reserved. Rapid jumps land before those files draw.
89+ */
90+ const REVEAL_SETTLE_MS = 3_000 ;
8391
8492/** Space left between the sticky file header and a revealed hunk. */
8593const REVEAL_MARGIN_PX = 8 ;
@@ -118,6 +126,11 @@ export interface ReviewStreamController {
118126 subscribeLayout ( listener : ( ) => void ) : ( ) => void ;
119127 /** Scroll the stream to one offset, as the reader moving by hand. */
120128 scrollTo ( top : number ) : void ;
129+ /**
130+ * Adopt a viewport move that has not settled yet as the selection now, so a command that
131+ * reads the selection starts from where the last scroll left the reader.
132+ */
133+ settle ( ) : void ;
121134}
122135
123136export interface ReviewStreamProps {
@@ -211,12 +224,16 @@ interface RevealHold {
211224 hunkIndex ?: number ;
212225 /** The target's offset from the top of the viewport right after the reveal. */
213226 viewportY : number ;
214- /** Set while the target's file has not drawn and the reveal still needs correcting. */
215- pendingUntil ?: number ;
227+ /** Where the target and the viewport were in the stream when the hold last placed it. */
228+ top : number ;
229+ scrollTop : number ;
230+ /** Until when, once the review has loaded, layout around the target is still followed. */
231+ settleUntil : number ;
216232 /**
217- * Set until the reader takes over (wheel, touch, pointer, key, or a scroll command): while
218- * a long review is still loading, files above the target that change height as they land
219- * leave the target where the reveal put it, and the selection stays on it.
233+ * Set until the reader takes over (wheel, touch, pointer, key, or a scroll command): files
234+ * around the target that change height as they draw or land leave the target where the
235+ * reveal put it, and the selection stays on it, for `settleUntil` and for as long as a long
236+ * review is still loading.
220237 */
221238 followLayout ?: true ;
222239}
@@ -231,7 +248,6 @@ function revealScrollTop(targetTop: number, hunkIndex: number | undefined) {
231248/** End a reveal's corrections: the reader has moved, so nothing pulls the viewport back. */
232249function releaseRevealHold ( held : RevealHold | undefined ) {
233250 if ( ! held ) return ;
234- delete held . pendingUntil ;
235251 delete held . followLayout ;
236252}
237253
@@ -441,8 +457,10 @@ export function ReviewStream(props: ReviewStreamProps) {
441457 fileKey,
442458 ...( hunkIndex !== undefined ? { hunkIndex } : { } ) ,
443459 viewportY : landed . top - root . scrollTop ,
460+ top : landed . top ,
461+ scrollTop : root . scrollTop ,
462+ settleUntil : performance . now ( ) + REVEAL_SETTLE_MS ,
444463 followLayout : true ,
445- ...( target . exact ? { } : { pendingUntil : performance . now ( ) + PENDING_REVEAL_MS } ) ,
446464 } ;
447465 } ,
448466 [ targetTop ] ,
@@ -476,19 +494,54 @@ export function ReviewStream(props: ReviewStreamProps) {
476494 // eslint-disable-next-line react-hooks/exhaustive-deps
477495 } , [ hunkToken ] ) ;
478496
479- // A file that drew corrects a reveal that had to estimate where its hunk was.
497+ /**
498+ * Scroll a followed reveal's target back to its place when the stream moved it, and say
499+ * whether the reveal is still being followed. A target the end of the stream kept lower than
500+ * its place rises to it once the stream grows.
501+ *
502+ * Once the review has loaded, following lasts `REVEAL_SETTLE_MS`, and a viewport that moved
503+ * on its own (not with the target, as the browser's scroll anchoring moves it, and not
504+ * clamped by a stream that shrank) is the reader, which ends it. While a long review loads,
505+ * only the reader's input ends it, since landing files move the viewport in every way.
506+ */
507+ const followReveal = useCallback ( ( ) => {
508+ const root = scrollRef . current ;
509+ const held = hold . current ;
510+ if ( ! root || ! held ?. followLayout ) return false ;
511+ const loading = latest . current . props . loading === true ;
512+ if ( ! loading && performance . now ( ) > held . settleUntil ) {
513+ releaseRevealHold ( held ) ;
514+ return false ;
515+ }
516+ const target = targetTop ( held . fileKey , held . hunkIndex ) ;
517+ if ( ! target ) return true ;
518+ const top = revealScrollTop ( target . top , held . hunkIndex ) ;
519+ // A viewport that ended up at the target's place (the browser's scroll anchoring, or a
520+ // stream that grew under a clamped reveal) is where the reveal wanted it, whoever moved it.
521+ if ( ! loading && Math . abs ( root . scrollTop - top ) > HOLD_TOLERANCE_PX ) {
522+ const scrolled = root . scrollTop - held . scrollTop ;
523+ const clamped = scrolled < 0 && root . scrollTop >= root . scrollHeight - root . clientHeight - 1 ;
524+ const followedTarget = Math . abs ( scrolled - ( target . top - held . top ) ) <= HOLD_TOLERANCE_PX ;
525+ if ( Math . abs ( scrolled ) > HOLD_TOLERANCE_PX && ! clamped && ! followedTarget ) {
526+ releaseRevealHold ( held ) ;
527+ return false ;
528+ }
529+ }
530+ if ( Math . abs ( root . scrollTop - top ) >= 1 ) root . scrollTop = top ;
531+ held . viewportY = target . top - root . scrollTop ;
532+ held . top = target . top ;
533+ held . scrollTop = root . scrollTop ;
534+ return true ;
535+ } , [ targetTop ] ) ;
536+
537+ // A file that drew corrects a reveal that had to estimate where its hunk was, or that the
538+ // file pushed out of place.
480539 const onGeometry = useCallback (
481540 ( fileKey : string , geometry : ReviewFileGeometry ) => {
482541 geometries . set ( fileKey , geometry ) ;
483- const held = hold . current ;
484- if ( held ?. pendingUntil === undefined || held . fileKey !== fileKey ) return ;
485- if ( performance . now ( ) > held . pendingUntil ) {
486- delete held . pendingUntil ;
487- return ;
488- }
489- reveal ( fileKey , held . hunkIndex ) ;
542+ followReveal ( ) ;
490543 } ,
491- [ reveal ] ,
544+ [ followReveal ] ,
492545 ) ;
493546
494547 /** Adopt the hunk nearest the middle of the viewport as the selection. */
@@ -497,10 +550,7 @@ export function ReviewStream(props: ReviewStreamProps) {
497550 if ( ! root ) return ;
498551 const held = hold . current ;
499552 if ( held ) {
500- if ( held . pendingUntil !== undefined && performance . now ( ) <= held . pendingUntil ) return ;
501- // Only the reader's own input ends a following reveal, so the stream moving under it
502- // while files land (the end of the stream holding it lower than its place) is not them.
503- if ( held . followLayout && latest . current . props . loading ) return ;
553+ if ( followReveal ( ) ) return ;
504554 const target = targetTop ( held . fileKey , held . hunkIndex ) ;
505555 if ( target && Math . abs ( target . top - root . scrollTop - held . viewportY ) <= HOLD_TOLERANCE_PX ) {
506556 return ;
@@ -514,20 +564,39 @@ export function ReviewStream(props: ReviewStreamProps) {
514564 const file = fileKey ? latest . current . filesByKey . get ( fileKey ) : undefined ;
515565 if ( ! section || ! fileKey || ! file ) return ;
516566 const offset = center - section . offsetTop - STREAM_HEADER_HEIGHT_PX ;
517- const geometry = geometries . get ( fileKey ) ;
518- const hunkIndex = geometry
519- ? reviewHunkIndexAtOffset ( file . hunks . length , ( index ) => geometry . hunks [ index ] , offset )
520- : 0 ;
567+ // A file that has not drawn is read from the rows it reserves, so a key pressed before it
568+ // draws starts from the hunk the reader will see there.
569+ const geometry =
570+ geometries . get ( fileKey ) ??
571+ estimateReviewFileGeometry (
572+ reviewDrawnRows ( latest . current . planOf ( file ) ) ,
573+ latest . current . props . layout ,
574+ STREAM_ROW_HEIGHT_PX ,
575+ ) ;
576+ const hunkIndex = reviewHunkIndexAtOffset (
577+ file . hunks . length ,
578+ ( index ) => geometry . hunks [ index ] ,
579+ offset ,
580+ ) ;
521581 latest . current . props . onViewportAnchor ( fileKey , hunkIndex ) ;
522- } , [ targetTop ] ) ;
582+ } , [ followReveal , targetTop ] ) ;
523583
524- // Anchor once scrolling pauses. Wheel, touch, pointer, and key input also end a reveal's
525- // corrections: the reader has taken over.
584+ // Anchor once scrolling pauses, or at once when a command needs the selection first. Wheel,
585+ // touch, pointer, and key input also end a reveal's corrections: the reader has taken over.
526586 const settleTimer = useRef < ReturnType < typeof setTimeout > | undefined > ( undefined ) ;
527- const onScroll = useCallback ( ( ) => {
587+ const settle = useCallback ( ( ) => {
588+ if ( settleTimer . current === undefined ) return ;
528589 clearTimeout ( settleTimer . current ) ;
529- settleTimer . current = setTimeout ( anchorToViewport , SCROLL_SETTLE_MS ) ;
590+ settleTimer . current = undefined ;
591+ anchorToViewport ( ) ;
530592 } , [ anchorToViewport ] ) ;
593+ const onScroll = useCallback ( ( ) => {
594+ // Read the scroll before the files it brings near mount and move things: a viewport the
595+ // reader moved ends the reveal's following here.
596+ followReveal ( ) ;
597+ clearTimeout ( settleTimer . current ) ;
598+ settleTimer . current = setTimeout ( settle , SCROLL_SETTLE_MS ) ;
599+ } , [ followReveal , settle ] ) ;
531600 useEffect ( ( ) => {
532601 const root = scrollRef . current ;
533602 if ( ! root ) return ;
@@ -553,7 +622,11 @@ export function ReviewStream(props: ReviewStreamProps) {
553622 releaseRevealHold ( hold . current ) ;
554623 // Page keys arrive on the document, not the stream, so they release the reload pin here.
555624 releasePin . current ?.( ) ;
625+ const before = root . scrollTop ;
556626 root . scrollTop += top ;
627+ // The scroll event comes a frame later; a key pressed sooner must still find this move
628+ // unsettled.
629+ if ( root . scrollTop !== before ) onScroll ( ) ;
557630 } ;
558631 controller . current = {
559632 element : ( ) => scrollRef . current ,
@@ -587,6 +660,7 @@ export function ReviewStream(props: ReviewStreamProps) {
587660 const root = scrollRef . current ;
588661 if ( root ) scrollBy ( top - root . scrollTop ) ;
589662 } ,
663+ settle,
590664 scroll ( direction , unit ) {
591665 const root = scrollRef . current ;
592666 if ( ! root ) return ;
@@ -625,7 +699,7 @@ export function ReviewStream(props: ReviewStreamProps) {
625699 releasePin . current ?.( ) ;
626700 controller . current = null ;
627701 } ;
628- } , [ controller ] ) ;
702+ } , [ controller , onScroll , settle ] ) ;
629703
630704 // Keep the reader's place by file across a document swap.
631705 const anchor = useRef < { key : string ; offset : number } | undefined > ( undefined ) ;
@@ -709,22 +783,9 @@ export function ReviewStream(props: ReviewStreamProps) {
709783 heights . set ( key , entry . contentRect . height ) ;
710784 }
711785 }
712- // Bodies above a revealed target changed height as a long review loads; scroll the
713- // target back to its place before paint. A target the end of the stream kept lower
714- // than that place rises to it once the stream grows.
715- const root = scrollRef . current ;
716- const held = hold . current ;
717- const target =
718- held ?. followLayout &&
719- latest . current . props . loading &&
720- targetTop ( held . fileKey , held . hunkIndex ) ;
721- if ( root && held && target ) {
722- const top = revealScrollTop ( target . top , held . hunkIndex ) ;
723- if ( Math . abs ( root . scrollTop - top ) >= 1 ) {
724- root . scrollTop = top ;
725- held . viewportY = target . top - root . scrollTop ;
726- }
727- }
786+ // Bodies around a revealed target changed height; scroll it back to its place before
787+ // paint.
788+ followReveal ( ) ;
728789 for ( const listener of Array . from ( layoutListeners . current ) ) listener ( ) ;
729790 } ) ;
730791 bodyObserver . current = observer ;
@@ -733,7 +794,7 @@ export function ReviewStream(props: ReviewStreamProps) {
733794 observer . disconnect ( ) ;
734795 bodyObserver . current = undefined ;
735796 } ;
736- } , [ targetTop ] ) ;
797+ } , [ followReveal ] ) ;
737798
738799 // A file that collapses unmounts its diff view while staying in the mounted window,
739800 // so its hunk geometry would outlive the drawing it describes.
0 commit comments