.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
15.5 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/scrollbar.rs
raw
| 1 | //! Scrollbar rendering. |
| 2 | //! |
| 3 | //! `Scrollbar` turns a [`View`](crate::View) into a one-column visual indicator. |
| 4 | //! Track and thumb are full [`Cell`](rustik_tui_core::Cell) values so glyph and style |
| 5 | //! stay configurable together. |
| 6 | |
| 7 | use crate::View; |
| 8 | use rustik_tui_core as tui_core; |
| 9 | use tui_core::{Buffer, Cell, Color, Constraint, Rect, Size, Style, Widget}; |
| 10 | |
| 11 | /// Default scrollbar track glyph. |
| 12 | pub const DEFAULT_TRACK: char = '│'; |
| 13 | /// Default scrollbar thumb glyph. |
| 14 | pub const DEFAULT_THUMB: char = '┃'; |
| 15 | /// Default scrollbar track style. |
| 16 | pub const DEFAULT_TRACK_STYLE: Style = Style::new(Color::Unset); |
| 17 | /// Default scrollbar thumb style. |
| 18 | pub const DEFAULT_THUMB_STYLE: Style = Style::new(Color::Unset); |
| 19 | |
| 20 | /// Vertical scrollbar for a [`View`]. |
| 21 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 22 | pub struct Scrollbar { |
| 23 | /// View used to derive thumb position and size. |
| 24 | pub view: View, |
| 25 | /// Cells used to render the track and thumb. |
| 26 | pub style: ScrollbarStyle, |
| 27 | } |
| 28 | |
| 29 | /// Reusable scrollbar cells. |
| 30 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 31 | pub struct ScrollbarStyle { |
| 32 | track: Cell, |
| 33 | thumb: Cell, |
| 34 | } |
| 35 | |
| 36 | impl ScrollbarStyle { |
| 37 | /// Creates a scrollbar style from track and thumb cells. |
| 38 | pub fn new(track: Cell, thumb: Cell) -> Self { |
| 39 | Self { track, thumb } |
| 40 | } |
| 41 | |
| 42 | /// Returns the track cell. |
| 43 | pub const fn track(&self) -> &Cell { |
| 44 | &self.track |
| 45 | } |
| 46 | |
| 47 | /// Returns the thumb cell. |
| 48 | pub const fn thumb(&self) -> &Cell { |
| 49 | &self.thumb |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl Default for ScrollbarStyle { |
| 54 | fn default() -> Self { |
| 55 | Self { |
| 56 | track: Cell::new(DEFAULT_TRACK, DEFAULT_TRACK_STYLE), |
| 57 | thumb: Cell::new(DEFAULT_THUMB, DEFAULT_THUMB_STYLE), |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl Scrollbar { |
| 63 | /// Creates a scrollbar for a view. |
| 64 | pub fn new(view: View) -> Self { |
| 65 | Self { |
| 66 | view, |
| 67 | style: ScrollbarStyle::default(), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /// Sets both cells from a reusable style. |
| 72 | pub fn style(mut self, style: ScrollbarStyle) -> Self { |
| 73 | self.style = style; |
| 74 | self |
| 75 | } |
| 76 | |
| 77 | /// Returns the thumb range in half-row units for a rendered height. |
| 78 | fn thumb_range(&self, rows: usize) -> std::ops::Range<usize> { |
| 79 | if rows == 0 || !self.view.is_scrollable() { |
| 80 | return 0..0; |
| 81 | } |
| 82 | let half_rows = rows.saturating_mul(2); |
| 83 | let thumb_half_rows = |
| 84 | proportional(half_rows, self.view.visible_rows, self.view.content_rows) |
| 85 | .clamp(2, half_rows); |
| 86 | let travel = half_rows - thumb_half_rows; |
| 87 | let start = if self.view.max_offset() == 0 { |
| 88 | 0 |
| 89 | } else { |
| 90 | proportional(travel, self.view.offset, self.view.max_offset()) |
| 91 | }; |
| 92 | start..start + thumb_half_rows |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl Widget for Scrollbar { |
| 97 | fn size(&self, parent: Constraint) -> Size { |
| 98 | let rows = if parent.max.rows == usize::MAX { |
| 99 | self.view.visible_rows |
| 100 | } else { |
| 101 | parent.max.rows |
| 102 | }; |
| 103 | Size::new(1, rows).constrain(parent) |
| 104 | } |
| 105 | |
| 106 | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
| 107 | if area.is_empty() { |
| 108 | return; |
| 109 | } |
| 110 | let thumb = self.thumb_range(area.rows); |
| 111 | |
| 112 | for row in 0..area.rows { |
| 113 | let half_row = row.saturating_mul(2); |
| 114 | let cell = match ( |
| 115 | thumb.contains(&half_row), |
| 116 | thumb.contains(&half_row.saturating_add(1)), |
| 117 | ) { |
| 118 | (true, true) => self.style.thumb.clone(), |
| 119 | (true, false) => Cell::new('▝', self.style.thumb.style()), |
| 120 | (false, true) => Cell::new('▗', self.style.thumb.style()), |
| 121 | (false, false) => self.style.track.clone(), |
| 122 | }; |
| 123 | buf.set(area.x, area.y + row, cell); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// Scales a part-over-whole ratio into a terminal row count. |
| 129 | fn proportional(total: usize, part: usize, whole: usize) -> usize { |
| 130 | if whole == 0 { |
| 131 | return 0; |
| 132 | } |
| 133 | total.saturating_mul(part) / whole |
| 134 | } |
| 135 | |
| 136 | #[cfg(test)] |
| 137 | mod tests { |
| 138 | use super::*; |
| 139 | use rustik_tui_core as tui_core; |
| 140 | use tui_core::{Buffer, Color, Style}; |
| 141 | |
| 142 | #[test] |
| 143 | fn scrollbar_renders_track_when_content_fits() { |
| 144 | let style = ScrollbarStyle::new( |
| 145 | Cell::new('·', Style::new(Color::Unset)), |
| 146 | Cell::new('█', Style::new(Color::Unset)), |
| 147 | ); |
| 148 | let mut scrollbar = Scrollbar::new(View::new(0, 3, 5)).style(style); |
| 149 | let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 5)); |
| 150 | |
| 151 | scrollbar.render(buffer.area(), &mut buffer); |
| 152 | |
| 153 | assert_eq!(buffer.lines(), vec!["·", "·", "·", "·", "·"]); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn scrollbar_keeps_track_and_thumb_styles_separate() { |
| 158 | let style = ScrollbarStyle::new( |
| 159 | Cell::new('·', Style::new(Color::Fixed(236))), |
| 160 | Cell::new('█', Style::new(Color::White)), |
| 161 | ); |
| 162 | let mut scrollbar = Scrollbar::new(View::new(2, 10, 4)).style(style); |
| 163 | let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 4)); |
| 164 | |
| 165 | scrollbar.render(buffer.area(), &mut buffer); |
| 166 | |
| 167 | assert_eq!(buffer.lines(), vec!["▗", "█", "·", "·"]); |
| 168 | assert_eq!( |
| 169 | buffer.get(0, 0).map(|cell| cell.style().fg_color()), |
| 170 | Some(Color::White) |
| 171 | ); |
| 172 | assert_eq!( |
| 173 | buffer.get(0, 1).map(|cell| cell.style().fg_color()), |
| 174 | Some(Color::White) |
| 175 | ); |
| 176 | assert_eq!( |
| 177 | buffer.get(0, 2).map(|cell| cell.style().fg_color()), |
| 178 | Some(Color::Fixed(236)) |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn scrollbar_renders_split_half_thumb() { |
| 184 | let style = ScrollbarStyle::new( |
| 185 | Cell::new('·', Style::new(Color::Unset)), |
| 186 | Cell::new('█', Style::new(Color::Unset)), |
| 187 | ); |
| 188 | let mut scrollbar = Scrollbar::new(View::new(45, 100, 10)).style(style); |
| 189 | let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 10)); |
| 190 | |
| 191 | scrollbar.render(buffer.area(), &mut buffer); |
| 192 | |
| 193 | assert_eq!( |
| 194 | buffer.lines(), |
| 195 | vec!["·", "·", "·", "·", "▗", "▝", "·", "·", "·", "·"] |
| 196 | ); |
| 197 | } |
| 198 | } |