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
use crate::Editor;
use serde::Deserialize;

#[derive(Clone, PartialEq, Deserialize)]
pub enum ScrollAmount {
    // Scroll N lines (positive is towards the end of the document)
    Line(f32),
    // Scroll N pages (positive is towards the end of the document)
    Page(f32),
}

impl ScrollAmount {
    pub fn lines(&self, editor: &mut Editor) -> f32 {
        match self {
            Self::Line(count) => *count,
            Self::Page(count) => editor
                .visible_line_count()
                .map(|mut l| {
                    // for full pages subtract one to leave an anchor line
                    if count.abs() == 1.0 {
                        l -= 1.0
                    }
                    (l * count).trunc()
                })
                .unwrap_or(0.),
        }
    }
}