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    LineUp,
10    LineDown,
11    HalfPageUp,
12    HalfPageDown,
13    PageUp,
14    PageDown,
15}
16
17impl ScrollAmount {
18    pub fn move_context_menu_selection(
19        &self,
20        editor: &mut Editor,
21        cx: &mut ViewContext<Editor>,
22    ) -> bool {
23        iife!({
24            let context_menu = editor.context_menu.as_mut()?;
25
26            match self {
27                Self::LineDown | Self::HalfPageDown => context_menu.select_next(cx),
28                Self::LineUp | Self::HalfPageUp => context_menu.select_prev(cx),
29                Self::PageDown => context_menu.select_last(cx),
30                Self::PageUp => context_menu.select_first(cx),
31            }
32            .then_some(())
33        })
34        .is_some()
35    }
36
37    pub fn lines(&self, editor: &mut Editor) -> f32 {
38        match self {
39            Self::LineDown => 1.,
40            Self::LineUp => -1.,
41            Self::HalfPageDown => editor.visible_line_count().map(|l| l / 2.).unwrap_or(1.),
42            Self::HalfPageUp => -editor.visible_line_count().map(|l| l / 2.).unwrap_or(1.),
43            // Minus 1. here so that there is a pivot line that stays on the screen
44            Self::PageDown => editor.visible_line_count().unwrap_or(1.) - 1.,
45            Self::PageUp => -editor.visible_line_count().unwrap_or(1.) - 1.,
46        }
47    }
48}