diff --git a/src/client/changes/GitGraphSection.tsx b/src/client/changes/GitGraphSection.tsx index 8572a4ee..a3962fda 100644 --- a/src/client/changes/GitGraphSection.tsx +++ b/src/client/changes/GitGraphSection.tsx @@ -7,7 +7,7 @@ * Mounted by {@link GitLens} on the repo/worktree target the lens has already * selected; it owns its own bounded page (`git.graph`) and reports the * selected commit upward for the tab's bottom pane (`details` tab). */ -import { useEffect, useMemo, useRef, useState, type MouseEvent } from 'react' +import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, type MouseEvent } from 'react' import { Button, IconBranchOutlineRegular, IconCheckOutlineRegular, IconGlobeOutlineRegular, } from '@deepseek-ai/dsh-client-ui-primitives' @@ -17,6 +17,8 @@ import { relativeTime, t } from '../locales.ts' import { Notice } from '../ui/index.ts' import { AnchoredPopover } from '../AnchoredPopover.tsx' import { edgePath, layoutGraph } from './graph-layout.ts' +import { createFrameBatcher } from '../frame-batcher.ts' +import { windowedRange } from './git-graph-windowing.ts' import css from './graph.module.css' type GraphPage = Awaited> @@ -138,6 +140,38 @@ export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onP const appending = useRef(false) const scrollRef = useRef(null) const sentinelRef = useRef(null) + /** + * The scroll container's measured viewport, and the per-frame coalescer that + * keeps it in step with a scroll stream (a trackpad fires several scroll + * events per display frame; one commit per frame is enough to move the + * window, and each commit reads layout once). + */ + const [view, setView] = useState({ scrollTop: 0, height: 0 }) + const scrollBatcher = useRef(createFrameBatcher()).current + const syncWindow = useCallback((): void => { + const node = scrollRef.current + if (node === null) return + const scrollTop = node.scrollTop + const height = node.clientHeight + setView(current => (current.scrollTop === scrollTop && current.height === height + ? current + : { scrollTop, height })) + }, []) + // A page append/shrink changes the content under the scroll position; the + // browser clamps a stale scrollTop only after the DOM commit, so re-read it + // in the layout phase. The pure range function clamps anyway. + useLayoutEffect(() => { syncWindow() }, [syncWindow, page?.entries.length]) + // A panel resize (the details pane opening, a window resize, the sidebar + // width drag) changes how many rows fit: re-measure so the mounted window + // still covers the viewport instead of leaving a blank band. + useEffect(() => { + const node = scrollRef.current + if (node === null || typeof ResizeObserver === 'undefined') return + const observer = new ResizeObserver(() => { syncWindow() }) + observer.observe(node) + return () => { observer.disconnect() } + }, [syncWindow]) + useEffect(() => () => { scrollBatcher.dispose() }, [scrollBatcher]) /** Row hover preview: the middle information rung between the one-line row * (pills + subject) and the details card (everything). It opens only after * a short dwell so scrolling past rows never flashes cards; the pointer @@ -227,6 +261,12 @@ export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onP const rows = useMemo(() => layoutGraph(page?.entries ?? []), [page?.entries]) const width = Math.max(28, ...rows.map(row => laneX(row.laneCount) + 2)) + const range = windowedRange({ + count: page?.entries.length ?? 0, + rowHeight: ROW_HEIGHT, + scrollTop: view.scrollTop, + viewportHeight: view.height, + }) const remotes = useMemo(() => remoteRefNames(branches), [branches]) const selectedIndex = selectedRef?.kind === 'commit' && selectedRef.worktree === worktree @@ -262,7 +302,8 @@ export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onP setPage(null) } - return
+ return
-
+
{ + // The window may unmount the row the hover card is anchored to. + if (hover !== null || hoverTimer.current !== null) cancelHover() + scrollBatcher.schedule(syncWindow) + }}> {error !== null &&
{stale ? t('graphStale') : t('graphLoadError')}{!stale && `: ${error}`}
} {loading &&
{t('loading')}
} {!loading && !error && page !== null && page.entries.length === 0 &&
{t(page.isRepo ? 'graphEmpty' : 'notRepo')}
} - {page?.entries.map((entry, index) => { + {range.top > 0 &&