1use std::{panic::Location, rc::Rc};
2
3use crate::{InteractiveElementExt as _, StyledExt};
4
5use super::{Scrollbar, ScrollbarAxis, ScrollbarHandle};
6use gpui::{
7 App, Div, Element, ElementId, InteractiveElement, IntoElement, ParentElement, RenderOnce,
8 ScrollHandle, Stateful, StatefulInteractiveElement, StyleRefinement, Styled, Window, div,
9 prelude::FluentBuilder,
10};
11
12pub trait ScrollableElement: InteractiveElement + Styled + ParentElement + Element {
17 #[track_caller]
19 fn scrollbar<H: ScrollbarHandle + Clone>(
20 self,
21 scroll_handle: &H,
22 axis: impl Into<ScrollbarAxis>,
23 ) -> Self {
24 self.child(ScrollbarLayer {
25 id: caller_id(),
26 axis: axis.into(),
27 scroll_handle: Rc::new(scroll_handle.clone()),
28 })
29 }
30
31 #[track_caller]
33 fn vertical_scrollbar<H: ScrollbarHandle + Clone>(self, scroll_handle: &H) -> Self {
34 self.scrollbar(scroll_handle, ScrollbarAxis::Vertical)
35 }
36
37 #[track_caller]
39 fn horizontal_scrollbar<H: ScrollbarHandle + Clone>(self, scroll_handle: &H) -> Self {
40 self.scrollbar(scroll_handle, ScrollbarAxis::Horizontal)
41 }
42
43 #[track_caller]
46 fn overflow_scrollbar(self) -> Scrollable<Self> {
47 Scrollable::new(self, ScrollbarAxis::Both)
48 }
49
50 #[track_caller]
53 fn overflow_x_scrollbar(self) -> Scrollable<Self> {
54 Scrollable::new(self, ScrollbarAxis::Horizontal)
55 }
56
57 #[track_caller]
60 fn overflow_y_scrollbar(self) -> Scrollable<Self> {
61 Scrollable::new(self, ScrollbarAxis::Vertical)
62 }
63}
64
65#[derive(IntoElement)]
67pub struct Scrollable<E: InteractiveElement + Styled + ParentElement + Element> {
68 id: ElementId,
69 element: E,
70 axis: ScrollbarAxis,
71}
72
73impl<E> Scrollable<E>
74where
75 E: InteractiveElement + Styled + ParentElement + Element,
76{
77 #[track_caller]
78 fn new(element: E, axis: impl Into<ScrollbarAxis>) -> Self {
79 Self {
80 id: caller_id(),
81 element,
82 axis: axis.into(),
83 }
84 }
85
86 pub fn id(mut self, id: impl Into<ElementId>) -> Self {
91 self.id = id.into();
92 self
93 }
94}
95
96impl<E> Styled for Scrollable<E>
97where
98 E: InteractiveElement + Styled + ParentElement + Element,
99{
100 fn style(&mut self) -> &mut StyleRefinement {
101 self.element.style()
102 }
103}
104
105impl<E> ParentElement for Scrollable<E>
106where
107 E: InteractiveElement + Styled + ParentElement + Element,
108{
109 fn extend(&mut self, elements: impl IntoIterator<Item = gpui::AnyElement>) {
110 self.element.extend(elements)
111 }
112}
113
114impl<E> InteractiveElement for Scrollable<E>
115where
116 E: InteractiveElement + Styled + ParentElement + Element,
117{
118 fn interactivity(&mut self) -> &mut gpui::Interactivity {
119 self.element.interactivity()
120 }
121}
122
123impl<E> RenderOnce for Scrollable<E>
124where
125 E: InteractiveElement + Styled + ParentElement + Element + 'static,
126{
127 fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
128 let scroll_handle = scroll_handle_for(&self.id, window, cx);
129
130 let root_style = root_style_from(&mut self.element);
133
134 let root_id = self.id.clone();
135 let area_id = (self.id.clone(), "area");
136 let content_id = (self.id.clone(), "content");
137 let scrollbar_id = (self.id.clone(), "scrollbar");
138
139 let content = self
140 .element
141 .id(content_id)
142 .flex_none()
143 .map(|this| match self.axis {
144 ScrollbarAxis::Vertical => this.h_auto().min_h_full(),
145 ScrollbarAxis::Horizontal => this.w_auto().min_w_full(),
146 ScrollbarAxis::Both => this.size_auto().min_size_full(),
147 });
148
149 let scroll_area = div()
154 .id(area_id)
155 .size_full()
156 .flex()
157 .track_scroll(&scroll_handle)
158 .map(|this| match self.axis {
159 ScrollbarAxis::Vertical => this.flex_col().overflow_y_scroll(),
160 ScrollbarAxis::Horizontal => this.flex_row().overflow_x_scroll(),
161 ScrollbarAxis::Both => this.overflow_scroll(),
162 })
163 .lock_scroll_axis()
166 .child(content);
167
168 div()
169 .id(root_id)
170 .size_full()
171 .refine_style(&root_style)
172 .relative()
173 .child(scroll_area)
174 .child(render_scrollbar(
175 scrollbar_id,
176 &scroll_handle,
177 self.axis,
178 window,
179 cx,
180 ))
181 }
182}
183
184impl ScrollableElement for Div {}
185impl<E> ScrollableElement for Stateful<E>
186where
187 E: ParentElement + Styled + Element,
188 Self: InteractiveElement,
189{
190}
191
192#[derive(IntoElement)]
193struct ScrollbarLayer<H: ScrollbarHandle + Clone> {
194 id: ElementId,
195 axis: ScrollbarAxis,
196 scroll_handle: Rc<H>,
197}
198
199impl<H> RenderOnce for ScrollbarLayer<H>
200where
201 H: ScrollbarHandle + Clone + 'static,
202{
203 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
204 render_scrollbar(self.id, self.scroll_handle.as_ref(), self.axis, window, cx)
205 }
206}
207
208#[inline]
209#[track_caller]
210pub(super) fn caller_id() -> ElementId {
211 ElementId::CodeLocation(*Location::caller())
212}
213
214#[inline]
215fn scroll_handle_for(id: &ElementId, window: &mut Window, cx: &mut App) -> ScrollHandle {
216 window
217 .use_keyed_state(id.clone(), cx, |_, _| ScrollHandle::default())
218 .read(cx)
219 .clone()
220}
221
222#[inline]
225fn root_style_from<E>(element: &mut E) -> StyleRefinement
226where
227 E: Styled,
228{
229 let style = element.style();
230 StyleRefinement {
231 size: style.size.clone(),
232 min_size: style.min_size.clone(),
233 max_size: style.max_size.clone(),
234 flex_grow: style.flex_grow,
235 flex_shrink: style.flex_shrink,
236 flex_basis: style.flex_basis,
237 align_self: style.align_self,
238 ..Default::default()
239 }
240}
241
242#[inline]
243fn render_scrollbar<H: ScrollbarHandle + Clone>(
244 id: impl Into<ElementId>,
245 scroll_handle: &H,
246 axis: ScrollbarAxis,
247 window: &mut Window,
248 cx: &mut App,
249) -> Div {
250 let is_inspector_picking = window.is_inspector_picking(cx);
253 if is_inspector_picking {
254 return div();
255 }
256
257 div()
258 .absolute()
259 .inset_0()
260 .debug_selector(|| "scrollbar-overlay".to_string())
261 .child(
262 Scrollbar::new(scroll_handle)
263 .id(id)
264 .axis(axis)
265 .viewport_from_layout(),
266 )
267}
268
269#[cfg(test)]
270mod tests {
271 use super::*;
272 use gpui::{
273 Context, Render, ScrollDelta, ScrollWheelEvent, TestAppContext, VisualTestContext, point,
274 px,
275 };
276
277 fn draw(cx: &mut VisualTestContext) {
278 cx.run_until_parked();
279 cx.update(|window, cx| {
280 _ = window.draw(cx);
281 });
282 }
283
284 fn scroll(cx: &mut VisualTestContext, x: f32, y: f32, dx: f32, dy: f32) {
285 cx.simulate_event(ScrollWheelEvent {
286 position: point(px(x), px(y)),
287 delta: ScrollDelta::Pixels(point(px(dx), px(dy))),
288 ..Default::default()
289 });
290 draw(cx);
291 }
292
293 fn row(selector: &'static str, height: f32) -> Div {
294 div()
295 .h(px(height))
296 .flex_shrink_0()
297 .debug_selector(move || selector.to_string())
298 }
299
300 fn plain_row(height: f32) -> Div {
301 div().h(px(height)).flex_shrink_0()
302 }
303
304 fn item(selector: &'static str, width: f32) -> Div {
305 div()
306 .w(px(width))
307 .h(px(20.))
308 .flex_shrink_0()
309 .debug_selector(move || selector.to_string())
310 }
311
312 fn plain_item(width: f32) -> Div {
313 div().w(px(width)).h(px(20.)).flex_shrink_0()
314 }
315
316 struct SizeFullChildTest;
317
318 impl Render for SizeFullChildTest {
319 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
320 div()
321 .w(px(100.))
322 .h(px(100.))
323 .overflow_y_scrollbar()
324 .child(
325 div()
326 .size_full()
327 .child(crate::v_flex().children((0..4).map(|ix| {
328 div().h(px(50.)).flex_shrink_0().when(ix == 3, |this| {
329 this.debug_selector(|| "last-row".to_string())
330 })
331 }))),
332 )
333 }
334 }
335
336 struct AutoHeightParentTest;
337
338 impl Render for AutoHeightParentTest {
339 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
340 div().size_full().child(
346 crate::v_flex()
347 .w(px(200.))
348 .child(
349 crate::v_flex().flex_1().overflow_hidden().child(
350 div().flex_1().overflow_hidden().child(
351 crate::v_flex()
352 .size_full()
353 .overflow_y_scrollbar()
354 .child(plain_row(50.))
355 .child(plain_row(50.)),
356 ),
357 ),
358 )
359 .child(row("auto-height-footer", 10.)),
360 )
361 }
362 }
363
364 struct MaxHeightParentTest;
365
366 impl Render for MaxHeightParentTest {
367 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
368 crate::v_flex()
371 .w(px(200.))
372 .max_h(px(100.))
373 .child(
374 crate::v_flex().flex_1().overflow_hidden().child(
375 div().flex_1().overflow_hidden().child(
376 crate::v_flex()
377 .size_full()
378 .overflow_y_scrollbar()
379 .child(plain_row(50.))
380 .child(plain_row(50.))
381 .child(row("max-height-last-row", 50.)),
382 ),
383 ),
384 )
385 .child(row("max-height-footer", 10.))
386 }
387 }
388
389 #[gpui::test]
390 fn auto_height_parent_gets_content_height(cx: &mut TestAppContext) {
391 cx.update(crate::init);
392 let (_, cx) = cx.add_window_view(|_, _| AutoHeightParentTest);
393 let cx: &mut VisualTestContext = cx;
394 draw(cx);
395
396 let footer = cx.debug_bounds("auto-height-footer").unwrap();
398 assert_eq!(footer.top(), px(100.));
399 }
400
401 #[gpui::test]
402 fn max_height_parent_clamps_and_scrolls(cx: &mut TestAppContext) {
403 cx.update(crate::init);
404 let (_, cx) = cx.add_window_view(|_, _| MaxHeightParentTest);
405 let cx: &mut VisualTestContext = cx;
406 draw(cx);
407
408 let footer = cx.debug_bounds("max-height-footer").unwrap();
411 assert_eq!(footer.top(), px(90.));
412
413 let last_initial_y = cx.debug_bounds("max-height-last-row").unwrap().origin.y;
414 scroll(cx, 10., 10., 0., -50.);
415 assert!(cx.debug_bounds("max-height-last-row").unwrap().origin.y < last_initial_y);
416 }
417
418 struct GapLayoutTest;
419
420 struct PaddedScrollbarOverlayTest;
421
422 impl Render for PaddedScrollbarOverlayTest {
423 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
424 crate::v_flex()
425 .w(px(100.))
426 .h(px(100.))
427 .p(px(20.))
428 .overflow_y_scrollbar()
429 .children((0..4).map(|_| plain_row(50.)))
430 }
431 }
432
433 #[gpui::test]
434 fn scrollbar_overlay_ignores_content_padding(cx: &mut TestAppContext) {
435 cx.update(crate::init);
436 let (_, cx) = cx.add_window_view(|_, _| PaddedScrollbarOverlayTest);
437 let cx: &mut VisualTestContext = cx;
438 draw(cx);
439
440 let overlay = cx.debug_bounds("scrollbar-overlay").unwrap();
441 assert_eq!(overlay.origin, point(px(0.), px(0.)));
442 assert_eq!(overlay.size, gpui::size(px(100.), px(100.)));
443 }
444
445 impl Render for GapLayoutTest {
446 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
447 crate::v_flex()
448 .w(px(100.))
449 .h(px(100.))
450 .gap(px(10.))
451 .overflow_y_scrollbar()
452 .child(row("first-row", 20.))
453 .child(row("second-row", 20.))
454 }
455 }
456
457 struct IssueGapRegressionTest;
458
459 impl Render for IssueGapRegressionTest {
460 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
461 div().w(px(100.)).h(px(100.)).child(
462 crate::v_flex()
463 .flex_1()
464 .gap(px(30.))
465 .overflow_y_scrollbar()
466 .px(px(12.))
467 .pb(px(16.))
468 .children((0..5).map(|ix| {
469 div()
470 .h(px(20.))
471 .flex_shrink_0()
472 .when(ix == 0, |this| {
473 this.debug_selector(|| "issue-first-card".to_string())
474 })
475 .when(ix == 1, |this| {
476 this.debug_selector(|| "issue-second-card".to_string())
477 })
478 .when(ix == 4, |this| {
479 this.debug_selector(|| "issue-last-card".to_string())
480 })
481 })),
482 )
483 }
484 }
485
486 struct HorizontalGapLayoutTest;
487
488 impl Render for HorizontalGapLayoutTest {
489 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
490 crate::h_flex()
491 .w(px(100.))
492 .h(px(40.))
493 .gap(px(10.))
494 .overflow_x_scrollbar()
495 .child(item("horizontal-first-item", 50.))
496 .child(item("horizontal-second-item", 50.))
497 .child(item("horizontal-last-item", 50.))
498 }
499 }
500
501 struct OverflowScrollbarVerticalTest;
502
503 impl Render for OverflowScrollbarVerticalTest {
504 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
505 crate::v_flex()
506 .w(px(100.))
507 .h(px(100.))
508 .gap(px(10.))
509 .overflow_scrollbar()
510 .child(row("both-axis-vertical-first-row", 50.))
511 .child(row("both-axis-vertical-second-row", 50.))
512 .child(row("both-axis-vertical-last-row", 50.))
513 }
514 }
515
516 struct OverflowScrollbarHorizontalTest;
517
518 impl Render for OverflowScrollbarHorizontalTest {
519 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
520 crate::h_flex()
521 .w(px(100.))
522 .h(px(40.))
523 .gap(px(10.))
524 .overflow_scrollbar()
525 .child(item("both-axis-horizontal-first-item", 50.))
526 .child(item("both-axis-horizontal-second-item", 50.))
527 .child(item("both-axis-horizontal-last-item", 50.))
528 }
529 }
530
531 struct IndependentScrollablesTest;
532
533 impl Render for IndependentScrollablesTest {
534 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
535 crate::h_flex()
536 .w(px(220.))
537 .h(px(100.))
538 .gap(px(20.))
539 .child(
540 div().w(px(100.)).h(px(100.)).overflow_y_scrollbar().child(
541 crate::v_flex()
542 .child(plain_row(50.))
543 .child(plain_row(50.))
544 .child(row("left-scrollable-last-row", 50.)),
545 ),
546 )
547 .child(
548 div().w(px(100.)).h(px(100.)).overflow_y_scrollbar().child(
549 crate::v_flex()
550 .child(plain_row(50.))
551 .child(plain_row(50.))
552 .child(row("right-scrollable-last-row", 50.)),
553 ),
554 )
555 }
556 }
557
558 struct NoOverflowTest;
559
560 impl Render for NoOverflowTest {
561 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
562 crate::v_flex()
563 .w(px(100.))
564 .h(px(100.))
565 .gap(px(10.))
566 .overflow_y_scrollbar()
567 .child(row("no-overflow-first-row", 20.))
568 .child(row("no-overflow-second-row", 20.))
569 }
570 }
571
572 #[gpui::test]
573 fn vertical_scrollbar_scrolls_past_a_size_full_child(cx: &mut TestAppContext) {
574 cx.update(crate::init);
575 let (_, cx) = cx.add_window_view(|_, _| SizeFullChildTest);
576 let cx: &mut VisualTestContext = cx;
577 draw(cx);
578
579 let initial_y = cx.debug_bounds("last-row").unwrap().origin.y;
580 scroll(cx, 10., 10., 0., -50.);
581
582 assert!(cx.debug_bounds("last-row").unwrap().origin.y < initial_y);
583 }
584
585 #[gpui::test]
586 fn vertical_scrollbar_preserves_source_gap(cx: &mut TestAppContext) {
587 cx.update(crate::init);
588 let (_, cx) = cx.add_window_view(|_, _| GapLayoutTest);
589 let cx: &mut VisualTestContext = cx;
590 draw(cx);
591
592 let first = cx.debug_bounds("first-row").unwrap();
593 let second = cx.debug_bounds("second-row").unwrap();
594 assert_eq!(second.top() - first.bottom(), px(10.));
595 }
596
597 #[gpui::test]
598 fn overflow_y_scrollbar_preserves_gap_for_exact_issue_chain(cx: &mut TestAppContext) {
599 cx.update(crate::init);
600 let (_, cx) = cx.add_window_view(|_, _| IssueGapRegressionTest);
601 let cx: &mut VisualTestContext = cx;
602 draw(cx);
603
604 let first = cx.debug_bounds("issue-first-card").unwrap();
605 let second = cx.debug_bounds("issue-second-card").unwrap();
606 let last_initial_y = cx.debug_bounds("issue-last-card").unwrap().origin.y;
607
608 assert_eq!(second.top() - first.bottom(), px(30.));
609 assert_eq!(first.left(), px(12.));
610
611 scroll(cx, 10., 10., 0., -50.);
612
613 let first_after_scroll = cx.debug_bounds("issue-first-card").unwrap();
614 let second_after_scroll = cx.debug_bounds("issue-second-card").unwrap();
615 let last_after_scroll_y = cx.debug_bounds("issue-last-card").unwrap().origin.y;
616
617 assert_eq!(
618 second_after_scroll.top() - first_after_scroll.bottom(),
619 px(30.)
620 );
621 assert_eq!(first_after_scroll.left(), px(12.));
622 assert!(last_after_scroll_y < last_initial_y);
623 }
624
625 #[gpui::test]
626 fn horizontal_scrollbar_preserves_source_gap_and_scrolls(cx: &mut TestAppContext) {
627 cx.update(crate::init);
628 let (_, cx) = cx.add_window_view(|_, _| HorizontalGapLayoutTest);
629 let cx: &mut VisualTestContext = cx;
630 draw(cx);
631
632 let first = cx.debug_bounds("horizontal-first-item").unwrap();
633 let second = cx.debug_bounds("horizontal-second-item").unwrap();
634 let last_initial_x = cx.debug_bounds("horizontal-last-item").unwrap().origin.x;
635
636 assert_eq!(second.left() - first.right(), px(10.));
637
638 scroll(cx, 10., 10., -50., 0.);
639
640 let first_after_scroll = cx.debug_bounds("horizontal-first-item").unwrap();
641 let second_after_scroll = cx.debug_bounds("horizontal-second-item").unwrap();
642 let last_after_scroll_x = cx.debug_bounds("horizontal-last-item").unwrap().origin.x;
643
644 assert_eq!(
645 second_after_scroll.left() - first_after_scroll.right(),
646 px(10.)
647 );
648 assert!(last_after_scroll_x < last_initial_x);
649 }
650
651 #[gpui::test]
652 fn overflow_scrollbar_preserves_vertical_source_gap(cx: &mut TestAppContext) {
653 cx.update(crate::init);
654 let (_, cx) = cx.add_window_view(|_, _| OverflowScrollbarVerticalTest);
655 let cx: &mut VisualTestContext = cx;
656 draw(cx);
657
658 let first = cx.debug_bounds("both-axis-vertical-first-row").unwrap();
659 let second = cx.debug_bounds("both-axis-vertical-second-row").unwrap();
660
661 assert_eq!(second.top() - first.bottom(), px(10.));
662 }
663
664 #[gpui::test]
665 fn overflow_scrollbar_preserves_gap_and_scrolls_horizontally(cx: &mut TestAppContext) {
666 cx.update(crate::init);
667 let (_, cx) = cx.add_window_view(|_, _| OverflowScrollbarHorizontalTest);
668 let cx: &mut VisualTestContext = cx;
669 draw(cx);
670
671 let first = cx.debug_bounds("both-axis-horizontal-first-item").unwrap();
672 let second = cx.debug_bounds("both-axis-horizontal-second-item").unwrap();
673 let last_initial_x = cx
674 .debug_bounds("both-axis-horizontal-last-item")
675 .unwrap()
676 .origin
677 .x;
678
679 assert_eq!(second.left() - first.right(), px(10.));
680
681 scroll(cx, 10., 10., -50., 0.);
682
683 let first_after_scroll = cx.debug_bounds("both-axis-horizontal-first-item").unwrap();
684 let second_after_scroll = cx.debug_bounds("both-axis-horizontal-second-item").unwrap();
685 let last_after_scroll_x = cx
686 .debug_bounds("both-axis-horizontal-last-item")
687 .unwrap()
688 .origin
689 .x;
690
691 assert_eq!(
692 second_after_scroll.left() - first_after_scroll.right(),
693 px(10.)
694 );
695 assert!(last_after_scroll_x < last_initial_x);
696 }
697
698 #[gpui::test]
699 fn multiple_scrollables_keep_independent_scroll_state(cx: &mut TestAppContext) {
700 cx.update(crate::init);
701 let (_, cx) = cx.add_window_view(|_, _| IndependentScrollablesTest);
702 let cx: &mut VisualTestContext = cx;
703 draw(cx);
704
705 let left_initial = cx.debug_bounds("left-scrollable-last-row").unwrap();
706 let right_initial = cx.debug_bounds("right-scrollable-last-row").unwrap();
707
708 scroll(cx, 10., 10., 0., -50.);
709
710 let left_after_scroll = cx.debug_bounds("left-scrollable-last-row").unwrap();
711 let right_after_scroll = cx.debug_bounds("right-scrollable-last-row").unwrap();
712
713 assert!(left_after_scroll.top() < left_initial.top());
714 assert_eq!(right_after_scroll.top(), right_initial.top());
715 }
716
717 #[gpui::test]
718 fn vertical_scrollbar_does_not_scroll_when_content_does_not_overflow(cx: &mut TestAppContext) {
719 cx.update(crate::init);
720 let (_, cx) = cx.add_window_view(|_, _| NoOverflowTest);
721 let cx: &mut VisualTestContext = cx;
722 draw(cx);
723
724 let first = cx.debug_bounds("no-overflow-first-row").unwrap();
725 let second = cx.debug_bounds("no-overflow-second-row").unwrap();
726
727 assert_eq!(second.top() - first.bottom(), px(10.));
728
729 scroll(cx, 10., 10., 0., -50.);
730
731 let first_after_scroll = cx.debug_bounds("no-overflow-first-row").unwrap();
732 let second_after_scroll = cx.debug_bounds("no-overflow-second-row").unwrap();
733
734 assert_eq!(first_after_scroll.top(), first.top());
735 assert_eq!(second_after_scroll.top(), second.top());
736 assert_eq!(
737 second_after_scroll.top() - first_after_scroll.bottom(),
738 px(10.)
739 );
740 }
741
742 #[gpui::test]
743 fn horizontal_scrollbar_does_not_scroll_when_content_does_not_overflow(
744 cx: &mut TestAppContext,
745 ) {
746 struct HorizontalNoOverflowTest;
747
748 impl Render for HorizontalNoOverflowTest {
749 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
750 crate::h_flex()
751 .w(px(100.))
752 .h(px(40.))
753 .gap(px(10.))
754 .overflow_x_scrollbar()
755 .child(item("no-overflow-first-item", 20.))
756 .child(item("no-overflow-second-item", 20.))
757 .child(plain_item(20.))
758 }
759 }
760
761 cx.update(crate::init);
762 let (_, cx) = cx.add_window_view(|_, _| HorizontalNoOverflowTest);
763 let cx: &mut VisualTestContext = cx;
764 draw(cx);
765
766 let first = cx.debug_bounds("no-overflow-first-item").unwrap();
767 let second = cx.debug_bounds("no-overflow-second-item").unwrap();
768
769 assert_eq!(second.left() - first.right(), px(10.));
770
771 scroll(cx, 10., 10., -50., 0.);
772
773 let first_after_scroll = cx.debug_bounds("no-overflow-first-item").unwrap();
774 let second_after_scroll = cx.debug_bounds("no-overflow-second-item").unwrap();
775
776 assert_eq!(first_after_scroll.left(), first.left());
777 assert_eq!(second_after_scroll.left(), second.left());
778 assert_eq!(
779 second_after_scroll.left() - first_after_scroll.right(),
780 px(10.)
781 );
782 }
783 struct IndependentDynamicScrollablesTest;
784
785 impl Render for IndependentDynamicScrollablesTest {
786 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
787 crate::v_flex().w(px(100.)).children((0..2).map(|ix| {
788 crate::h_flex()
789 .w(px(100.))
790 .h(px(40.))
791 .overflow_x_scrollbar()
792 .id(format!("dynamic-scroll-{ix}"))
793 .child(
794 div()
795 .w(px(200.))
796 .h(px(20.))
797 .flex_shrink_0()
798 .when(ix == 0, |this| {
799 this.debug_selector(|| "dynamic-scroll-first".to_string())
800 })
801 .when(ix == 1, |this| {
802 this.debug_selector(|| "dynamic-scroll-second".to_string())
803 }),
804 )
805 }))
806 }
807 }
808
809 #[gpui::test]
810 fn dynamic_scrollables_with_unique_scroll_ids_keep_independent_state(cx: &mut TestAppContext) {
811 cx.update(crate::init);
812
813 let (_, cx) = cx.add_window_view(|_, _| IndependentDynamicScrollablesTest);
814 let cx: &mut VisualTestContext = cx;
815
816 draw(cx);
817
818 let first_initial = cx.debug_bounds("dynamic-scroll-first").unwrap();
819
820 let second_initial = cx.debug_bounds("dynamic-scroll-second").unwrap();
821
822 scroll(cx, 10., 10., -50., 0.);
824
825 let first_after_scroll = cx.debug_bounds("dynamic-scroll-first").unwrap();
826
827 let second_after_scroll = cx.debug_bounds("dynamic-scroll-second").unwrap();
828
829 assert!(first_after_scroll.left() < first_initial.left());
831
832 assert_eq!(second_after_scroll.left(), second_initial.left());
834 }
835}