use gpui::prelude::*;
use gpui::{div, px, AnyElement, App, ElementId, IntoElement, Window};
#[derive(IntoElement)]
pub struct ScrollArea {
id: ElementId,
children: Vec<AnyElement>,
max_height: Option<f32>,
horizontal: bool,
}
impl ScrollArea {
pub fn new(id: impl Into<ElementId>) -> Self {
ScrollArea {
id: id.into(),
children: Vec::new(),
max_height: None,
horizontal: false,
}
}
pub fn max_height(mut self, height: f32) -> Self {
self.max_height = Some(height);
self
}
pub fn horizontal(mut self, horizontal: bool) -> Self {
self.horizontal = horizontal;
self
}
}
impl ParentElement for ScrollArea {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for ScrollArea {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
let mut el = div().id(self.id).flex();
el = if self.horizontal {
el.flex_row().overflow_x_scroll()
} else {
el.flex_col().overflow_y_scroll()
};
if let Some(height) = self.max_height {
el = el.max_h(px(height));
}
el.children(self.children)
}
}