rush_sync_server/output/
scroll.rs1pub struct ScrollState {
2 pub offset: usize,
3 pub window_height: usize,
4 content_height: usize,
5 auto_scroll: bool,
6 force_scroll: bool,
7}
8
9impl ScrollState {
10 pub fn new() -> Self {
11 Self {
12 offset: 0,
13 window_height: 0,
14 content_height: 0,
15 auto_scroll: true,
16 force_scroll: false,
17 }
18 }
19
20 pub fn update_dimensions(&mut self, window_height: usize, content_height: usize) {
21 let max_offset = content_height.saturating_sub(window_height);
22
23 if window_height > self.window_height && !self.auto_scroll {
24 let ratio = self.offset as f64 / self.content_height.max(1) as f64;
25 self.offset = (ratio * content_height as f64).round() as usize;
26 }
27
28 self.window_height = window_height;
29 self.content_height = content_height;
30 self.offset = self.offset.min(max_offset);
31
32 if self.auto_scroll || self.force_scroll {
33 self.offset = max_offset;
34 self.force_scroll = false;
35 }
36 }
37
38 pub fn scroll_up(&mut self, amount: usize) {
39 self.auto_scroll = false;
40 self.force_scroll = false;
41 if self.offset > 0 {
42 self.offset = self.offset.saturating_sub(amount);
43 }
44 }
45
46 pub fn scroll_down(&mut self, amount: usize) {
47 let max_offset = if self.content_height > self.window_height {
48 self.content_height - self.window_height
49 } else {
50 0
51 };
52
53 if self.offset >= max_offset {
54 self.auto_scroll = true;
55 } else {
56 self.auto_scroll = false;
57 self.offset = (self.offset + amount).min(max_offset);
58 }
59 self.force_scroll = false;
60 }
61
62 pub fn force_auto_scroll(&mut self) {
64 self.force_scroll = true;
65 self.auto_scroll = true;
66 }
67
68 pub fn get_visible_range(&self) -> (usize, usize) {
69 if self.content_height <= self.window_height {
70 return (0, self.content_height);
71 }
72
73 let start = self.offset;
74 let end = (self.offset + self.window_height).min(self.content_height);
75
76 (start, end)
77 }
78
79 pub fn can_scroll(&self) -> bool {
80 true
81 }
82
83 pub fn is_auto_scroll(&self) -> bool {
84 self.auto_scroll
85 }
86}
87
88impl Default for ScrollState {
89 fn default() -> Self {
90 Self::new()
91 }
92}