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
use crate::render::layout::Layout;
use crate::render::widget_cache::WidgetContainer;
use std::cell::RefCell;
pub struct LayoutContainer {
pub layout: RefCell<Box<dyn Layout>>,
layout_id: i32,
}
impl LayoutContainer {
pub fn new(layout: Box<dyn Layout>, layout_id: i32) -> Self {
Self {
layout: RefCell::new(layout),
layout_id,
}
}
pub fn get_layout_id(&self) -> i32 {
self.layout_id
}
}
pub struct LayoutCache {
cache: Vec<LayoutContainer>,
}
impl LayoutCache {
pub fn new() -> Self {
Self { cache: Vec::new() }
}
pub fn add_layout(&mut self, layout: Box<dyn Layout>) -> i32 {
let layout_id = self.cache.len() as i32;
self.cache.push(LayoutContainer::new(layout, layout_id));
layout_id
}
pub fn get_layout_by_id(&mut self, id: i32) -> &mut LayoutContainer {
&mut self.cache[id as usize]
}
pub fn get_layout_cache(&self) -> &[LayoutContainer] {
&self.cache
}
pub fn do_layout(&self, widgets: &[WidgetContainer]) {
for x in &self.cache {
let needs_layout = x.layout.borrow().needs_layout();
if needs_layout {
x.layout.borrow_mut().do_layout(widgets);
}
}
}
}
impl Default for LayoutCache {
fn default() -> Self {
Self::new()
}
}