Into the woods
This commit is contained in:
+153
-148
@@ -9,7 +9,7 @@ use crate::{
|
||||
Anchor, DisplayPoint, Editor, EditorMode, Event, InlayHintRefreshReason, MultiBufferSnapshot,
|
||||
ToPoint,
|
||||
};
|
||||
use gpui::{point, AppContext, Pixels, Task, ViewContext};
|
||||
use gpui::{point, px, AppContext, Pixels, Styled, Task, ViewContext};
|
||||
use language::{Bias, Point};
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
@@ -44,7 +44,7 @@ impl ScrollAnchor {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> Point<Pixels> {
|
||||
pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> gpui::Point<Pixels> {
|
||||
let mut scroll_position = self.offset;
|
||||
if self.anchor != Anchor::min() {
|
||||
let scroll_top = self.anchor.to_display_point(snapshot).row() as f32;
|
||||
@@ -80,13 +80,13 @@ impl OngoingScroll {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn filter(&self, delta: &mut Point<Pixels>) -> Option<Axis> {
|
||||
pub fn filter(&self, delta: &mut gpui::Point<Pixels>) -> Option<Axis> {
|
||||
const UNLOCK_PERCENT: f32 = 1.9;
|
||||
const UNLOCK_LOWER_BOUND: f32 = 6.;
|
||||
let mut axis = self.axis;
|
||||
|
||||
let x = delta.x().abs();
|
||||
let y = delta.y().abs();
|
||||
let x = delta.x.abs();
|
||||
let y = delta.y.abs();
|
||||
let duration = Instant::now().duration_since(self.last_event);
|
||||
if duration > SCROLL_EVENT_SEPARATION {
|
||||
//New ongoing scroll will start, determine axis
|
||||
@@ -115,8 +115,12 @@ impl OngoingScroll {
|
||||
}
|
||||
|
||||
match axis {
|
||||
Some(Axis::Vertical) => *delta = point(0., delta.y()),
|
||||
Some(Axis::Horizontal) => *delta = point(delta.x(), 0.),
|
||||
Some(Axis::Vertical) => {
|
||||
*delta = point(pk(0.), delta.y());
|
||||
}
|
||||
Some(Axis::Horizontal) => {
|
||||
*delta = point(delta.x(), px(0.));
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
@@ -167,13 +171,13 @@ impl ScrollManager {
|
||||
self.ongoing.axis = axis;
|
||||
}
|
||||
|
||||
pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> Point<Pixels> {
|
||||
pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> gpui::Point<Pixels> {
|
||||
self.anchor.scroll_position(snapshot)
|
||||
}
|
||||
|
||||
fn set_scroll_position(
|
||||
&mut self,
|
||||
scroll_position: Point<Pixels>,
|
||||
scroll_position: gpui::Point<Pixels>,
|
||||
map: &DisplaySnapshot,
|
||||
local: bool,
|
||||
autoscroll: bool,
|
||||
@@ -282,160 +286,161 @@ impl ScrollManager {
|
||||
}
|
||||
}
|
||||
|
||||
impl Editor {
|
||||
pub fn vertical_scroll_margin(&mut self) -> usize {
|
||||
self.scroll_manager.vertical_scroll_margin as usize
|
||||
}
|
||||
// todo!()
|
||||
// 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: 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<Pixels>,
|
||||
// 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: 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<Pixels>,
|
||||
// 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);
|
||||
}
|
||||
// self.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx);
|
||||
// }
|
||||
|
||||
pub fn scroll_position(&self, cx: &mut ViewContext<Self>) -> 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);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user