.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
benches/
src/
action_bar.rs
4.7 KiB
badge.rs
2.6 KiB
dialog.rs
12.4 KiB
input.rs
13.3 KiB
lib.rs
1.6 KiB
list.rs
13.1 KiB
overlay.rs
3.4 KiB
panel.rs
18.6 KiB
scrollbar.rs
5.9 KiB
scrollview.rs
13.2 KiB
tree.rs
27.9 KiB
viewport.rs
15.7 KiB
tests/
Cargo.toml
408 B
README.md
294 B
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/tui-widgets/src/scrollview.rs
raw
| 1 | //! Scrollable line widget. |
| 2 | //! |
| 3 | //! `ScrollView` renders scrollable line collections and draws a scrollbar when |
| 4 | //! content exceeds the visible area. It is the widget-level bridge between |
| 5 | //! viewport math and actual line rendering. |
| 6 | |
| 7 | use crate::{Scrollbar, ScrollbarStyle, View}; |
| 8 | use rustik_tui_core as tui_core; |
| 9 | use tui_core::{Buffer, Constraint, Line, Rect, Size, Widget}; |
| 10 | |
| 11 | /// Scrollable line metrics for a rendered area. |
| 12 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 13 | pub struct ScrollMetrics { |
| 14 | /// Number of content rows. |
| 15 | pub content_rows: usize, |
| 16 | /// Number of visible rows. |
| 17 | pub visible_rows: usize, |
| 18 | /// Visible view after offset clamping. |
| 19 | pub view: View, |
| 20 | } |
| 21 | |
| 22 | impl ScrollMetrics { |
| 23 | /// Returns the largest valid scroll offset for the rendered view. |
| 24 | pub fn max_offset(self) -> usize { |
| 25 | self.view.max_offset() |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | impl Default for ScrollMetrics { |
| 30 | fn default() -> Self { |
| 31 | Self { |
| 32 | content_rows: 0, |
| 33 | visible_rows: 0, |
| 34 | view: View::new(0, 0, 0), |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// A scrollable collection of lines. |
| 40 | #[derive(Debug, Clone)] |
| 41 | pub struct ScrollView { |
| 42 | lines: Vec<Line>, |
| 43 | offset: usize, |
| 44 | sticky: Vec<Line>, |
| 45 | scrollbar: ScrollbarStyle, |
| 46 | } |
| 47 | |
| 48 | /// A scrollable collection that renders rows on demand. |
| 49 | pub struct VirtualScrollView<F> { |
| 50 | /// Number of rows in the full content. |
| 51 | content_rows: usize, |
| 52 | /// First visible content row before clamping. |
| 53 | offset: usize, |
| 54 | /// Optional view used only for scrollbar geometry. |
| 55 | scrollbar_view: Option<View>, |
| 56 | /// Sticky rows pinned at the top of the view. |
| 57 | sticky: Vec<Line>, |
| 58 | /// Scrollbar style. |
| 59 | scrollbar: ScrollbarStyle, |
| 60 | /// Width-sensitive row producer. |
| 61 | line: F, |
| 62 | } |
| 63 | |
| 64 | /// Layout rectangles for a scroll-view render pass. |
| 65 | #[derive(Clone, Copy, Debug)] |
| 66 | pub struct ScrollLayout { |
| 67 | /// Text area excluding the scrollbar. |
| 68 | pub text: Rect, |
| 69 | /// Optional scrollbar area. |
| 70 | pub scrollbar: Option<Rect>, |
| 71 | /// Sticky rows that fit in the text area. |
| 72 | pub sticky_rows: usize, |
| 73 | /// Body area for scrolled content rows. |
| 74 | pub body: Rect, |
| 75 | } |
| 76 | |
| 77 | /// Layout information for one scroll-view render pass. |
| 78 | #[derive(Clone, Copy, Debug)] |
| 79 | struct ScrollFrame { |
| 80 | /// Area rectangles for the render pass. |
| 81 | layout: ScrollLayout, |
| 82 | /// Clamped view for the scrolled body. |
| 83 | view: View, |
| 84 | } |
| 85 | |
| 86 | impl ScrollView { |
| 87 | /// Returns the body width inside the reserved scrollbar column. |
| 88 | /// |
| 89 | /// Callers that pre-build content lines should size them to this width so |
| 90 | /// they exactly fit the body next to the scrollbar. |
| 91 | pub const fn body_cols(area: Rect) -> usize { |
| 92 | if area.cols > 2 { |
| 93 | area.cols - 1 |
| 94 | } else { |
| 95 | area.cols |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// Creates a scroll view from prebuilt render lines. |
| 100 | pub fn from_lines(lines: impl IntoIterator<Item = Line>) -> Self { |
| 101 | Self { |
| 102 | lines: lines.into_iter().collect(), |
| 103 | offset: 0, |
| 104 | sticky: Vec::new(), |
| 105 | scrollbar: ScrollbarStyle::default(), |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /// Builds a scroll view for an area from a width-sensitive line producer. |
| 110 | /// |
| 111 | /// The producer is called once with the body width that ScrollView will use |
| 112 | /// (see [`body_cols`](Self::body_cols)). |
| 113 | pub fn new(area: Rect, offset: usize, mut lines: impl FnMut(usize) -> Vec<Line>) -> Self { |
| 114 | Self { |
| 115 | lines: lines(Self::body_cols(area)), |
| 116 | offset, |
| 117 | sticky: Vec::new(), |
| 118 | scrollbar: ScrollbarStyle::default(), |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /// Creates a scroll view that renders only visible rows. |
| 123 | pub fn virtualized<F>(content_rows: usize, offset: usize, line: F) -> VirtualScrollView<F> |
| 124 | where |
| 125 | F: FnMut(usize, usize) -> Line, |
| 126 | { |
| 127 | VirtualScrollView::new(content_rows, offset, line) |
| 128 | } |
| 129 | |
| 130 | /// Sets the first visible row. |
| 131 | pub const fn offset(mut self, offset: usize) -> Self { |
| 132 | self.offset = offset; |
| 133 | self |
| 134 | } |
| 135 | |
| 136 | /// Sets sticky rows pinned at the top of the view above the scrolled body. |
| 137 | pub fn sticky(mut self, lines: impl IntoIterator<Item = Line>) -> Self { |
| 138 | self.sticky = lines.into_iter().collect(); |
| 139 | self |
| 140 | } |
| 141 | |
| 142 | /// Sets the vertical scrollbar style. |
| 143 | pub fn scrollbar(mut self, style: ScrollbarStyle) -> Self { |
| 144 | self.scrollbar = style; |
| 145 | self |
| 146 | } |
| 147 | |
| 148 | /// Returns metrics for a rendered area. |
| 149 | pub fn metrics(&self, area: Rect) -> ScrollMetrics { |
| 150 | let frame = scroll_frame(area, self.offset, self.lines.len(), self.sticky.len()); |
| 151 | |
| 152 | ScrollMetrics { |
| 153 | content_rows: self.lines.len(), |
| 154 | visible_rows: frame.layout.body.rows, |
| 155 | view: frame.view, |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | impl<F> VirtualScrollView<F> |
| 161 | where |
| 162 | F: FnMut(usize, usize) -> Line, |
| 163 | { |
| 164 | /// Creates a virtual scroll view from a row count and row producer. |
| 165 | pub fn new(content_rows: usize, offset: usize, line: F) -> Self { |
| 166 | Self { |
| 167 | content_rows, |
| 168 | offset, |
| 169 | scrollbar_view: None, |
| 170 | sticky: Vec::new(), |
| 171 | scrollbar: ScrollbarStyle::default(), |
| 172 | line, |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /// Sets sticky rows pinned at the top of the view above the scrolled body. |
| 177 | pub fn sticky(mut self, lines: impl IntoIterator<Item = Line>) -> Self { |
| 178 | self.sticky = lines.into_iter().collect(); |
| 179 | self |
| 180 | } |
| 181 | |
| 182 | /// Sets the vertical scrollbar style. |
| 183 | pub fn scrollbar(mut self, style: ScrollbarStyle) -> Self { |
| 184 | self.scrollbar = style; |
| 185 | self |
| 186 | } |
| 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 | |
| 194 | /// Returns metrics for a rendered area. |
| 195 | pub fn metrics(&self, area: Rect) -> ScrollMetrics { |
| 196 | let frame = scroll_frame(area, self.offset, self.content_rows, self.sticky.len()); |
| 197 | |
| 198 | ScrollMetrics { |
| 199 | content_rows: self.content_rows, |
| 200 | visible_rows: frame.layout.body.rows, |
| 201 | view: frame.view, |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | impl<F> std::fmt::Debug for VirtualScrollView<F> { |
| 207 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 208 | f.debug_struct("VirtualScrollView") |
| 209 | .field("content_rows", &self.content_rows) |
| 210 | .field("offset", &self.offset) |
| 211 | .field("scrollbar_view", &self.scrollbar_view) |
| 212 | .field("sticky", &self.sticky) |
| 213 | .field("scrollbar", &self.scrollbar) |
| 214 | .finish_non_exhaustive() |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | impl Widget for ScrollView { |
| 219 | fn size(&self, parent: Constraint) -> Size { |
| 220 | Size::new(parent.max.cols, parent.max.rows).constrain(parent) |
| 221 | } |
| 222 | |
| 223 | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
| 224 | if area.is_empty() { |
| 225 | return; |
| 226 | } |
| 227 | let frame = scroll_frame(area, self.offset, self.lines.len(), self.sticky.len()); |
| 228 | |
| 229 | render_sticky(&self.sticky, frame.layout, buf); |
| 230 | frame.view.render_lines(&self.lines, frame.layout.body, buf); |
| 231 | |
| 232 | if let Some(area) = frame.layout.scrollbar { |
| 233 | Scrollbar::new(frame.view) |
| 234 | .style(self.scrollbar.clone()) |
| 235 | .render(area, buf); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | impl<F> Widget for VirtualScrollView<F> |
| 241 | where |
| 242 | F: FnMut(usize, usize) -> Line + Send, |
| 243 | { |
| 244 | fn size(&self, parent: Constraint) -> Size { |
| 245 | Size::new(parent.max.cols, self.content_rows).constrain(parent) |
| 246 | } |
| 247 | |
| 248 | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
| 249 | if area.is_empty() { |
| 250 | return; |
| 251 | } |
| 252 | let frame = scroll_frame(area, self.offset, self.content_rows, self.sticky.len()); |
| 253 | |
| 254 | render_sticky(&self.sticky, frame.layout, buf); |
| 255 | |
| 256 | let lines = frame |
| 257 | .view |
| 258 | .range() |
| 259 | .map(|row| (self.line)(row, frame.layout.body.cols)); |
| 260 | Line::renders(lines, frame.layout.body, buf); |
| 261 | |
| 262 | if let Some(area) = frame.layout.scrollbar { |
| 263 | Scrollbar::new(self.scrollbar_view.unwrap_or(frame.view)) |
| 264 | .style(self.scrollbar.clone()) |
| 265 | .render(area, buf); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /// Renders sticky lines into the top of a scroll layout's text area. |
| 271 | fn render_sticky(sticky: &[Line], layout: ScrollLayout, buf: &mut Buffer) { |
| 272 | if layout.sticky_rows == 0 { |
| 273 | return; |
| 274 | } |
| 275 | let sticky_area = Rect::new( |
| 276 | layout.text.x, |
| 277 | layout.text.y, |
| 278 | layout.text.cols, |
| 279 | layout.sticky_rows, |
| 280 | ); |
| 281 | Line::renders( |
| 282 | sticky.iter().take(layout.sticky_rows).cloned(), |
| 283 | sticky_area, |
| 284 | buf, |
| 285 | ); |
| 286 | } |
| 287 | |
| 288 | /// Returns the area rectangles for a scroll view with `sticky_rows` pinned. |
| 289 | /// |
| 290 | /// Splits `area` into a text column (excluding the rightmost scrollbar column |
| 291 | /// when there is room for one), then divides the text area into a sticky |
| 292 | /// header band of `sticky_rows` rows and a body for scrolled content. |
| 293 | pub fn scroll_layout(area: Rect, sticky_rows: usize) -> ScrollLayout { |
| 294 | let (text, scrollbar) = if area.cols > 2 { |
| 295 | let text = Rect::new(area.x, area.y, area.cols - 1, area.rows); |
| 296 | let scrollbar = Some(Rect::new(area.right() - 1, area.y, 1, area.rows)); |
| 297 | |
| 298 | (text, scrollbar) |
| 299 | } else { |
| 300 | (area, None) |
| 301 | }; |
| 302 | let sticky_rows = sticky_rows.min(text.rows); |
| 303 | let body = Rect::new( |
| 304 | text.x, |
| 305 | text.y + sticky_rows, |
| 306 | text.cols, |
| 307 | text.rows.saturating_sub(sticky_rows), |
| 308 | ); |
| 309 | |
| 310 | ScrollLayout { |
| 311 | text, |
| 312 | scrollbar, |
| 313 | sticky_rows, |
| 314 | body, |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /// Returns layout rectangles and view state for one scroll-view render pass. |
| 319 | fn scroll_frame(area: Rect, offset: usize, content_rows: usize, sticky_rows: usize) -> ScrollFrame { |
| 320 | let layout = scroll_layout(area, sticky_rows); |
| 321 | let view = View::new(offset, content_rows, layout.body.rows); |
| 322 | |
| 323 | ScrollFrame { layout, view } |
| 324 | } |
| 325 | |
| 326 | #[cfg(test)] |
| 327 | mod tests { |
| 328 | use super::*; |
| 329 | use rustik_tui_core::{Buffer, Cell, Color, Style}; |
| 330 | |
| 331 | #[test] |
| 332 | fn scrollbar_column_shows_track_only_when_content_fits() { |
| 333 | let mut view = ScrollView::from_lines([Line::new("hello")]); |
| 334 | let mut buffer = Buffer::empty(Rect::new(0, 0, 7, 2)); |
| 335 | |
| 336 | view.render(buffer.area(), &mut buffer); |
| 337 | |
| 338 | // 6-col body + 1-col scrollbar; non-scrollable view paints track only. |
| 339 | assert_eq!(buffer.lines(), vec!["hello │", " │"]); |
| 340 | } |
| 341 | |
| 342 | #[test] |
| 343 | fn sticky_rows_pin_to_top_above_scrolled_body() { |
| 344 | let lines = (0..6) |
| 345 | .map(|i| Line::new(format!("row{i}"))) |
| 346 | .collect::<Vec<_>>(); |
| 347 | let mut view = ScrollView::from_lines(lines) |
| 348 | .offset(2) |
| 349 | .sticky([Line::new("HEAD")]); |
| 350 | let mut buffer = Buffer::empty(Rect::new(0, 0, 5, 3)); |
| 351 | |
| 352 | view.render(buffer.area(), &mut buffer); |
| 353 | |
| 354 | assert_eq!(buffer.lines(), vec!["HEAD│", "row2┃", "row3│"]); |
| 355 | } |
| 356 | |
| 357 | #[test] |
| 358 | fn scroll_view_builds_lines_at_body_width() { |
| 359 | let style = ScrollbarStyle::new( |
| 360 | Cell::new('|', Style::new(Color::Unset)), |
| 361 | Cell::new('#', Style::new(Color::Unset)), |
| 362 | ); |
| 363 | let area = Rect::new(0, 0, 4, 2); |
| 364 | let mut view = ScrollView::new(area, 1, |cols| { |
| 365 | (0..3) |
| 366 | .map(|row| Line::new(format!("{row}:{cols}"))) |
| 367 | .collect() |
| 368 | }) |
| 369 | .scrollbar(style); |
| 370 | let metrics = view.metrics(area); |
| 371 | let mut buffer = Buffer::empty(area); |
| 372 | |
| 373 | view.render(buffer.area(), &mut buffer); |
| 374 | |
| 375 | assert_eq!(ScrollView::body_cols(area), 3); |
| 376 | assert_eq!(metrics.content_rows, 3); |
| 377 | assert_eq!(metrics.visible_rows, 2); |
| 378 | assert_eq!(buffer.lines(), vec!["1:3|", "2:3#"]); |
| 379 | } |
| 380 | |
| 381 | #[test] |
| 382 | fn virtual_scroll_view_renders_only_visible_rows() { |
| 383 | let mut rendered = Vec::new(); |
| 384 | let mut view = ScrollView::virtualized(10, 4, |row, cols| { |
| 385 | rendered.push(row); |
| 386 | Line::new(format!("{row}:{cols}")) |
| 387 | }); |
| 388 | let mut buffer = Buffer::empty(Rect::new(0, 0, 5, 3)); |
| 389 | |
| 390 | view.render(buffer.area(), &mut buffer); |
| 391 | |
| 392 | assert_eq!(rendered, vec![4, 5, 6]); |
| 393 | assert_eq!(buffer.lines(), vec!["4:4 │", "5:4 ┃", "6:4 │"]); |
| 394 | } |
| 395 | |
| 396 | #[test] |
| 397 | fn virtual_scroll_view_keeps_sticky_rows_out_of_source() { |
| 398 | let mut rendered = Vec::new(); |
| 399 | let mut view = ScrollView::virtualized(10, 4, |row, cols| { |
| 400 | rendered.push(row); |
| 401 | Line::new(format!("{row}:{cols}")) |
| 402 | }) |
| 403 | .sticky([Line::new("HEAD")]); |
| 404 | let mut buffer = Buffer::empty(Rect::new(0, 0, 5, 3)); |
| 405 | |
| 406 | view.render(buffer.area(), &mut buffer); |
| 407 | |
| 408 | assert_eq!(rendered, vec![4, 5]); |
| 409 | assert_eq!(buffer.lines(), vec!["HEAD│", "4:4 ┃", "5:4 │"]); |
| 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.ends_with('#')) |
| 430 | .count(); |
| 431 | assert_eq!(thumb_rows, 5); |
| 432 | } |
| 433 | } |