Skip to content

diff: Add line_bounds, viewport_bounds and top_file, and notify on scroll - #4019

Closed
sam-phinizy wants to merge 1 commit into
longbridge:mainfrom
sam-phinizy:diff-bounds
Closed

sam-phinizy wants to merge 1 commit into
longbridge:mainfrom
sam-phinizy:diff-bounds

Conversation

@sam-phinizy

Copy link
Copy Markdown

Description

An application that puts its own content beside a diff's lines — a floating comment card next to the lines it's about, a marker following a line as the diff scrolls — needs to know where those lines are on screen, and to hear when that changes. DiffState keeps the list's layout to itself, so today the only way is to fork the component.

This adds read-only position queries and notifies the state's observers when the diff scrolls, so an observer can re-place its content:

  • line_bounds — where a source line is laid out now, or None when it isn't (outside the rendered range, folded, or in a collapsed file).
  • viewport_bounds — the part of the diff on screen, to keep placed content inside it.
  • top_file — the path of the file at the top of the viewport, for "this file" commands (mark viewed, copy path) that should act on what's being read rather than on the last file clicked.

We've been carrying these as a local patch in a code-review app, for floating comment cards and a guided tour that follow their lines.

Public API

gpui_component (diff::DiffState):

  • pub fn line_bounds(&self, position: &DiffLinePosition) -> Option<Bounds<Pixels>> — where a source line is laid out now, in window coordinates; None when it isn't laid out.
  • pub fn viewport_bounds(&self) -> Bounds<Pixels> — the visible part of the diff, in window coordinates.
  • pub fn top_file(&self) -> Option<SharedString> — the path of the file at the top of the viewport.

Behaviour: DiffState now calls cx.notify() when its list scrolls, so cx.observe(&state, …) hears scrolling.

How to Test

cargo test -p gpui-component --lib diff:: — adds a_line_scrolled_to_has_bounds_inside_the_viewport_and_names_the_top_file in diff/scroll_tests.rs: a line isn't laid out before it's scrolled to, is inside the viewport after, and the top file is named.

Checklist

  • I have read the CONTRIBUTING document and followed the guidelines.
  • Reviewed the changes in this PR and confirmed AI generated code (If any) is accurate.
  • Passed cargo run for story tests related to the changes. (No story change; covered by the test above.)
  • Tested macOS, Windows and Linux platforms performance (if the change is platform-specific) — not platform-specific.

🤖 Generated with Claude Code

… on scroll

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@huacnlee

Copy link
Copy Markdown
Member

💡 Needs the maintainer's call: three new public read-only queries on DiffState plus scroll notifications.

Reviewed at f505427..01e3f45 (head 01e3f45). cargo test -p gpui-component --lib diff: 65 passed.

  • Public API added: line_bounds, viewport_bounds, top_file. They are thin wrappers over ListState::bounds_for_item / viewport_bounds, read-only, no new types.
  • Behaviour change: the list's scroll handler now calls cx.notify() on every scroll, so every observer of DiffState re-renders while scrolling. Worth confirming that is acceptable for large diffs; an alternative is emitting a dedicated event only to subscribers that care.
  • top_file overlaps with what an app can derive from row_position at logical_scroll_top; consider whether it needs to be public.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

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.

2 participants