1use ratatui::layout::{Position, Size};
2
3#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
4pub struct ScrollViewState {
5 pub(crate) offset: Position,
7 pub(crate) size: Option<Size>,
9 pub(crate) page_size: Option<Size>,
11}
12
13impl ScrollViewState {
14 pub fn new() -> Self {
16 Self::default()
17 }
18
19 pub fn with_offset(offset: Position) -> Self {
21 Self {
22 offset,
23 ..Default::default()
24 }
25 }
26
27 pub const fn set_offset(&mut self, offset: Position) {
29 self.offset = offset;
30 }
31
32 pub const fn offset(&self) -> Position {
34 self.offset
35 }
36
37 pub const fn scroll_up(&mut self) {
39 self.offset.y = self.offset.y.saturating_sub(1);
40 }
41
42 pub const fn scroll_down(&mut self) {
44 self.offset.y = self.offset.y.saturating_add(1);
45 }
46
47 pub fn scroll_page_down(&mut self) {
49 let page_size = self.page_size.map_or(1, |size| size.height);
50 self.offset.y = self.offset.y.saturating_add(page_size).saturating_sub(1);
52 }
53
54 pub fn scroll_page_up(&mut self) {
56 let page_size = self.page_size.map_or(1, |size| size.height);
57 self.offset.y = self.offset.y.saturating_add(1).saturating_sub(page_size);
59 }
60
61 pub const fn scroll_left(&mut self) {
63 self.offset.x = self.offset.x.saturating_sub(1);
64 }
65
66 pub const fn scroll_right(&mut self) {
68 self.offset.x = self.offset.x.saturating_add(1);
69 }
70
71 pub const fn scroll_to_top(&mut self) {
73 self.offset = Position::ORIGIN;
74 }
75
76 pub fn scroll_to_bottom(&mut self) {
78 let bottom = self
81 .size
82 .map_or(u16::MAX, |size| size.height.saturating_sub(1));
83 self.offset.y = bottom;
84 }
85}