actions.rs

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