Skip to main content

elvis/widgets/layouts/
flex.rs

1//! Flex Layout
2use elvis_core::{derive::Setter, style::FlexStyle, value::layouts::Alignment, Class, Node};
3use elvis_derive::IntoNode;
4
5/// `Align` inherits the core usage of Alignment, it's quite simple, just one property.
6#[derive(Default, Setter)]
7pub struct Align {
8    /// Align child
9    pub child: Node,
10    /// Align style
11    pub align: Alignment,
12}
13
14impl Into<Node> for Align {
15    fn into(self) -> Node {
16        Node::default().children(vec![self.child]).style(self.align)
17    }
18}
19
20/// `Center` is a very nice widget, if your website only have a line of chars, use it!
21#[derive(Default, Setter)]
22pub struct Center {
23    /// Center child
24    #[skip]
25    pub child: Node,
26}
27
28impl Center {
29    /// Set Child
30    pub fn child(mut self, n: impl Into<Node>) -> Self {
31        self.child = n.into();
32        self
33    }
34}
35
36impl Into<Node> for Center {
37    fn into(self) -> Node {
38        Node::default()
39            .children(vec![self.child])
40            .class(vec![Class::Flex, Class::Center])
41    }
42}
43
44/// `Col` is the typical flow in html, with flexible enhance.
45#[derive(Default, Setter)]
46pub struct Col {
47    /// Column children
48    pub children: Vec<Node>,
49}
50
51impl Into<Node> for Col {
52    fn into(self) -> Node {
53        Node::default()
54            .children(self.children)
55            .class(vec![Class::Flex, Class::Col])
56    }
57}
58
59/// This is the Lunatic Widget to Ground Control, 'I`m stepping throw the Window.'
60#[derive(Default, IntoNode, Setter)]
61pub struct Flex {
62    /// Flex child
63    pub child: Node,
64    /// Flex style
65    pub style: FlexStyle,
66}
67
68/// Both `Col` and `Row` are using flex-start, if you want to reverse the children of them, better to work on the list order.
69#[derive(Default, Setter)]
70pub struct Row {
71    /// Row children
72    pub children: Vec<Node>,
73}
74
75impl Into<Node> for Row {
76    fn into(self) -> Node {
77        Node::default()
78            .children(self.children)
79            .class(vec![Class::Flex, Class::Row])
80    }
81}