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 = self.content_height.saturating_sub(self.window_height);
48
49 if self.offset >= max_offset {
50 self.auto_scroll = true;
51 } else {
52 self.auto_scroll = false;
53 self.offset = (self.offset + amount).min(max_offset);
54 }
55 self.force_scroll = false;
56 }
57
58 pub fn force_auto_scroll(&mut self) {
60 self.force_scroll = true;
61 self.auto_scroll = true;
62 }
63
64 pub fn get_visible_range(&self) -> (usize, usize) {
65 if self.content_height <= self.window_height {
66 return (0, self.content_height);
67 }
68
69 let start = self.offset;
70 let end = (self.offset + self.window_height).min(self.content_height);
71 (start, end)
72 }
73
74 pub fn can_scroll(&self) -> bool {
75 true
76 }
77
78 pub fn is_auto_scroll(&self) -> bool {
79 self.auto_scroll
80 }
81}
82
83impl Default for ScrollState {
84 fn default() -> Self {
85 Self::new()
86 }
87}