scroll_amount.rs

 1use crate::Editor;
 2use serde::Deserialize;
 3use ui::{px, Pixels};
 4
 5#[derive(Debug, Clone, PartialEq, Deserialize)]
 6pub enum ScrollAmount {
 7    // Scroll N lines (positive is towards the end of the document)
 8    Line(f32),
 9    // Scroll N pages (positive is towards the end of the document)
10    Page(f32),
11}
12
13impl ScrollAmount {
14    pub fn lines(&self, editor: &mut Editor) -> f32 {
15        match self {
16            Self::Line(count) => *count,
17            Self::Page(count) => editor
18                .visible_line_count()
19                .map(|mut l| {
20                    // for full pages subtract one to leave an anchor line
21                    if count.abs() == 1.0 {
22                        l -= 1.0
23                    }
24                    (l * count).trunc()
25                })
26                .unwrap_or(0.),
27        }
28    }
29
30    pub fn pixels(&self, line_height: Pixels, height: Pixels) -> Pixels {
31        match self {
32            ScrollAmount::Line(x) => px(line_height.0 * x),
33            ScrollAmount::Page(x) => px(height.0 * x),
34        }
35    }
36}