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
use std::cmp::{max, min};
pub const RESERVED_ROWS: usize = 3;
#[derive(Debug, Clone)]
pub struct ContentWindow {
pub top: usize,
pub bottom: usize,
pub len: usize,
pub height: usize,
}
impl ContentWindow {
pub fn new(len: usize, height: usize) -> Self {
ContentWindow {
top: 0,
bottom: min(len, height - RESERVED_ROWS),
len,
height: height - RESERVED_ROWS,
}
}
const WINDOW_PADDING: usize = 4;
pub const WINDOW_MARGIN_TOP: usize = 2;
pub fn set_height(&mut self, height: usize) {
self.height = height - RESERVED_ROWS;
}
pub fn scroll_up_one(&mut self, index: usize) {
if (index < self.top + Self::WINDOW_PADDING || index > self.bottom) && self.top > 0 {
self.top -= 1;
self.bottom -= 1;
}
self.scroll_to(index)
}
pub fn scroll_down_one(&mut self, index: usize) {
if self.len < self.height {
return;
}
if index < self.top || index > self.bottom - Self::WINDOW_PADDING {
self.top += 1;
self.bottom += 1;
}
self.scroll_to(index)
}
pub fn reset(&mut self, len: usize) {
self.len = len;
self.top = 0;
self.bottom = min(len, self.height);
}
pub fn scroll_to(&mut self, index: usize) {
if index < self.top || index > self.bottom {
self.top = max(index, Self::WINDOW_PADDING) - Self::WINDOW_PADDING;
self.bottom = self.top + min(self.len, self.height - 3);
}
}
}