summaryrefslogtreecommitdiffstats
path: root/src/hbox.rs
blob: 391aae015342662b368494b11ab1a05f2e32902b (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
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 termion::event::{Event};

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

#[derive(Debug, PartialEq)]
pub struct HBox<T: Widget> {
    pub core: WidgetCore,
    pub widgets: Vec<T>,
    pub ratios: Option<Vec<usize>>,
    pub zoom_active: bool,
    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![],
               ratios: None,
               zoom_active: false,
               active: None
         }
    }


    pub fn resize_children(&mut self) -> HResult<()> {
        let len = self.widgets.len();
        if len == 0 { return Ok(()) }

        if self.zoom_active {
            let coords = self.core.coordinates.clone();
            self.active_widget_mut()?.set_coordinates(&coords).log();
            return Ok(());
        }

        let coords: Vec<Coordinates> = self.calculate_coordinates()?;


        for (widget, coord) in self.widgets.iter_mut().zip(coords.iter()) {
            widget.set_coordinates(coord).log();
        }

        Ok(())
    }

    pub fn push_widget(&mut self, widget: T) {
        self.widgets.push(widget);
    }

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

    pub fn remove_widget(&mut self, index: usize) -> T {
        self.widgets.remove(index)
    }

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

    pub fn insert_widget(&mut self, index: usize, widget: T) {
        self.widgets.insert(index, widget);
    }

    pub fn replace_widget(&mut self, index: usize, mut widget: T) -> T {
        std::mem::swap(&mut self.widgets[index], &mut widget);
        widget
    }

    pub fn toggle_zoom(&mut self) -> HResult<()> {
        self.core.clear().log();
        self.zoom_active = !self.zoom_active;
        self.resize_children()
    }

    pub fn set_ratios(&mut self, ratios: Vec<usize>) {
        self.ratios = Some(ratios);
    }

    pub fn calculate_equal_ratios(&self) -> HResult<Vec<usize>> {
        let len = self.widgets.len();
        if len == 0 { return HError::no_widget(); }

        let ratios = (0..len).map(|_| 100 / len).collect();
        Ok(ratios)
    }

    pub fn calculate_coordinates(&self)
                                 -> HResult<Vec<Coordinates>> {
        let box_coords = self.get_coordinates()?;
        let box_xsize = box_coords.xsize();
        let box_ysize = box_coords.ysize();
        let box_top = box_coords.top().y();

        let mut ratios = match &self.ratios {
            Some(ratios) => ratios.clone(),
            None => self.calculate_equal_ratios()?
        };

        let mut ratios_sum: usize = ratios.iter().sum();

        ratios = ratios.iter().map(|&r|
            (r as f64 * box_xsize as f64 / ratios_sum as f64).round() as usize).collect();

        for r in &mut ratios {
            if *r < 10 { *r = 10 }
        }

        ratios_sum = ratios.iter().sum();

        while ratios_sum + ratios.len() > box_xsize as usize {
            let ratios_max = ratios.iter()
                .position(|&r| r == *ratios.iter().max().unwrap()).unwrap();
            ratios[ratios_max] = ratios[ratios_max] - 1;
            ratios_sum -= 1;
        }

        let coords = ratios.iter().fold(Vec::<Coordinates>::new(), |mut coords, ratio| {
            let len = coords.len();
            let gap = if len == ratios.len() { 0 } else { 1 };

            let widget_xsize = *ratio as u16;
            let widget_xpos = if len == 0 {
                box_coords.top().x()
            } else {
                let prev_coords = coords.last().unwrap();
                let prev_xsize = prev_coords.xsize();
                let prev_xpos = prev_coords.position().x();

                prev_xsize + prev_xpos + gap
            };

            coords.push(Coordinates {
                size: Size((widget_xsize,
                            box_ysize)),
                position: Position((widget_xpos,
                                   box_top))
            });
            coords
        });

        Ok(coords)
    }

    pub fn set_active(&mut self, i: usize) -> HResult<()> {
        if i+1 > self.widgets.len() {
            HError::no_widget()?
        }
        self.active = Some(i);
        Ok(())
    }

    pub fn active_widget(&self) -> Option<&T> {
        self.widgets.get(self.active?)
    }

    pub fn active_widget_mut(&mut self) -> Option<&mut T> {
        self.widgets.get_mut(self.active?)
    }
}




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 set_coordinates(&mut self, coordinates: &Coordinates) -> HResult<()> {
        self.core.coordinates = coordinates.clone();
        self.resize_children()
    }

    fn render_header(&self) -> HResult<String> {
        self.active_widget()?.render_header()
    }

    fn refresh(&mut self) -> HResult<()> {
        if self.zoom_active {
            self.active_widget_mut()?.refresh().log();
            return Ok(());
        }

        self.resize_children().log();
        for child in &mut self.widgets {
            child.refresh().log();
        }
        Ok(())
    }

    fn get_drawlist(&self) -> HResult<String> {
        if self.zoom_active {
            return self.active_widget()?.get_drawlist();
        }

        Ok(self.widgets.iter().map(|child| {
            child.get_drawlist().log_and().unwrap_or_else(|_| String::new())
        }).collect())
    }

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

    fn on_key(&mut self, key: termion::event::Key) -> HResult<()> {
        self.active_widget_mut()?.on_key(key)
    }
}