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
use crate::div::div_up;
use crate::theme::ColorStyle;
use crate::vec::Vec2;
use crate::Printer;
use std::cmp::{max, min};
#[derive(Default, Debug)]
pub struct ScrollBase {
pub start_line: usize,
pub content_height: usize,
pub view_height: usize,
pub scrollbar_offset: usize,
pub right_padding: usize,
pub thumb_grab: Option<usize>,
}
impl ScrollBase {
pub fn new() -> Self {
ScrollBase {
start_line: 0,
content_height: 0,
view_height: 0,
scrollbar_offset: 0,
right_padding: 1,
thumb_grab: None,
}
}
pub fn scrollbar_offset(mut self, offset: usize) -> Self {
self.scrollbar_offset = offset;
self
}
pub fn right_padding(mut self, padding: usize) -> Self {
self.right_padding = padding;
self
}
pub fn set_heights(&mut self, view_height: usize, content_height: usize) {
self.view_height = view_height;
self.content_height = content_height;
if self.scrollable() {
self.start_line =
min(self.start_line, self.content_height - self.view_height);
} else {
self.start_line = 0;
}
}
pub fn scrollable(&self) -> bool {
self.view_height < self.content_height
}
pub fn can_scroll_up(&self) -> bool {
self.start_line > 0
}
pub fn can_scroll_down(&self) -> bool {
self.start_line + self.view_height < self.content_height
}
pub fn scroll_top(&mut self) {
self.start_line = 0;
}
pub fn scroll_to(&mut self, y: usize) {
if y >= self.start_line + self.view_height {
self.start_line = 1 + y - self.view_height;
} else if y < self.start_line {
self.start_line = y;
}
}
pub fn scroll_bottom(&mut self) {
if self.scrollable() {
self.start_line = self.content_height - self.view_height;
}
}
pub fn scroll_down(&mut self, n: usize) {
if self.scrollable() {
self.start_line = min(
self.start_line + n,
self.content_height - self.view_height,
);
}
}
pub fn scroll_to_thumb(&mut self, thumb_y: usize, thumb_height: usize) {
self.start_line = min(
div_up(
(1 + self.content_height - self.view_height) * thumb_y,
self.view_height - thumb_height + 1,
),
self.content_height - self.view_height,
);
}
pub fn scroll_up(&mut self, n: usize) {
if self.scrollable() {
self.start_line -= min(self.start_line, n);
}
}
pub fn start_drag(&mut self, position: Vec2, width: usize) -> bool {
let scrollbar_x = self.scrollbar_x(width);
if position.x != scrollbar_x {
return false;
}
let height = self.scrollbar_thumb_height();
let thumb_y = self.scrollbar_thumb_y(height);
if position.y >= thumb_y && position.y < thumb_y + height {
self.thumb_grab = Some(position.y - thumb_y);
} else {
self.thumb_grab = Some((height - 1) / 2);
self.drag(position);
}
true
}
pub fn drag(&mut self, position: Vec2) {
if let Some(grab) = self.thumb_grab {
let height = self.scrollbar_thumb_height();
self.scroll_to_thumb(position.y.saturating_sub(grab), height);
}
}
pub fn is_dragging(&self) -> bool {
self.thumb_grab.is_some()
}
pub fn release_grab(&mut self) {
self.thumb_grab = None;
}
pub fn draw<F>(&self, printer: &Printer<'_, '_>, line_drawer: F)
where
F: Fn(&Printer<'_, '_>, usize),
{
if self.view_height == 0 {
return;
}
let max_y =
min(self.view_height, self.content_height - self.start_line);
let w = if self.scrollable() {
printer.size.x.saturating_sub(1 + self.right_padding)
} else {
printer.size.x
};
for y in 0..max_y {
line_drawer(
&printer.offset((0, y)).cropped((w, 1)),
y + self.start_line,
);
}
if self.view_height < self.content_height {
let height = self.scrollbar_thumb_height();
let start = self.scrollbar_thumb_y(height);
let color = if printer.focused {
ColorStyle::highlight()
} else {
ColorStyle::highlight_inactive()
};
let scrollbar_x = self.scrollbar_x(printer.size.x);
printer.print_vline((scrollbar_x, 0), printer.size.y, "|");
printer.with_color(color, |printer| {
printer.print_vline((scrollbar_x, start), height, "▒");
});
}
}
pub fn scrollbar_x(&self, total_size: usize) -> usize {
total_size.saturating_sub(1 + self.scrollbar_offset)
}
pub fn scrollbar_thumb_height(&self) -> usize {
max(1, self.view_height * self.view_height / self.content_height)
}
pub fn scrollbar_thumb_y(&self, scrollbar_thumb_height: usize) -> usize {
let steps = self.view_height - scrollbar_thumb_height + 1;
steps * self.start_line / (1 + self.content_height - self.view_height)
}
}