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
|
use termion::event::{Event};
use crate::widget::Widget;
// pub struct Child<T> {
// widget: T,
// position: (u16, u16),
// size: (u16, u16),
// active: bool
// }
pub struct HBox {
dimensions: (u16, u16),
position: (u16, u16),
children: Vec<Box<Widget>>,
active: usize
}
impl HBox {
pub fn new(widgets: Vec<Box<Widget>>,
dimensions: (u16, u16),
position: (u16, u16),
main: usize) -> HBox {
let mut hbox = HBox {
dimensions: dimensions,
position: position,
children: widgets,
active: main
};
hbox.resize_children();
hbox
}
pub fn resize_children(&mut self) {
let hbox_size = dbg!(self.dimensions);
let hbox_position = dbg!(self.position);
let cell_size = dbg!(hbox_size.0 / self.children.len() as u16);
let mut current_pos = dbg!(hbox_position.1);
for widget in &mut self.children {
widget.set_dimensions(dbg!((cell_size, hbox_size.1)));
widget.set_position(dbg!((current_pos, hbox_position.1)));
widget.refresh();
dbg!(current_pos += cell_size);
}
}
// pub fn widget(&self, index: usize) -> &Box<Widget> {
// &self.children[index]
// }
pub fn active_widget(&self) -> &Box<Widget> {
&self.children[self.active]
}
}
impl Widget for HBox {
fn render(&self) -> Vec<String> {
// HBox doesnt' draw anything itself
vec![]
}
fn render_header(&self) -> String {
self.active_widget().render_header()
}
fn refresh(&mut self) {
for child in &mut self.children {
child.refresh();
}
}
fn get_drawlist(&mut self) -> String {
self.children.iter_mut().map(|child| {
child.get_drawlist()
}).collect()
}
fn get_dimensions(&self) -> (u16, u16) {
self.dimensions
}
fn get_position(&self) -> (u16, u16) {
self.position
}
fn set_dimensions(&mut self, size: (u16, u16)) {
self.dimensions = size;
}
fn set_position(&mut self, position: (u16, u16)) {
self.position = position;
}
fn on_event(&mut self, event: Event) {
self.children[self.active].on_event(event);
}
}
|