//! Scrollbar rendering. //! //! `Scrollbar` turns a [`View`](crate::View) into a one-column visual indicator. //! Track and thumb are full [`Cell`](rustik_tui_core::Cell) values so glyph and style //! stay configurable together. use crate::View; use rustik_tui_core as tui_core; use tui_core::{Buffer, Cell, Color, Constraint, Rect, Size, Style, Widget}; /// Default scrollbar track glyph. pub const DEFAULT_TRACK: char = '│'; /// Default scrollbar thumb glyph. pub const DEFAULT_THUMB: char = '┃'; /// Default scrollbar track style. pub const DEFAULT_TRACK_STYLE: Style = Style::new(Color::Unset); /// Default scrollbar thumb style. pub const DEFAULT_THUMB_STYLE: Style = Style::new(Color::Unset); /// Vertical scrollbar for a [`View`]. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Scrollbar { /// View used to derive thumb position and size. pub view: View, /// Cells used to render the track and thumb. pub style: ScrollbarStyle, } /// Reusable scrollbar cells. #[derive(Debug, Clone, PartialEq, Eq)] pub struct ScrollbarStyle { track: Cell, thumb: Cell, } impl ScrollbarStyle { /// Creates a scrollbar style from track and thumb cells. pub fn new(track: Cell, thumb: Cell) -> Self { Self { track, thumb } } /// Returns the track cell. pub const fn track(&self) -> &Cell { &self.track } /// Returns the thumb cell. pub const fn thumb(&self) -> &Cell { &self.thumb } } impl Default for ScrollbarStyle { fn default() -> Self { Self { track: Cell::new(DEFAULT_TRACK, DEFAULT_TRACK_STYLE), thumb: Cell::new(DEFAULT_THUMB, DEFAULT_THUMB_STYLE), } } } impl Scrollbar { /// Creates a scrollbar for a view. pub fn new(view: View) -> Self { Self { view, style: ScrollbarStyle::default(), } } /// Sets both cells from a reusable style. pub fn style(mut self, style: ScrollbarStyle) -> Self { self.style = style; self } /// Returns the thumb range in half-row units for a rendered height. fn thumb_range(&self, rows: usize) -> std::ops::Range { if rows == 0 || !self.view.is_scrollable() { return 0..0; } let half_rows = rows.saturating_mul(2); let thumb_half_rows = proportional(half_rows, self.view.visible_rows, self.view.content_rows) .clamp(2, half_rows); let travel = half_rows - thumb_half_rows; let start = if self.view.max_offset() == 0 { 0 } else { proportional(travel, self.view.offset, self.view.max_offset()) }; start..start + thumb_half_rows } } impl Widget for Scrollbar { fn size(&self, parent: Constraint) -> Size { let rows = if parent.max.rows == usize::MAX { self.view.visible_rows } else { parent.max.rows }; Size::new(1, rows).constrain(parent) } fn render(&mut self, area: Rect, buf: &mut Buffer) { if area.is_empty() { return; } let thumb = self.thumb_range(area.rows); for row in 0..area.rows { let half_row = row.saturating_mul(2); let cell = match ( thumb.contains(&half_row), thumb.contains(&half_row.saturating_add(1)), ) { (true, true) => self.style.thumb.clone(), (true, false) => Cell::new('▝', self.style.thumb.style()), (false, true) => Cell::new('▗', self.style.thumb.style()), (false, false) => self.style.track.clone(), }; buf.set(area.x, area.y + row, cell); } } } /// Scales a part-over-whole ratio into a terminal row count. fn proportional(total: usize, part: usize, whole: usize) -> usize { if whole == 0 { return 0; } total.saturating_mul(part) / whole } #[cfg(test)] mod tests { use super::*; use rustik_tui_core as tui_core; use tui_core::{Buffer, Color, Style}; #[test] fn scrollbar_renders_track_when_content_fits() { let style = ScrollbarStyle::new( Cell::new('·', Style::new(Color::Unset)), Cell::new('█', Style::new(Color::Unset)), ); let mut scrollbar = Scrollbar::new(View::new(0, 3, 5)).style(style); let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 5)); scrollbar.render(buffer.area(), &mut buffer); assert_eq!(buffer.lines(), vec!["·", "·", "·", "·", "·"]); } #[test] fn scrollbar_keeps_track_and_thumb_styles_separate() { let style = ScrollbarStyle::new( Cell::new('·', Style::new(Color::Fixed(236))), Cell::new('█', Style::new(Color::White)), ); let mut scrollbar = Scrollbar::new(View::new(2, 10, 4)).style(style); let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 4)); scrollbar.render(buffer.area(), &mut buffer); assert_eq!(buffer.lines(), vec!["▗", "█", "·", "·"]); assert_eq!( buffer.get(0, 0).map(|cell| cell.style().fg_color()), Some(Color::White) ); assert_eq!( buffer.get(0, 1).map(|cell| cell.style().fg_color()), Some(Color::White) ); assert_eq!( buffer.get(0, 2).map(|cell| cell.style().fg_color()), Some(Color::Fixed(236)) ); } #[test] fn scrollbar_renders_split_half_thumb() { let style = ScrollbarStyle::new( Cell::new('·', Style::new(Color::Unset)), Cell::new('█', Style::new(Color::Unset)), ); let mut scrollbar = Scrollbar::new(View::new(45, 100, 10)).style(style); let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 10)); scrollbar.render(buffer.area(), &mut buffer); assert_eq!( buffer.lines(), vec!["·", "·", "·", "·", "▗", "▝", "·", "·", "·", "·"] ); } }