~tm85/lintuini

ref: 44f71ca723f3b73981a343799b1f64dc6a9e2aec lintuini/src/widgets/widget.rs -rw-r--r-- 2.5 KiB
44f71ca7TerraMaster85 feat: misguided 1st attempt a month 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
use crate::error;

pub trait Widget {
    fn set_size(&mut self, size: WidgetSize) -> Result<(), error::WitError>;
    fn size(&self) -> WidgetSize;
    fn add_child(&mut self, child: Box<dyn Widget>) -> Result<(), error::WitError>;
    fn children(&self) -> &[Box<dyn Widget>];
    fn set_pos(&mut self, pos: WidgetPos) -> Result<(), error::WitError>;
    fn pos(&self) -> WidgetPos;
    fn set_display_mode(&mut self, display: DisplayMode) -> Result<(), error::WitError>;
    fn display_mode(&self) -> DisplayMode;

    fn set_static(&mut self) -> Result<(), error::WitError> { self.set_display_mode(DisplayMode::Static) }
    fn is_static(&self) -> bool { matches!(self.display_mode(), DisplayMode::Static) }
    fn set_floating(&mut self) -> Result<(), error::WitError> { self.set_display_mode(DisplayMode::Floating) }
    fn is_floating(&self) -> bool { matches!(self.display_mode(), DisplayMode::Floating) }
    fn hide(&mut self) -> Result<(), error::WitError> { self.set_display_mode(DisplayMode::Hidden) }
    fn is_hidden(&self) -> bool { matches!(self.display_mode(), DisplayMode::Hidden) }
}

// should write some macros to make this nicer

#[derive(Clone, Copy)]
pub struct WidgetSize(WidgetSizeDimn, WidgetSizeDimn);

impl WidgetSize {
    #[inline(always)]
    pub fn x(&self) -> WidgetSizeDimn {
        self.0
    }
    #[inline(always)]
    pub fn y(&self) -> WidgetSizeDimn {
        self.1
    }
}

impl From<WidgetSize> for (u16, u16) {
    #[inline(always)]
    fn from(size: WidgetSize) -> Self {
        (size.0.into(), size.1.into())
    }
}

#[derive(Clone, Copy)]
pub enum WidgetSizeDimn {
    Static(u16),
    Fill,
    Fit,
}

impl From<WidgetSizeDimn> for u16 {
    #[inline(always)]
    fn from(dim: WidgetSizeDimn) -> Self {
        match dim {
            WidgetSizeDimn::Static(x) => x,
            WidgetSizeDimn::Fill => todo!(),
            WidgetSizeDimn::Fit => todo!(),
        }
    }
}

#[derive(Clone, Copy)]
pub struct WidgetPos(WidgetPosDimn, WidgetPosDimn);

impl WidgetPos {
    #[inline(always)]
    pub fn x(&self) -> WidgetPosDimn {
        self.0
    }
    #[inline(always)]
    pub fn y(&self) -> WidgetPosDimn {
        self.1
    }
}

impl Default for WidgetPos {
    fn default() -> WidgetPos { 
        WidgetPos(WidgetPosDimn::FloatStart, WidgetPosDimn::FloatStart)
    }
}

#[derive(Clone, Copy)]
pub enum WidgetPosDimn {
    Static(u16),
    FloatStart,
    FloatEnd,
}

impl Default for WidgetPosDimn {
    fn default() -> Self { Self::FloatStart }
}

#[derive(Clone, Copy)]
pub enum DisplayMode {
    Static,
    Floating,
    Hidden,
}