//! Renders children in a scrollable pane with an overlay [`Scrollbar`]. //! //! # Examples //! //! ```no_run //! use gpui::{div, px, ParentElement as _, Styled as _}; //! use ui::scroll_area::{ScrollArea, ScrollAxes}; //! //! let log = ScrollArea::new("event-log") //! .axes(ScrollAxes::Vertical) //! .h(px(320.)) //! .children((0..24).map(|line| div().child(format!("event {line}")))); //! ``` use gpui::prelude::FluentBuilder as _; use gpui::{ App, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce, ScrollHandle, SharedString, StatefulInteractiveElement as _, Styled, Window, div, }; use crate::components::common::StyledExt; use crate::components::scrollbar::{Scrollbar, ScrollbarVisibility}; /// Which directions the content scrolls in. #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub enum ScrollAxes { /// Vertical only; the default. #[default] Vertical, /// Horizontal only. Horizontal, /// Both directions. Both, } /// A scrollable container with an overlay scrollbar. #[derive(IntoElement)] #[must_use] pub struct ScrollArea { id: ElementId, handle: Option, axes: ScrollAxes, visibility: ScrollbarVisibility, a11y_label: Option, style: gpui::StyleRefinement, children: Vec, } impl ParentElement for ScrollArea { fn extend(&mut self, children: impl IntoIterator) { self.children.extend(children); } } impl Styled for ScrollArea { fn style(&mut self) -> &mut gpui::StyleRefinement { &mut self.style } } impl ScrollArea { /// Creates a vertically scrolling area. pub fn new(id: I) -> Self where I: Into, { Self { id: id.into(), handle: None, axes: ScrollAxes::Vertical, visibility: ScrollbarVisibility::default(), a11y_label: None, style: gpui::StyleRefinement::default(), children: Vec::new(), } } /// Sets which directions the content scrolls in. pub fn axes(mut self, axes: ScrollAxes) -> Self { self.axes = axes; self } /// Overrides when the scrollbar shows, instead of the platform default. pub fn visibility(mut self, visibility: ScrollbarVisibility) -> Self { self.visibility = visibility; self } /// Scrolls with a handle held on the owning view. pub fn handle(mut self, handle: ScrollHandle) -> Self { self.handle = Some(handle); self } /// Overrides the accessibility label of the container. pub fn accessibility_label(mut self, label: L) -> Self where L: Into, { self.a11y_label = Some(label.into()); self } } impl RenderOnce for ScrollArea { fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement { let mut pane_style = self.style.clone(); pane_style.padding = Default::default(); let pane = div() .id(self.id.clone()) .overflow_hidden() .when(self.axes != ScrollAxes::Horizontal, |area| { area.overflow_y_scroll() }) .when(self.axes != ScrollAxes::Vertical, |area| { area.overflow_x_scroll() }) .when_some(self.handle.clone(), |area, handle| { area.track_scroll(&handle) }) .when_some(self.a11y_label, |area, label| area.aria_label(label)) .on_scroll(|_, _, cx| { // Keep wheel events from scrolling an outer scroll area too. cx.stop_propagation(); }) .refine_style(&pane_style) .children(self.children); div() .relative() .refine_style(&self.style) .child(pane.into_any_element()) .when_some(self.handle, |wrapper, handle| { let id = self.id.clone(); wrapper .when(self.axes != ScrollAxes::Horizontal, |wrapper| { wrapper.child( Scrollbar::vertical((id.clone(), "v-scrollbar"), handle.clone()) .visibility(self.visibility) .into_any_element(), ) }) .when(self.axes != ScrollAxes::Vertical, |wrapper| { wrapper.child( Scrollbar::horizontal((id, "h-scrollbar"), handle) .visibility(self.visibility) .into_any_element(), ) }) }) } }