| 1 | use std::{panic::Location, rc::Rc}; |
| 2 | |
| 3 | use crate::{InteractiveElementExt as _, StyledExt}; |
| 4 | |
| 5 | use super::{Scrollbar, ScrollbarAxis, ScrollbarHandle}; |
| 6 | use gpui::{ |
| 7 | App, Div, Element, ElementId, InteractiveElement, IntoElement, Overflow, ParentElement, |
| 8 | PointRefinement, RenderOnce, ScrollHandle, Stateful, StatefulInteractiveElement, |
| 9 | StyleRefinement, Styled, Window, div, prelude::FluentBuilder, |
| 10 | }; |
| 11 | |
| 12 | /// A trait for elements that can be made scrollable with scrollbars. |
| 13 | /// |
| 14 | /// The wrapped element is the scroll area itself, rather than being inserted as |
| 15 | /// a child of a new scroll area. |
| 16 | pub trait ScrollableElement: InteractiveElement + Styled + ParentElement + Element { |
| 17 | /// Adds a scrollbar to the element. |
| 18 | #[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 | /// Adds a vertical scrollbar to the element. |
| 32 | #[track_caller] |
| 33 | fn vertical_scrollbar<H: ScrollbarHandle + Clone>(self, scroll_handle: &H) -> Self { |
| 34 | self.scrollbar(scroll_handle, ScrollbarAxis::Vertical) |
| 35 | } |
| 36 | |
| 37 | /// Adds a horizontal scrollbar to the element. |
| 38 | #[track_caller] |
| 39 | fn horizontal_scrollbar<H: ScrollbarHandle + Clone>(self, scroll_handle: &H) -> Self { |
| 40 | self.scrollbar(scroll_handle, ScrollbarAxis::Horizontal) |
| 41 | } |
| 42 | |
| 43 | /// Almost equivalent to [`StatefulInteractiveElement::overflow_scroll`], but adds scrollbars. |
| 44 | /// Preserves the source element as the scrollable container. |
| 45 | #[track_caller] |
| 46 | fn overflow_scrollbar(self) -> Scrollable<Self> { |
| 47 | Scrollable::new(self, ScrollbarAxis::Both) |
| 48 | } |
| 49 | |
| 50 | /// Almost equivalent to [`StatefulInteractiveElement::overflow_x_scroll`], but adds Horizontal scrollbar. |
| 51 | /// Preserves the source element as the scrollable container. |
| 52 | #[track_caller] |
| 53 | fn overflow_x_scrollbar(self) -> Scrollable<Self> { |
| 54 | Scrollable::new(self, ScrollbarAxis::Horizontal) |
| 55 | } |
| 56 | |
| 57 | /// Almost equivalent to [`StatefulInteractiveElement::overflow_y_scroll`], but adds Vertical scrollbar. |
| 58 | /// Preserves the source element as the scrollable container. |
| 59 | #[track_caller] |
| 60 | fn overflow_y_scrollbar(self) -> Scrollable<Self> { |
| 61 | Scrollable::new(self, ScrollbarAxis::Vertical) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// A scrollable element wrapper that renders the original element as the scroll area and overlays scrollbars. |
| 66 | #[derive(IntoElement)] |
| 67 | pub struct Scrollable<E: InteractiveElement + Styled + ParentElement + Element> { |
| 68 | id: ElementId, |
| 69 | element: E, |
| 70 | axis: ScrollbarAxis, |
| 71 | scroll_handle: Option<ScrollHandle>, |
| 72 | } |
| 73 | |
| 74 | impl<E> Scrollable<E> |
| 75 | where |
| 76 | E: InteractiveElement + Styled + ParentElement + Element, |
| 77 | { |
| 78 | #[track_caller] |
| 79 | fn new(element: E, axis: impl Into<ScrollbarAxis>) -> Self { |
| 80 | Self { |
| 81 | id: caller_id(), |
| 82 | element, |
| 83 | axis: axis.into(), |
| 84 | scroll_handle: None, |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Scroll with the caller's handle instead of one kept per element id, so |
| 89 | /// the caller can read and set the offset. |
| 90 | pub fn track_scroll(mut self, scroll_handle: &ScrollHandle) -> Self { |
| 91 | self.scroll_handle = Some(scroll_handle.clone()); |
| 92 | self |
| 93 | } |
| 94 | |
| 95 | /// Set a specific element id, default is the [`std::panic::Location::caller`]. |
| 96 | /// |
| 97 | /// Only needed when one call site creates several scrollables, which would |
| 98 | /// otherwise share a single scroll position. |
| 99 | pub fn id(mut self, id: impl Into<ElementId>) -> Self { |
| 100 | self.id = id.into(); |
| 101 | self |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl<E> Styled for Scrollable<E> |
| 106 | where |
| 107 | E: InteractiveElement + Styled + ParentElement + Element, |
| 108 | { |
| 109 | fn style(&mut self) -> &mut StyleRefinement { |
| 110 | self.element.style() |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl<E> ParentElement for Scrollable<E> |
| 115 | where |
| 116 | E: InteractiveElement + Styled + ParentElement + Element, |
| 117 | { |
| 118 | fn extend(&mut self, elements: impl IntoIterator<Item = gpui::AnyElement>) { |
| 119 | self.element.extend(elements) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | impl<E> InteractiveElement for Scrollable<E> |
| 124 | where |
| 125 | E: InteractiveElement + Styled + ParentElement + Element, |
| 126 | { |
| 127 | fn interactivity(&mut self) -> &mut gpui::Interactivity { |
| 128 | self.element.interactivity() |
| 129 | } |
| 130 | |
| 131 | fn track_focus(mut self, focus: &gpui::FocusHandle) -> Self { |
| 132 | self.element = self.element.track_focus(focus); |
| 133 | self |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | impl<E> RenderOnce for Scrollable<E> |
| 138 | where |
| 139 | E: InteractiveElement + Styled + ParentElement + Element + 'static, |
| 140 | { |
| 141 | fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement { |
| 142 | let scroll_handle = match self.scroll_handle.take() { |
| 143 | Some(scroll_handle) => scroll_handle, |
| 144 | None => scroll_handle_for(&self.id, window, cx), |
| 145 | }; |
| 146 | |
| 147 | // Preserve the caller-requested size on the wrapper, while keeping the |
| 148 | // caller's element as the actual scroll-tracked layout container. |
| 149 | let root_style = root_style_from(&mut self.element, self.axis); |
| 150 | |
| 151 | let root_id = self.id.clone(); |
| 152 | let area_id = (self.id.clone(), "area"); |
| 153 | let content_id = (self.id.clone(), "content"); |
| 154 | let scrollbar_id = (self.id.clone(), "scrollbar"); |
| 155 | |
| 156 | let content = self |
| 157 | .element |
| 158 | .id(content_id) |
| 159 | .flex_none() |
| 160 | .map(|this| match self.axis { |
| 161 | ScrollbarAxis::Vertical => this.h_auto().min_h_full(), |
| 162 | ScrollbarAxis::Horizontal => this.w_auto().min_w_full(), |
| 163 | ScrollbarAxis::Both => this.size_auto().min_size_full(), |
| 164 | }); |
| 165 | |
| 166 | // Keep the scroll area in the normal flow: its content size must |
| 167 | // propagate to auto-sized ancestors (e.g. a Dialog that grows with |
| 168 | // its content). An absolutely positioned scroll area would collapse |
| 169 | // such ancestors to zero height. |
| 170 | let scroll_area = div() |
| 171 | .id(area_id) |
| 172 | .size_full() |
| 173 | .flex() |
| 174 | .track_scroll(&scroll_handle) |
| 175 | .map(|this| match self.axis { |
| 176 | ScrollbarAxis::Vertical => this.flex_col().overflow_y_scroll(), |
| 177 | ScrollbarAxis::Horizontal => this.flex_row().overflow_x_scroll(), |
| 178 | ScrollbarAxis::Both => this.overflow_scroll(), |
| 179 | }) |
| 180 | // On a single-axis area gpui otherwise remaps the other axis' delta |
| 181 | // onto ours, so a purely horizontal swipe scrolls this vertically. |
| 182 | .lock_scroll_axis() |
| 183 | .child(content); |
| 184 | |
| 185 | div() |
| 186 | .id(root_id) |
| 187 | .size_full() |
| 188 | .refine_style(&root_style) |
| 189 | .relative() |
| 190 | .child(scroll_area) |
| 191 | .child(render_scrollbar( |
| 192 | scrollbar_id, |
| 193 | &scroll_handle, |
| 194 | self.axis, |
| 195 | window, |
| 196 | cx, |
| 197 | )) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | impl ScrollableElement for Div {} |
| 202 | impl<E> ScrollableElement for Stateful<E> |
| 203 | where |
| 204 | E: ParentElement + Styled + Element, |
| 205 | Self: InteractiveElement, |
| 206 | { |
| 207 | } |
| 208 | |
| 209 | #[derive(IntoElement)] |
| 210 | struct ScrollbarLayer<H: ScrollbarHandle + Clone> { |
| 211 | id: ElementId, |
| 212 | axis: ScrollbarAxis, |
| 213 | scroll_handle: Rc<H>, |
| 214 | } |
| 215 | |
| 216 | impl<H> RenderOnce for ScrollbarLayer<H> |
| 217 | where |
| 218 | H: ScrollbarHandle + Clone + 'static, |
| 219 | { |
| 220 | fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { |
| 221 | render_scrollbar(self.id, self.scroll_handle.as_ref(), self.axis, window, cx) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | #[inline] |
| 226 | #[track_caller] |
| 227 | fn caller_id() -> ElementId { |
| 228 | ElementId::CodeLocation(*Location::caller()) |
| 229 | } |
| 230 | |
| 231 | #[inline] |
| 232 | fn scroll_handle_for(id: &ElementId, window: &mut Window, cx: &mut App) -> ScrollHandle { |
| 233 | window |
| 234 | .use_keyed_state(id.clone(), cx, |_, _| ScrollHandle::default()) |
| 235 | .read(cx) |
| 236 | .clone() |
| 237 | } |
| 238 | |
| 239 | /// Copies the outer layout styles from the element, so the wrapper can |
| 240 | /// participate in the parent's layout the same way the source element would. |
| 241 | /// |
| 242 | /// A flex item only drops its content-based automatic minimum size when its own |
| 243 | /// overflow is not [`Overflow::Visible`]; otherwise the item refuses to shrink |
| 244 | /// around its content. The scrolled overflow lives on the inner scroll area, so |
| 245 | /// the wrapper has to declare the scrolled axis clipped itself — without it a |
| 246 | /// scroll region used as a flex item pushes its siblings out of the container |
| 247 | /// instead of scrolling, and every call site has to remember `min_h_0()` / |
| 248 | /// `min_w_0()`. |
| 249 | /// |
| 250 | /// [`Overflow::Hidden`] rather than a zero minimum: it is the axis' real |
| 251 | /// behavior, it leaves an explicit `min_size` from the caller in charge, and it |
| 252 | /// keeps the region's content size contributing to an auto-sized ancestor such |
| 253 | /// as a Dialog that grows with its body. The mask it installs matches the inner |
| 254 | /// scroll area's own mask, so nothing new is clipped. |
| 255 | #[inline] |
| 256 | fn root_style_from<E>(element: &mut E, axis: ScrollbarAxis) -> StyleRefinement |
| 257 | where |
| 258 | E: Styled, |
| 259 | { |
| 260 | let style = element.style(); |
| 261 | StyleRefinement { |
| 262 | size: style.size.clone(), |
| 263 | min_size: style.min_size.clone(), |
| 264 | max_size: style.max_size.clone(), |
| 265 | flex_grow: style.flex_grow, |
| 266 | flex_shrink: style.flex_shrink, |
| 267 | flex_basis: style.flex_basis, |
| 268 | align_self: style.align_self, |
| 269 | overflow: PointRefinement { |
| 270 | x: axis.has_horizontal().then_some(Overflow::Hidden), |
| 271 | y: axis.has_vertical().then_some(Overflow::Hidden), |
| 272 | }, |
| 273 | ..Default::default() |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | #[inline] |
| 278 | fn render_scrollbar<H: ScrollbarHandle + Clone>( |
| 279 | id: impl Into<ElementId>, |
| 280 | scroll_handle: &H, |
| 281 | axis: ScrollbarAxis, |
| 282 | window: &mut Window, |
| 283 | cx: &mut App, |
| 284 | ) -> Div { |
| 285 | // Do not render scrollbar when inspector is picking elements, |
| 286 | // to allow us to pick the background elements. |
| 287 | let is_inspector_picking = window.is_inspector_picking(cx); |
| 288 | if is_inspector_picking { |
| 289 | return div(); |
| 290 | } |
| 291 | |
| 292 | div() |
| 293 | .absolute() |
| 294 | .inset_0() |
| 295 | .debug_selector(|| "scrollbar-overlay".to_string()) |
| 296 | .child( |
| 297 | Scrollbar::new(scroll_handle) |
| 298 | .id(id) |
| 299 | .axis(axis) |
| 300 | .viewport_from_layout(), |
| 301 | ) |
| 302 | } |
| 303 | |
| 304 | #[cfg(feature = "test-support")] |
| 305 | impl<E> ScrollableElement for gpui_base::test_support::Observed<E> where |
| 306 | E: ScrollableElement + Element<PrepaintState = Option<gpui::Hitbox>> |
| 307 | { |
| 308 | } |
| 309 | |
| 310 | #[cfg(test)] |
| 311 | mod tests { |
| 312 | use super::*; |
| 313 | use gpui::{ |
| 314 | Context, Render, ScrollDelta, ScrollWheelEvent, TestAppContext, VisualTestContext, point, |
| 315 | px, |
| 316 | }; |
| 317 | |
| 318 | fn draw(cx: &mut VisualTestContext) { |
| 319 | cx.run_until_parked(); |
| 320 | cx.update(|window, cx| { |
| 321 | _ = window.draw(cx); |
| 322 | }); |
| 323 | } |
| 324 | |
| 325 | fn scroll(cx: &mut VisualTestContext, x: f32, y: f32, dx: f32, dy: f32) { |
| 326 | cx.simulate_event(ScrollWheelEvent { |
| 327 | position: point(px(x), px(y)), |
| 328 | delta: ScrollDelta::Pixels(point(px(dx), px(dy))), |
| 329 | ..Default::default() |
| 330 | }); |
| 331 | draw(cx); |
| 332 | } |
| 333 | |
| 334 | fn row(selector: &'static str, height: f32) -> Div { |
| 335 | div() |
| 336 | .h(px(height)) |
| 337 | .flex_shrink_0() |
| 338 | .debug_selector(move || selector.to_string()) |
| 339 | } |
| 340 | |
| 341 | fn plain_row(height: f32) -> Div { |
| 342 | div().h(px(height)).flex_shrink_0() |
| 343 | } |
| 344 | |
| 345 | fn item(selector: &'static str, width: f32) -> Div { |
| 346 | div() |
| 347 | .w(px(width)) |
| 348 | .h(px(20.)) |
| 349 | .flex_shrink_0() |
| 350 | .debug_selector(move || selector.to_string()) |
| 351 | } |
| 352 | |
| 353 | fn plain_item(width: f32) -> Div { |
| 354 | div().w(px(width)).h(px(20.)).flex_shrink_0() |
| 355 | } |
| 356 | |
| 357 | struct SizeFullChildTest; |
| 358 | |
| 359 | impl Render for SizeFullChildTest { |
| 360 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 361 | div() |
| 362 | .w(px(100.)) |
| 363 | .h(px(100.)) |
| 364 | .overflow_y_scrollbar() |
| 365 | .child( |
| 366 | div() |
| 367 | .size_full() |
| 368 | .child(crate::v_flex().children((0..4).map(|ix| { |
| 369 | div().h(px(50.)).flex_shrink_0().when(ix == 3, |this| { |
| 370 | this.debug_selector(|| "last-row".to_string()) |
| 371 | }) |
| 372 | }))), |
| 373 | ) |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | struct AutoHeightParentTest; |
| 378 | |
| 379 | impl Render for AutoHeightParentTest { |
| 380 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 381 | // Mimics Dialog: the panel height is auto (content-driven), the |
| 382 | // body is flex_1 + overflow_hidden, and the scrollable content |
| 383 | // should give the panel its intrinsic height. |
| 384 | // GPUI window roots with auto dimensions stretch to the viewport, |
| 385 | // so keep the auto-height panel below an explicit viewport root. |
| 386 | div().size_full().child( |
| 387 | crate::v_flex() |
| 388 | .w(px(200.)) |
| 389 | .child( |
| 390 | crate::v_flex().flex_1().overflow_hidden().child( |
| 391 | div().flex_1().overflow_hidden().child( |
| 392 | crate::v_flex() |
| 393 | .size_full() |
| 394 | .overflow_y_scrollbar() |
| 395 | .child(plain_row(50.)) |
| 396 | .child(plain_row(50.)), |
| 397 | ), |
| 398 | ), |
| 399 | ) |
| 400 | .child(row("auto-height-footer", 10.)), |
| 401 | ) |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | struct MaxHeightParentTest; |
| 406 | |
| 407 | impl Render for MaxHeightParentTest { |
| 408 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 409 | // Mimics a Dialog with `max_h`: the panel grows with content up |
| 410 | // to the max height, then the body starts scrolling. |
| 411 | crate::v_flex() |
| 412 | .w(px(200.)) |
| 413 | .max_h(px(100.)) |
| 414 | .child( |
| 415 | crate::v_flex().flex_1().overflow_hidden().child( |
| 416 | div().flex_1().overflow_hidden().child( |
| 417 | crate::v_flex() |
| 418 | .size_full() |
| 419 | .overflow_y_scrollbar() |
| 420 | .child(plain_row(50.)) |
| 421 | .child(plain_row(50.)) |
| 422 | .child(row("max-height-last-row", 50.)), |
| 423 | ), |
| 424 | ), |
| 425 | ) |
| 426 | .child(row("max-height-footer", 10.)) |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | #[gpui::test] |
| 431 | fn auto_height_parent_gets_content_height(cx: &mut TestAppContext) { |
| 432 | cx.update(crate::init); |
| 433 | let (_, cx) = cx.add_window_view(|_, _| AutoHeightParentTest); |
| 434 | let cx: &mut VisualTestContext = cx; |
| 435 | draw(cx); |
| 436 | |
| 437 | // The two 50px rows should push the footer down to y = 100. |
| 438 | let footer = cx.debug_bounds("auto-height-footer").unwrap(); |
| 439 | assert_eq!(footer.top(), px(100.)); |
| 440 | } |
| 441 | |
| 442 | #[gpui::test] |
| 443 | fn max_height_parent_clamps_and_scrolls(cx: &mut TestAppContext) { |
| 444 | cx.update(crate::init); |
| 445 | let (_, cx) = cx.add_window_view(|_, _| MaxHeightParentTest); |
| 446 | let cx: &mut VisualTestContext = cx; |
| 447 | draw(cx); |
| 448 | |
| 449 | // Content (150) + footer (10) exceeds max_h(100): the footer is |
| 450 | // pinned at the bottom and the body gets the remaining 90px viewport. |
| 451 | let footer = cx.debug_bounds("max-height-footer").unwrap(); |
| 452 | assert_eq!(footer.top(), px(90.)); |
| 453 | |
| 454 | let last_initial_y = cx.debug_bounds("max-height-last-row").unwrap().origin.y; |
| 455 | scroll(cx, 10., 10., 0., -50.); |
| 456 | assert!(cx.debug_bounds("max-height-last-row").unwrap().origin.y < last_initial_y); |
| 457 | } |
| 458 | |
| 459 | struct FlexItemScrollableTest; |
| 460 | |
| 461 | impl Render for FlexItemScrollableTest { |
| 462 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 463 | // A fixed-height column of header, flexible scroll area, footer. |
| 464 | // The content is far taller than the room left for the area, so |
| 465 | // the area must shrink into the remaining 60px and scroll. |
| 466 | crate::v_flex() |
| 467 | .w(px(100.)) |
| 468 | .h(px(100.)) |
| 469 | .child(row("flex-item-header", 20.)) |
| 470 | .child( |
| 471 | crate::v_flex() |
| 472 | .flex_1() |
| 473 | .overflow_y_scrollbar() |
| 474 | .children((0..6).map(|ix| { |
| 475 | div().h(px(50.)).flex_shrink_0().when(ix == 0, |this| { |
| 476 | this.debug_selector(|| "flex-item-first-row".to_string()) |
| 477 | }) |
| 478 | })), |
| 479 | ) |
| 480 | .child(row("flex-item-footer", 20.)) |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | #[gpui::test] |
| 485 | fn scrollable_flex_item_shrinks_below_its_content(cx: &mut TestAppContext) { |
| 486 | cx.update(crate::init); |
| 487 | let (_, cx) = cx.add_window_view(|_, _| FlexItemScrollableTest); |
| 488 | let cx: &mut VisualTestContext = cx; |
| 489 | draw(cx); |
| 490 | |
| 491 | // Header and footer stay inside the 100px column, so the area took |
| 492 | // the 60px left over instead of its content height. |
| 493 | assert_eq!(cx.debug_bounds("flex-item-header").unwrap().top(), px(0.)); |
| 494 | assert_eq!(cx.debug_bounds("flex-item-footer").unwrap().top(), px(80.)); |
| 495 | |
| 496 | // And the shrunken area really scrolls. |
| 497 | let initial_y = cx.debug_bounds("flex-item-first-row").unwrap().origin.y; |
| 498 | scroll(cx, 10., 50., 0., -50.); |
| 499 | assert!(cx.debug_bounds("flex-item-first-row").unwrap().origin.y < initial_y); |
| 500 | } |
| 501 | |
| 502 | struct HorizontalFlexItemScrollableTest; |
| 503 | |
| 504 | impl Render for HorizontalFlexItemScrollableTest { |
| 505 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 506 | crate::h_flex() |
| 507 | .w(px(100.)) |
| 508 | .h(px(40.)) |
| 509 | .child(item("horizontal-flex-item-leading", 20.)) |
| 510 | .child( |
| 511 | crate::h_flex() |
| 512 | .flex_1() |
| 513 | .overflow_x_scrollbar() |
| 514 | .children((0..6).map(|ix| { |
| 515 | div() |
| 516 | .w(px(50.)) |
| 517 | .h(px(20.)) |
| 518 | .flex_shrink_0() |
| 519 | .when(ix == 0, |this| { |
| 520 | this.debug_selector(|| { |
| 521 | "horizontal-flex-item-first-item".to_string() |
| 522 | }) |
| 523 | }) |
| 524 | })), |
| 525 | ) |
| 526 | .child(item("horizontal-flex-item-trailing", 20.)) |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | #[gpui::test] |
| 531 | fn horizontal_scrollable_flex_item_shrinks_below_its_content(cx: &mut TestAppContext) { |
| 532 | cx.update(crate::init); |
| 533 | let (_, cx) = cx.add_window_view(|_, _| HorizontalFlexItemScrollableTest); |
| 534 | let cx: &mut VisualTestContext = cx; |
| 535 | draw(cx); |
| 536 | |
| 537 | assert_eq!( |
| 538 | cx.debug_bounds("horizontal-flex-item-leading") |
| 539 | .unwrap() |
| 540 | .left(), |
| 541 | px(0.) |
| 542 | ); |
| 543 | assert_eq!( |
| 544 | cx.debug_bounds("horizontal-flex-item-trailing") |
| 545 | .unwrap() |
| 546 | .left(), |
| 547 | px(80.) |
| 548 | ); |
| 549 | |
| 550 | let initial_x = cx |
| 551 | .debug_bounds("horizontal-flex-item-first-item") |
| 552 | .unwrap() |
| 553 | .origin |
| 554 | .x; |
| 555 | scroll(cx, 50., 10., -50., 0.); |
| 556 | assert!( |
| 557 | cx.debug_bounds("horizontal-flex-item-first-item") |
| 558 | .unwrap() |
| 559 | .origin |
| 560 | .x |
| 561 | < initial_x |
| 562 | ); |
| 563 | } |
| 564 | |
| 565 | struct ExplicitMinHeightScrollableTest; |
| 566 | |
| 567 | impl Render for ExplicitMinHeightScrollableTest { |
| 568 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 569 | // An explicit `min_h` is the caller's decision and must survive. |
| 570 | crate::v_flex() |
| 571 | .w(px(100.)) |
| 572 | .h(px(100.)) |
| 573 | .child(row("explicit-min-header", 20.)) |
| 574 | .child( |
| 575 | crate::v_flex() |
| 576 | .flex_1() |
| 577 | .min_h(px(70.)) |
| 578 | .overflow_y_scrollbar() |
| 579 | .children((0..6).map(|_| plain_row(50.))), |
| 580 | ) |
| 581 | .child(row("explicit-min-footer", 20.)) |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | #[gpui::test] |
| 586 | fn explicit_min_size_survives_the_scrollable_wrapper(cx: &mut TestAppContext) { |
| 587 | cx.update(crate::init); |
| 588 | let (_, cx) = cx.add_window_view(|_, _| ExplicitMinHeightScrollableTest); |
| 589 | let cx: &mut VisualTestContext = cx; |
| 590 | draw(cx); |
| 591 | |
| 592 | // The area cannot shrink past the requested 70px, so the footer is |
| 593 | // pushed to 20 + 70 instead of being clamped into the column. |
| 594 | assert_eq!( |
| 595 | cx.debug_bounds("explicit-min-header").unwrap().top(), |
| 596 | px(0.) |
| 597 | ); |
| 598 | assert_eq!( |
| 599 | cx.debug_bounds("explicit-min-footer").unwrap().top(), |
| 600 | px(90.) |
| 601 | ); |
| 602 | } |
| 603 | |
| 604 | struct GapLayoutTest; |
| 605 | |
| 606 | struct PaddedScrollbarOverlayTest; |
| 607 | |
| 608 | impl Render for PaddedScrollbarOverlayTest { |
| 609 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 610 | crate::v_flex() |
| 611 | .w(px(100.)) |
| 612 | .h(px(100.)) |
| 613 | .p(px(20.)) |
| 614 | .overflow_y_scrollbar() |
| 615 | .children((0..4).map(|_| plain_row(50.))) |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | #[gpui::test] |
| 620 | fn scrollbar_overlay_ignores_content_padding(cx: &mut TestAppContext) { |
| 621 | cx.update(crate::init); |
| 622 | let (_, cx) = cx.add_window_view(|_, _| PaddedScrollbarOverlayTest); |
| 623 | let cx: &mut VisualTestContext = cx; |
| 624 | draw(cx); |
| 625 | |
| 626 | let overlay = cx.debug_bounds("scrollbar-overlay").unwrap(); |
| 627 | assert_eq!(overlay.origin, point(px(0.), px(0.))); |
| 628 | assert_eq!(overlay.size, gpui::size(px(100.), px(100.))); |
| 629 | } |
| 630 | |
| 631 | impl Render for GapLayoutTest { |
| 632 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 633 | crate::v_flex() |
| 634 | .w(px(100.)) |
| 635 | .h(px(100.)) |
| 636 | .gap(px(10.)) |
| 637 | .overflow_y_scrollbar() |
| 638 | .child(row("first-row", 20.)) |
| 639 | .child(row("second-row", 20.)) |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | struct IssueGapRegressionTest; |
| 644 | |
| 645 | impl Render for IssueGapRegressionTest { |
| 646 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 647 | div().w(px(100.)).h(px(100.)).child( |
| 648 | crate::v_flex() |
| 649 | .flex_1() |
| 650 | .gap(px(30.)) |
| 651 | .overflow_y_scrollbar() |
| 652 | .px(px(12.)) |
| 653 | .pb(px(16.)) |
| 654 | .children((0..5).map(|ix| { |
| 655 | div() |
| 656 | .h(px(20.)) |
| 657 | .flex_shrink_0() |
| 658 | .when(ix == 0, |this| { |
| 659 | this.debug_selector(|| "issue-first-card".to_string()) |
| 660 | }) |
| 661 | .when(ix == 1, |this| { |
| 662 | this.debug_selector(|| "issue-second-card".to_string()) |
| 663 | }) |
| 664 | .when(ix == 4, |this| { |
| 665 | this.debug_selector(|| "issue-last-card".to_string()) |
| 666 | }) |
| 667 | })), |
| 668 | ) |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | struct HorizontalGapLayoutTest; |
| 673 | |
| 674 | impl Render for HorizontalGapLayoutTest { |
| 675 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 676 | crate::h_flex() |
| 677 | .w(px(100.)) |
| 678 | .h(px(40.)) |
| 679 | .gap(px(10.)) |
| 680 | .overflow_x_scrollbar() |
| 681 | .child(item("horizontal-first-item", 50.)) |
| 682 | .child(item("horizontal-second-item", 50.)) |
| 683 | .child(item("horizontal-last-item", 50.)) |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | struct OverflowScrollbarVerticalTest; |
| 688 | |
| 689 | impl Render for OverflowScrollbarVerticalTest { |
| 690 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 691 | crate::v_flex() |
| 692 | .w(px(100.)) |
| 693 | .h(px(100.)) |
| 694 | .gap(px(10.)) |
| 695 | .overflow_scrollbar() |
| 696 | .child(row("both-axis-vertical-first-row", 50.)) |
| 697 | .child(row("both-axis-vertical-second-row", 50.)) |
| 698 | .child(row("both-axis-vertical-last-row", 50.)) |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | struct OverflowScrollbarHorizontalTest; |
| 703 | |
| 704 | impl Render for OverflowScrollbarHorizontalTest { |
| 705 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 706 | crate::h_flex() |
| 707 | .w(px(100.)) |
| 708 | .h(px(40.)) |
| 709 | .gap(px(10.)) |
| 710 | .overflow_scrollbar() |
| 711 | .child(item("both-axis-horizontal-first-item", 50.)) |
| 712 | .child(item("both-axis-horizontal-second-item", 50.)) |
| 713 | .child(item("both-axis-horizontal-last-item", 50.)) |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | struct IndependentScrollablesTest; |
| 718 | |
| 719 | impl Render for IndependentScrollablesTest { |
| 720 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 721 | crate::h_flex() |
| 722 | .w(px(220.)) |
| 723 | .h(px(100.)) |
| 724 | .gap(px(20.)) |
| 725 | .child( |
| 726 | div().w(px(100.)).h(px(100.)).overflow_y_scrollbar().child( |
| 727 | crate::v_flex() |
| 728 | .child(plain_row(50.)) |
| 729 | .child(plain_row(50.)) |
| 730 | .child(row("left-scrollable-last-row", 50.)), |
| 731 | ), |
| 732 | ) |
| 733 | .child( |
| 734 | div().w(px(100.)).h(px(100.)).overflow_y_scrollbar().child( |
| 735 | crate::v_flex() |
| 736 | .child(plain_row(50.)) |
| 737 | .child(plain_row(50.)) |
| 738 | .child(row("right-scrollable-last-row", 50.)), |
| 739 | ), |
| 740 | ) |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | struct NoOverflowTest; |
| 745 | |
| 746 | impl Render for NoOverflowTest { |
| 747 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 748 | crate::v_flex() |
| 749 | .w(px(100.)) |
| 750 | .h(px(100.)) |
| 751 | .gap(px(10.)) |
| 752 | .overflow_y_scrollbar() |
| 753 | .child(row("no-overflow-first-row", 20.)) |
| 754 | .child(row("no-overflow-second-row", 20.)) |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | #[gpui::test] |
| 759 | fn vertical_scrollbar_scrolls_past_a_size_full_child(cx: &mut TestAppContext) { |
| 760 | cx.update(crate::init); |
| 761 | let (_, cx) = cx.add_window_view(|_, _| SizeFullChildTest); |
| 762 | let cx: &mut VisualTestContext = cx; |
| 763 | draw(cx); |
| 764 | |
| 765 | let initial_y = cx.debug_bounds("last-row").unwrap().origin.y; |
| 766 | scroll(cx, 10., 10., 0., -50.); |
| 767 | |
| 768 | assert!(cx.debug_bounds("last-row").unwrap().origin.y < initial_y); |
| 769 | } |
| 770 | |
| 771 | #[gpui::test] |
| 772 | fn vertical_scrollbar_preserves_source_gap(cx: &mut TestAppContext) { |
| 773 | cx.update(crate::init); |
| 774 | let (_, cx) = cx.add_window_view(|_, _| GapLayoutTest); |
| 775 | let cx: &mut VisualTestContext = cx; |
| 776 | draw(cx); |
| 777 | |
| 778 | let first = cx.debug_bounds("first-row").unwrap(); |
| 779 | let second = cx.debug_bounds("second-row").unwrap(); |
| 780 | assert_eq!(second.top() - first.bottom(), px(10.)); |
| 781 | } |
| 782 | |
| 783 | #[gpui::test] |
| 784 | fn overflow_y_scrollbar_preserves_gap_for_exact_issue_chain(cx: &mut TestAppContext) { |
| 785 | cx.update(crate::init); |
| 786 | let (_, cx) = cx.add_window_view(|_, _| IssueGapRegressionTest); |
| 787 | let cx: &mut VisualTestContext = cx; |
| 788 | draw(cx); |
| 789 | |
| 790 | let first = cx.debug_bounds("issue-first-card").unwrap(); |
| 791 | let second = cx.debug_bounds("issue-second-card").unwrap(); |
| 792 | let last_initial_y = cx.debug_bounds("issue-last-card").unwrap().origin.y; |
| 793 | |
| 794 | assert_eq!(second.top() - first.bottom(), px(30.)); |
| 795 | assert_eq!(first.left(), px(12.)); |
| 796 | |
| 797 | scroll(cx, 10., 10., 0., -50.); |
| 798 | |
| 799 | let first_after_scroll = cx.debug_bounds("issue-first-card").unwrap(); |
| 800 | let second_after_scroll = cx.debug_bounds("issue-second-card").unwrap(); |
| 801 | let last_after_scroll_y = cx.debug_bounds("issue-last-card").unwrap().origin.y; |
| 802 | |
| 803 | assert_eq!( |
| 804 | second_after_scroll.top() - first_after_scroll.bottom(), |
| 805 | px(30.) |
| 806 | ); |
| 807 | assert_eq!(first_after_scroll.left(), px(12.)); |
| 808 | assert!(last_after_scroll_y < last_initial_y); |
| 809 | } |
| 810 | |
| 811 | #[gpui::test] |
| 812 | fn horizontal_scrollbar_preserves_source_gap_and_scrolls(cx: &mut TestAppContext) { |
| 813 | cx.update(crate::init); |
| 814 | let (_, cx) = cx.add_window_view(|_, _| HorizontalGapLayoutTest); |
| 815 | let cx: &mut VisualTestContext = cx; |
| 816 | draw(cx); |
| 817 | |
| 818 | let first = cx.debug_bounds("horizontal-first-item").unwrap(); |
| 819 | let second = cx.debug_bounds("horizontal-second-item").unwrap(); |
| 820 | let last_initial_x = cx.debug_bounds("horizontal-last-item").unwrap().origin.x; |
| 821 | |
| 822 | assert_eq!(second.left() - first.right(), px(10.)); |
| 823 | |
| 824 | scroll(cx, 10., 10., -50., 0.); |
| 825 | |
| 826 | let first_after_scroll = cx.debug_bounds("horizontal-first-item").unwrap(); |
| 827 | let second_after_scroll = cx.debug_bounds("horizontal-second-item").unwrap(); |
| 828 | let last_after_scroll_x = cx.debug_bounds("horizontal-last-item").unwrap().origin.x; |
| 829 | |
| 830 | assert_eq!( |
| 831 | second_after_scroll.left() - first_after_scroll.right(), |
| 832 | px(10.) |
| 833 | ); |
| 834 | assert!(last_after_scroll_x < last_initial_x); |
| 835 | } |
| 836 | |
| 837 | #[gpui::test] |
| 838 | fn overflow_scrollbar_preserves_vertical_source_gap(cx: &mut TestAppContext) { |
| 839 | cx.update(crate::init); |
| 840 | let (_, cx) = cx.add_window_view(|_, _| OverflowScrollbarVerticalTest); |
| 841 | let cx: &mut VisualTestContext = cx; |
| 842 | draw(cx); |
| 843 | |
| 844 | let first = cx.debug_bounds("both-axis-vertical-first-row").unwrap(); |
| 845 | let second = cx.debug_bounds("both-axis-vertical-second-row").unwrap(); |
| 846 | |
| 847 | assert_eq!(second.top() - first.bottom(), px(10.)); |
| 848 | } |
| 849 | |
| 850 | #[gpui::test] |
| 851 | fn overflow_scrollbar_preserves_gap_and_scrolls_horizontally(cx: &mut TestAppContext) { |
| 852 | cx.update(crate::init); |
| 853 | let (_, cx) = cx.add_window_view(|_, _| OverflowScrollbarHorizontalTest); |
| 854 | let cx: &mut VisualTestContext = cx; |
| 855 | draw(cx); |
| 856 | |
| 857 | let first = cx.debug_bounds("both-axis-horizontal-first-item").unwrap(); |
| 858 | let second = cx.debug_bounds("both-axis-horizontal-second-item").unwrap(); |
| 859 | let last_initial_x = cx |
| 860 | .debug_bounds("both-axis-horizontal-last-item") |
| 861 | .unwrap() |
| 862 | .origin |
| 863 | .x; |
| 864 | |
| 865 | assert_eq!(second.left() - first.right(), px(10.)); |
| 866 | |
| 867 | scroll(cx, 10., 10., -50., 0.); |
| 868 | |
| 869 | let first_after_scroll = cx.debug_bounds("both-axis-horizontal-first-item").unwrap(); |
| 870 | let second_after_scroll = cx.debug_bounds("both-axis-horizontal-second-item").unwrap(); |
| 871 | let last_after_scroll_x = cx |
| 872 | .debug_bounds("both-axis-horizontal-last-item") |
| 873 | .unwrap() |
| 874 | .origin |
| 875 | .x; |
| 876 | |
| 877 | assert_eq!( |
| 878 | second_after_scroll.left() - first_after_scroll.right(), |
| 879 | px(10.) |
| 880 | ); |
| 881 | assert!(last_after_scroll_x < last_initial_x); |
| 882 | } |
| 883 | |
| 884 | #[gpui::test] |
| 885 | fn multiple_scrollables_keep_independent_scroll_state(cx: &mut TestAppContext) { |
| 886 | cx.update(crate::init); |
| 887 | let (_, cx) = cx.add_window_view(|_, _| IndependentScrollablesTest); |
| 888 | let cx: &mut VisualTestContext = cx; |
| 889 | draw(cx); |
| 890 | |
| 891 | let left_initial = cx.debug_bounds("left-scrollable-last-row").unwrap(); |
| 892 | let right_initial = cx.debug_bounds("right-scrollable-last-row").unwrap(); |
| 893 | |
| 894 | scroll(cx, 10., 10., 0., -50.); |
| 895 | |
| 896 | let left_after_scroll = cx.debug_bounds("left-scrollable-last-row").unwrap(); |
| 897 | let right_after_scroll = cx.debug_bounds("right-scrollable-last-row").unwrap(); |
| 898 | |
| 899 | assert!(left_after_scroll.top() < left_initial.top()); |
| 900 | assert_eq!(right_after_scroll.top(), right_initial.top()); |
| 901 | } |
| 902 | |
| 903 | #[gpui::test] |
| 904 | fn vertical_scrollbar_does_not_scroll_when_content_does_not_overflow(cx: &mut TestAppContext) { |
| 905 | cx.update(crate::init); |
| 906 | let (_, cx) = cx.add_window_view(|_, _| NoOverflowTest); |
| 907 | let cx: &mut VisualTestContext = cx; |
| 908 | draw(cx); |
| 909 | |
| 910 | let first = cx.debug_bounds("no-overflow-first-row").unwrap(); |
| 911 | let second = cx.debug_bounds("no-overflow-second-row").unwrap(); |
| 912 | |
| 913 | assert_eq!(second.top() - first.bottom(), px(10.)); |
| 914 | |
| 915 | scroll(cx, 10., 10., 0., -50.); |
| 916 | |
| 917 | let first_after_scroll = cx.debug_bounds("no-overflow-first-row").unwrap(); |
| 918 | let second_after_scroll = cx.debug_bounds("no-overflow-second-row").unwrap(); |
| 919 | |
| 920 | assert_eq!(first_after_scroll.top(), first.top()); |
| 921 | assert_eq!(second_after_scroll.top(), second.top()); |
| 922 | assert_eq!( |
| 923 | second_after_scroll.top() - first_after_scroll.bottom(), |
| 924 | px(10.) |
| 925 | ); |
| 926 | } |
| 927 | |
| 928 | #[gpui::test] |
| 929 | fn horizontal_scrollbar_does_not_scroll_when_content_does_not_overflow( |
| 930 | cx: &mut TestAppContext, |
| 931 | ) { |
| 932 | struct HorizontalNoOverflowTest; |
| 933 | |
| 934 | impl Render for HorizontalNoOverflowTest { |
| 935 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 936 | crate::h_flex() |
| 937 | .w(px(100.)) |
| 938 | .h(px(40.)) |
| 939 | .gap(px(10.)) |
| 940 | .overflow_x_scrollbar() |
| 941 | .child(item("no-overflow-first-item", 20.)) |
| 942 | .child(item("no-overflow-second-item", 20.)) |
| 943 | .child(plain_item(20.)) |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | cx.update(crate::init); |
| 948 | let (_, cx) = cx.add_window_view(|_, _| HorizontalNoOverflowTest); |
| 949 | let cx: &mut VisualTestContext = cx; |
| 950 | draw(cx); |
| 951 | |
| 952 | let first = cx.debug_bounds("no-overflow-first-item").unwrap(); |
| 953 | let second = cx.debug_bounds("no-overflow-second-item").unwrap(); |
| 954 | |
| 955 | assert_eq!(second.left() - first.right(), px(10.)); |
| 956 | |
| 957 | scroll(cx, 10., 10., -50., 0.); |
| 958 | |
| 959 | let first_after_scroll = cx.debug_bounds("no-overflow-first-item").unwrap(); |
| 960 | let second_after_scroll = cx.debug_bounds("no-overflow-second-item").unwrap(); |
| 961 | |
| 962 | assert_eq!(first_after_scroll.left(), first.left()); |
| 963 | assert_eq!(second_after_scroll.left(), second.left()); |
| 964 | assert_eq!( |
| 965 | second_after_scroll.left() - first_after_scroll.right(), |
| 966 | px(10.) |
| 967 | ); |
| 968 | } |
| 969 | struct IndependentDynamicScrollablesTest; |
| 970 | |
| 971 | impl Render for IndependentDynamicScrollablesTest { |
| 972 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 973 | crate::v_flex().w(px(100.)).children((0..2).map(|ix| { |
| 974 | crate::h_flex() |
| 975 | .w(px(100.)) |
| 976 | .h(px(40.)) |
| 977 | .overflow_x_scrollbar() |
| 978 | .id(format!("dynamic-scroll-{ix}")) |
| 979 | .child( |
| 980 | div() |
| 981 | .w(px(200.)) |
| 982 | .h(px(20.)) |
| 983 | .flex_shrink_0() |
| 984 | .when(ix == 0, |this| { |
| 985 | this.debug_selector(|| "dynamic-scroll-first".to_string()) |
| 986 | }) |
| 987 | .when(ix == 1, |this| { |
| 988 | this.debug_selector(|| "dynamic-scroll-second".to_string()) |
| 989 | }), |
| 990 | ) |
| 991 | })) |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | #[gpui::test] |
| 996 | fn dynamic_scrollables_with_unique_scroll_ids_keep_independent_state(cx: &mut TestAppContext) { |
| 997 | cx.update(crate::init); |
| 998 | |
| 999 | let (_, cx) = cx.add_window_view(|_, _| IndependentDynamicScrollablesTest); |
| 1000 | let cx: &mut VisualTestContext = cx; |
| 1001 | |
| 1002 | draw(cx); |
| 1003 | |
| 1004 | let first_initial = cx.debug_bounds("dynamic-scroll-first").unwrap(); |
| 1005 | |
| 1006 | let second_initial = cx.debug_bounds("dynamic-scroll-second").unwrap(); |
| 1007 | |
| 1008 | // Scroll horizontally inside the first row. |
| 1009 | scroll(cx, 10., 10., -50., 0.); |
| 1010 | |
| 1011 | let first_after_scroll = cx.debug_bounds("dynamic-scroll-first").unwrap(); |
| 1012 | |
| 1013 | let second_after_scroll = cx.debug_bounds("dynamic-scroll-second").unwrap(); |
| 1014 | |
| 1015 | // First scrollable should move horizontally. |
| 1016 | assert!(first_after_scroll.left() < first_initial.left()); |
| 1017 | |
| 1018 | // Second scrollable must keep its own independent scroll state. |
| 1019 | assert_eq!(second_after_scroll.left(), second_initial.left()); |
| 1020 | } |
| 1021 | } |