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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use {
Color,
Dimensions,
Mouse,
Point,
Range,
Rect,
Scalar,
Theme,
};
use utils::{map_range, partial_max};
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Scrolling {
pub horizontal: bool,
pub vertical: bool,
pub style: Style,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct State {
pub maybe_vertical: Option<Bar>,
pub maybe_horizontal: Option<Bar>,
pub visible: Rect,
pub total_dim: Dimensions,
pub thickness: Scalar,
pub color: Color,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Style {
pub maybe_thickness: Option<Scalar>,
pub maybe_color: Option<Color>,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Bar {
pub interaction: Interaction,
pub scrollable: Range,
pub offset: Scalar,
pub start_overlap: Scalar,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Interaction {
Normal,
Highlighted(Elem),
Clicked(Elem),
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Elem {
Handle(Scalar),
Track,
}
impl Scrolling {
pub fn new() -> Scrolling {
Scrolling {
vertical: false,
horizontal: false,
style: Style::new(),
}
}
}
impl Style {
pub fn new() -> Style {
Style {
maybe_thickness: None,
maybe_color: None,
}
}
pub fn thickness(&self, theme: &Theme) -> Scalar {
const DEFAULT_THICKNESS: Scalar = 10.0;
self.maybe_thickness.or(theme.maybe_scrollbar.as_ref().map(|style| {
style.maybe_thickness.unwrap_or(DEFAULT_THICKNESS)
})).unwrap_or(DEFAULT_THICKNESS)
}
pub fn color(&self, theme: &Theme) -> Color {
self.maybe_color.or(theme.maybe_scrollbar.as_ref().map(|style| {
style.maybe_color.unwrap_or(theme.shape_color.plain_contrast())
})).unwrap_or(theme.shape_color.plain_contrast())
}
}
impl Interaction {
pub fn color(&self, color: Color) -> Color {
match *self {
Interaction::Normal => color,
Interaction::Highlighted(_) => color.highlighted(),
Interaction::Clicked(_) => color.clicked(),
}
}
}
impl State {
pub fn new(scrolling: Scrolling,
visible: Rect,
kids: Rect,
theme: &Theme,
maybe_prev: Option<&State>) -> State
{
State {
maybe_vertical: if scrolling.vertical {
let maybe_prev = maybe_prev.as_ref()
.and_then(|prev| prev.maybe_vertical.as_ref());
Bar::new_if_scrollable(visible.y.invert(), kids.y.invert(), maybe_prev)
} else {
None
},
maybe_horizontal: if scrolling.horizontal {
let maybe_prev = maybe_prev.as_ref()
.and_then(|prev| prev.maybe_horizontal.as_ref());
Bar::new_if_scrollable(visible.x, kids.x, maybe_prev)
} else {
None
},
total_dim: visible.max(kids).dim(),
visible: visible,
thickness: scrolling.style.thickness(theme),
color: scrolling.style.color(theme),
}
}
pub fn handle_input(self, mouse: Mouse) -> State {
State {
maybe_vertical: self.maybe_vertical.map(|bar| {
let track = vertical_track(self.visible, self.thickness);
let handle = vertical_handle(&bar, track, self.total_dim[1]);
let visible = self.visible.y.invert();
let mouse_pos_scalar = mouse.xy[1] - track.top();
bar.handle_input(visible, track, handle, &mouse, mouse_pos_scalar, mouse.scroll.y)
}),
maybe_horizontal: self.maybe_horizontal.map(|bar| {
let track = horizontal_track(self.visible, self.thickness);
let handle = horizontal_handle(&bar, track, self.total_dim[0]);
let visible = self.visible.x;
let mouse_pos_scalar = mouse.xy[0] - track.left();
bar.handle_input(visible, track, handle, &mouse, mouse_pos_scalar, -mouse.scroll.x)
}),
.. self
}
}
pub fn is_over(&self, target_xy: Point) -> bool {
if self.maybe_vertical.is_some() {
if vertical_track(self.visible, self.thickness).is_over(target_xy) {
return true;
}
}
if self.maybe_horizontal.is_some() {
if horizontal_track(self.visible, self.thickness).is_over(target_xy) {
return true;
}
}
false
}
pub fn kids_pos_offset(&self) -> Dimensions {
let maybe_x_offset = self.maybe_horizontal.map(|bar| bar.kids_pos_offset());
let maybe_y_offset = self.maybe_vertical.map(|bar| bar.kids_pos_offset());
[maybe_x_offset.unwrap_or(0.0), maybe_y_offset.unwrap_or(0.0)]
}
}
impl Bar {
pub fn new_if_scrollable(visible: Range, kids: Range, maybe_prev: Option<&Bar>) -> Option<Bar> {
let kids_at_origin = {
let offset_from_origin = maybe_prev.map(|bar| bar.kids_pos_offset()).unwrap_or(0.0);
kids.shift(-offset_from_origin)
};
let total = visible.max_directed(kids_at_origin);
let scrollable = Range::new(total.start, total.end - visible.magnitude());
if scrollable.magnitude().is_normal() && scrollable.direction() == kids.direction() {
let interaction = maybe_prev.map(|bar| bar.interaction)
.unwrap_or(Interaction::Normal);
let start_overlap = {
let start_diff_at_origin = kids_at_origin.start - visible.start;
if start_diff_at_origin.signum() == visible.direction() {
start_diff_at_origin
} else {
0.0
}
};
let offset = maybe_prev.map(|bar| bar.offset).unwrap_or(0.0);
Some(Bar {
interaction: interaction,
scrollable: scrollable,
offset: offset,
start_overlap: start_overlap,
})
} else {
None
}
}
pub fn handle_input(self,
visible: Range,
track: Rect,
handle: Rect,
mouse: &Mouse,
mouse_pos_scalar: Scalar,
mouse_scroll_scalar: Scalar) -> Bar
{
use self::Elem::{Handle, Track};
use self::Interaction::{Highlighted, Clicked};
let is_over_elem = is_over_elem(track, handle, mouse, mouse_pos_scalar);
let new_interaction = new_interaction(&self, is_over_elem, mouse, mouse_pos_scalar);
let bar_offset_max = || {
let bar_mag = handle.len() * visible.direction();
track.len() * visible.direction() - bar_mag
};
let pos_offset_from_bar_offset = |bar_offset: Scalar, bar_offset_max: Scalar| {
map_range(bar_offset, 0.0, bar_offset_max, 0.0, self.scrollable.magnitude())
};
let new_offset = match (self.interaction, new_interaction) {
(Highlighted(Track), Clicked(Handle(mouse_pos_scalar))) => {
let direction = visible.direction();
let half_handle_len = handle.len() * direction / 2.0;
let target_offset = mouse_pos_scalar - half_handle_len;
let target_pos_offset = pos_offset_from_bar_offset(target_offset, bar_offset_max());
::utils::clamp(target_pos_offset, 0.0, self.scrollable.magnitude())
},
(Clicked(Handle(prev_mouse_scalar)), Clicked(Handle(mouse_pos_scalar))) => {
let scroll_amount = mouse_pos_scalar - prev_mouse_scalar;
let pos_scroll_amount = pos_offset_from_bar_offset(scroll_amount, bar_offset_max());
self.add_to_scroll_offset(pos_scroll_amount)
},
(_, _) if mouse_scroll_scalar != 0.0 =>
self.add_to_scroll_offset(mouse_scroll_scalar),
_ => self.offset,
};
Bar { interaction: new_interaction, offset: new_offset, ..self }
}
fn add_to_scroll_offset(&self, amount: Scalar) -> Scalar {
use utils::clamp;
let target_offset = self.offset + amount;
let min_offset = self.start_overlap;
let max_offset = self.scrollable.magnitude();
let clamp_current_to_max = || clamp(target_offset, self.offset, max_offset);
let clamp_min_to_current = || clamp(target_offset, min_offset, self.offset);
let clamp_min_to_max = || clamp(target_offset, min_offset, max_offset);
if max_offset >= 0.0 {
if self.offset < min_offset { clamp_current_to_max() }
else if self.offset > max_offset { clamp_min_to_current() }
else { clamp_min_to_max() }
} else {
if self.offset > min_offset { clamp_current_to_max() }
else if self.offset < max_offset { clamp_min_to_current() }
else { clamp_min_to_max() }
}
}
pub fn kids_pos_offset(&self) -> Scalar {
let Bar { offset, scrollable, .. } = *self;
if scrollable.len() == 0.0 { 0.0 } else { -offset }
}
pub fn pos_offset_from_bar_offset(&self, bar_offset: Scalar, bar_len: Scalar) -> Scalar {
map_range(bar_offset, 0.0, bar_len, 0.0, self.scrollable.magnitude())
}
}
pub fn vertical_track(container: Rect, thickness: Scalar) -> Rect {
let w = thickness;
let x = container.x() + container.w() / 2.0 - w / 2.0;
Rect {
x: Range::from_pos_and_len(x, w),
y: container.y,
}
}
pub fn vertical_handle(bar: &Bar, track: Rect, total_len: Scalar) -> Rect {
let offset = partial_max(bar.start_overlap - bar.offset, 0.0);
let max_offset = bar.scrollable.len() - (bar.offset.abs() - offset);
let track_h = track.h();
let h = map_range(track_h, 0.0, total_len, 0.0, track_h);
let y = map_range(offset, 0.0, max_offset, track.top(), track.bottom() + h) - h / 2.0;
Rect {
x: track.x,
y: Range::from_pos_and_len(y, h),
}
}
pub fn horizontal_track(container: Rect, thickness: Scalar) -> Rect {
let h = thickness;
let y = container.y() - container.h() / 2.0 + h / 2.0;
Rect {
x: container.x,
y: Range::from_pos_and_len(y, h),
}
}
pub fn horizontal_handle(bar: &Bar, track: Rect, total_len: Scalar) -> Rect {
let offset = partial_max(bar.offset - bar.start_overlap, 0.0);
let max_offset = bar.scrollable.len() - (bar.offset.abs() - offset);
let track_w = track.w();
let w = map_range(track_w, 0.0, total_len, 0.0, track_w);
let x = map_range(offset, 0.0, max_offset, track.left(), track.right() - w) + w / 2.0;
Rect {
x: Range::from_pos_and_len(x, w),
y: track.y,
}
}
fn is_over_elem(track: Rect, handle: Rect, mouse: &Mouse, mouse_scalar: Scalar) -> Option<Elem> {
if handle.is_over(mouse.xy) {
Some(Elem::Handle(mouse_scalar))
} else if track.is_over(mouse.xy) {
Some(Elem::Track)
} else {
None
}
}
fn new_interaction(bar: &Bar,
is_over_elem: Option<Elem>,
mouse: &Mouse,
mouse_scalar: Scalar) -> Interaction
{
use self::Interaction::{Normal, Highlighted, Clicked};
if bar.scrollable.len() == 0.0 {
Normal
} else {
use self::Elem::Handle;
use mouse::ButtonPosition::{Down, Up};
match (is_over_elem, bar.interaction, mouse.left.position) {
(Some(_), Normal, Down) => Normal,
(Some(elem), _, Up) => Highlighted(elem),
(Some(_), Highlighted(_), Down) |
(_, Clicked(Handle(_)), Down) => Clicked(Handle(mouse_scalar)),
(_, Clicked(elem), Down) => Clicked(elem),
_ => Normal,
}
}
}
pub fn capture_mouse(prev: &State, new: &State) -> bool {
match (prev.maybe_vertical, new.maybe_vertical) {
(Some(ref prev_bar), Some(ref new_bar)) =>
match (prev_bar.interaction, new_bar.interaction) {
(Interaction::Highlighted(_), Interaction::Clicked(_)) => return true,
_ => (),
},
_ => (),
}
match (prev.maybe_horizontal, new.maybe_horizontal) {
(Some(ref prev_bar), Some(ref new_bar)) =>
match (prev_bar.interaction, new_bar.interaction) {
(Interaction::Highlighted(_), Interaction::Clicked(_)) => return true,
_ => (),
},
_ => (),
}
false
}
pub fn uncapture_mouse(prev: &State, new: &State) -> bool {
match (prev.maybe_vertical, new.maybe_vertical) {
(Some(ref prev_bar), Some(ref new_bar)) =>
match (prev_bar.interaction, new_bar.interaction) {
(Interaction::Clicked(_), Interaction::Highlighted(_)) |
(Interaction::Clicked(_), Interaction::Normal) => return true,
_ => (),
},
_ => (),
}
match (prev.maybe_horizontal, new.maybe_horizontal) {
(Some(ref prev_bar), Some(ref new_bar)) =>
match (prev_bar.interaction, new_bar.interaction) {
(Interaction::Clicked(_), Interaction::Highlighted(_)) |
(Interaction::Clicked(_), Interaction::Normal) => return true,
_ => (),
},
_ => (),
}
false
}
#[test]
fn test_bar_new_no_scroll() {
let visible = Range::new(-5.0, 5.0);
let kids = Range::new(-3.0, 3.0);
let maybe_bar = Bar::new_if_scrollable(visible, kids, None);
assert_eq!(maybe_bar, None);
}
#[test]
fn test_bar_new_no_scroll_rev_range() {
let visible = Range::new(5.0, -5.0);
let kids = Range::new(3.0, -3.0);
let maybe_bar = Bar::new_if_scrollable(visible, kids, None);
assert_eq!(maybe_bar, None);
}