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