~tm85/lintuini

ref: 281900fe61d7df4c18826576a3ed56064e914e45 lintuini/src/elements/macros.rs -rw-r--r-- 1.9 KiB
281900feTerraMaster85 fmt: all 26 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
macro_rules! element_with_children {
    ($el:ty) => {
        impl ElementWithChildren for $el {
            #[inline(always)]
            fn add_child(
                &mut self,
                child: impl Into<Box<dyn Element>>,
            ) -> Result<(), crate::error::LintuiniError> {
                self.children.push(child.into());
                self.regenerate_layout();
                Ok(())
            }

            #[inline(always)]
            fn children(&self) -> &[Box<dyn Element>] {
                self.children.as_slice()
            }

            #[inline(always)]
            fn children_mut(&mut self) -> &mut [Box<dyn Element>] {
                self.children.as_mut_slice()
            }
        }
    };
}
pub(crate) use element_with_children;

macro_rules! basic_config_methods {
    ($el:ty) => {
        impl $el {
            #[inline(always)]
            pub fn new() -> Self {
                <$el>::default()
            }

            #[inline(always)]
            pub fn constraint(
                &mut self,
                constraint: ratatui_core::layout::Constraint,
            ) -> &mut Self {
                self.constraint = constraint;
                self
            }

            // beware the owned self!
            #[inline(always)]
            pub fn direction(
                mut self,
                direction: ratatui_core::layout::Direction,
            ) -> Self {
                self.layout = self.layout.direction(direction);
                self.regenerate_layout();
                self
            }

            pub fn regenerate_layout(&mut self) -> &mut Self {
                if self.children.len() == 0 {
                    return self;
                }
                self.layout = self.layout.clone().constraints(
                    self.children.iter().map(|el| el.constraint()),
                );
                self
            }
        }
    };
}
pub(crate) use basic_config_methods;