autoscroll.rs

  1use crate::{
  2    DisplayRow, Editor, EditorMode, LineWithInvisibles, RowExt, SelectionEffects,
  3    display_map::ToDisplayPoint,
  4    scroll::{ScrollOffset, WasScrolled},
  5};
  6use gpui::{Bounds, Context, Pixels, Window};
  7use language::Point;
  8use multi_buffer::Anchor;
  9use std::cmp;
 10
 11#[derive(Debug, PartialEq, Eq, Clone, Copy)]
 12pub enum Autoscroll {
 13    Next,
 14    Strategy(AutoscrollStrategy, Option<Anchor>),
 15}
 16
 17impl Autoscroll {
 18    /// scrolls the minimal amount to (try) and fit all cursors onscreen
 19    pub fn fit() -> Self {
 20        Self::Strategy(AutoscrollStrategy::Fit, None)
 21    }
 22
 23    /// scrolls the minimal amount to fit the newest cursor
 24    pub fn newest() -> Self {
 25        Self::Strategy(AutoscrollStrategy::Newest, None)
 26    }
 27
 28    /// scrolls so the newest cursor is vertically centered
 29    pub fn center() -> Self {
 30        Self::Strategy(AutoscrollStrategy::Center, None)
 31    }
 32
 33    /// scrolls so the newest cursor is near the top
 34    /// (offset by vertical_scroll_margin)
 35    pub fn focused() -> Self {
 36        Self::Strategy(AutoscrollStrategy::Focused, None)
 37    }
 38
 39    /// Scrolls so that the newest cursor is roughly an n-th line from the top.
 40    pub fn top_relative(n: usize) -> Self {
 41        Self::Strategy(AutoscrollStrategy::TopRelative(n), None)
 42    }
 43
 44    /// Scrolls so that the newest cursor is at the top.
 45    pub fn top() -> Self {
 46        Self::Strategy(AutoscrollStrategy::Top, None)
 47    }
 48
 49    /// Scrolls so that the newest cursor is roughly an n-th line from the bottom.
 50    pub fn bottom_relative(n: usize) -> Self {
 51        Self::Strategy(AutoscrollStrategy::BottomRelative(n), None)
 52    }
 53
 54    /// Scrolls so that the newest cursor is at the bottom.
 55    pub fn bottom() -> Self {
 56        Self::Strategy(AutoscrollStrategy::Bottom, None)
 57    }
 58
 59    /// Applies a given auto-scroll strategy to a given anchor instead of a cursor.
 60    /// E.G: Autoscroll::center().for_anchor(...) results in the anchor being at the center of the screen.
 61    pub fn for_anchor(self, anchor: Anchor) -> Self {
 62        match self {
 63            Autoscroll::Next => self,
 64            Autoscroll::Strategy(autoscroll_strategy, _) => {
 65                Self::Strategy(autoscroll_strategy, Some(anchor))
 66            }
 67        }
 68    }
 69}
 70
 71impl Into<SelectionEffects> for Option<Autoscroll> {
 72    fn into(self) -> SelectionEffects {
 73        match self {
 74            Some(autoscroll) => SelectionEffects::scroll(autoscroll),
 75            None => SelectionEffects::no_scroll(),
 76        }
 77    }
 78}
 79
 80#[derive(Debug, PartialEq, Eq, Default, Clone, Copy)]
 81pub enum AutoscrollStrategy {
 82    Fit,
 83    Newest,
 84    #[default]
 85    Center,
 86    Focused,
 87    Top,
 88    Bottom,
 89    TopRelative(usize),
 90    BottomRelative(usize),
 91}
 92
 93impl AutoscrollStrategy {
 94    fn next(&self) -> Self {
 95        match self {
 96            AutoscrollStrategy::Center => AutoscrollStrategy::Top,
 97            AutoscrollStrategy::Top => AutoscrollStrategy::Bottom,
 98            _ => AutoscrollStrategy::Center,
 99        }
100    }
101}
102
103pub(crate) struct NeedsHorizontalAutoscroll(pub(crate) bool);
104
105impl Editor {
106    pub(crate) fn autoscroll_vertically(
107        &mut self,
108        bounds: Bounds<Pixels>,
109        line_height: Pixels,
110        max_scroll_top: ScrollOffset,
111        autoscroll_request: Option<(Autoscroll, bool)>,
112        window: &mut Window,
113        cx: &mut Context<Editor>,
114    ) -> (NeedsHorizontalAutoscroll, WasScrolled) {
115        let viewport_height = bounds.size.height;
116        let visible_lines = ScrollOffset::from(viewport_height / line_height);
117        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
118        let mut scroll_position = self.scroll_manager.scroll_position(&display_map);
119        let original_y = scroll_position.y;
120        if let Some(last_bounds) = self.expect_bounds_change.take()
121            && scroll_position.y != 0.
122        {
123            scroll_position.y +=
124                ScrollOffset::from((bounds.top() - last_bounds.top()) / line_height);
125            if scroll_position.y < 0. {
126                scroll_position.y = 0.;
127            }
128        }
129        if scroll_position.y > max_scroll_top {
130            scroll_position.y = max_scroll_top;
131        }
132
133        let editor_was_scrolled = if original_y != scroll_position.y {
134            self.set_scroll_position(scroll_position, window, cx)
135        } else {
136            WasScrolled(false)
137        };
138
139        let Some((autoscroll, local)) = autoscroll_request else {
140            return (NeedsHorizontalAutoscroll(false), editor_was_scrolled);
141        };
142
143        let mut target_top;
144        let mut target_bottom;
145        if let Some(first_highlighted_row) =
146            self.highlighted_display_row_for_autoscroll(&display_map)
147        {
148            target_top = first_highlighted_row.as_f64();
149            target_bottom = target_top + 1.;
150        } else {
151            let selections = self.selections.all::<Point>(&display_map);
152
153            target_top = selections
154                .first()
155                .unwrap()
156                .head()
157                .to_display_point(&display_map)
158                .row()
159                .as_f64();
160            target_bottom = selections
161                .last()
162                .unwrap()
163                .head()
164                .to_display_point(&display_map)
165                .row()
166                .next_row()
167                .as_f64();
168
169            let selections_fit = target_bottom - target_top <= visible_lines;
170            if matches!(
171                autoscroll,
172                Autoscroll::Strategy(AutoscrollStrategy::Newest, _)
173            ) || (matches!(autoscroll, Autoscroll::Strategy(AutoscrollStrategy::Fit, _))
174                && !selections_fit)
175            {
176                let newest_selection_top = selections
177                    .iter()
178                    .max_by_key(|s| s.id)
179                    .unwrap()
180                    .head()
181                    .to_display_point(&display_map)
182                    .row()
183                    .as_f64();
184                target_top = newest_selection_top;
185                target_bottom = newest_selection_top + 1.;
186            }
187        }
188
189        let margin = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
190            0.
191        } else {
192            ((visible_lines - (target_bottom - target_top)) / 2.0).floor()
193        };
194
195        let strategy = match autoscroll {
196            Autoscroll::Strategy(strategy, _) => strategy,
197            Autoscroll::Next => {
198                let last_autoscroll = &self.scroll_manager.last_autoscroll;
199                if let Some(last_autoscroll) = last_autoscroll {
200                    if self.scroll_manager.anchor.offset == last_autoscroll.0
201                        && target_top == last_autoscroll.1
202                        && target_bottom == last_autoscroll.2
203                    {
204                        last_autoscroll.3.next()
205                    } else {
206                        AutoscrollStrategy::default()
207                    }
208                } else {
209                    AutoscrollStrategy::default()
210                }
211            }
212        };
213        if let Autoscroll::Strategy(_, Some(anchor)) = autoscroll {
214            target_top = anchor.to_display_point(&display_map).row().as_f64();
215            target_bottom = target_top + 1.;
216        }
217
218        let was_autoscrolled = match strategy {
219            AutoscrollStrategy::Fit | AutoscrollStrategy::Newest => {
220                let margin = margin.min(self.scroll_manager.vertical_scroll_margin);
221                let target_top = (target_top - margin).max(0.0);
222                let target_bottom = target_bottom + margin;
223                let start_row = scroll_position.y;
224                let end_row = start_row + visible_lines;
225
226                let needs_scroll_up = target_top < start_row;
227                let needs_scroll_down = target_bottom >= end_row;
228
229                if needs_scroll_up && !needs_scroll_down {
230                    scroll_position.y = target_top;
231                } else if !needs_scroll_up && needs_scroll_down {
232                    scroll_position.y = target_bottom - visible_lines;
233                }
234
235                if needs_scroll_up ^ needs_scroll_down {
236                    self.set_scroll_position_internal(scroll_position, local, true, window, cx)
237                } else {
238                    WasScrolled(false)
239                }
240            }
241            AutoscrollStrategy::Center => {
242                scroll_position.y = (target_top - margin).max(0.0);
243                self.set_scroll_position_internal(scroll_position, local, true, window, cx)
244            }
245            AutoscrollStrategy::Focused => {
246                let margin = margin.min(self.scroll_manager.vertical_scroll_margin);
247                scroll_position.y = (target_top - margin).max(0.0);
248                self.set_scroll_position_internal(scroll_position, local, true, window, cx)
249            }
250            AutoscrollStrategy::Top => {
251                scroll_position.y = (target_top).max(0.0);
252                self.set_scroll_position_internal(scroll_position, local, true, window, cx)
253            }
254            AutoscrollStrategy::Bottom => {
255                scroll_position.y = (target_bottom - visible_lines).max(0.0);
256                self.set_scroll_position_internal(scroll_position, local, true, window, cx)
257            }
258            AutoscrollStrategy::TopRelative(lines) => {
259                scroll_position.y = target_top - lines as ScrollOffset;
260                self.set_scroll_position_internal(scroll_position, local, true, window, cx)
261            }
262            AutoscrollStrategy::BottomRelative(lines) => {
263                scroll_position.y = target_bottom + lines as ScrollOffset;
264                self.set_scroll_position_internal(scroll_position, local, true, window, cx)
265            }
266        };
267
268        self.scroll_manager.last_autoscroll = Some((
269            self.scroll_manager.anchor.offset,
270            target_top,
271            target_bottom,
272            strategy,
273        ));
274
275        let was_scrolled = WasScrolled(editor_was_scrolled.0 || was_autoscrolled.0);
276        (NeedsHorizontalAutoscroll(true), was_scrolled)
277    }
278
279    pub(crate) fn autoscroll_horizontally(
280        &mut self,
281        start_row: DisplayRow,
282        viewport_width: Pixels,
283        scroll_width: Pixels,
284        em_advance: Pixels,
285        layouts: &[LineWithInvisibles],
286        autoscroll_request: Option<(Autoscroll, bool)>,
287        window: &mut Window,
288        cx: &mut Context<Self>,
289    ) -> Option<gpui::Point<ScrollOffset>> {
290        let (_, local) = autoscroll_request?;
291        let em_advance = ScrollOffset::from(em_advance);
292        let viewport_width = ScrollOffset::from(viewport_width);
293        let scroll_width = ScrollOffset::from(scroll_width);
294
295        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
296        let selections = self.selections.all::<Point>(&display_map);
297        let mut scroll_position = self.scroll_manager.scroll_position(&display_map);
298
299        let mut target_left;
300        let mut target_right: f64;
301
302        if self
303            .highlighted_display_row_for_autoscroll(&display_map)
304            .is_none()
305        {
306            target_left = f64::INFINITY;
307            target_right = 0.;
308            for selection in selections {
309                let head = selection.head().to_display_point(&display_map);
310                if head.row() >= start_row
311                    && head.row() < DisplayRow(start_row.0 + layouts.len() as u32)
312                {
313                    let start_column = head.column();
314                    let end_column = cmp::min(display_map.line_len(head.row()), head.column());
315                    target_left = target_left.min(ScrollOffset::from(
316                        layouts[head.row().minus(start_row) as usize]
317                            .x_for_index(start_column as usize)
318                            + self.gutter_dimensions.margin,
319                    ));
320                    target_right = target_right.max(
321                        ScrollOffset::from(
322                            layouts[head.row().minus(start_row) as usize]
323                                .x_for_index(end_column as usize),
324                        ) + em_advance,
325                    );
326                }
327            }
328        } else {
329            target_left = 0.;
330            target_right = 0.;
331        }
332
333        target_right = target_right.min(scroll_width);
334
335        if target_right - target_left > viewport_width {
336            return None;
337        }
338
339        let scroll_left = self.scroll_manager.anchor.offset.x * em_advance;
340        let scroll_right = scroll_left + viewport_width;
341
342        let was_scrolled = if target_left < scroll_left {
343            scroll_position.x = target_left / em_advance;
344            self.set_scroll_position_internal(scroll_position, local, true, window, cx)
345        } else if target_right > scroll_right {
346            scroll_position.x = (target_right - viewport_width) / em_advance;
347            self.set_scroll_position_internal(scroll_position, local, true, window, cx)
348        } else {
349            WasScrolled(false)
350        };
351
352        if was_scrolled.0 {
353            Some(scroll_position)
354        } else {
355            None
356        }
357    }
358
359    pub fn request_autoscroll(&mut self, autoscroll: Autoscroll, cx: &mut Context<Self>) {
360        self.scroll_manager.autoscroll_request = Some((autoscroll, true));
361        cx.notify();
362    }
363
364    pub(crate) fn request_autoscroll_remotely(
365        &mut self,
366        autoscroll: Autoscroll,
367        cx: &mut Context<Self>,
368    ) {
369        self.scroll_manager.autoscroll_request = Some((autoscroll, false));
370        cx.notify();
371    }
372}