This commit is contained in:
Nathan Sobo
2023-11-02 22:56:04 -06:00
parent a731f8fb1e
commit f3b8a9d8c2
26 changed files with 1782 additions and 1665 deletions
+153 -150
View File
@@ -9,7 +9,7 @@ use crate::{
Anchor, DisplayPoint, Editor, EditorMode, Event, InlayHintRefreshReason, MultiBufferSnapshot,
ToPoint,
};
use gpui::{point, px, AppContext, Pixels, Styled, Task, ViewContext};
use gpui::{point, px, AppContext, Entity, Pixels, Styled, Task, ViewContext};
use language::{Bias, Point};
use std::{
cmp::Ordering,
@@ -39,18 +39,18 @@ pub struct ScrollAnchor {
impl ScrollAnchor {
fn new() -> Self {
Self {
offset: gpui::Point::zero(),
offset: gpui::Point::default(),
anchor: Anchor::min(),
}
}
pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> gpui::Point<Pixels> {
pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> gpui::Point<f32> {
let mut scroll_position = self.offset;
if self.anchor != Anchor::min() {
let scroll_top = self.anchor.to_display_point(snapshot).row() as f32;
scroll_position.set_y(scroll_top + scroll_position.y);
scroll_position.y = scroll_top + scroll_position.y;
} else {
scroll_position.set_y(0.);
scroll_position.y = 0.;
}
scroll_position
}
@@ -133,7 +133,7 @@ pub struct ScrollManager {
anchor: ScrollAnchor,
ongoing: OngoingScroll,
autoscroll_request: Option<(Autoscroll, bool)>,
last_autoscroll: Option<(gpui::Point<Pixels>, f32, f32, AutoscrollStrategy)>,
last_autoscroll: Option<(gpui::Point<f32>, f32, f32, AutoscrollStrategy)>,
show_scrollbars: bool,
hide_scrollbar_task: Option<Task<()>>,
visible_line_count: Option<f32>,
@@ -171,7 +171,7 @@ impl ScrollManager {
self.ongoing.axis = axis;
}
pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> gpui::Point<Pixels> {
pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> gpui::Point<f32> {
self.anchor.scroll_position(snapshot)
}
@@ -188,7 +188,7 @@ impl ScrollManager {
(
ScrollAnchor {
anchor: Anchor::min(),
offset: scroll_position.max(Point::zero()),
offset: scroll_position.max(&gpui::Point::default()),
},
0,
)
@@ -228,9 +228,9 @@ impl ScrollManager {
self.show_scrollbar(cx);
self.autoscroll_request.take();
if let Some(workspace_id) = workspace_id {
let item_id = cx.view_id();
let item_id = cx.view().entity_id();
cx.background()
cx.foreground_executor()
.spawn(async move {
DB.save_scroll_position(
item_id,
@@ -255,7 +255,9 @@ impl ScrollManager {
if cx.default_global::<ScrollbarAutoHide>().0 {
self.hide_scrollbar_task = Some(cx.spawn(|editor, mut cx| async move {
cx.background().timer(SCROLLBAR_SHOW_INTERVAL).await;
cx.background_executor()
.timer(SCROLLBAR_SHOW_INTERVAL)
.await;
editor
.update(&mut cx, |editor, cx| {
editor.scroll_manager.show_scrollbars = false;
@@ -287,160 +289,161 @@ impl ScrollManager {
}
// todo!()
// impl Editor {
// pub fn vertical_scroll_margin(&mut self) -> usize {
// self.scroll_manager.vertical_scroll_margin as usize
// }
impl Editor {
// pub fn vertical_scroll_margin(&mut self) -> usize {
// self.scroll_manager.vertical_scroll_margin as usize
// }
// pub fn set_vertical_scroll_margin(&mut self, margin_rows: usize, cx: &mut ViewContext<Self>) {
// self.scroll_manager.vertical_scroll_margin = margin_rows as f32;
// cx.notify();
// }
// pub fn set_vertical_scroll_margin(&mut self, margin_rows: usize, cx: &mut ViewContext<Self>) {
// self.scroll_manager.vertical_scroll_margin = margin_rows as f32;
// cx.notify();
// }
// pub fn visible_line_count(&self) -> Option<f32> {
// self.scroll_manager.visible_line_count
// }
// pub fn visible_line_count(&self) -> Option<f32> {
// self.scroll_manager.visible_line_count
// }
// pub(crate) fn set_visible_line_count(&mut self, lines: f32, cx: &mut ViewContext<Self>) {
// let opened_first_time = self.scroll_manager.visible_line_count.is_none();
// self.scroll_manager.visible_line_count = Some(lines);
// if opened_first_time {
// cx.spawn(|editor, mut cx| async move {
// editor
// .update(&mut cx, |editor, cx| {
// editor.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx)
// })
// .ok()
// })
// .detach()
// }
// }
// pub(crate) fn set_visible_line_count(&mut self, lines: f32, cx: &mut ViewContext<Self>) {
// let opened_first_time = self.scroll_manager.visible_line_count.is_none();
// self.scroll_manager.visible_line_count = Some(lines);
// if opened_first_time {
// cx.spawn(|editor, mut cx| async move {
// editor
// .update(&mut cx, |editor, cx| {
// editor.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx)
// })
// .ok()
// })
// .detach()
// }
// }
// pub fn set_scroll_position(
// &mut self,
// scroll_position: gpui::Point<Pixels>,
// cx: &mut ViewContext<Self>,
// ) {
// self.set_scroll_position_internal(scroll_position, true, false, cx);
// }
pub fn set_scroll_position(
&mut self,
scroll_position: gpui::Point<f32>,
cx: &mut ViewContext<Self>,
) {
self.set_scroll_position_internal(scroll_position, true, false, cx);
}
// pub(crate) fn set_scroll_position_internal(
// &mut self,
// scroll_position: gpui::Point<Pixels>,
// local: bool,
// autoscroll: bool,
// cx: &mut ViewContext<Self>,
// ) {
// let map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
pub(crate) fn set_scroll_position_internal(
&mut self,
scroll_position: gpui::Point<f32>,
local: bool,
autoscroll: bool,
cx: &mut ViewContext<Self>,
) {
let map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
// hide_hover(self, cx);
// let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1);
// self.scroll_manager.set_scroll_position(
// scroll_position,
// &map,
// local,
// autoscroll,
// workspace_id,
// cx,
// );
hide_hover(self, cx);
let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1);
self.scroll_manager.set_scroll_position(
scroll_position,
&map,
local,
autoscroll,
workspace_id,
cx,
);
// self.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx);
// }
// todo!()
// self.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx);
}
// pub fn scroll_position(&self, cx: &mut ViewContext<Self>) -> gpui::Point<Pixels> {
// let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
// self.scroll_manager.anchor.scroll_position(&display_map)
// }
// pub fn scroll_position(&self, cx: &mut ViewContext<Self>) -> gpui::Point<Pixels> {
// let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
// self.scroll_manager.anchor.scroll_position(&display_map)
// }
// pub fn set_scroll_anchor(&mut self, scroll_anchor: ScrollAnchor, cx: &mut ViewContext<Self>) {
// hide_hover(self, cx);
// let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1);
// let top_row = scroll_anchor
// .anchor
// .to_point(&self.buffer().read(cx).snapshot(cx))
// .row;
// self.scroll_manager
// .set_anchor(scroll_anchor, top_row, true, false, workspace_id, cx);
// }
// pub fn set_scroll_anchor(&mut self, scroll_anchor: ScrollAnchor, cx: &mut ViewContext<Self>) {
// hide_hover(self, cx);
// let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1);
// let top_row = scroll_anchor
// .anchor
// .to_point(&self.buffer().read(cx).snapshot(cx))
// .row;
// self.scroll_manager
// .set_anchor(scroll_anchor, top_row, true, false, workspace_id, cx);
// }
// pub(crate) fn set_scroll_anchor_remote(
// &mut self,
// scroll_anchor: ScrollAnchor,
// cx: &mut ViewContext<Self>,
// ) {
// hide_hover(self, cx);
// let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1);
// let top_row = scroll_anchor
// .anchor
// .to_point(&self.buffer().read(cx).snapshot(cx))
// .row;
// self.scroll_manager
// .set_anchor(scroll_anchor, top_row, false, false, workspace_id, cx);
// }
// pub(crate) fn set_scroll_anchor_remote(
// &mut self,
// scroll_anchor: ScrollAnchor,
// cx: &mut ViewContext<Self>,
// ) {
// hide_hover(self, cx);
// let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1);
// let top_row = scroll_anchor
// .anchor
// .to_point(&self.buffer().read(cx).snapshot(cx))
// .row;
// self.scroll_manager
// .set_anchor(scroll_anchor, top_row, false, false, workspace_id, cx);
// }
// pub fn scroll_screen(&mut self, amount: &ScrollAmount, cx: &mut ViewContext<Self>) {
// if matches!(self.mode, EditorMode::SingleLine) {
// cx.propagate_action();
// return;
// }
// pub fn scroll_screen(&mut self, amount: &ScrollAmount, cx: &mut ViewContext<Self>) {
// if matches!(self.mode, EditorMode::SingleLine) {
// cx.propagate_action();
// return;
// }
// if self.take_rename(true, cx).is_some() {
// return;
// }
// if self.take_rename(true, cx).is_some() {
// return;
// }
// let cur_position = self.scroll_position(cx);
// let new_pos = cur_position + point(0., amount.lines(self));
// self.set_scroll_position(new_pos, cx);
// }
// let cur_position = self.scroll_position(cx);
// let new_pos = cur_position + point(0., amount.lines(self));
// self.set_scroll_position(new_pos, cx);
// }
// /// Returns an ordering. The newest selection is:
// /// Ordering::Equal => on screen
// /// Ordering::Less => above the screen
// /// Ordering::Greater => below the screen
// pub fn newest_selection_on_screen(&self, cx: &mut AppContext) -> Ordering {
// let snapshot = self.display_map.update(cx, |map, cx| map.snapshot(cx));
// let newest_head = self
// .selections
// .newest_anchor()
// .head()
// .to_display_point(&snapshot);
// let screen_top = self
// .scroll_manager
// .anchor
// .anchor
// .to_display_point(&snapshot);
// /// Returns an ordering. The newest selection is:
// /// Ordering::Equal => on screen
// /// Ordering::Less => above the screen
// /// Ordering::Greater => below the screen
// pub fn newest_selection_on_screen(&self, cx: &mut AppContext) -> Ordering {
// let snapshot = self.display_map.update(cx, |map, cx| map.snapshot(cx));
// let newest_head = self
// .selections
// .newest_anchor()
// .head()
// .to_display_point(&snapshot);
// let screen_top = self
// .scroll_manager
// .anchor
// .anchor
// .to_display_point(&snapshot);
// if screen_top > newest_head {
// return Ordering::Less;
// }
// if screen_top > newest_head {
// return Ordering::Less;
// }
// if let Some(visible_lines) = self.visible_line_count() {
// if newest_head.row() < screen_top.row() + visible_lines as u32 {
// return Ordering::Equal;
// }
// }
// if let Some(visible_lines) = self.visible_line_count() {
// if newest_head.row() < screen_top.row() + visible_lines as u32 {
// return Ordering::Equal;
// }
// }
// Ordering::Greater
// }
// Ordering::Greater
// }
// pub fn read_scroll_position_from_db(
// &mut self,
// item_id: usize,
// workspace_id: WorkspaceId,
// cx: &mut ViewContext<Editor>,
// ) {
// let scroll_position = DB.get_scroll_position(item_id, workspace_id);
// if let Ok(Some((top_row, x, y))) = scroll_position {
// let top_anchor = self
// .buffer()
// .read(cx)
// .snapshot(cx)
// .anchor_at(Point::new(top_row as u32, 0), Bias::Left);
// let scroll_anchor = ScrollAnchor {
// offset: Point::new(x, y),
// anchor: top_anchor,
// };
// self.set_scroll_anchor(scroll_anchor, cx);
// }
// }
// }
// pub fn read_scroll_position_from_db(
// &mut self,
// item_id: usize,
// workspace_id: WorkspaceId,
// cx: &mut ViewContext<Editor>,
// ) {
// let scroll_position = DB.get_scroll_position(item_id, workspace_id);
// if let Ok(Some((top_row, x, y))) = scroll_position {
// let top_anchor = self
// .buffer()
// .read(cx)
// .snapshot(cx)
// .anchor_at(Point::new(top_row as u32, 0), Bias::Left);
// let scroll_anchor = ScrollAnchor {
// offset: Point::new(x, y),
// anchor: top_anchor,
// };
// self.set_scroll_anchor(scroll_anchor, cx);
// }
// }
}