Skip to main content

tui/widgets/
table_panel.rs

1//! The panel every list tab draws: a Dos Navigator frame around a header, rows and a scrollbar.
2
3use std::ops::Range;
4
5use crate::hits::{Hits, Surface, Target};
6use ratatui::buffer::Buffer;
7use ratatui::layout::{Constraint, Rect};
8use ratatui::text::Line;
9use ratatui::widgets::{Cell as TableCell, Row, StatefulWidget, Table, TableState};
10use tvui::{FrameState, Panel, Role, RowWindow, Theme, render_scrollbar, scrollbar_parts};
11
12pub struct TablePanel<'a> {
13    pub theme: &'a Theme,
14    pub title: &'a str,
15    pub state: FrameState,
16    pub headers: &'a [&'a str],
17    pub widths: &'a [Constraint],
18    pub rows: Vec<Row<'static>>,
19    pub selected: usize,
20    /// The rows the list has in all and the index of the first loaded one among them.
21    pub extent: (usize, usize),
22}
23
24impl TablePanel<'_> {
25    /// Draws the panel in `area` and returns the range of rows on screen.
26    pub fn draw(
27        self,
28        buf: &mut Buffer,
29        area: Rect,
30        window: &RowWindow,
31        hits: &mut Hits,
32    ) -> Range<usize> {
33        let panel = Panel::new(self.theme, self.title, self.state).widths(self.widths);
34        let columns = panel.columns(area);
35        panel.render_frame(area, buf);
36
37        let inner = Panel::inner(area);
38        let view = usize::from(Panel::rows(area).height);
39        let rows = self.rows.len();
40        let header_style = self.theme.style(Role::PanelHeader);
41        let header = Row::new(
42            self.headers
43                .iter()
44                .map(|h| TableCell::new(Line::from(*h).centered()).style(header_style)),
45        );
46        let table = Table::new(
47            self.rows,
48            columns.iter().map(|c| Constraint::Length(c.width)),
49        )
50        .header(header)
51        .row_highlight_style(self.theme.style(Role::PanelCursor));
52        let mut state = TableState::default()
53            .with_offset(window.fit(rows, view))
54            .with_selected(Some(self.selected));
55        StatefulWidget::render(table, inner, buf, &mut state);
56
57        let top = state.offset();
58        let cursor = state
59            .selected()
60            .and_then(|selected| selected.checked_sub(top))
61            .filter(|row| *row < view)
62            .and_then(|row| u16::try_from(row).ok())
63            .map(|row| inner.y + 1 + row);
64        panel.render_dividers(area, buf, cursor);
65
66        window.set_top(top);
67        let (total, first) = self.extent;
68        let bar = (total.max(rows), view.min(rows), first.saturating_add(top));
69        render_scrollbar(self.theme, area, buf, bar);
70        let shown = top..top.saturating_add(view).min(rows);
71        record_panel(hits, area, top, &shown);
72        for (rect, part) in scrollbar_parts(area, bar) {
73            hits.push(rect, Target::Scroll(Surface::List, part));
74        }
75        shown
76    }
77}
78
79fn record_panel(hits: &mut Hits, area: Rect, top: usize, shown: &Range<usize>) {
80    hits.push(Panel::inner(area), Target::Body(Surface::List));
81    let body = Panel::rows(area);
82    for row in shown.clone() {
83        let y = body
84            .y
85            .saturating_add(u16::try_from(row - top).unwrap_or(u16::MAX));
86        hits.push(Rect::new(body.x, y, body.width, 1), Target::Row(row));
87    }
88}
89
90#[cfg(test)]
91mod tests;