1use serde::Deserialize;
2use ui::{Pixels, px};
3
4#[derive(Debug)]
5pub enum ScrollDirection {
6 Upwards,
7 Downwards,
8}
9
10impl ScrollDirection {
11 pub fn is_upwards(&self) -> bool {
12 matches!(self, ScrollDirection::Upwards)
13 }
14}
15
16#[derive(Debug, Clone, PartialEq, Deserialize)]
17pub enum ScrollAmount {
18 // Scroll N lines (positive is towards the end of the document)
19 Line(f32),
20 // Scroll N pages (positive is towards the end of the document)
21 Page(f32),
22}
23
24impl ScrollAmount {
25 pub fn lines(&self, mut visible_line_count: f32) -> f32 {
26 match self {
27 Self::Line(count) => *count,
28 Self::Page(count) => {
29 // for full pages subtract one to leave an anchor line
30 if self.is_full_page() {
31 visible_line_count -= 1.0
32 }
33 (visible_line_count * count).trunc()
34 }
35 }
36 }
37
38 pub fn pixels(&self, line_height: Pixels, height: Pixels) -> Pixels {
39 match self {
40 ScrollAmount::Line(x) => px(line_height.0 * x),
41 ScrollAmount::Page(x) => px(height.0 * x),
42 }
43 }
44
45 pub fn is_full_page(&self) -> bool {
46 match self {
47 ScrollAmount::Page(count) if count.abs() == 1.0 => true,
48 _ => false,
49 }
50 }
51
52 pub fn direction(&self) -> ScrollDirection {
53 match self {
54 Self::Line(amount) if amount.is_sign_positive() => ScrollDirection::Downwards,
55 Self::Page(amount) if amount.is_sign_positive() => ScrollDirection::Downwards,
56 _ => ScrollDirection::Upwards,
57 }
58 }
59}