@@ -1,72 +1,31 @@
|
||||
use gpui::{actions, geometry::vector::Vector2F, AppContext, Axis, ViewContext};
|
||||
use language::Bias;
|
||||
|
||||
use crate::{Editor, EditorMode};
|
||||
|
||||
use super::{autoscroll::Autoscroll, scroll_amount::ScrollAmount, ScrollAnchor};
|
||||
|
||||
actions!(
|
||||
editor,
|
||||
[
|
||||
LineDown,
|
||||
LineUp,
|
||||
HalfPageDown,
|
||||
HalfPageUp,
|
||||
PageDown,
|
||||
PageUp,
|
||||
NextScreen,
|
||||
ScrollCursorTop,
|
||||
ScrollCursorCenter,
|
||||
ScrollCursorBottom,
|
||||
]
|
||||
);
|
||||
|
||||
pub fn init(cx: &mut AppContext) {
|
||||
cx.add_action(Editor::next_screen);
|
||||
cx.add_action(Editor::scroll_cursor_top);
|
||||
cx.add_action(Editor::scroll_cursor_center);
|
||||
cx.add_action(Editor::scroll_cursor_bottom);
|
||||
cx.add_action(|this: &mut Editor, _: &LineDown, cx| {
|
||||
this.scroll_screen(&ScrollAmount::Line(1.), cx)
|
||||
});
|
||||
cx.add_action(|this: &mut Editor, _: &LineUp, cx| {
|
||||
this.scroll_screen(&ScrollAmount::Line(-1.), cx)
|
||||
});
|
||||
cx.add_action(|this: &mut Editor, _: &HalfPageDown, cx| {
|
||||
this.scroll_screen(&ScrollAmount::Page(0.5), cx)
|
||||
});
|
||||
cx.add_action(|this: &mut Editor, _: &HalfPageUp, cx| {
|
||||
this.scroll_screen(&ScrollAmount::Page(-0.5), cx)
|
||||
});
|
||||
cx.add_action(|this: &mut Editor, _: &PageDown, cx| {
|
||||
this.scroll_screen(&ScrollAmount::Page(1.), cx)
|
||||
});
|
||||
cx.add_action(|this: &mut Editor, _: &PageUp, cx| {
|
||||
this.scroll_screen(&ScrollAmount::Page(-1.), cx)
|
||||
});
|
||||
}
|
||||
use super::Axis;
|
||||
use crate::{
|
||||
Autoscroll, Bias, Editor, EditorMode, NextScreen, ScrollAnchor, ScrollCursorBottom,
|
||||
ScrollCursorCenter, ScrollCursorTop,
|
||||
};
|
||||
use gpui::{Point, ViewContext};
|
||||
|
||||
impl Editor {
|
||||
pub fn next_screen(&mut self, _: &NextScreen, cx: &mut ViewContext<Editor>) -> Option<()> {
|
||||
pub fn next_screen(&mut self, _: &NextScreen, cx: &mut ViewContext<Editor>) {
|
||||
if self.take_rename(true, cx).is_some() {
|
||||
return None;
|
||||
return;
|
||||
}
|
||||
|
||||
if self.mouse_context_menu.read(cx).visible() {
|
||||
return None;
|
||||
}
|
||||
// todo!()
|
||||
// if self.mouse_context_menu.read(cx).visible() {
|
||||
// return None;
|
||||
// }
|
||||
|
||||
if matches!(self.mode, EditorMode::SingleLine) {
|
||||
cx.propagate_action();
|
||||
return None;
|
||||
cx.propagate();
|
||||
return;
|
||||
}
|
||||
self.request_autoscroll(Autoscroll::Next, cx);
|
||||
Some(())
|
||||
}
|
||||
|
||||
pub fn scroll(
|
||||
&mut self,
|
||||
scroll_position: Vector2F,
|
||||
scroll_position: Point<f32>,
|
||||
axis: Option<Axis>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
@@ -74,17 +33,17 @@ impl Editor {
|
||||
self.set_scroll_position(scroll_position, cx);
|
||||
}
|
||||
|
||||
fn scroll_cursor_top(editor: &mut Editor, _: &ScrollCursorTop, cx: &mut ViewContext<Editor>) {
|
||||
let snapshot = editor.snapshot(cx).display_snapshot;
|
||||
let scroll_margin_rows = editor.vertical_scroll_margin() as u32;
|
||||
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;
|
||||
|
||||
let mut new_screen_top = editor.selections.newest_display(cx).head();
|
||||
let mut new_screen_top = self.selections.newest_display(cx).head();
|
||||
*new_screen_top.row_mut() = new_screen_top.row().saturating_sub(scroll_margin_rows);
|
||||
*new_screen_top.column_mut() = 0;
|
||||
let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
|
||||
let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);
|
||||
|
||||
editor.set_scroll_anchor(
|
||||
self.set_scroll_anchor(
|
||||
ScrollAnchor {
|
||||
anchor: new_anchor,
|
||||
offset: Default::default(),
|
||||
@@ -93,25 +52,21 @@ impl Editor {
|
||||
)
|
||||
}
|
||||
|
||||
fn scroll_cursor_center(
|
||||
editor: &mut Editor,
|
||||
_: &ScrollCursorCenter,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
let snapshot = editor.snapshot(cx).display_snapshot;
|
||||
let visible_rows = if let Some(visible_rows) = editor.visible_line_count() {
|
||||
pub fn scroll_cursor_center(&mut self, _: &ScrollCursorCenter, cx: &mut ViewContext<Editor>) {
|
||||
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 mut new_screen_top = editor.selections.newest_display(cx).head();
|
||||
let mut new_screen_top = self.selections.newest_display(cx).head();
|
||||
*new_screen_top.row_mut() = new_screen_top.row().saturating_sub(visible_rows / 2);
|
||||
*new_screen_top.column_mut() = 0;
|
||||
let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
|
||||
let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);
|
||||
|
||||
editor.set_scroll_anchor(
|
||||
self.set_scroll_anchor(
|
||||
ScrollAnchor {
|
||||
anchor: new_anchor,
|
||||
offset: Default::default(),
|
||||
@@ -120,20 +75,16 @@ impl Editor {
|
||||
)
|
||||
}
|
||||
|
||||
fn scroll_cursor_bottom(
|
||||
editor: &mut Editor,
|
||||
_: &ScrollCursorBottom,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) {
|
||||
let snapshot = editor.snapshot(cx).display_snapshot;
|
||||
let scroll_margin_rows = editor.vertical_scroll_margin() as u32;
|
||||
let visible_rows = if let Some(visible_rows) = editor.visible_line_count() {
|
||||
pub fn scroll_cursor_bottom(&mut self, _: &ScrollCursorBottom, cx: &mut ViewContext<Editor>) {
|
||||
let snapshot = self.snapshot(cx).display_snapshot;
|
||||
let scroll_margin_rows = self.vertical_scroll_margin() as u32;
|
||||
let visible_rows = if let Some(visible_rows) = self.visible_line_count() {
|
||||
visible_rows as u32
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
let mut new_screen_top = editor.selections.newest_display(cx).head();
|
||||
let mut new_screen_top = self.selections.newest_display(cx).head();
|
||||
*new_screen_top.row_mut() = new_screen_top
|
||||
.row()
|
||||
.saturating_sub(visible_rows.saturating_sub(scroll_margin_rows));
|
||||
@@ -141,7 +92,7 @@ impl Editor {
|
||||
let new_screen_top = new_screen_top.to_offset(&snapshot, Bias::Left);
|
||||
let new_anchor = snapshot.buffer_snapshot.anchor_before(new_screen_top);
|
||||
|
||||
editor.set_scroll_anchor(
|
||||
self.set_scroll_anchor(
|
||||
ScrollAnchor {
|
||||
anchor: new_anchor,
|
||||
offset: Default::default(),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::cmp;
|
||||
use std::{cmp, f32};
|
||||
|
||||
use gpui::ViewContext;
|
||||
use gpui::{px, Pixels, ViewContext};
|
||||
use language::Point;
|
||||
|
||||
use crate::{display_map::ToDisplayPoint, Editor, EditorMode, LineWithInvisibles};
|
||||
@@ -48,11 +48,11 @@ impl AutoscrollStrategy {
|
||||
impl Editor {
|
||||
pub fn autoscroll_vertically(
|
||||
&mut self,
|
||||
viewport_height: f32,
|
||||
line_height: f32,
|
||||
viewport_height: Pixels,
|
||||
line_height: Pixels,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) -> bool {
|
||||
let visible_lines = viewport_height / line_height;
|
||||
let visible_lines = f32::from(viewport_height / line_height);
|
||||
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
|
||||
let mut scroll_position = self.scroll_manager.scroll_position(&display_map);
|
||||
let max_scroll_top = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
|
||||
@@ -60,8 +60,8 @@ impl Editor {
|
||||
} else {
|
||||
display_map.max_point().row() as f32
|
||||
};
|
||||
if scroll_position.y() > max_scroll_top {
|
||||
scroll_position.set_y(max_scroll_top);
|
||||
if scroll_position.y > max_scroll_top {
|
||||
scroll_position.y = max_scroll_top;
|
||||
self.set_scroll_position(scroll_position, cx);
|
||||
}
|
||||
|
||||
@@ -136,31 +136,31 @@ impl Editor {
|
||||
let margin = margin.min(self.scroll_manager.vertical_scroll_margin);
|
||||
let target_top = (target_top - margin).max(0.0);
|
||||
let target_bottom = target_bottom + margin;
|
||||
let start_row = scroll_position.y();
|
||||
let start_row = scroll_position.y;
|
||||
let end_row = start_row + visible_lines;
|
||||
|
||||
let needs_scroll_up = target_top < start_row;
|
||||
let needs_scroll_down = target_bottom >= end_row;
|
||||
|
||||
if needs_scroll_up && !needs_scroll_down {
|
||||
scroll_position.set_y(target_top);
|
||||
scroll_position.y = target_top;
|
||||
self.set_scroll_position_internal(scroll_position, local, true, cx);
|
||||
}
|
||||
if !needs_scroll_up && needs_scroll_down {
|
||||
scroll_position.set_y(target_bottom - visible_lines);
|
||||
scroll_position.y = target_bottom - visible_lines;
|
||||
self.set_scroll_position_internal(scroll_position, local, true, cx);
|
||||
}
|
||||
}
|
||||
AutoscrollStrategy::Center => {
|
||||
scroll_position.set_y((target_top - margin).max(0.0));
|
||||
scroll_position.y = (target_top - margin).max(0.0);
|
||||
self.set_scroll_position_internal(scroll_position, local, true, cx);
|
||||
}
|
||||
AutoscrollStrategy::Top => {
|
||||
scroll_position.set_y((target_top).max(0.0));
|
||||
scroll_position.y = (target_top).max(0.0);
|
||||
self.set_scroll_position_internal(scroll_position, local, true, cx);
|
||||
}
|
||||
AutoscrollStrategy::Bottom => {
|
||||
scroll_position.set_y((target_bottom - visible_lines).max(0.0));
|
||||
scroll_position.y = (target_bottom - visible_lines).max(0.0);
|
||||
self.set_scroll_position_internal(scroll_position, local, true, cx);
|
||||
}
|
||||
}
|
||||
@@ -178,9 +178,9 @@ impl Editor {
|
||||
pub fn autoscroll_horizontally(
|
||||
&mut self,
|
||||
start_row: u32,
|
||||
viewport_width: f32,
|
||||
scroll_width: f32,
|
||||
max_glyph_width: f32,
|
||||
viewport_width: Pixels,
|
||||
scroll_width: Pixels,
|
||||
max_glyph_width: Pixels,
|
||||
layouts: &[LineWithInvisibles],
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> bool {
|
||||
@@ -191,11 +191,11 @@ impl Editor {
|
||||
let mut target_right;
|
||||
|
||||
if self.highlighted_rows.is_some() {
|
||||
target_left = 0.0_f32;
|
||||
target_right = 0.0_f32;
|
||||
target_left = px(0.);
|
||||
target_right = px(0.);
|
||||
} else {
|
||||
target_left = std::f32::INFINITY;
|
||||
target_right = 0.0_f32;
|
||||
target_left = px(f32::INFINITY);
|
||||
target_right = px(0.);
|
||||
for selection in selections {
|
||||
let head = selection.head().to_display_point(&display_map);
|
||||
if head.row() >= start_row && head.row() < start_row + layouts.len() as u32 {
|
||||
@@ -222,20 +222,15 @@ impl Editor {
|
||||
return false;
|
||||
}
|
||||
|
||||
let scroll_left = self.scroll_manager.anchor.offset.x() * max_glyph_width;
|
||||
let scroll_left = self.scroll_manager.anchor.offset.x * max_glyph_width;
|
||||
let scroll_right = scroll_left + viewport_width;
|
||||
|
||||
if target_left < scroll_left {
|
||||
self.scroll_manager
|
||||
.anchor
|
||||
.offset
|
||||
.set_x(target_left / max_glyph_width);
|
||||
self.scroll_manager.anchor.offset.x = (target_left / max_glyph_width).into();
|
||||
true
|
||||
} else if target_right > scroll_right {
|
||||
self.scroll_manager
|
||||
.anchor
|
||||
.offset
|
||||
.set_x((target_right - viewport_width) / max_glyph_width);
|
||||
self.scroll_manager.anchor.offset.x =
|
||||
((target_right - viewport_width) / max_glyph_width).into();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
|
||||
Reference in New Issue
Block a user