use ratatui::layout::{Position, Size};
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
pub struct ScrollViewState {
pub(crate) offset: Position,
pub(crate) size: Option<Size>,
pub(crate) page_size: Option<Size>,
}
impl ScrollViewState {
pub fn new() -> Self {
Self::default()
}
pub fn with_offset(offset: Position) -> Self {
Self {
offset,
..Default::default()
}
}
pub fn set_offset(&mut self, offset: Position) {
self.offset = offset;
}
pub fn offset(&self) -> Position {
self.offset
}
pub fn scroll_up(&mut self) {
self.offset.y = self.offset.y.saturating_sub(1);
}
pub fn scroll_down(&mut self) {
self.offset.y = self.offset.y.saturating_add(1);
}
pub fn scroll_page_down(&mut self) {
let page_size = self.page_size.map_or(1, |size| size.height);
self.offset.y = self.offset.y.saturating_add(page_size).saturating_sub(1);
}
pub fn scroll_page_up(&mut self) {
let page_size = self.page_size.map_or(1, |size| size.height);
self.offset.y = self.offset.y.saturating_add(1).saturating_sub(page_size);
}
pub fn scroll_left(&mut self) {
self.offset.x = self.offset.x.saturating_sub(1);
}
pub fn scroll_right(&mut self) {
self.offset.x = self.offset.x.saturating_add(1);
}
pub fn scroll_to_top(&mut self) {
self.offset = (0, 0).into();
}
pub fn scroll_to_bottom(&mut self) {
let bottom = self.size.map_or(u16::MAX, |size| size.height.saturating_sub(1));
self.offset.y = bottom;
}
}