1use crate::{
2 display_map::ToDisplayPoint, DisplayRow, Editor, EditorMode, LineWithInvisibles, RowExt,
3};
4use gpui::{px, Bounds, Pixels, ViewContext};
5use language::Point;
6use std::{cmp, f32};
7
8#[derive(PartialEq, Eq, Clone, Copy)]
9pub enum Autoscroll {
10 Next,
11 Strategy(AutoscrollStrategy),
12}
13
14impl Autoscroll {
15 /// scrolls the minimal amount to (try) and fit all cursors onscreen
16 pub fn fit() -> Self {
17 Self::Strategy(AutoscrollStrategy::Fit)
18 }
19
20 /// scrolls the minimal amount to fit the newest cursor
21 pub fn newest() -> Self {
22 Self::Strategy(AutoscrollStrategy::Newest)
23 }
24
25 /// scrolls so the newest cursor is vertically centered
26 pub fn center() -> Self {
27 Self::Strategy(AutoscrollStrategy::Center)
28 }
29
30 /// scrolls so the newest cursor is near the top
31 /// (offset by vertical_scroll_margin)
32 pub fn focused() -> Self {
33 Self::Strategy(AutoscrollStrategy::Focused)
34 }
35 /// Scrolls so that the newest cursor is roughly an n-th line from the top.
36 pub fn top_relative(n: usize) -> Self {
37 Self::Strategy(AutoscrollStrategy::TopRelative(n))
38 }
39}
40
41#[derive(PartialEq, Eq, Default, Clone, Copy)]
42pub enum AutoscrollStrategy {
43 Fit,
44 Newest,
45 #[default]
46 Center,
47 Focused,
48 Top,
49 Bottom,
50 TopRelative(usize),
51}
52
53impl AutoscrollStrategy {
54 fn next(&self) -> Self {
55 match self {
56 AutoscrollStrategy::Center => AutoscrollStrategy::Top,
57 AutoscrollStrategy::Top => AutoscrollStrategy::Bottom,
58 _ => AutoscrollStrategy::Center,
59 }
60 }
61}
62
63impl Editor {
64 pub fn autoscroll_request(&self) -> Option<Autoscroll> {
65 self.scroll_manager.autoscroll_request()
66 }
67
68 pub fn autoscroll_vertically(
69 &mut self,
70 bounds: Bounds<Pixels>,
71 line_height: Pixels,
72 max_scroll_top: f32,
73 cx: &mut ViewContext<Editor>,
74 ) -> bool {
75 let viewport_height = bounds.size.height;
76 let visible_lines = viewport_height / line_height;
77 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
78 let mut scroll_position = self.scroll_manager.scroll_position(&display_map);
79 let original_y = scroll_position.y;
80 if let Some(last_bounds) = self.expect_bounds_change.take() {
81 if scroll_position.y != 0. {
82 scroll_position.y += (bounds.top() - last_bounds.top()) / line_height;
83 if scroll_position.y < 0. {
84 scroll_position.y = 0.;
85 }
86 }
87 }
88 if scroll_position.y > max_scroll_top {
89 scroll_position.y = max_scroll_top;
90 }
91
92 if original_y != scroll_position.y {
93 self.set_scroll_position(scroll_position, cx);
94 }
95
96 let Some((autoscroll, local)) = self.scroll_manager.autoscroll_request.take() else {
97 return false;
98 };
99
100 let mut target_top;
101 let mut target_bottom;
102 if let Some(first_highlighted_row) =
103 self.highlighted_display_row_for_autoscroll(&display_map)
104 {
105 target_top = first_highlighted_row.as_f32();
106 target_bottom = target_top + 1.;
107 } else {
108 let selections = self.selections.all::<Point>(cx);
109 target_top = selections
110 .first()
111 .unwrap()
112 .head()
113 .to_display_point(&display_map)
114 .row()
115 .as_f32();
116 target_bottom = selections
117 .last()
118 .unwrap()
119 .head()
120 .to_display_point(&display_map)
121 .row()
122 .next_row()
123 .as_f32();
124
125 // If the selections can't all fit on screen, scroll to the newest.
126 if autoscroll == Autoscroll::newest()
127 || autoscroll == Autoscroll::fit() && target_bottom - target_top > visible_lines
128 {
129 let newest_selection_top = selections
130 .iter()
131 .max_by_key(|s| s.id)
132 .unwrap()
133 .head()
134 .to_display_point(&display_map)
135 .row()
136 .as_f32();
137 target_top = newest_selection_top;
138 target_bottom = newest_selection_top + 1.;
139 }
140 }
141
142 let margin = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
143 0.
144 } else {
145 ((visible_lines - (target_bottom - target_top)) / 2.0).floor()
146 };
147
148 let strategy = match autoscroll {
149 Autoscroll::Strategy(strategy) => strategy,
150 Autoscroll::Next => {
151 let last_autoscroll = &self.scroll_manager.last_autoscroll;
152 if let Some(last_autoscroll) = last_autoscroll {
153 if self.scroll_manager.anchor.offset == last_autoscroll.0
154 && target_top == last_autoscroll.1
155 && target_bottom == last_autoscroll.2
156 {
157 last_autoscroll.3.next()
158 } else {
159 AutoscrollStrategy::default()
160 }
161 } else {
162 AutoscrollStrategy::default()
163 }
164 }
165 };
166
167 match strategy {
168 AutoscrollStrategy::Fit | AutoscrollStrategy::Newest => {
169 let margin = margin.min(self.scroll_manager.vertical_scroll_margin);
170 let target_top = (target_top - margin).max(0.0);
171 let target_bottom = target_bottom + margin;
172 let start_row = scroll_position.y;
173 let end_row = start_row + visible_lines;
174
175 let needs_scroll_up = target_top < start_row;
176 let needs_scroll_down = target_bottom >= end_row;
177
178 if needs_scroll_up && !needs_scroll_down {
179 scroll_position.y = target_top;
180 self.set_scroll_position_internal(scroll_position, local, true, cx);
181 }
182 if !needs_scroll_up && needs_scroll_down {
183 scroll_position.y = target_bottom - visible_lines;
184 self.set_scroll_position_internal(scroll_position, local, true, cx);
185 }
186 }
187 AutoscrollStrategy::Center => {
188 scroll_position.y = (target_top - margin).max(0.0);
189 self.set_scroll_position_internal(scroll_position, local, true, cx);
190 }
191 AutoscrollStrategy::Focused => {
192 let margin = margin.min(self.scroll_manager.vertical_scroll_margin);
193 scroll_position.y = (target_top - margin).max(0.0);
194 self.set_scroll_position_internal(scroll_position, local, true, cx);
195 }
196 AutoscrollStrategy::Top => {
197 scroll_position.y = (target_top).max(0.0);
198 self.set_scroll_position_internal(scroll_position, local, true, cx);
199 }
200 AutoscrollStrategy::Bottom => {
201 scroll_position.y = (target_bottom - visible_lines).max(0.0);
202 self.set_scroll_position_internal(scroll_position, local, true, cx);
203 }
204 AutoscrollStrategy::TopRelative(lines) => {
205 scroll_position.y = target_top - lines as f32;
206 self.set_scroll_position_internal(scroll_position, local, true, cx);
207 }
208 }
209
210 self.scroll_manager.last_autoscroll = Some((
211 self.scroll_manager.anchor.offset,
212 target_top,
213 target_bottom,
214 strategy,
215 ));
216
217 true
218 }
219
220 pub(crate) fn autoscroll_horizontally(
221 &mut self,
222 start_row: DisplayRow,
223 viewport_width: Pixels,
224 scroll_width: Pixels,
225 max_glyph_width: Pixels,
226 layouts: &[LineWithInvisibles],
227 cx: &mut ViewContext<Self>,
228 ) -> bool {
229 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
230 let selections = self.selections.all::<Point>(cx);
231
232 let mut target_left;
233 let mut target_right;
234
235 if self
236 .highlighted_display_row_for_autoscroll(&display_map)
237 .is_none()
238 {
239 target_left = px(f32::INFINITY);
240 target_right = px(0.);
241 for selection in selections {
242 let head = selection.head().to_display_point(&display_map);
243 if head.row() >= start_row
244 && head.row() < DisplayRow(start_row.0 + layouts.len() as u32)
245 {
246 let start_column = head.column().saturating_sub(3);
247 let end_column = cmp::min(display_map.line_len(head.row()), head.column() + 3);
248 target_left = target_left.min(
249 layouts[head.row().minus(start_row) as usize]
250 .x_for_index(start_column as usize),
251 );
252 target_right = target_right.max(
253 layouts[head.row().minus(start_row) as usize]
254 .x_for_index(end_column as usize)
255 + max_glyph_width,
256 );
257 }
258 }
259 } else {
260 target_left = px(0.);
261 target_right = px(0.);
262 }
263
264 target_right = target_right.min(scroll_width);
265
266 if target_right - target_left > viewport_width {
267 return false;
268 }
269
270 let scroll_left = self.scroll_manager.anchor.offset.x * max_glyph_width;
271 let scroll_right = scroll_left + viewport_width;
272
273 if target_left < scroll_left {
274 self.scroll_manager.anchor.offset.x = target_left / max_glyph_width;
275 true
276 } else if target_right > scroll_right {
277 self.scroll_manager.anchor.offset.x = (target_right - viewport_width) / max_glyph_width;
278 true
279 } else {
280 false
281 }
282 }
283
284 pub fn request_autoscroll(&mut self, autoscroll: Autoscroll, cx: &mut ViewContext<Self>) {
285 self.scroll_manager.autoscroll_request = Some((autoscroll, true));
286 cx.notify();
287 }
288
289 pub(crate) fn request_autoscroll_remotely(
290 &mut self,
291 autoscroll: Autoscroll,
292 cx: &mut ViewContext<Self>,
293 ) {
294 self.scroll_manager.autoscroll_request = Some((autoscroll, false));
295 cx.notify();
296 }
297}