summaryrefslogtreecommitdiffstats
path: root/src/hbox.rs
blob: 6a2aa8e333a7affe6ec12515708ec129c05600cf (plain)
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
use termion::event::{Event};

use crate::widget::{Widget, WidgetCore};
use crate::coordinates::{Coordinates, Size, Position};
use crate::fail::{HResult, ErrorLog};

#[derive(PartialEq)]
pub struct HBox<T: Widget> {
    pub core: WidgetCore,
    pub widgets: Vec<T>,
    pub active: Option<usize>,
}


impl<T> HBox<T> where T: Widget + PartialEq {
    pub fn new(core: &WidgetCore) -> HBox<T> {
        HBox { core: core.clone(),
               widgets: vec![],
               active: None
         }
    }


    pub fn resize_children(&mut self) {
        let coords: Vec<Coordinates>
            = self.widgets.iter().map(
                |w|
                self.calculate_coordinates(w)).collect();
        for (widget, coord) in self.widgets.iter_mut().zip(coords.iter()) {
            widget.set_coordinates(coord).log();
        }
    }

    pub fn push_widget(&mut self, widget: T) where T: PartialEq {
        self.widgets.push(widget);
        self.resize_children();
        self.refresh().log();
    }

    pub fn pop_widget(&mut self) -> Option<T> {
        let widget = self.widgets.pop();
        self.resize_children();
        self.refresh().log();
        widget
    }

    pub fn prepend_widget(&mut self, widget: T) {
        self.widgets.insert(0, widget);
        self.resize_children();
        self.refresh().log();
    }

    pub fn calculate_coordinates(&self, widget: &T)
                                 -> Coordinates where T: PartialEq  {
        let coordinates = self.get_coordinates().unwrap();
        let xsize = coordinates.xsize();
        let ysize = coordinates.ysize();
        let top = coordinates.top().y();

        let pos = self.widgets.iter().position(|w | w == widget).unwrap();
        let num = self.widgets.len();

        let widget_xsize = (xsize / num as u16) + 1;
        let widget_xpos = widget_xsize * pos as u16;

        Coordinates {
            size: Size((widget_xsize,
                        ysize)),
            position: Position((widget_xpos,
                                top))
        }
    }

    pub fn active_widget(&self) -> &T {
        &self.widgets.last().unwrap()
    }

}




impl<T> Widget for HBox<T> where T: Widget + PartialEq {
    fn get_core(&self) -> HResult<&WidgetCore> {
        Ok(&self.core)
    }
    fn get_core_mut(&mut self) -> HResult<&mut WidgetCore> {
        Ok(&mut self.core)
    }
    fn render_header(&self) -> HResult<String> {
        self.active_widget().render_header()
    }

    fn refresh(&mut self) -> HResult<()> {
        self.resize_children();
        for child in &mut self.widgets {
            child.refresh()?
        }
        Ok(())
    }

    fn get_drawlist(&self) -> HResult<String> {
        Ok(self.widgets.iter().map(|child| {
            child.get_drawlist().unwrap()
        }).collect())
    }

    fn on_event(&mut self, event: Event) -> HResult<()> {
        self.widgets.last_mut()?.on_event(event)?;
        Ok(())
    }
}