use std::num::NonZeroU16;
use tui::{layout::Rect, widgets::TableState};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub enum ScrollDirection {
Up,
#[default]
Down,
}
pub struct DataTableState {
pub display_start_index: usize,
pub current_index: usize,
pub scroll_direction: ScrollDirection,
pub table_state: TableState,
pub calculated_widths: Vec<NonZeroU16>,
pub inner_rect: Rect,
}
impl Default for DataTableState {
fn default() -> Self {
Self {
display_start_index: 0,
current_index: 0,
scroll_direction: ScrollDirection::Down,
calculated_widths: vec![],
table_state: TableState::default(),
inner_rect: Rect::default(),
}
}
}
impl DataTableState {
pub fn get_start_position(&mut self, num_rows: usize, is_force_redraw: bool) {
let start_index = if is_force_redraw {
0
} else {
self.display_start_index
};
let current_scroll_position = self.current_index;
let scroll_direction = self.scroll_direction;
self.display_start_index = match scroll_direction {
ScrollDirection::Down => {
if current_scroll_position < start_index + num_rows {
start_index
} else if current_scroll_position >= num_rows {
current_scroll_position - num_rows + 1
} else {
0
}
}
ScrollDirection::Up => {
if current_scroll_position <= start_index {
current_scroll_position
} else if current_scroll_position >= start_index + num_rows {
current_scroll_position - num_rows + 1
} else {
start_index
}
}
};
}
}