Skip to main content

alf/tui/state/
scroll.rs

1//! Scroll management for all panels
2
3use super::navigation::NavigationState;
4use super::ui::{Panel, UiState};
5
6/// Scroll manager - handles scroll operations across all panels
7pub struct ScrollManager;
8
9impl ScrollManager {
10   /// Scroll the active panel up
11   pub fn scroll_up(
12      ui: &mut UiState,
13      navigation: &mut NavigationState,
14      amount: usize,
15   ) {
16      match ui.active_panel() {
17         Panel::List => {
18            navigation.scroll_up(amount);
19            ui.reset_detail_scroll();
20         },
21         Panel::Description => {
22            let new_offset = ui.description_scroll_offset().saturating_sub(amount);
23            ui.set_description_scroll_offset(new_offset);
24         },
25         Panel::Script => {
26            let new_offset = ui.script_scroll_offset().saturating_sub(amount);
27            ui.set_script_scroll_offset(new_offset);
28         },
29      }
30   }
31
32   /// Scroll the active panel down
33   pub fn scroll_down(
34      ui: &mut UiState,
35      navigation: &mut NavigationState,
36      amount: usize,
37      visible_count: usize,
38   ) {
39      match ui.active_panel() {
40         Panel::List => {
41            navigation.scroll_down(amount, visible_count);
42            ui.reset_detail_scroll();
43         },
44         Panel::Description => {
45            let new_offset = ui.description_scroll_offset() + amount;
46            let clamped = new_offset.min(ui.description_max_scroll());
47            ui.set_description_scroll_offset(clamped);
48         },
49         Panel::Script => {
50            let new_offset = ui.script_scroll_offset() + amount;
51            let clamped = new_offset.min(ui.script_max_scroll());
52            ui.set_script_scroll_offset(clamped);
53         },
54      }
55   }
56
57   /// Jump to top of active panel
58   pub fn move_top(
59      ui: &mut UiState,
60      navigation: &mut NavigationState,
61   ) {
62      match ui.active_panel() {
63         Panel::List => {
64            navigation.move_top();
65            ui.set_list_scroll_offset(0);
66            ui.reset_detail_scroll();
67         },
68         Panel::Description => {
69            ui.set_description_scroll_offset(0);
70         },
71         Panel::Script => {
72            ui.set_script_scroll_offset(0);
73         },
74      }
75   }
76
77   /// Jump to bottom of active panel
78   pub fn move_bottom(
79      ui: &mut UiState,
80      navigation: &mut NavigationState,
81      visible_count: usize,
82   ) {
83      match ui.active_panel() {
84         Panel::List => {
85            navigation.move_bottom(visible_count);
86            ui.reset_detail_scroll();
87         },
88         Panel::Description => {
89            ui.set_description_scroll_offset(ui.description_max_scroll());
90         },
91         Panel::Script => {
92            ui.set_script_scroll_offset(ui.script_max_scroll());
93         },
94      }
95   }
96
97   /// Scroll help modal up
98   pub fn help_scroll_up(ui: &mut UiState) {
99      let new_offset = ui.help_scroll_offset().saturating_sub(1);
100      ui.set_help_scroll_offset(new_offset);
101   }
102
103   /// Scroll help modal down
104   pub fn help_scroll_down(ui: &mut UiState) {
105      let current = ui.help_scroll_offset();
106      let max = ui.help_max_scroll();
107      if current < max {
108         ui.set_help_scroll_offset(current + 1);
109      }
110   }
111
112   /// Jump to top of help modal
113   pub fn help_jump_top(ui: &mut UiState) {
114      ui.set_help_scroll_offset(0);
115   }
116
117   /// Jump to bottom of help modal
118   pub fn help_jump_bottom(ui: &mut UiState) {
119      ui.set_help_scroll_offset(ui.help_max_scroll());
120   }
121}