Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/agent_ui/src/conversation_view/thread_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,14 @@ fn render_cat_numbered_code_block(
// `restrict_scroll_to_axis` then keeps vertical wheel events flowing through
// to the outer thread scroller. This mirrors the standard markdown
// code-block path in `crates/markdown/src/markdown.rs`.
let mut code_scroll = div()
let code_scroll = div()
.id(code_scroll_id)
.flex()
.flex_1()
.min_w_0()
.overflow_x_scroll()
.restrict_scroll_to_axis()
.child(div().flex_none().child(code_text));
code_scroll.style().restrict_scroll_to_axis = Some(true);

container
.child(
Expand Down
4 changes: 1 addition & 3 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ use project::{
use rand::seq::SliceRandom;
use regex::Regex;
use rpc::{ErrorCode, ErrorExt, proto::PeerId};
use scroll::{Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager, SharedScrollAnchor};
use scroll::{Autoscroll, ScrollAnchor, ScrollManager, SharedScrollAnchor};
use selections_collection::{MutableSelectionsCollection, SelectionsCollection};
use serde::{Deserialize, Serialize};
use settings::{
Expand Down Expand Up @@ -1222,7 +1222,6 @@ pub struct EditorSnapshot {
pub placeholder_display_snapshot: Option<DisplaySnapshot>,
is_focused: bool,
scroll_anchor: SharedScrollAnchor,
ongoing_scroll: OngoingScroll,
current_line_highlight: CurrentLineHighlight,
gutter_hovered: bool,
semantic_tokens_enabled: bool,
Expand Down Expand Up @@ -3003,7 +3002,6 @@ impl Editor {
.placeholder_display_map
.as_ref()
.map(|display_map| display_map.update(cx, |map, cx| map.snapshot(cx))),
ongoing_scroll: self.scroll_manager.ongoing_scroll(),
is_focused: self.focus_handle.is_focused(window),
current_line_highlight: self
.current_line_highlight
Expand Down
17 changes: 8 additions & 9 deletions crates/editor/src/editor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28926,7 +28926,7 @@ async fn test_expand_first_line_diff_hunk_keeps_deleted_lines_visible(
cx.set_state("ˇnew\nsecond\nthird\n");
cx.set_head_text("old\nsecond\nthird\n");
cx.update_editor(|editor, window, cx| {
editor.scroll(gpui::Point { x: 0., y: 0. }, None, window, cx);
editor.scroll(gpui::Point { x: 0., y: 0. }, window, cx);
});
executor.run_until_parked();
assert_eq!(cx.update_editor(|e, _, cx| e.scroll_position(cx)).y, 0.0);
Expand Down Expand Up @@ -37483,7 +37483,7 @@ async fn test_sticky_scroll(cx: &mut TestAppContext) {

let mut sticky_headers = |offset: ScrollOffset| {
cx.update_editor(|e, window, cx| {
e.scroll(gpui::Point { x: 0., y: offset }, None, window, cx);
e.scroll(gpui::Point { x: 0., y: offset }, window, cx);
});
cx.run_until_parked();
cx.update_editor(|e, window, cx| {
Expand Down Expand Up @@ -37572,7 +37572,7 @@ async fn test_sticky_scroll_with_decoration_prefix_in_item(cx: &mut TestAppConte

let mut sticky_headers = |offset: ScrollOffset| {
cx.update_editor(|e, window, cx| {
e.scroll(gpui::Point { x: 0., y: offset }, None, window, cx);
e.scroll(gpui::Point { x: 0., y: offset }, window, cx);
});
cx.run_until_parked();
cx.update_editor(|e, window, cx| {
Expand Down Expand Up @@ -37635,7 +37635,7 @@ async fn test_sticky_scroll_anchors_multiline_c_signature_on_name_row(cx: &mut T

let mut sticky_headers = |offset: ScrollOffset| {
cx.update_editor(|editor, window, cx| {
editor.scroll(gpui::Point { x: 0., y: offset }, None, window, cx);
editor.scroll(gpui::Point { x: 0., y: offset }, window, cx);
});
cx.run_until_parked();
cx.update_editor(|editor, window, cx| {
Expand Down Expand Up @@ -37718,7 +37718,7 @@ async fn test_sticky_scroll_with_expanded_deleted_diff_hunks(

let mut sticky_headers = |offset: ScrollOffset| {
cx.update_editor(|e, window, cx| {
e.scroll(gpui::Point { x: 0., y: offset }, None, window, cx);
e.scroll(gpui::Point { x: 0., y: offset }, window, cx);
});
cx.run_until_parked();
cx.update_editor(|e, window, cx| {
Expand Down Expand Up @@ -37774,7 +37774,7 @@ async fn test_no_duplicated_sticky_headers(cx: &mut TestAppContext) {

let mut sticky_headers = |offset: ScrollOffset| {
cx.update_editor(|e, window, cx| {
e.scroll(gpui::Point { x: 0., y: offset }, None, window, cx);
e.scroll(gpui::Point { x: 0., y: offset }, window, cx);
});
cx.run_until_parked();
cx.update_editor(|e, window, cx| {
Expand Down Expand Up @@ -38070,7 +38070,6 @@ async fn test_scroll_by_clicking_sticky_header(cx: &mut TestAppContext) {
x: 0.,
y: scroll_offset,
},
None,
window,
cx,
);
Expand Down Expand Up @@ -38162,7 +38161,7 @@ async fn test_scroll_by_clicking_sticky_header(cx: &mut TestAppContext) {
// The text "impl Bar {" starts at column 0, so column 5 = 'B'.
let click_x = text_origin_x + em_width * 5.5;
cx.update_editor(|e, window, cx| {
e.scroll(gpui::Point { x: 0., y: 4.5 }, None, window, cx);
e.scroll(gpui::Point { x: 0., y: 4.5 }, window, cx);
});
cx.run_until_parked();
cx.simulate_click(
Expand Down Expand Up @@ -38236,7 +38235,7 @@ async fn test_clicking_sticky_header_sets_character_select_mode(cx: &mut TestApp
editor.end_selection(window, cx);

// Scroll down one row to make `fn foo() {` a sticky header
editor.scroll(gpui::Point { x: 0., y: 1. }, None, window, cx);
editor.scroll(gpui::Point { x: 0., y: 1. }, window, cx);
});
cx.run_until_parked();

Expand Down
26 changes: 8 additions & 18 deletions crates/editor/src/element/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,19 +538,18 @@ impl EditorElement {
editor.update(cx, |editor, cx| {
let line_height = position_map.line_height;
let glyph_width = position_map.em_layout_width;
let (delta, axis) = match delta {
let delta = match delta {
gpui::ScrollDelta::Pixels(mut pixels) => {
//Trackpad
let axis =
position_map.snapshot.ongoing_scroll.filter(&mut pixels);
(pixels, axis)
editor
.scroll_manager
.filter_scroll_delta(&mut pixels, event.touch_phase);
pixels
}

gpui::ScrollDelta::Lines(lines) => {
//Not trackpad
let pixels =
point(lines.x * glyph_width, lines.y * line_height);
(pixels, None)
point(lines.x * glyph_width, lines.y * line_height)
}
};

Expand All @@ -572,17 +571,8 @@ impl EditorElement {
}

if scroll_position != current_scroll_position {
editor.scroll(scroll_position, axis, window, cx);
editor.scroll(scroll_position, window, cx);
cx.stop_propagation();
} else if y < 0. && !forbid_vertical_scroll {
// Due to clamping, we may fail to detect cases of overscroll to the top;
// We want the scroll manager to get an update in such cases and detect the change of direction
// on the next frame.
if editor.scroll_manager.should_notify_top_overscroll(axis) {
cx.notify();
}
} else {
editor.scroll_manager.reset_top_overscroll_notification();
}
});
}
Expand Down Expand Up @@ -1244,7 +1234,7 @@ mod tests {
});

cx.update_editor(|editor, window, cx| {
editor.scroll(gpui::Point { x: 0., y: 5.5 }, None, window, cx);
editor.scroll(gpui::Point { x: 0., y: 5.5 }, window, cx);
});
cx.run_until_parked();

Expand Down
104 changes: 10 additions & 94 deletions crates/editor/src/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@ use crate::{
pub use autoscroll::{Autoscroll, AutoscrollStrategy};
use core::fmt::Debug;
use gpui::{
Along, App, AppContext as _, Axis, Context, Entity, EntityId, Pixels, Task, Window, point, px,
Along, App, AppContext as _, Axis, Context, Entity, EntityId, OngoingScroll, Pixels, Task,
TouchPhase, Window, point,
};
use language::language_settings::{AllLanguageSettings, SoftWrap};
use language::{Bias, Point};
pub use scroll_amount::ScrollAmount;
use settings::Settings;
use std::{
cmp::Ordering,
time::{Duration, Instant},
};
use std::{cmp::Ordering, time::Duration};
use ui::scrollbars::ScrollbarAutoHide;
use util::ResultExt;
use workspace::{ItemId, WorkspaceId};

pub const SCROLL_EVENT_SEPARATION: Duration = Duration::from_millis(28);
const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1);

pub struct WasScrolled(pub(crate) bool);
Expand Down Expand Up @@ -64,12 +61,6 @@ impl ScrollAnchor {
}
}

#[derive(Clone, Copy, Debug)]
pub struct OngoingScroll {
last_event: Instant,
axis: Option<Axis>,
}

/// In the split diff view, the two sides share a ScrollAnchor using this struct.
/// Either side can set a ScrollAnchor that points to its own multibuffer, and we store the ID of the display map
/// that the last-written anchor came from so that we know how to resolve it to a DisplayPoint.
Expand Down Expand Up @@ -121,62 +112,6 @@ impl SharedScrollAnchor {
}
}

impl OngoingScroll {
fn new() -> Self {
Self {
last_event: Instant::now() - SCROLL_EVENT_SEPARATION,
axis: None,
}
}

pub fn filter(&self, delta: &mut gpui::Point<Pixels>) -> Option<Axis> {
const UNLOCK_PERCENT: f32 = 1.9;
const UNLOCK_LOWER_BOUND: Pixels = px(6.);
let mut axis = self.axis;

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
axis = if x <= y {
Some(Axis::Vertical)
} else {
Some(Axis::Horizontal)
};
} else if x.max(y) >= UNLOCK_LOWER_BOUND {
//Check if the current ongoing will need to unlock
match axis {
Some(Axis::Vertical) => {
if x > y && x >= y * UNLOCK_PERCENT {
axis = None;
}
}

Some(Axis::Horizontal) => {
if y > x && y >= x * UNLOCK_PERCENT {
axis = None;
}
}

None => {}
}
}

match axis {
Some(Axis::Vertical) => {
*delta = point(px(0.), delta.y);
}
Some(Axis::Horizontal) => {
*delta = point(delta.x, px(0.));
}
None => {}
}

axis
}
}

#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub enum ScrollbarThumbState {
#[default]
Expand Down Expand Up @@ -227,7 +162,6 @@ pub struct ScrollManager {
visible_line_count: Option<f64>,
visible_column_count: Option<f64>,
forbid_vertical_scroll: bool,
notified_top_overscroll: bool,
minimap_thumb_state: Option<ScrollbarThumbState>,
_save_scroll_position_task: Task<()>,
}
Expand All @@ -242,7 +176,7 @@ impl ScrollManager {
vertical_scroll_margin: EditorSettings::get_global(cx).vertical_scroll_margin,
anchor,
scroll_max_x: None,
ongoing: OngoingScroll::new(),
ongoing: OngoingScroll::default(),
autoscroll_request: None,
show_scrollbars: true,
hide_scrollbar_task: None,
Expand All @@ -251,7 +185,6 @@ impl ScrollManager {
visible_line_count: None,
visible_column_count: None,
forbid_vertical_scroll: false,
notified_top_overscroll: false,
minimap_thumb_state: None,
_save_scroll_position_task: Task::ready(()),
}
Expand Down Expand Up @@ -361,28 +294,12 @@ impl ScrollManager {
});
}

pub fn ongoing_scroll(&self) -> OngoingScroll {
self.ongoing
}

pub fn update_ongoing_scroll(&mut self, axis: Option<Axis>) {
self.ongoing.last_event = Instant::now();
self.ongoing.axis = axis;
}

pub fn should_notify_top_overscroll(&mut self, axis: Option<Axis>) -> bool {
let now = Instant::now();
let new_scroll = now.duration_since(self.ongoing.last_event) > SCROLL_EVENT_SEPARATION;
let axis_changed = self.ongoing.axis != axis;
let should_notify = !self.notified_top_overscroll || new_scroll || axis_changed;
self.ongoing.last_event = now;
self.ongoing.axis = axis;
self.notified_top_overscroll = true;
should_notify
}

pub fn reset_top_overscroll_notification(&mut self) {
self.notified_top_overscroll = false;
pub fn filter_scroll_delta(
&mut self,
delta: &mut gpui::Point<Pixels>,
touch_phase: TouchPhase,
) {
self.ongoing.filter(delta, touch_phase);
}

pub fn scroll_position(
Expand Down Expand Up @@ -486,7 +403,6 @@ impl ScrollManager {
return WasScrolled(false);
}

self.notified_top_overscroll = false;
self.anchor.update(cx, |shared, _| {
shared.scroll_anchor = adjusted_anchor;
shared.display_map_id = Some(display_map.display_map_id);
Expand Down
3 changes: 0 additions & 3 deletions crates/editor/src/scroll/actions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::Axis;
use crate::{
Autoscroll, Editor, EditorMode, NextScreen, NextScrollCursorCenterTopBottom,
SCROLL_CENTER_TOP_BOTTOM_DEBOUNCE_TIMEOUT, ScrollCursorBottom, ScrollCursorCenter,
Expand Down Expand Up @@ -26,11 +25,9 @@ impl Editor {
pub fn scroll(
&mut self,
scroll_position: Point<ScrollOffset>,
axis: Option<Axis>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.scroll_manager.update_ongoing_scroll(axis);
self.set_scroll_position(scroll_position, window, cx);
}

Expand Down
Loading
Loading