Re-enable scrolling for EditorElement

Co-Authored-By: Julia <julia@zed.dev>
Co-Authored-By: Piotr <piotr@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-11-07 15:48:08 +01:00
parent 9cb8512603
commit b9e98c112f
4 changed files with 151 additions and 128 deletions

View file

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

View file

@ -9,8 +9,9 @@ use anyhow::Result;
use collections::{BTreeMap, HashMap}; use collections::{BTreeMap, HashMap};
use gpui::{ use gpui::{
black, hsla, point, px, relative, size, transparent_black, AnyElement, BorrowWindow, Bounds, black, hsla, point, px, relative, size, transparent_black, AnyElement, BorrowWindow, Bounds,
ContentMask, Corners, Edges, Element, Hsla, Line, Pixels, ShapedGlyph, Size, Style, TextRun, ContentMask, Corners, DispatchPhase, Edges, Element, ElementId, Hsla, Line, Pixels,
TextStyle, TextSystem, ViewContext, WindowContext, ScrollWheelEvent, ShapedGlyph, Size, StatefulInteraction, Style, TextRun, TextStyle,
TextSystem, ViewContext, WindowContext,
}; };
use itertools::Itertools; use itertools::Itertools;
use language::language_settings::ShowWhitespaceSetting; use language::language_settings::ShowWhitespaceSetting;
@ -464,39 +465,41 @@ impl EditorElement {
// true // true
// } // }
// fn scroll( fn scroll(
// editor: &mut Editor, editor: &mut Editor,
// position: gpui::Point<Pixels>, event: &ScrollWheelEvent,
// mut delta: gpui::Point<Pixels>, position_map: &PositionMap,
// precise: bool, bounds: Bounds<Pixels>,
// position_map: &PositionMap, cx: &mut ViewContext<Editor>,
// bounds: Bounds<Pixels>, ) -> bool {
// cx: &mut ViewContext<Editor>, if !bounds.contains_point(&event.position) {
// ) -> bool { return false;
// if !bounds.contains_point(position) { }
// return false;
// }
// let line_height = position_map.line_height; let line_height = position_map.line_height;
// let max_glyph_width = position_map.em_width; 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 { gpui::ScrollDelta::Lines(lines) => {
// //Trackpad //Not trackpad
// position_map.snapshot.ongoing_scroll.filter(&mut delta) let pixels = point(lines.x * max_glyph_width, lines.y * line_height);
// } else { (pixels, None)
// //Not trackpad }
// delta *= point(max_glyph_width, line_height); };
// None //Resets ongoing scroll
// };
// let scroll_position = position_map.snapshot.scroll_position(); let scroll_position = position_map.snapshot.scroll_position();
// let x = (scroll_position.x * max_glyph_width - delta.x) / max_glyph_width; let x = f32::from((scroll_position.x * max_glyph_width - delta.x) / max_glyph_width);
// let y = (scroll_position.y * line_height - delta.y) / line_height; let y = f32::from((scroll_position.y * line_height - delta.y) / line_height);
// let scroll_position = point(x, y).clamp(gpui::Point::<Pixels>::zero(), position_map.scroll_max); let scroll_position = point(x, y).clamp(&point(0., 0.), &position_map.scroll_max);
// editor.scroll(scroll_position, axis, cx); editor.scroll(scroll_position, axis, cx);
// true true
// } }
fn paint_background( fn paint_background(
&self, &self,
@ -951,7 +954,7 @@ impl EditorElement {
) )
} }
cx.stack(9999, |cx| { cx.stack(0, |cx| {
for cursor in cursors { for cursor in cursors {
cursor.paint(content_origin, cx); cursor.paint(content_origin, cx);
} }
@ -2573,6 +2576,20 @@ impl Element<Editor> for EditorElement {
cx: &mut gpui::ViewContext<Editor>, cx: &mut gpui::ViewContext<Editor>,
) { ) {
let layout = self.compute_layout(editor, cx, bounds); 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| { cx.with_content_mask(ContentMask { bounds }, |cx| {
let gutter_bounds = Bounds { let gutter_bounds = Bounds {
origin: bounds.origin, origin: bounds.origin,

View file

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

View file

@ -120,6 +120,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> { impl<T: Clone + Default + Debug> Clone for Point<T> {