1use std::{cmp, f32};
2
3use gpui::{px, Bounds, Pixels, ViewContext};
4use language::Point;
5
6use crate::{display_map::ToDisplayPoint, Editor, EditorMode, LineWithInvisibles};
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 neweset 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_vertically(
65 &mut self,
66 bounds: Bounds<Pixels>,
67 line_height: Pixels,
68 cx: &mut ViewContext<Editor>,
69 ) -> bool {
70 let viewport_height = bounds.size.height;
71 let visible_lines = viewport_height / line_height;
72 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
73 let mut scroll_position = self.scroll_manager.scroll_position(&display_map);
74 let original_y = scroll_position.y;
75 if let Some(last_bounds) = self.expect_bounds_change.take() {
76 if scroll_position.y != 0. {
77 scroll_position.y += (bounds.top() - last_bounds.top()) / line_height;
78 if scroll_position.y < 0. {
79 scroll_position.y = 0.;
80 }
81 }
82 }
83 let max_scroll_top = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
84 (display_map.max_point().row() as f32 - visible_lines + 1.).max(0.)
85 } else {
86 display_map.max_point().row() as f32
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) = &self.highlighted_display_rows(cx).first_entry() {
103 target_top = *first_highlighted_row.key() as f32;
104 target_bottom = target_top + 1.;
105 } else {
106 let selections = self.selections.all::<Point>(cx);
107 target_top = selections
108 .first()
109 .unwrap()
110 .head()
111 .to_display_point(&display_map)
112 .row() as f32;
113 target_bottom = selections
114 .last()
115 .unwrap()
116 .head()
117 .to_display_point(&display_map)
118 .row() as f32
119 + 1.0;
120
121 // If the selections can't all fit on screen, scroll to the newest.
122 if autoscroll == Autoscroll::newest()
123 || autoscroll == Autoscroll::fit() && target_bottom - target_top > visible_lines
124 {
125 let newest_selection_top = selections
126 .iter()
127 .max_by_key(|s| s.id)
128 .unwrap()
129 .head()
130 .to_display_point(&display_map)
131 .row() as f32;
132 target_top = newest_selection_top;
133 target_bottom = newest_selection_top + 1.;
134 }
135 }
136
137 let margin = if matches!(self.mode, EditorMode::AutoHeight { .. }) {
138 0.
139 } else {
140 ((visible_lines - (target_bottom - target_top)) / 2.0).floor()
141 };
142
143 let strategy = match autoscroll {
144 Autoscroll::Strategy(strategy) => strategy,
145 Autoscroll::Next => {
146 let last_autoscroll = &self.scroll_manager.last_autoscroll;
147 if let Some(last_autoscroll) = last_autoscroll {
148 if self.scroll_manager.anchor.offset == last_autoscroll.0
149 && target_top == last_autoscroll.1
150 && target_bottom == last_autoscroll.2
151 {
152 last_autoscroll.3.next()
153 } else {
154 AutoscrollStrategy::default()
155 }
156 } else {
157 AutoscrollStrategy::default()
158 }
159 }
160 };
161
162 match strategy {
163 AutoscrollStrategy::Fit | AutoscrollStrategy::Newest => {
164 let margin = margin.min(self.scroll_manager.vertical_scroll_margin);
165 let target_top = (target_top - margin).max(0.0);
166 let target_bottom = target_bottom + margin;
167 let start_row = scroll_position.y;
168 let end_row = start_row + visible_lines;
169
170 let needs_scroll_up = target_top < start_row;
171 let needs_scroll_down = target_bottom >= end_row;
172
173 if needs_scroll_up && !needs_scroll_down {
174 scroll_position.y = target_top;
175 self.set_scroll_position_internal(scroll_position, local, true, cx);
176 }
177 if !needs_scroll_up && needs_scroll_down {
178 scroll_position.y = target_bottom - visible_lines;
179 self.set_scroll_position_internal(scroll_position, local, true, cx);
180 }
181 }
182 AutoscrollStrategy::Center => {
183 scroll_position.y = (target_top - margin).max(0.0);
184 self.set_scroll_position_internal(scroll_position, local, true, cx);
185 }
186 AutoscrollStrategy::Focused => {
187 scroll_position.y =
188 (target_top - self.scroll_manager.vertical_scroll_margin).max(0.0);
189 self.set_scroll_position_internal(scroll_position, local, true, cx);
190 }
191 AutoscrollStrategy::Top => {
192 scroll_position.y = (target_top).max(0.0);
193 self.set_scroll_position_internal(scroll_position, local, true, cx);
194 }
195 AutoscrollStrategy::Bottom => {
196 scroll_position.y = (target_bottom - visible_lines).max(0.0);
197 self.set_scroll_position_internal(scroll_position, local, true, cx);
198 }
199 AutoscrollStrategy::TopRelative(lines) => {
200 scroll_position.y = target_top - lines as f32;
201 self.set_scroll_position_internal(scroll_position, local, true, cx);
202 }
203 }
204
205 self.scroll_manager.last_autoscroll = Some((
206 self.scroll_manager.anchor.offset,
207 target_top,
208 target_bottom,
209 strategy,
210 ));
211
212 true
213 }
214
215 pub(crate) fn autoscroll_horizontally(
216 &mut self,
217 start_row: u32,
218 viewport_width: Pixels,
219 scroll_width: Pixels,
220 max_glyph_width: Pixels,
221 layouts: &[LineWithInvisibles],
222 cx: &mut ViewContext<Self>,
223 ) -> bool {
224 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
225 let selections = self.selections.all::<Point>(cx);
226
227 let mut target_left;
228 let mut target_right;
229
230 if self.highlighted_rows.is_empty() {
231 target_left = px(f32::INFINITY);
232 target_right = px(0.);
233 for selection in selections {
234 let head = selection.head().to_display_point(&display_map);
235 if head.row() >= start_row && head.row() < start_row + layouts.len() as u32 {
236 let start_column = head.column().saturating_sub(3);
237 let end_column = cmp::min(display_map.line_len(head.row()), head.column() + 3);
238 target_left = target_left.min(
239 layouts[(head.row() - start_row) as usize]
240 .line
241 .x_for_index(start_column as usize),
242 );
243 target_right = target_right.max(
244 layouts[(head.row() - start_row) as usize]
245 .line
246 .x_for_index(end_column as usize)
247 + max_glyph_width,
248 );
249 }
250 }
251 } else {
252 target_left = px(0.);
253 target_right = px(0.);
254 }
255
256 target_right = target_right.min(scroll_width);
257
258 if target_right - target_left > viewport_width {
259 return false;
260 }
261
262 let scroll_left = self.scroll_manager.anchor.offset.x * max_glyph_width;
263 let scroll_right = scroll_left + viewport_width;
264
265 if target_left < scroll_left {
266 self.scroll_manager.anchor.offset.x = target_left / max_glyph_width;
267 true
268 } else if target_right > scroll_right {
269 self.scroll_manager.anchor.offset.x = (target_right - viewport_width) / max_glyph_width;
270 true
271 } else {
272 false
273 }
274 }
275
276 pub fn request_autoscroll(&mut self, autoscroll: Autoscroll, cx: &mut ViewContext<Self>) {
277 self.scroll_manager.autoscroll_request = Some((autoscroll, true));
278 cx.notify();
279 }
280
281 pub(crate) fn request_autoscroll_remotely(
282 &mut self,
283 autoscroll: Autoscroll,
284 cx: &mut ViewContext<Self>,
285 ) {
286 self.scroll_manager.autoscroll_request = Some((autoscroll, false));
287 cx.notify();
288 }
289}