Allow to cycle through center/top/bot scroll positions (#16134)

On top of `editor::ScrollCursorCenter`, `editor::ScrollCursorTop`,
`editor::ScrollCursorBottom` actions, adds an
`editor::ScrollCursorCenterTopBottom` one, that allows using a single
keybinding to scroll between positions on the screen.

The implementation matches a corresponding Emacs feature: there's a
timeout (1s) that is kept after every switch, to allow continuously
changing the positions, center (initial) -> top -> bottom
Scrolling behavior is the same as the existing actions (e.g. editor will
ignore scroll to bottom, if there's not enough space above).

After 1s, next position is reset to the initial, center, one.


Release Notes:

- Added an `editor::ScrollCursorCenterTopBottom` action for toggling
scroll position with a single keybinding

---------

Co-authored-by: Alex Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
Kirill Bulatov
2024-08-13 00:32:30 +03:00
committed by GitHub
co-authored by Alex Kladov
parent 98516b5527
commit 3bebb8b401
5 changed files with 153 additions and 2 deletions
+57 -2
View File
@@ -1,7 +1,8 @@
use super::Axis;
use crate::{
Autoscroll, Bias, Editor, EditorMode, NextScreen, ScrollAnchor, ScrollCursorBottom,
ScrollCursorCenter, ScrollCursorTop,
Autoscroll, Bias, Editor, EditorMode, NextScreen, NextScrollCursorCenterTopBottom,
ScrollAnchor, ScrollCursorBottom, ScrollCursorCenter, ScrollCursorCenterTopBottom,
ScrollCursorTop, SCROLL_CENTER_TOP_BOTTOM_DEBOUNCE_TIMEOUT,
};
use gpui::{Point, ViewContext};
@@ -32,6 +33,60 @@ impl Editor {
self.set_scroll_position(scroll_position, cx);
}
pub fn scroll_cursor_center_top_bottom(
&mut self,
_: &ScrollCursorCenterTopBottom,
cx: &mut ViewContext<Self>,
) {
let snapshot = self.snapshot(cx).display_snapshot;
let visible_rows = if let Some(visible_rows) = self.visible_line_count() {
visible_rows as u32
} else {
return;
};
let scroll_margin_rows = self.vertical_scroll_margin() as u32;
let mut new_screen_top = self.selections.newest_display(cx).head();
*new_screen_top.column_mut() = 0;
match self.next_scroll_position {
NextScrollCursorCenterTopBottom::Center => {
*new_screen_top.row_mut() = new_screen_top.row().0.saturating_sub(visible_rows / 2);
}
NextScrollCursorCenterTopBottom::Top => {
*new_screen_top.row_mut() =
new_screen_top.row().0.saturating_sub(scroll_margin_rows);
}
NextScrollCursorCenterTopBottom::Bottom => {
*new_screen_top.row_mut() = new_screen_top
.row()
.0
.saturating_sub(visible_rows.saturating_sub(scroll_margin_rows));
}
}
self.set_scroll_anchor(
ScrollAnchor {
anchor: snapshot
.buffer_snapshot
.anchor_before(new_screen_top.to_offset(&snapshot, Bias::Left)),
offset: Default::default(),
},
cx,
);
self.next_scroll_position = self.next_scroll_position.next();
self._scroll_cursor_center_top_bottom_task =
cx.spawn(|editor, mut cx: gpui::AsyncWindowContext| async move {
cx.background_executor()
.timer(SCROLL_CENTER_TOP_BOTTOM_DEBOUNCE_TIMEOUT)
.await;
editor
.update(&mut cx, |editor, _| {
editor.next_scroll_position = NextScrollCursorCenterTopBottom::default();
})
.ok();
});
}
pub fn scroll_cursor_top(&mut self, _: &ScrollCursorTop, cx: &mut ViewContext<Editor>) {
let snapshot = self.snapshot(cx).display_snapshot;
let scroll_margin_rows = self.vertical_scroll_margin() as u32;