scroll.rs

  1pub mod actions;
  2pub mod autoscroll;
  3pub mod scroll_amount;
  4
  5use std::{
  6    cmp::Ordering,
  7    time::{Duration, Instant},
  8};
  9
 10use gpui::{
 11    geometry::vector::{vec2f, Vector2F},
 12    AppContext, Axis, Task, ViewContext,
 13};
 14use language::{Bias, Point};
 15use util::ResultExt;
 16use workspace::WorkspaceId;
 17
 18use crate::{
 19    display_map::{DisplaySnapshot, ToDisplayPoint},
 20    hover_popover::hide_hover,
 21    inlay_hint_cache::InlayRefreshReason,
 22    persistence::DB,
 23    Anchor, DisplayPoint, Editor, EditorMode, Event, MultiBufferSnapshot, ToPoint,
 24};
 25
 26use self::{
 27    autoscroll::{Autoscroll, AutoscrollStrategy},
 28    scroll_amount::ScrollAmount,
 29};
 30
 31pub const SCROLL_EVENT_SEPARATION: Duration = Duration::from_millis(28);
 32const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1);
 33
 34#[derive(Default)]
 35pub struct ScrollbarAutoHide(pub bool);
 36
 37#[derive(Clone, Copy, Debug, PartialEq)]
 38pub struct ScrollAnchor {
 39    pub offset: Vector2F,
 40    pub anchor: Anchor,
 41}
 42
 43impl ScrollAnchor {
 44    fn new() -> Self {
 45        Self {
 46            offset: Vector2F::zero(),
 47            anchor: Anchor::min(),
 48        }
 49    }
 50
 51    pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> Vector2F {
 52        let mut scroll_position = self.offset;
 53        if self.anchor != Anchor::min() {
 54            let scroll_top = self.anchor.to_display_point(snapshot).row() as f32;
 55            scroll_position.set_y(scroll_top + scroll_position.y());
 56        } else {
 57            scroll_position.set_y(0.);
 58        }
 59        scroll_position
 60    }
 61
 62    pub fn top_row(&self, buffer: &MultiBufferSnapshot) -> u32 {
 63        self.anchor.to_point(buffer).row
 64    }
 65}
 66
 67#[derive(Clone, Copy, Debug)]
 68pub struct OngoingScroll {
 69    last_event: Instant,
 70    axis: Option<Axis>,
 71}
 72
 73impl OngoingScroll {
 74    fn new() -> Self {
 75        Self {
 76            last_event: Instant::now() - SCROLL_EVENT_SEPARATION,
 77            axis: None,
 78        }
 79    }
 80
 81    pub fn filter(&self, delta: &mut Vector2F) -> Option<Axis> {
 82        const UNLOCK_PERCENT: f32 = 1.9;
 83        const UNLOCK_LOWER_BOUND: f32 = 6.;
 84        let mut axis = self.axis;
 85
 86        let x = delta.x().abs();
 87        let y = delta.y().abs();
 88        let duration = Instant::now().duration_since(self.last_event);
 89        if duration > SCROLL_EVENT_SEPARATION {
 90            //New ongoing scroll will start, determine axis
 91            axis = if x <= y {
 92                Some(Axis::Vertical)
 93            } else {
 94                Some(Axis::Horizontal)
 95            };
 96        } else if x.max(y) >= UNLOCK_LOWER_BOUND {
 97            //Check if the current ongoing will need to unlock
 98            match axis {
 99                Some(Axis::Vertical) => {
100                    if x > y && x >= y * UNLOCK_PERCENT {
101                        axis = None;
102                    }
103                }
104
105                Some(Axis::Horizontal) => {
106                    if y > x && y >= x * UNLOCK_PERCENT {
107                        axis = None;
108                    }
109                }
110
111                None => {}
112            }
113        }
114
115        match axis {
116            Some(Axis::Vertical) => *delta = vec2f(0., delta.y()),
117            Some(Axis::Horizontal) => *delta = vec2f(delta.x(), 0.),
118            None => {}
119        }
120
121        axis
122    }
123}
124
125pub struct ScrollManager {
126    vertical_scroll_margin: f32,
127    anchor: ScrollAnchor,
128    ongoing: OngoingScroll,
129    autoscroll_request: Option<(Autoscroll, bool)>,
130    last_autoscroll: Option<(Vector2F, f32, f32, AutoscrollStrategy)>,
131    show_scrollbars: bool,
132    hide_scrollbar_task: Option<Task<()>>,
133    visible_line_count: Option<f32>,
134}
135
136impl ScrollManager {
137    pub fn new() -> Self {
138        ScrollManager {
139            vertical_scroll_margin: 3.0,
140            anchor: ScrollAnchor::new(),
141            ongoing: OngoingScroll::new(),
142            autoscroll_request: None,
143            show_scrollbars: true,
144            hide_scrollbar_task: None,
145            last_autoscroll: None,
146            visible_line_count: None,
147        }
148    }
149
150    pub fn clone_state(&mut self, other: &Self) {
151        self.anchor = other.anchor;
152        self.ongoing = other.ongoing;
153    }
154
155    pub fn anchor(&self) -> ScrollAnchor {
156        self.anchor
157    }
158
159    pub fn ongoing_scroll(&self) -> OngoingScroll {
160        self.ongoing
161    }
162
163    pub fn update_ongoing_scroll(&mut self, axis: Option<Axis>) {
164        self.ongoing.last_event = Instant::now();
165        self.ongoing.axis = axis;
166    }
167
168    pub fn scroll_position(&self, snapshot: &DisplaySnapshot) -> Vector2F {
169        self.anchor.scroll_position(snapshot)
170    }
171
172    fn set_scroll_position(
173        &mut self,
174        scroll_position: Vector2F,
175        map: &DisplaySnapshot,
176        local: bool,
177        autoscroll: bool,
178        workspace_id: Option<i64>,
179        cx: &mut ViewContext<Editor>,
180    ) -> ScrollAnchor {
181        let (new_anchor, top_row) = if scroll_position.y() <= 0. {
182            (
183                ScrollAnchor {
184                    anchor: Anchor::min(),
185                    offset: scroll_position.max(vec2f(0., 0.)),
186                },
187                0,
188            )
189        } else {
190            let scroll_top_buffer_point =
191                DisplayPoint::new(scroll_position.y() as u32, 0).to_point(&map);
192            let top_anchor = map
193                .buffer_snapshot
194                .anchor_at(scroll_top_buffer_point, Bias::Right);
195
196            (
197                ScrollAnchor {
198                    anchor: top_anchor,
199                    offset: vec2f(
200                        scroll_position.x(),
201                        scroll_position.y() - top_anchor.to_display_point(&map).row() as f32,
202                    ),
203                },
204                scroll_top_buffer_point.row,
205            )
206        };
207
208        self.set_anchor(new_anchor, top_row, local, autoscroll, workspace_id, cx);
209        new_anchor
210    }
211
212    fn set_anchor(
213        &mut self,
214        anchor: ScrollAnchor,
215        top_row: u32,
216        local: bool,
217        autoscroll: bool,
218        workspace_id: Option<i64>,
219        cx: &mut ViewContext<Editor>,
220    ) {
221        self.anchor = anchor;
222        cx.emit(Event::ScrollPositionChanged { local, autoscroll });
223        self.show_scrollbar(cx);
224        self.autoscroll_request.take();
225        if let Some(workspace_id) = workspace_id {
226            let item_id = cx.view_id();
227
228            cx.background()
229                .spawn(async move {
230                    DB.save_scroll_position(
231                        item_id,
232                        workspace_id,
233                        top_row,
234                        anchor.offset.x(),
235                        anchor.offset.y(),
236                    )
237                    .await
238                    .log_err()
239                })
240                .detach()
241        }
242        cx.notify();
243    }
244
245    pub fn show_scrollbar(&mut self, cx: &mut ViewContext<Editor>) {
246        if !self.show_scrollbars {
247            self.show_scrollbars = true;
248            cx.notify();
249        }
250
251        if cx.default_global::<ScrollbarAutoHide>().0 {
252            self.hide_scrollbar_task = Some(cx.spawn(|editor, mut cx| async move {
253                cx.background().timer(SCROLLBAR_SHOW_INTERVAL).await;
254                editor
255                    .update(&mut cx, |editor, cx| {
256                        editor.scroll_manager.show_scrollbars = false;
257                        cx.notify();
258                    })
259                    .log_err();
260            }));
261        } else {
262            self.hide_scrollbar_task = None;
263        }
264    }
265
266    pub fn scrollbars_visible(&self) -> bool {
267        self.show_scrollbars
268    }
269
270    pub fn has_autoscroll_request(&self) -> bool {
271        self.autoscroll_request.is_some()
272    }
273
274    pub fn clamp_scroll_left(&mut self, max: f32) -> bool {
275        if max < self.anchor.offset.x() {
276            self.anchor.offset.set_x(max);
277            true
278        } else {
279            false
280        }
281    }
282}
283
284impl Editor {
285    pub fn vertical_scroll_margin(&mut self) -> usize {
286        self.scroll_manager.vertical_scroll_margin as usize
287    }
288
289    pub fn set_vertical_scroll_margin(&mut self, margin_rows: usize, cx: &mut ViewContext<Self>) {
290        self.scroll_manager.vertical_scroll_margin = margin_rows as f32;
291        cx.notify();
292    }
293
294    pub fn visible_line_count(&self) -> Option<f32> {
295        self.scroll_manager.visible_line_count
296    }
297
298    pub(crate) fn set_visible_line_count(&mut self, lines: f32, cx: &mut ViewContext<Self>) {
299        let had_no_visibles = self.scroll_manager.visible_line_count.is_none();
300        self.scroll_manager.visible_line_count = Some(lines);
301        if had_no_visibles {
302            self.refresh_inlays(InlayRefreshReason::VisibleExcerptsChange, cx);
303        }
304    }
305
306    pub fn set_scroll_position(&mut self, scroll_position: Vector2F, cx: &mut ViewContext<Self>) {
307        self.set_scroll_position_internal(scroll_position, true, false, cx);
308    }
309
310    pub(crate) fn set_scroll_position_internal(
311        &mut self,
312        scroll_position: Vector2F,
313        local: bool,
314        autoscroll: bool,
315        cx: &mut ViewContext<Self>,
316    ) {
317        let map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
318
319        hide_hover(self, cx);
320        let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1);
321        let scroll_anchor = self.scroll_manager.set_scroll_position(
322            scroll_position,
323            &map,
324            local,
325            autoscroll,
326            workspace_id,
327            cx,
328        );
329        self.refresh_inlays(InlayRefreshReason::Scroll(scroll_anchor), cx);
330    }
331
332    pub fn scroll_position(&self, cx: &mut ViewContext<Self>) -> Vector2F {
333        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
334        self.scroll_manager.anchor.scroll_position(&display_map)
335    }
336
337    pub fn set_scroll_anchor(&mut self, scroll_anchor: ScrollAnchor, cx: &mut ViewContext<Self>) {
338        hide_hover(self, cx);
339        let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1);
340        let top_row = scroll_anchor
341            .anchor
342            .to_point(&self.buffer().read(cx).snapshot(cx))
343            .row;
344        self.scroll_manager
345            .set_anchor(scroll_anchor, top_row, true, false, workspace_id, cx);
346    }
347
348    pub(crate) fn set_scroll_anchor_remote(
349        &mut self,
350        scroll_anchor: ScrollAnchor,
351        cx: &mut ViewContext<Self>,
352    ) {
353        hide_hover(self, cx);
354        let workspace_id = self.workspace.as_ref().map(|workspace| workspace.1);
355        let top_row = scroll_anchor
356            .anchor
357            .to_point(&self.buffer().read(cx).snapshot(cx))
358            .row;
359        self.scroll_manager
360            .set_anchor(scroll_anchor, top_row, false, false, workspace_id, cx);
361    }
362
363    pub fn scroll_screen(&mut self, amount: &ScrollAmount, cx: &mut ViewContext<Self>) {
364        if matches!(self.mode, EditorMode::SingleLine) {
365            cx.propagate_action();
366            return;
367        }
368
369        if self.take_rename(true, cx).is_some() {
370            return;
371        }
372
373        if amount.move_context_menu_selection(self, cx) {
374            return;
375        }
376
377        let cur_position = self.scroll_position(cx);
378        let new_pos = cur_position + vec2f(0., amount.lines(self));
379        self.set_scroll_position(new_pos, cx);
380    }
381
382    /// Returns an ordering. The newest selection is:
383    ///     Ordering::Equal => on screen
384    ///     Ordering::Less => above the screen
385    ///     Ordering::Greater => below the screen
386    pub fn newest_selection_on_screen(&self, cx: &mut AppContext) -> Ordering {
387        let snapshot = self.display_map.update(cx, |map, cx| map.snapshot(cx));
388        let newest_head = self
389            .selections
390            .newest_anchor()
391            .head()
392            .to_display_point(&snapshot);
393        let screen_top = self
394            .scroll_manager
395            .anchor
396            .anchor
397            .to_display_point(&snapshot);
398
399        if screen_top > newest_head {
400            return Ordering::Less;
401        }
402
403        if let Some(visible_lines) = self.visible_line_count() {
404            if newest_head.row() < screen_top.row() + visible_lines as u32 {
405                return Ordering::Equal;
406            }
407        }
408
409        Ordering::Greater
410    }
411
412    pub fn read_scroll_position_from_db(
413        &mut self,
414        item_id: usize,
415        workspace_id: WorkspaceId,
416        cx: &mut ViewContext<Editor>,
417    ) {
418        let scroll_position = DB.get_scroll_position(item_id, workspace_id);
419        if let Ok(Some((top_row, x, y))) = scroll_position {
420            let top_anchor = self
421                .buffer()
422                .read(cx)
423                .snapshot(cx)
424                .anchor_at(Point::new(top_row as u32, 0), Bias::Left);
425            let scroll_anchor = ScrollAnchor {
426                offset: Vector2F::new(x, y),
427                anchor: top_anchor,
428            };
429            self.set_scroll_anchor(scroll_anchor, cx);
430        }
431    }
432}