~tm85/lintuini

ref: e3d8c2115570e47d29b67c4aa810b0a68a998a50 lintuini/src/elements/frame.rs -rw-r--r-- 2.1 KiB
e3d8c211TerraMaster85 feat: misguided attempt #2 30 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
use ratatui_core::layout;
use ratatui_core::style;
use ratatui_core::symbols;
use ratatui_core::widgets::Widget;

use super::element::Element;
use crate::error;

pub struct VFrame<'a> {
    children: Vec<Box<dyn Element>>,
    constraint: layout::Constraint,
    fill: style::Style,
}

impl Element for Frame {
    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()
    }

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

#[derive(Default)]
pub struct FrameBuilder<'a> {
    fill: style::Style,
    constraint: layout::Constraint,
    border: Option<BorderStyle<'a>>,
}

impl FrameBuilder<'_> {
    #[inline(always)]
    fn new() -> Self {
        FrameBuilder::default()
    }
    #[inline(always)]
    fn fill(mut self, style: style::Style) -> Self {
        self.fill = style;
        self
    }
    #[inline(always)]
    fn border_style(mut self, style: style::Style) -> Self {
        match self.border {
            Some(mut bs) => { bs.style = style; },
            None => { self.border = Some(BorderStyle { style: style, ..Default::default() }) },
        }
        self
    }
    #[inline(always)]
    fn border_charset(mut self, charset: symbols::border::Set<'_>) -> Self {
        match self.border {
            Some(mut bs) => { bs.charset = charset; },
            None => { self.border = Some(BorderStyle { charset: charset, ..Default::default() }) },
        }
        self
    }

    #[inline(always)]
    fn build<'a>(self) -> Frame<'a> {
        Frame {
            border: self.border.clone(),
            constraint: self.constraint,
            children: Vec::new(),
            fill: self.fill,
        }
    }
}

#[derive(Clone, Copy, Default)]
pub struct BorderStyle<'a> {
    style: style::Style,
    charset: symbols::border::Set<'a>,
}