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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::Event;
use crate::{buffer::Buffer, Cmd};
pub use button::Button;
pub use checkbox::Checkbox;
pub use flex_box::FlexBox;
pub use group_box::GroupBox;
pub use image_control::Image;
pub use link::Link;
pub use list_box::ListBox;
pub use radio::Radio;
pub use slider::Slider;
use std::{any::Any, fmt};
use stretch::result::Layout;
use stretch::{
    node::{Node, Stretch},
    style::Style,
};
pub use tab_box::TabBox;
pub use text_area::TextArea;
pub use text_input::TextInput;
pub use text_label::TextLabel;

mod button;
mod checkbox;
mod flex_box;
mod group_box;
mod image_control;
mod link;
mod list_box;
mod radio;
mod slider;
mod tab_box;
mod text_area;
mod text_input;
mod text_label;

/// All widgets must implement the Widget trait
pub trait Widget<MSG>
where
    Self: fmt::Debug,
{
    /// return the style of this widget
    fn style(&self) -> Style;

    /// return the layout of thiswidget
    fn layout(&self) -> Option<&Layout>;

    /// return the offset of the parent,
    /// this before any of it's children to be drawn
    fn get_offset(&self) -> (f32, f32) {
        (0.0, 0.0)
    }

    fn set_layout(&mut self, layout: Layout);

    /// add a child to this widget
    /// returns true if it can accept a child, false otherwise
    fn add_child(&mut self, _child: Box<dyn Widget<MSG>>) -> bool {
        false
    }

    /// get a referemce tp the children of this widget
    fn children(&self) -> Option<&[Box<dyn Widget<MSG>>]> {
        None
    }

    /// get a mutable reference to the children of this widget
    fn children_mut(&mut self) -> Option<&mut [Box<dyn Widget<MSG>>]> {
        None
    }

    /// return a mutable reference to a child at index location
    fn child_mut(
        &mut self,
        _index: usize,
    ) -> Option<&mut Box<dyn Widget<MSG>>> {
        None
    }

    /// this is called in the render loop in the renderer where the widget
    /// writes into the buffer. The result will then be written into the
    /// stdout terminal.
    fn draw(&self, but: &mut Buffer) -> Vec<Cmd>;

    /// build a node with styles from this widget and its children
    /// The Layout tree is then calculated see `layout::compute_layout`
    fn style_node(&self, stretch: &mut Stretch) -> Option<Node> {
        let children_styles = if let Some(children) = self.children() {
            children
                .iter()
                .filter_map(|c| c.style_node(stretch))
                .collect()
        } else {
            vec![]
        };
        stretch.new_node(self.style(), &children_styles).ok()
    }

    /// set the widget as focused
    fn set_focused(&mut self, _focused: bool) {}

    /// get an Any reference
    fn as_any(&self) -> &dyn Any;

    /// get an Any mutable reference for casting purposed
    fn as_any_mut(&mut self) -> &mut dyn Any;

    /// set the size of the widget
    fn set_size(&mut self, width: Option<f32>, height: Option<f32>);

    /// get a mutable reference of this widget
    fn as_mut(&mut self) -> Option<&mut Self>
    where
        Self: Sized + 'static,
    {
        self.as_any_mut().downcast_mut::<Self>()
    }

    /// this process the event and all callbacks attached to the widgets will be dispatched.
    fn process_event(&mut self, _event: Event) -> Vec<MSG> {
        vec![]
    }

    ///  take the children at this index location
    fn take_child(&mut self, _index: usize) -> Option<Box<dyn Widget<MSG>>> {
        None
    }

    /// set the id of this widget
    fn set_id(&mut self, id: &str);

    /// get the id of this widget
    fn get_id(&self) -> &Option<String>;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::*;
    use std::boxed;
    use stretch::{geometry::*, number::Number};

    #[test]
    fn layout() {
        let mut control = FlexBox::<()>::new();
        control.horizontal();
        let mut btn = Button::<()>::new("Hello");
        btn.set_size(Some(30.0), Some(34.0));

        control.add_child(boxed::Box::new(btn));

        let mut btn = Button::<()>::new("world");
        btn.set_size(Some(20.0), Some(10.0));
        control.add_child(boxed::Box::new(btn));

        crate::layout::compute_node_layout(
            &mut control,
            Size {
                width: Number::Defined(100.0),
                height: Number::Defined(100.0),
            },
        );

        let layout1 = control.children().expect("must have children")[1]
            .layout()
            .expect("must have layout");
        assert_eq!(
            layout1.size,
            Size {
                width: 20.0,
                height: 10.0
            }
        );

        assert_eq!(layout1.location, Point { x: 30.0, y: 0.0 });
    }

    #[test]
    fn layout2() {
        let mut control = FlexBox::<()>::new();
        control.vertical();

        let mut btn1 = Button::<()>::new("Hello");
        btn1.set_size(Some(100.0), Some(20.0));

        control.add_child(boxed::Box::new(btn1));

        let mut btn2 = Button::<()>::new("world");
        btn2.set_size(Some(20.0), Some(10.0));

        let mut btn3 = Button::<()>::new("world");
        btn3.set_size(Some(20.0), Some(10.0));

        let mut hrow = FlexBox::<()>::new();
        hrow.horizontal();

        hrow.add_child(boxed::Box::new(btn2));
        hrow.add_child(boxed::Box::new(btn3));

        control.add_child(boxed::Box::new(hrow));

        crate::layout::compute_node_layout(
            &mut control,
            Size {
                width: Number::Defined(100.0),
                height: Number::Defined(100.0),
            },
        );

        let layout_btn2 = control.children().expect("must have children")[1]
            .children()
            .expect("must have grand children")[1]
            .layout()
            .expect("must have a layout");
        assert_eq!(layout_btn2.location, Point { x: 20.0, y: 17.0 });
    }
}