Skip to main content

guise/flex/
wrap.rs

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