Debian Codegraph @

src/canvas/components/scroll_bar.rs

raw symbols SCIP
1 //! A shared helper for drawing a vertical scroll bar.
2
3 use ratatui::{
4 Frame,
5 layout::Rect,
6 style::Style,
7 symbols::{self, scrollbar},
8 widgets::{Scrollbar, ScrollbarOrientation, ScrollbarState},
9 };
10
11 /// Arguments for [`draw_scroll_bar`].
12 pub struct ScrollBarArgs {
13 /// Total number of items in the content.
14 pub content_length: usize,
15 /// Number of items that can be seen in the viewport.
16 pub viewport_length: usize,
17 /// Current scroll position in the list of items.
18 pub position: usize,
19 /// Style to be applied to the scrollbar.
20 pub style: Style,
21 }
22
23 /// Returns a [`Rect`] for a vertical scroll bar drawn just inside the right
24 /// border of a dialog block.
25 pub fn dialog_scroll_bar_area(block_area: Rect) -> Rect {
26 Rect {
27 x: block_area.x + block_area.width.saturating_sub(2),
28 y: block_area.y + 1,
29 width: 1,
30 height: block_area.height.saturating_sub(2),
31 }
32 }
33
34 /// Draw a vertical scroll bar in `area`.
35 pub fn draw_scroll_bar(f: &mut Frame<'_>, area: Rect, args: ScrollBarArgs) {
36 if args.content_length <= args.viewport_length || area.width == 0 || area.height == 0 {
37 return;
38 }
39
40 const SYMBOLS: scrollbar::Set<'_> = scrollbar::Set {
41 track: "",
42 thumb: symbols::block::FULL,
43 begin: "▲",
44 end: "▼",
45 };
46
47 let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
48 .style(args.style)
49 .symbols(SYMBOLS);
50
51 let mut state = ScrollbarState::new(args.content_length)
52 .position(args.position)
53 .viewport_content_length(args.viewport_length);
54
55 f.render_stateful_widget(scrollbar, area, &mut state);
56 }