Skip to main content

ansiq_widgets/
box_widget.rs

1use ansiq_core::{BoxProps, Direction, Element, ElementKind, Layout, Length, Style};
2
3pub struct Box<Message> {
4    element: Element<Message>,
5}
6
7impl<Message> Box<Message> {
8    pub fn column() -> Self {
9        Self::new(Direction::Column)
10    }
11
12    pub fn row() -> Self {
13        Self::new(Direction::Row)
14    }
15
16    fn new(direction: Direction) -> Self {
17        Self {
18            element: Element::new(ElementKind::Box(BoxProps { direction, gap: 0 })).with_layout(
19                Layout {
20                    width: Length::Fill,
21                    height: Length::Fill,
22                },
23            ),
24        }
25    }
26
27    pub fn gap(mut self, gap: u16) -> Self {
28        if let ElementKind::Box(props) = &mut self.element.kind {
29            props.gap = gap;
30        }
31        self
32    }
33
34    pub fn child(mut self, child: Element<Message>) -> Self {
35        self.element.children.push(child);
36        self
37    }
38
39    pub fn children<I>(mut self, children: I) -> Self
40    where
41        I: IntoIterator<Item = Element<Message>>,
42    {
43        self.element.children.extend(children);
44        self
45    }
46
47    pub fn style(mut self, style: Style) -> Self {
48        self.element.style = style;
49        self
50    }
51
52    pub fn layout(mut self, layout: Layout) -> Self {
53        self.element.layout = layout;
54        self
55    }
56
57    pub fn build(self) -> Element<Message> {
58        self.element
59    }
60}