Skip to main content

ansiq_widgets/
shell.rs

1use ansiq_core::{Element, ElementKind, Layout, Length, ShellProps, Style};
2
3pub struct Shell<Message = ()> {
4    element: Element<Message>,
5}
6
7impl<Message> Shell<Message> {
8    pub fn new() -> Self {
9        Self {
10            element: Element::new(ElementKind::Shell(ShellProps)).with_layout(Layout {
11                width: Length::Fill,
12                height: Length::Fill,
13            }),
14        }
15    }
16
17    pub fn header(mut self, child: Element<Message>) -> Self {
18        set_shell_slot(&mut self.element.children, 0, child);
19        self
20    }
21
22    pub fn body(mut self, child: Element<Message>) -> Self {
23        set_shell_slot(&mut self.element.children, 1, child);
24        self
25    }
26
27    pub fn footer(mut self, child: Element<Message>) -> Self {
28        set_shell_slot(&mut self.element.children, 2, child);
29        self
30    }
31
32    pub fn style(mut self, style: Style) -> Self {
33        self.element.style = style;
34        self
35    }
36
37    pub fn layout(mut self, layout: Layout) -> Self {
38        self.element.layout = layout;
39        self
40    }
41
42    pub fn build(self) -> Element<Message> {
43        self.element
44    }
45}
46
47fn set_shell_slot<Message>(
48    children: &mut Vec<Element<Message>>,
49    index: usize,
50    child: Element<Message>,
51) {
52    if children.len() <= index {
53        children.resize_with(index + 1, || {
54            Element::new(ElementKind::Text(ansiq_core::TextProps {
55                content: String::new(),
56            }))
57            .with_layout(Layout {
58                width: Length::Fill,
59                height: Length::Auto,
60            })
61        });
62    }
63
64    children[index] = child;
65}