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
use crate::{common::Dimensions, AbsolutePositioning, PositioningAlgorithm};
use super::{Widget, DisplayBuffer};
use std::rc::Rc;
use std::cell::RefCell;
/// #### Description
/// The root of the interface
///
pub struct Tui {
/// #### Description
/// The buffer that holds the characters to be drawn to the screen
///
display_buffer: DisplayBuffer,
/// #### Description
/// The child widgets
///
children: Vec<Rc<RefCell<dyn Widget>>>
}
impl Tui {
/// #### Description
/// Creates a new tui with the specified dimensions
///
/// #### Arguments
/// * `width`: [u32] - The width of the display
/// * `height`: [u32] - The height of the display
///
/// #### Returns
/// [Tui] - A tui object
///
pub fn new(width: u32, height: u32) -> Self {
return Self {
display_buffer: DisplayBuffer::new(Dimensions::new(width, height)),
children: vec![]
};
}
/// #### Description
/// Draws the tui to the terminal
///
pub fn draw(&mut self) {
let root_positioning_algorithm: Box<dyn PositioningAlgorithm> =
AbsolutePositioning::new();
for widget in &mut self.children {
widget.borrow_mut()
.draw(&root_positioning_algorithm, &mut self.display_buffer);
}
self.display_buffer.draw();
self.display_buffer.clear(' ');
}
/// #### Description
/// Adds a widget to the tui
///
/// #### Arguments
/// `widget`: [Rc]<[RefCell]<dyn [Widget]>> - The widget to be added
///
pub fn add_child(&mut self, widget: Rc<RefCell<dyn crate::Widget>>) {
self.children.push(widget);
}
}