Implement scrolling for editor2 (#3251)

Release Notes:

- N/A
This commit is contained in:
Antonio Scandurra 2023-11-07 17:53:57 +01:00 committed by GitHub
commit 64b899c68c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 402 additions and 377 deletions

View File

@ -36,9 +36,9 @@ pub use element::{
use futures::FutureExt;
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
div, px, AnyElement, AppContext, BackgroundExecutor, Context, Div, Element, EventEmitter,
FocusHandle, FontStyle, FontWeight, Hsla, Model, Pixels, Render, Styled, Subscription, Task,
TextStyle, View, ViewContext, VisualContext, WeakView, WindowContext,
div, px, AnyElement, AppContext, BackgroundExecutor, Context, Div, Element, Entity,
EventEmitter, FocusHandle, FontStyle, FontWeight, Hsla, Model, Pixels, Render, Styled,
Subscription, Task, TextStyle, View, ViewContext, VisualContext, WeakView, WindowContext,
};
use highlight_matching_bracket::refresh_matching_bracket_highlights;
use hover_popover::{hide_hover, HoverState};

View File

@ -9,8 +9,9 @@ use anyhow::Result;
use collections::{BTreeMap, HashMap};
use gpui::{
black, hsla, point, px, relative, size, transparent_black, AnyElement, BorrowWindow, Bounds,
ContentMask, Corners, Edges, Element, Hsla, Line, Pixels, ShapedGlyph, Size, Style, TextRun,
TextStyle, TextSystem, ViewContext, WindowContext,
ContentMask, Corners, DispatchPhase, Edges, Element, ElementId, Hsla, Line, Pixels,
ScrollWheelEvent, ShapedGlyph, Size, StatefulInteraction, Style, TextRun, TextStyle,
TextSystem, ViewContext, WindowContext,
};
use itertools::Itertools;
use language::language_settings::ShowWhitespaceSetting;
@ -464,39 +465,41 @@ impl EditorElement {
// true
// }
// fn scroll(
// editor: &mut Editor,
// position: gpui::Point<Pixels>,
// mut delta: gpui::Point<Pixels>,
// precise: bool,
// position_map: &PositionMap,
// bounds: Bounds<Pixels>,
// cx: &mut ViewContext<Editor>,
// ) -> bool {
// if !bounds.contains_point(position) {
// return false;
// }
fn scroll(
editor: &mut Editor,
event: &ScrollWheelEvent,
position_map: &PositionMap,
bounds: Bounds<Pixels>,
cx: &mut ViewContext<Editor>,
) -> bool {
if !bounds.contains_point(&event.position) {
return false;
}
// let line_height = position_map.line_height;
// let max_glyph_width = position_map.em_width;
let line_height = position_map.line_height;
let max_glyph_width = position_map.em_width;
let (delta, axis) = match event.delta {
gpui::ScrollDelta::Pixels(mut pixels) => {
//Trackpad
let axis = position_map.snapshot.ongoing_scroll.filter(&mut pixels);
(pixels, axis)
}
// let axis = if precise {
// //Trackpad
// position_map.snapshot.ongoing_scroll.filter(&mut delta)
// } else {
// //Not trackpad
// delta *= point(max_glyph_width, line_height);
// None //Resets ongoing scroll
// };
gpui::ScrollDelta::Lines(lines) => {
//Not trackpad
let pixels = point(lines.x * max_glyph_width, lines.y * line_height);
(pixels, None)
}
};
// let scroll_position = position_map.snapshot.scroll_position();
// let x = (scroll_position.x * max_glyph_width - delta.x) / max_glyph_width;
// let y = (scroll_position.y * line_height - delta.y) / line_height;
// let scroll_position = point(x, y).clamp(gpui::Point::<Pixels>::zero(), position_map.scroll_max);
// editor.scroll(scroll_position, axis, cx);
let scroll_position = position_map.snapshot.scroll_position();
let x = f32::from((scroll_position.x * max_glyph_width - delta.x) / max_glyph_width);
let y = f32::from((scroll_position.y * line_height - delta.y) / line_height);
let scroll_position = point(x, y).clamp(&point(0., 0.), &position_map.scroll_max);
editor.scroll(scroll_position, axis, cx);
// true
// }
true
}
fn paint_background(
&self,
@ -774,6 +777,7 @@ impl EditorElement {
let line_end_overshoot = 0.15 * layout.position_map.line_height;
let whitespace_setting = editor.buffer.read(cx).settings_at(0, cx).show_whitespaces;
cx.with_content_mask(ContentMask { bounds }, |cx| {
// todo!("cursor region")
// cx.scene().push_cursor_region(CursorRegion {
// bounds,
@ -883,8 +887,8 @@ impl EditorElement {
let cursor_column = cursor_position.column() as usize;
let cursor_character_x = cursor_row_layout.x_for_index(cursor_column);
let mut block_width =
cursor_row_layout.x_for_index(cursor_column + 1) - cursor_character_x;
let mut block_width = cursor_row_layout.x_for_index(cursor_column + 1)
- cursor_character_x;
if block_width == Pixels::ZERO {
block_width = layout.position_map.em_width;
}
@ -951,7 +955,7 @@ impl EditorElement {
)
}
cx.stack(9999, |cx| {
cx.stack(0, |cx| {
for cursor in cursors {
cursor.paint(content_origin, cx);
}
@ -1065,6 +1069,7 @@ impl EditorElement {
// cx.scene().pop_stacking_context();
// }
})
}
fn scrollbar_left(&self, bounds: &Bounds<Pixels>) -> Pixels {
@ -2573,6 +2578,20 @@ impl Element<Editor> for EditorElement {
cx: &mut gpui::ViewContext<Editor>,
) {
let layout = self.compute_layout(editor, cx, bounds);
cx.on_mouse_event({
let position_map = layout.position_map.clone();
move |editor, event: &ScrollWheelEvent, phase, cx| {
if phase != DispatchPhase::Bubble {
return;
}
if Self::scroll(editor, event, &position_map, bounds, cx) {
cx.stop_propagation();
}
}
});
cx.with_content_mask(ContentMask { bounds }, |cx| {
let gutter_bounds = Bounds {
origin: bounds.origin,

View File

@ -1,4 +1,6 @@
use gpui::AppContext;
use super::Axis;
use crate::Editor;
use gpui::{AppContext, Point, ViewContext};
// actions!(
// editor,
@ -42,7 +44,7 @@ pub fn init(cx: &mut AppContext) {
// });
}
// impl Editor {
impl Editor {
// pub fn next_screen(&mut self, _: &NextScreen, cx: &mut ViewContext<Editor>) -> Option<()> {
// if self.take_rename(true, cx).is_some() {
// return None;
@ -60,15 +62,15 @@ pub fn init(cx: &mut AppContext) {
// Some(())
// }
// pub fn scroll(
// &mut self,
// scroll_position: Vector2F,
// axis: Option<Axis>,
// cx: &mut ViewContext<Self>,
// ) {
// self.scroll_manager.update_ongoing_scroll(axis);
// self.set_scroll_position(scroll_position, cx);
// }
pub fn scroll(
&mut self,
scroll_position: Point<f32>,
axis: Option<Axis>,
cx: &mut ViewContext<Self>,
) {
self.scroll_manager.update_ongoing_scroll(axis);
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;
@ -145,4 +147,4 @@ pub fn init(cx: &mut AppContext) {
// cx,
// )
// }
// }
}

View File

@ -124,6 +124,10 @@ where
},
}
}
pub fn clamp(&self, min: &Self, max: &Self) -> Self {
self.max(min).min(max)
}
}
impl<T: Clone + Default + Debug> Clone for Point<T> {