Skip to content

fix(ui): preserve unreviewed tree scroll - #259

Merged
umputun merged 5 commits into
umputun:masterfrom
mishamsk:258-fix-auto-scroll
Jul 13, 2026
Merged

umputun merged 5 commits into
umputun:masterfrom
mishamsk:258-fix-auto-scroll

Conversation

@mishamsk

Copy link
Copy Markdown
Contributor
  • Keep the next unreviewed file on the same visible row after marking the current file reviewed.
  • Preserve the unreviewed tree scroll position across force reloads, resetting it when the filtered tree becomes empty.

Closes #258.

Copilot AI review requested due to automatic review settings July 13, 2026 02:57
@mishamsk
mishamsk requested a review from umputun as a code owner July 13, 2026 02:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves the sidepane file tree behavior when using the unreviewed-only filter by keeping the viewport stable as files are marked reviewed and by preserving scroll position across file-list reloads, eliminating disruptive scroll jumps (Issue #258).

Changes:

  • Preserve the file tree viewport offset when the unreviewed-only list is rebuilt so the next selected file stays on the same visible row after marking reviewed.
  • Preserve unreviewed-tree scroll offset across reloads, while resetting it when the unreviewed filtered tree becomes empty.
  • Add unit tests covering row-preserving advance, empty-state offset reset, and reload scroll-offset preservation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
app/ui/sidepane/filetree.go Preserves offset during unreviewed-filter rebuilds and avoids unnecessary viewport resets so EnsureVisible scrolls only when needed.
app/ui/sidepane/filetree_test.go Adds tests for row-stable selection advance and for resetting offset when the unreviewed filtered tree becomes empty.
app/ui/loaders_test.go Adds a model-level test ensuring reloads preserve the unreviewed scroll offset for a stable viewport.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core fix is right and does what #258 asked. Verified the reload test fails without it (offset clamps to 2 instead of 4), and build/test/lint/race are all green locally. Two viewport nits, one of them a small regression this PR adds.

F toggle scrolls the ./ header off-screen (should fix before merge). selectAfterRebuild (app/ui/sidepane/filetree.go:445) no longer resets offset in its no-anchor fallback, and ToggleUnreviewedFilter reaches that fallback with both args empty. So toggling F while the tree is scrolled down leaves offset clamped to the first file's index (1) instead of 0, and the leading ./ directory header gets scrolled off the top. It's the milder version of the same jump #258 was about. The mark-reviewed and reload paths return early via SelectByPath, so resetting in the fallback doesn't touch the fix:

	if preferred != "" && ft.SelectByPath(preferred) {
		return
	}
	ft.offset = 0 // no anchor survived, reset viewport to top
	for i, e := range ft.entries {
		if !e.isDir {
			ft.cursor = i
			return
		}
	}

worth a small test for the toggle-from-scrolled case, none of the new ones exercise it.

offset is preserved by index, not visible row (optional). on a reload that changes the unreviewed set above the current viewport, the selected file's row drifts by the number of rows added or removed above it, since offset is kept by absolute index rather than by visible row (cursor - offset). It's rare, it's outside the mark-reviewed case (#258 is exact there), and it's still better than the old full reset, so fine to defer. If you want it airtight later: capture visibleRow = cursor - offset before Rebuild in handleFilesLoaded, then set offset = clamp(newCursor - visibleRow) once the selection is restored.

@mishamsk

Copy link
Copy Markdown
Contributor Author

Addressed both viewport points. The resulting behavior is:

  • Marking the selected file reviewed in F view removes it and keeps the next unreviewed file on the same visible row.
  • Reloading with R in F view keeps the surviving selected file on the same visible row.
  • Consecutive R reloads no longer drift the selection toward the top.
  • If a reload adds or removes filtered files above the selection, the selected file is restored by path and remains on the same visible row rather than preserving a stale absolute offset.
  • Switching from the unfiltered view into F view makes a best effort to preserve both the selected file and its visible row when that file remains unreviewed.
  • If no selection anchor survives when entering F view, the tree falls back to the first visible file with offset 0, keeping the ./ header visible.
  • An empty unreviewed view resets its offset to 0.
  • Switching from F view back to the unfiltered view still resets the viewport to the top, matching current master behavior. This is a little asymmetric with the new best-effort preservation in the other direction; happy to make the reverse toggle preserve position in a follow-up if that is preferable.
  • Reload behavior outside F view is unchanged.

Manually smoke-tested both entering F from a scrolled middle selection and multiple consecutive R reloads with reviewed files filtered above the selection. Automated race/coverage tests and lint are also green.

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both viewport points from last round are handled and the fix itself is correct. Build/test/lint/race are green, and the visible-row anchoring (offset = max(cursor - visibleRow, 0)) is the exact inverse of the capture, so the row survives index shifts. One thing still open.

the ./-header regression fix has no test. The reset is in place: selectAfterRebuild zeroes offset on the non-empty no-anchor fallback at app/ui/sidepane/filetree.go:459. But nothing reaches that path from a scrolled state. TestFileTree_EnableUnreviewedFilterPreservesVisibleSelection keeps the selected file unreviewed, so toggling F takes the anchored branch at line 452, not 459. TestFileTree_UnreviewedFilterEmptyStateResetsOffset hits the separate len(entries)==0 early return at 447. So deleting line 459 leaves the whole suite green, and EnsureVisible won't cover for it: after the reset the cursor sits on the first file at index 1, below the ./ header at index 0, so a stale offset still scrolls the header off. This is the toggle-from-scrolled case I flagged last round. Add a filetree_test.go case that reaches 459 from offset > 0 (e.g. select a reviewed file, then ToggleUnreviewedFilter with entries remaining) and asserts offset == 0 and the first entry visible.

optional, take or leave:

  • selectAfterRebuild repeats ft.offset = max(ft.cursor-visibleRow, 0) verbatim in the previous and preferred branches (452, 456). One if a || b { ... } covers both.
  • visibleRow := ft.cursor - ft.offset is re-derived at three call sites (Rebuild, ToggleUnreviewedFilter, RefreshUnreviewedFilter). A small ft.visibleRow() method would name the concept and stop it drifting.
  • when ft.unreviewed, Rebuild anchors against the full unfiltered list even though RefreshUnreviewedFilter rebuilds the filtered view right after. It's load-bearing only because it carries the cursor - offset delta forward, and nothing says so. A one-line comment there would keep a future Rebuild cleanup from silently dropping scroll preservation.

@umputun
umputun merged commit 44a1b24 into umputun:master Jul 13, 2026
2 checks passed
@mishamsk
mishamsk deleted the 258-fix-auto-scroll branch July 13, 2026 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Marking a file reviewed while filtering unreviewed files jumps the tree scroll position

3 participants