~tm85/lintuini

ref: a247719c2c965a261e1571fbe45cff8d2ab6175c lintuini/src/elements/container.rs -rw-r--r-- 3.3 KiB
a247719cTerraMaster85 feat: rapid development 28 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use ratatui_core::layout;
use ratatui_core::style;
use ratatui_core::terminal::Frame;

use ratatui_widgets::block;
use ratatui_widgets::borders;

use super::element::{Element, ElementWithChildren};
use crate::error;

pub struct Container {
    border: Option<ContainerBorder>,
    children: Vec<Box<dyn Element>>,
    constraint: layout::Constraint,
    direction: layout::Direction,
    fill: style::Style,
    layout: Option<layout::Layout>,
}

impl ElementWithChildren for Container {
    #[inline(always)]
    fn add_child(&mut self, child: Box<dyn Element>) -> Result<(), error::LintuiniError>{
        self.children.push(child);
        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()
    }
}

impl Element for Container {
    #[inline(always)]
    fn constraint(&self) -> &layout::Constraint {
        &self.constraint
    }

    fn render(&self, frm: &mut Frame, rect: layout::Rect) -> Result<(), error::LintuiniError> {
        let blk = match &self.border {
            Some(bs) => block::Block::bordered().style(self.fill).border_type(bs.kind).border_style(bs.style),
            None => block::Block::new().style(self.fill),
        };

        frm.render_widget(blk, rect);

        if self.layout.is_none() {
            debug_assert!(
                self.children.len() == 0,
                "child count did not match number of layout fields"
            );
            return Ok(());
        }


        // TODO: children

        Ok(())
    }
}

#[derive(Default)]
pub struct ContainerBuilder {
    border: Option<ContainerBorder>,
    constraint: layout::Constraint,
    direction: layout::Direction,
    fill: style::Style,
}

impl ContainerBuilder {
    #[inline(always)]
    pub fn new() -> Self {
        ContainerBuilder::default()
    }
    #[inline(always)]
    pub fn fill(mut self, style: style::Style) -> Self {
        self.fill = style;
        self
    }
    #[inline(always)]
    pub fn constraint(mut self, constraint: layout::Constraint) -> Self {
        self.constraint = constraint;
        self
    }
    #[inline(always)]
    pub fn direction(mut self, direction: layout::Direction) -> Self {
        self.direction = direction;
        self
    }
    #[inline(always)]
    pub fn border_style(mut self, style: style::Style) -> Self {
        match self.border {
            Some(mut bs) => { bs.style = style; },
            None => { self.border = Some(ContainerBorder { style: style, ..Default::default() }) },
        }
        self
    }
    #[inline(always)]
    pub fn border_type(mut self, kind: borders::BorderType) -> Self {
        match self.border {
            Some(mut bs) => { bs.kind = kind; },
            None => { self.border = Some(ContainerBorder { kind: kind, ..Default::default() }) },
        }
        self
    }

    #[inline(always)]
    pub fn build(self) -> Container {
        Container {
            border: self.border,
            constraint: self.constraint,
            children: Vec::new(),
            direction: self.direction,
            fill: self.fill,
            layout: None,
        }
    }
}
#[derive(Clone, Copy, Default)]
pub struct ContainerBorder {
    style: style::Style,
    kind: borders::BorderType,
}