actions.rs

  1use super::Axis;
  2use crate::{
  3    Autoscroll, Bias, Editor, EditorMode, NextScreen, ScrollAnchor, ScrollCursorBottom,
  4    ScrollCursorCenter, ScrollCursorTop,
  5};
  6use gpui::{actions, AppContext, Point, ViewContext};
  7
  8impl Editor {
  9    pub fn next_screen(&mut self, _: &NextScreen, cx: &mut ViewContext<Editor>) {
 10        if self.take_rename(true, cx).is_some() {
 11            return;
 12        }
 13
 14        // todo!()
 15        // if self.mouse_context_menu.read(cx).visible() {
 16        //     return None;
 17        // }
 18
 19        if matches!(self.mode, EditorMode::SingleLine) {
 20            cx.propagate();
 21            return;
 22        }
 23        self.request_autoscroll(Autoscroll::Next, cx);
 24    }
 25
 26    pub fn scroll(
 27        &mut self,
 28        scroll_position: Point<f32>,
 29        axis: Option<Axis>,
 30        cx: &mut ViewContext<Self>,
 31    ) {
 32        self.scroll_manager.update_ongoing_scroll(axis);
 33        self.set_scroll_position(scroll_position, cx);
 34    }
 35
 36    pub fn scroll_cursor_top(&mut self, _: &ScrollCursorTop, cx: &mut ViewContext<Editor>) {
 37        let snapshot = self.snapshot(cx).display_snapshot;
 38        let scroll_margin_rows = self.vertical_scroll_margin() as u32;
 39
 40        let mut new_screen_top = self.selections.newest_display(cx).head();
 41        *new_screen_top.row_mut() = new_screen_top.row().saturating_sub(scroll_margin_rows);
 42        *new_screen_top.column_mut() = 0;
 43        let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
 44        let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);
 45
 46        self.set_scroll_anchor(
 47            ScrollAnchor {
 48                anchor: new_anchor,
 49                offset: Default::default(),
 50            },
 51            cx,
 52        )
 53    }
 54
 55    pub fn scroll_cursor_center(&mut self, _: &ScrollCursorCenter, cx: &mut ViewContext<Editor>) {
 56        let snapshot = self.snapshot(cx).display_snapshot;
 57        let visible_rows = if let Some(visible_rows) = self.visible_line_count() {
 58            visible_rows as u32
 59        } else {
 60            return;
 61        };
 62
 63        let mut new_screen_top = self.selections.newest_display(cx).head();
 64        *new_screen_top.row_mut() = new_screen_top.row().saturating_sub(visible_rows / 2);
 65        *new_screen_top.column_mut() = 0;
 66        let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
 67        let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);
 68
 69        self.set_scroll_anchor(
 70            ScrollAnchor {
 71                anchor: new_anchor,
 72                offset: Default::default(),
 73            },
 74            cx,
 75        )
 76    }
 77
 78    pub fn scroll_cursor_bottom(&mut self, _: &ScrollCursorBottom, cx: &mut ViewContext<Editor>) {
 79        let snapshot = self.snapshot(cx).display_snapshot;
 80        let scroll_margin_rows = self.vertical_scroll_margin() as u32;
 81        let visible_rows = if let Some(visible_rows) = self.visible_line_count() {
 82            visible_rows as u32
 83        } else {
 84            return;
 85        };
 86
 87        let mut new_screen_top = self.selections.newest_display(cx).head();
 88        *new_screen_top.row_mut() = new_screen_top
 89            .row()
 90            .saturating_sub(visible_rows.saturating_sub(scroll_margin_rows));
 91        *new_screen_top.column_mut() = 0;
 92        let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
 93        let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);
 94
 95        self.set_scroll_anchor(
 96            ScrollAnchor {
 97                anchor: new_anchor,
 98                offset: Default::default(),
 99            },
100            cx,
101        )
102    }
103}