| 1 | use std::num::NonZeroU16; |
| 2 | |
| 3 | use ratatui::{layout::Rect, widgets::TableState}; |
| 4 | |
| 5 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Default)] |
| 6 | pub enum ScrollDirection { |
| 7 | // UP means scrolling up --- this usually DECREMENTS |
| 8 | Up, |
| 9 | |
| 10 | // DOWN means scrolling down --- this usually INCREMENTS |
| 11 | #[default] |
| 12 | Down, |
| 13 | } |
| 14 | |
| 15 | /// Internal state representation of a [`DataTable`](super::DataTable). |
| 16 | pub struct DataTableState { |
| 17 | /// The index from where to start displaying the rows. |
| 18 | pub display_start_index: usize, |
| 19 | |
| 20 | /// The current scroll position. |
| 21 | pub current_index: usize, |
| 22 | |
| 23 | /// The direction of the last attempted scroll. |
| 24 | pub scroll_direction: ScrollDirection, |
| 25 | |
| 26 | /// ratatui's internal table state. |
| 27 | pub table_state: TableState, |
| 28 | |
| 29 | /// The calculated widths. |
| 30 | pub calculated_widths: Vec<NonZeroU16>, |
| 31 | |
| 32 | /// The current inner [`Rect`]. |
| 33 | pub inner_rect: Rect, |
| 34 | } |
| 35 | |
| 36 | impl Default for DataTableState { |
| 37 | fn default() -> Self { |
| 38 | Self { |
| 39 | display_start_index: 0, |
| 40 | current_index: 0, |
| 41 | scroll_direction: ScrollDirection::Down, |
| 42 | calculated_widths: vec![], |
| 43 | table_state: TableState::default(), |
| 44 | inner_rect: Rect::default(), |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl DataTableState { |
| 50 | /// Gets the starting position of a table. |
| 51 | pub fn get_start_position(&mut self, num_rows: usize, is_force_redraw: bool) { |
| 52 | let start_index = if is_force_redraw { |
| 53 | 0 |
| 54 | } else { |
| 55 | self.display_start_index |
| 56 | }; |
| 57 | let current_scroll_position = self.current_index; |
| 58 | let scroll_direction = self.scroll_direction; |
| 59 | |
| 60 | self.display_start_index = match scroll_direction { |
| 61 | ScrollDirection::Down => { |
| 62 | if current_scroll_position < start_index + num_rows { |
| 63 | // If, using the current scroll position, we can see the element |
| 64 | // (so within that and + num_rows) just reuse the current previously |
| 65 | // scrolled position. |
| 66 | start_index |
| 67 | } else if current_scroll_position >= num_rows { |
| 68 | // If the current position past the last element visible in the list, |
| 69 | // then skip until we can see that element. |
| 70 | current_scroll_position - num_rows + 1 |
| 71 | } else { |
| 72 | // Else, if it is not past the last element visible, do not omit anything. |
| 73 | 0 |
| 74 | } |
| 75 | } |
| 76 | ScrollDirection::Up => { |
| 77 | if current_scroll_position <= start_index { |
| 78 | // If it's past the first element, then show from that element downwards |
| 79 | current_scroll_position |
| 80 | } else if current_scroll_position >= start_index + num_rows { |
| 81 | current_scroll_position - num_rows + 1 |
| 82 | } else { |
| 83 | start_index |
| 84 | } |
| 85 | } |
| 86 | }; |
| 87 | } |
| 88 | } |