diff: Stabilize sticky scrollbar thumb

0691061b45e3562024c922137920b5fb1b7ce9a3
Separate the rows rendered under sticky headers from the logical
viewport used for scrollbar geometry. This keeps rustik-diff's thumb
size stable while moving between files, while preserving the
sticky-header render offset.
Alexis Sellier committed ago 1 parent e0f6f1b9
crates/diff/src/renderer.rs +1 -0
310 310
            self.render_row(row, cols)
311 311
                .map(|row| row.line)
312 312
                .unwrap_or_default()
313 313
        })
314 314
        .sticky(sticky)
315 +
        .scrollbar_view(View::new(scroll, content_rows, area.rows))
315 316
        .scrollbar(Self::scrollbar_style())
316 317
        .render(area, buf);
317 318
    }
318 319
319 320
    /// Applies an action to the viewer state.
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
a.rs +0 -0
crates/diff/tests/golden/bottom_of_file_64x5.txt +1 -1
1 1
╭─────────────────────────────────────────────────────────────╮🮘
2 2
│ ○ a.rs                                                +2 -1 │🮘
3 3
├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤🮘
4 -
│ 3    4       tail                                           │🮘
4 +
│ 3    4       tail                                           │▓
5 5
╰─────────────────────────────────────────────────────────────╯▓
crates/diff/tests/golden/sticky_header_inside_file_64x5.txt +1 -1
1 1
╭─────────────────────────────────────────────────────────────╮🮘
2 2
│ ○ a.rs                                                +2 -1 │🮘
3 -
├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤🮘
3 +
├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤▓
4 4
│      3     + extra                                          │▓
5 5
│ 3    4       tail                                           │🮘
crates/diff/tests/golden/sticky_header_replaces_chrome_64x5.txt +3 -3
1 1
╭─────────────────────────────────────────────────────────────╮🮘
2 -
│ ○ a.rs                                                +2 -1 │🮘
3 -
├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤🮘
4 -
│      2     + new                                            │▓
2 +
│ ○ a.rs                                                +2 -1 │▓
3 +
├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤▓
4 +
│      2     + new                                            │🮘
5 5
│      3     + extra                                          │🮘
crates/diff/tests/golden/sticky_header_starting_64x5.txt +3 -3
1 -
╭─────────────────────────────────────────────────────────────╮🮘
2 -
│ ○ a.rs                                                +2 -1 │🮘
3 -
├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤▓
1 +
╭─────────────────────────────────────────────────────────────╮▓
2 +
│ ○ a.rs                                                +2 -1 │▓
3 +
├╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┤🮘
4 4
│ 1    1       context                                        │🮘
5 5
│ 2          - old                                            │🮘
crates/tui-widgets/src/scrollview.rs +33 -1
49 49
pub struct VirtualScrollView<F> {
50 50
    /// Number of rows in the full content.
51 51
    content_rows: usize,
52 52
    /// First visible content row before clamping.
53 53
    offset: usize,
54 +
    /// Optional view used only for scrollbar geometry.
55 +
    scrollbar_view: Option<View>,
54 56
    /// Sticky rows pinned at the top of the view.
55 57
    sticky: Vec<Line>,
56 58
    /// Scrollbar style.
57 59
    scrollbar: ScrollbarStyle,
58 60
    /// Width-sensitive row producer.
162 164
    /// Creates a virtual scroll view from a row count and row producer.
163 165
    pub fn new(content_rows: usize, offset: usize, line: F) -> Self {
164 166
        Self {
165 167
            content_rows,
166 168
            offset,
169 +
            scrollbar_view: None,
167 170
            sticky: Vec::new(),
168 171
            scrollbar: ScrollbarStyle::default(),
169 172
            line,
170 173
        }
171 174
    }
180 183
    pub fn scrollbar(mut self, style: ScrollbarStyle) -> Self {
181 184
        self.scrollbar = style;
182 185
        self
183 186
    }
184 187
188 +
    /// Sets the view used to derive scrollbar position and size.
189 +
    pub const fn scrollbar_view(mut self, view: View) -> Self {
190 +
        self.scrollbar_view = Some(view);
191 +
        self
192 +
    }
193 +
185 194
    /// Returns metrics for a rendered area.
186 195
    pub fn metrics(&self, area: Rect) -> ScrollMetrics {
187 196
        let frame = scroll_frame(area, self.offset, self.content_rows, self.sticky.len());
188 197
189 198
        ScrollMetrics {
197 206
impl<F> std::fmt::Debug for VirtualScrollView<F> {
198 207
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
199 208
        f.debug_struct("VirtualScrollView")
200 209
            .field("content_rows", &self.content_rows)
201 210
            .field("offset", &self.offset)
211 +
            .field("scrollbar_view", &self.scrollbar_view)
202 212
            .field("sticky", &self.sticky)
203 213
            .field("scrollbar", &self.scrollbar)
204 214
            .finish_non_exhaustive()
205 215
    }
206 216
}
248 258
            .range()
249 259
            .map(|row| (self.line)(row, frame.layout.body.cols));
250 260
        Line::renders(lines, frame.layout.body, buf);
251 261
252 262
        if let Some(area) = frame.layout.scrollbar {
253 -
            Scrollbar::new(frame.view)
263 +
            Scrollbar::new(self.scrollbar_view.unwrap_or(frame.view))
254 264
                .style(self.scrollbar.clone())
255 265
                .render(area, buf);
256 266
        }
257 267
    }
258 268
}
396 406
        view.render(buffer.area(), &mut buffer);
397 407
398 408
        assert_eq!(rendered, vec![4, 5]);
399 409
        assert_eq!(buffer.lines(), vec!["HEAD│", "4:4 ┃", "5:4 │"]);
400 410
    }
411 +
412 +
    #[test]
413 +
    fn virtual_scroll_view_can_use_logical_scrollbar_view() {
414 +
        let style = ScrollbarStyle::new(
415 +
            Cell::new('.', Style::new(Color::Unset)),
416 +
            Cell::new('#', Style::new(Color::Unset)),
417 +
        );
418 +
        let mut view = ScrollView::virtualized(20, 7, |row, _| Line::new(row.to_string()))
419 +
            .sticky([Line::blank(), Line::blank(), Line::blank()])
420 +
            .scrollbar_view(View::new(4, 20, 10))
421 +
            .scrollbar(style);
422 +
        let mut buffer = Buffer::empty(Rect::new(0, 0, 5, 10));
423 +
424 +
        view.render(buffer.area(), &mut buffer);
425 +
426 +
        let thumb_rows = buffer
427 +
            .lines()
428 +
            .iter()
429 +
            .filter(|line| line.chars().last() == Some('#'))
430 +
            .count();
431 +
        assert_eq!(thumb_rows, 5);
432 +
    }
401 433
}