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