1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// (C) 2025 - Enzo Lombardi
//! Scroller view - scrollable viewport base for text viewers and editors.
use crate::core::geometry::{Point, Rect};
use crate::core::event::Event;
use crate::terminal::Terminal;
use super::view::View;
use super::scrollbar::ScrollBar;
/// Scroller is a base class for scrollable views.
/// It manages scroll offsets (delta) and content size (limit),
/// and coordinates with horizontal and vertical scrollbars.
pub struct Scroller {
bounds: Rect,
delta: Point, // Current scroll offset
limit: Point, // Maximum scroll range (content size)
h_scrollbar: Option<Box<ScrollBar>>,
v_scrollbar: Option<Box<ScrollBar>>,
palette_chain: Option<crate::core::palette_chain::PaletteChainNode>,
}
impl Scroller {
pub fn new(bounds: Rect, h_scrollbar: Option<Box<ScrollBar>>, v_scrollbar: Option<Box<ScrollBar>>) -> Self {
let mut scroller = Self {
bounds,
delta: Point::zero(),
limit: Point::zero(),
h_scrollbar,
v_scrollbar,
palette_chain: None,
};
scroller.update_scrollbars();
scroller
}
/// Set the scroll offset
pub fn scroll_to(&mut self, x: i16, y: i16) {
self.delta.x = x.max(0).min(self.limit.x);
self.delta.y = y.max(0).min(self.limit.y);
self.update_scrollbars();
}
/// Set the content size limit
pub fn set_limit(&mut self, x: i16, y: i16) {
self.limit.x = x.max(0);
self.limit.y = y.max(0);
// Adjust delta if it exceeds new limit
self.delta.x = self.delta.x.min(self.limit.x);
self.delta.y = self.delta.y.min(self.limit.y);
self.update_scrollbars();
}
/// Get current scroll offset
pub fn get_delta(&self) -> Point {
self.delta
}
/// Get content size limit
pub fn get_limit(&self) -> Point {
self.limit
}
/// Update scrollbar positions to match current delta
fn update_scrollbars(&mut self) {
if let Some(ref mut h_bar) = self.h_scrollbar {
h_bar.set_params(
self.delta.x as i32,
0,
self.limit.x as i32,
self.bounds.width() as i32,
1,
);
}
if let Some(ref mut v_bar) = self.v_scrollbar {
v_bar.set_params(
self.delta.y as i32,
0,
self.limit.y as i32,
self.bounds.height() as i32,
1,
);
}
}
/// Draw the scroller (draws scrollbars, subclasses override to draw content)
pub fn draw_scrollbars(&mut self, terminal: &mut Terminal) {
if let Some(ref mut h_bar) = self.h_scrollbar {
h_bar.draw(terminal);
}
if let Some(ref mut v_bar) = self.v_scrollbar {
v_bar.draw(terminal);
}
}
/// Handle scrollbar events
pub fn handle_scrollbar_events(&mut self, event: &mut Event) {
let old_delta = self.delta;
// Let scrollbars handle the event
if let Some(ref mut h_bar) = self.h_scrollbar {
h_bar.handle_event(event);
self.delta.x = h_bar.get_value() as i16;
}
if let Some(ref mut v_bar) = self.v_scrollbar {
v_bar.handle_event(event);
self.delta.y = v_bar.get_value() as i16;
}
// If delta changed, the event was handled
if old_delta != self.delta {
event.clear();
}
}
}
impl View for Scroller {
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
// Update scrollbar positions (they are typically at edges)
if let Some(ref mut h_bar) = self.h_scrollbar {
let h_bounds = Rect::new(
bounds.a.x,
bounds.b.y - 1,
bounds.b.x - 1,
bounds.b.y,
);
h_bar.set_bounds(h_bounds);
}
if let Some(ref mut v_bar) = self.v_scrollbar {
let v_bounds = Rect::new(
bounds.b.x - 1,
bounds.a.y,
bounds.b.x,
bounds.b.y - 1,
);
v_bar.set_bounds(v_bounds);
}
self.update_scrollbars();
}
fn draw(&mut self, terminal: &mut Terminal) {
// Default implementation: draw scrollbars only
// Subclasses should override this to draw content + scrollbars
self.draw_scrollbars(terminal);
}
fn handle_event(&mut self, event: &mut Event) {
self.handle_scrollbar_events(event);
}
fn set_palette_chain(&mut self, node: Option<crate::core::palette_chain::PaletteChainNode>) {
self.palette_chain = node;
}
fn get_palette_chain(&self) -> Option<&crate::core::palette_chain::PaletteChainNode> {
self.palette_chain.as_ref()
}
fn get_palette(&self) -> Option<crate::core::palette::Palette> {
use crate::core::palette::{palettes, Palette};
Some(Palette::from_slice(palettes::CP_SCROLLER))
}
}
/// Builder for creating scrollers with a fluent API.
///
/// # Examples
///
/// ```ignore
/// use turbo_vision::views::scroller::ScrollerBuilder;
/// use turbo_vision::views::scrollbar::ScrollBarBuilder;
/// use turbo_vision::core::geometry::Rect;
///
/// // Create a scroller with both scrollbars
/// let v_scrollbar = ScrollBarBuilder::new()
/// .bounds(Rect::new(78, 0, 79, 24))
/// .vertical()
/// .build_boxed();
///
/// let h_scrollbar = ScrollBarBuilder::new()
/// .bounds(Rect::new(0, 24, 78, 25))
/// .horizontal()
/// .build_boxed();
///
/// let scroller = ScrollerBuilder::new()
/// .bounds(Rect::new(0, 0, 79, 25))
/// .v_scrollbar(v_scrollbar)
/// .h_scrollbar(h_scrollbar)
/// .build();
///
/// // Create a scroller with only vertical scrollbar
/// let v_scrollbar = ScrollBarBuilder::new()
/// .bounds(Rect::new(78, 0, 79, 25))
/// .vertical()
/// .build_boxed();
///
/// let scroller = ScrollerBuilder::new()
/// .bounds(Rect::new(0, 0, 79, 25))
/// .v_scrollbar(v_scrollbar)
/// .build();
/// ```
pub struct ScrollerBuilder {
bounds: Option<Rect>,
h_scrollbar: Option<Box<ScrollBar>>,
v_scrollbar: Option<Box<ScrollBar>>,
}
impl ScrollerBuilder {
/// Creates a new ScrollerBuilder with default values.
pub fn new() -> Self {
Self {
bounds: None,
h_scrollbar: None,
v_scrollbar: None,
}
}
/// Sets the scroller bounds (required).
#[must_use]
pub fn bounds(mut self, bounds: Rect) -> Self {
self.bounds = Some(bounds);
self
}
/// Sets the horizontal scrollbar (optional).
#[must_use]
pub fn h_scrollbar(mut self, scrollbar: Box<ScrollBar>) -> Self {
self.h_scrollbar = Some(scrollbar);
self
}
/// Sets the vertical scrollbar (optional).
#[must_use]
pub fn v_scrollbar(mut self, scrollbar: Box<ScrollBar>) -> Self {
self.v_scrollbar = Some(scrollbar);
self
}
/// Builds the Scroller.
///
/// # Panics
///
/// Panics if required fields (bounds) are not set.
pub fn build(self) -> Scroller {
let bounds = self.bounds.expect("Scroller bounds must be set");
Scroller::new(bounds, self.h_scrollbar, self.v_scrollbar)
}
/// Builds the Scroller as a Box.
pub fn build_boxed(self) -> Box<Scroller> {
Box::new(self.build())
}
}
impl Default for ScrollerBuilder {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_scroller_scroll_to() {
let scroller = Scroller::new(Rect::new(0, 0, 80, 25), None, None);
let mut scroller = scroller;
scroller.set_limit(100, 100);
scroller.scroll_to(10, 20);
assert_eq!(scroller.get_delta(), Point::new(10, 20));
// Test clamping to limit
scroller.scroll_to(150, 150);
assert_eq!(scroller.get_delta(), Point::new(100, 100));
// Test clamping to zero
scroller.scroll_to(-10, -10);
assert_eq!(scroller.get_delta(), Point::new(0, 0));
}
#[test]
fn test_scroller_set_limit() {
let scroller = Scroller::new(Rect::new(0, 0, 80, 25), None, None);
let mut scroller = scroller;
// First set a large limit
scroller.set_limit(100, 100);
scroller.scroll_to(50, 50);
assert_eq!(scroller.get_delta(), Point::new(50, 50));
// Reducing limit should clamp delta
scroller.set_limit(30, 30);
assert_eq!(scroller.get_delta(), Point::new(30, 30));
assert_eq!(scroller.get_limit(), Point::new(30, 30));
}
#[test]
fn test_scroller_builder() {
let scroller = ScrollerBuilder::new()
.bounds(Rect::new(0, 0, 80, 25))
.build();
assert_eq!(scroller.get_delta(), Point::zero());
assert_eq!(scroller.get_limit(), Point::zero());
}
}