Skip to main content

guise/flex/
wrap.rs

1//! `Wrap` — a flex row that wraps onto multiple lines. Flutter's `Wrap`.
2
3use gpui::prelude::*;
4use gpui::{div, px, AnyElement, App, IntoElement, Window};
5
6/// Lays children out in a row, wrapping to new lines as needed.
7#[derive(IntoElement)]
8pub struct Wrap {
9    children: Vec<AnyElement>,
10    spacing: f32,
11}
12
13impl Wrap {
14    pub fn new() -> Self {
15        Wrap {
16            children: Vec::new(),
17            spacing: 8.0,
18        }
19    }
20
21    /// Gap between children, on both axes.
22    pub fn spacing(mut self, spacing: f32) -> Self {
23        self.spacing = spacing;
24        self
25    }
26}
27
28impl Default for Wrap {
29    fn default() -> Self {
30        Wrap::new()
31    }
32}
33
34impl ParentElement for Wrap {
35    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
36        self.children.extend(elements);
37    }
38}
39
40impl RenderOnce for Wrap {
41    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
42        div()
43            .flex()
44            .flex_row()
45            .flex_wrap()
46            .gap(px(self.spacing))
47            .children(self.children)
48    }
49}