Skip to main content

codetether_agent/tui/app/state/
scroll.rs

1//! Chat and tool-preview scroll methods.
2//!
3//! Implements the sentinel-value scheme: `chat_scroll >= 1_000_000` means
4//! "follow latest" and is clamped to `chat_last_max_scroll` at render time.
5
6impl super::AppState {
7    pub fn scroll_up(&mut self, amount: usize) {
8        if self.chat_scroll >= 1_000_000 {
9            self.chat_scroll = self.chat_last_max_scroll.saturating_sub(amount);
10        } else {
11            self.chat_scroll = self.chat_scroll.saturating_sub(amount);
12        }
13    }
14
15    pub fn scroll_down(&mut self, amount: usize) {
16        if self.chat_scroll >= 1_000_000 {
17            return;
18        }
19        let next = self.chat_scroll.saturating_add(amount);
20        if next >= self.chat_last_max_scroll {
21            self.scroll_to_bottom();
22        } else {
23            self.chat_scroll = next;
24        }
25    }
26
27    /// Set sentinel value — clamped to actual content height at render time.
28    pub fn scroll_to_bottom(&mut self) {
29        self.chat_scroll = 1_000_000;
30    }
31
32    pub fn scroll_to_top(&mut self) {
33        self.chat_scroll = 0;
34    }
35
36    pub fn set_chat_max_scroll(&mut self, max_scroll: usize) {
37        self.chat_last_max_scroll = max_scroll;
38        if max_scroll == 0 {
39            self.chat_scroll = 0;
40        } else if self.chat_scroll < 1_000_000 {
41            self.chat_scroll = self.chat_scroll.min(max_scroll);
42        }
43    }
44
45    pub fn scroll_tool_preview_up(&mut self, amount: usize) {
46        self.tool_preview_scroll = self.tool_preview_scroll.saturating_sub(amount);
47    }
48
49    pub fn scroll_tool_preview_down(&mut self, amount: usize) {
50        let next = self.tool_preview_scroll.saturating_add(amount);
51        self.tool_preview_scroll = next.min(self.tool_preview_last_max_scroll);
52    }
53
54    pub fn reset_tool_preview_scroll(&mut self) {
55        self.tool_preview_scroll = 0;
56    }
57
58    pub fn set_tool_preview_max_scroll(&mut self, max_scroll: usize) {
59        self.tool_preview_last_max_scroll = max_scroll;
60        self.tool_preview_scroll = self.tool_preview_scroll.min(max_scroll);
61    }
62}