scroll_amount.rs

 1use gpui::ViewContext;
 2use serde::Deserialize;
 3use util::iife;
 4
 5use crate::Editor;
 6
 7#[derive(Clone, PartialEq, Deserialize)]
 8pub enum ScrollAmount {
 9    // Scroll N lines (positive is towards the end of the document)
10    Line(f32),
11    // Scroll N pages (positive is towards the end of the document)
12    Page(f32),
13}
14
15impl ScrollAmount {
16    pub fn move_context_menu_selection(
17        &self,
18        editor: &mut Editor,
19        cx: &mut ViewContext<Editor>,
20    ) -> bool {
21        iife!({
22            let context_menu = editor.context_menu.as_mut()?;
23
24            match self {
25                Self::Line(c) if *c > 0. => context_menu.select_next(cx),
26                Self::Line(_) => context_menu.select_prev(cx),
27                Self::Page(c) if *c > 0. => context_menu.select_last(cx),
28                Self::Page(_) => context_menu.select_first(cx),
29            }
30            .then_some(())
31        })
32        .is_some()
33    }
34
35    pub fn lines(&self, editor: &mut Editor) -> f32 {
36        match self {
37            Self::Line(count) => *count,
38            Self::Page(count) => editor
39                .visible_line_count()
40                // subtract one to leave an anchor line
41                // round towards zero (so page-up and page-down are symmetric)
42                .map(|l| (l * count).trunc() - count.signum())
43                .unwrap_or(0.),
44        }
45    }
46}