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
use std::{cell::RefCell, collections::BTreeMap, rc::Rc};
use dces::prelude::{Entity, EntityComponentManager, System};
use crate::{css_engine::*, prelude::*, shell::WindowShell, tree::Tree};
pub struct InitSystem {
pub shell: Rc<RefCell<WindowShell<WindowAdapter>>>,
pub states: Rc<RefCell<BTreeMap<Entity, Rc<dyn State>>>>,
pub render_objects: Rc<RefCell<BTreeMap<Entity, Box<dyn RenderObject>>>>,
pub layouts: Rc<RefCell<BTreeMap<Entity, Box<dyn Layout>>>>,
pub handlers: EventHandlerMap,
}
impl InitSystem {
fn init_id(&self, node: Entity, store: &mut StringComponentStore, root: Entity) {
let id = if let Ok(selector) = store.get::<Selector>("selector", node) {
if let Some(id) = &selector.id {
Some((node, id.clone()))
} else {
None
}
} else {
None
};
if let Some((entity, id)) = id {
if let Ok(global) = store.get_mut::<Global>("global", root) {
global.id_map.insert(id, entity);
}
}
}
fn read_init_from_theme(&self, ctx: &mut Context) {
ctx.widget().update_theme_by_state(true);
}
}
impl System<Tree, StringComponentStore> for InitSystem {
fn run(&self, ecm: &mut EntityComponentManager<Tree, StringComponentStore>) {
let root = ecm.entity_store().root;
#[cfg(feature = "debug")]
let debug = true;
#[cfg(not(feature = "debug"))]
let debug = false;
if debug {
crate::shell::CONSOLE.log("\n------ Widget tree ------\n".to_string());
print_tree(root, 0, ecm);
crate::shell::CONSOLE.log("\n------ Widget tree ------\n".to_string());
}
let window_shell = &mut self.shell.borrow_mut();
let theme = ecm
.component_store()
.get::<Theme>("theme", root)
.unwrap()
.clone();
let mut current_node = root;
loop {
self.init_id(current_node, ecm.component_store_mut(), root);
{
let mut ctx = Context::new(
(current_node, ecm),
window_shell,
&theme,
self.render_objects.clone(),
self.layouts.clone(),
self.handlers.clone(),
self.states.clone(),
);
if let Some(state) = self.states.borrow().get(¤t_node) {
state.init(&mut ctx);
}
self.read_init_from_theme(&mut ctx);
}
let mut it = ecm.entity_store().start_node(current_node).into_iter();
it.next();
if let Some(node) = it.next() {
current_node = node;
} else {
break;
}
}
}
}
pub fn print_tree(
entity: Entity,
depth: usize,
ecm: &mut EntityComponentManager<Tree, StringComponentStore>,
) {
let name = ecm.component_store().get::<String>("name", entity).unwrap();
let selector = if let Ok(selector) = ecm.component_store().get::<Selector>("selector", entity) {
selector.clone()
} else {
Selector::default()
};
crate::shell::CONSOLE.log(format!(
"{}{} (entity: {}{})",
"| ".repeat(depth),
name,
entity.0,
selector
));
for child in ecm.entity_store().clone().children.get(&entity).unwrap() {
print_tree(*child, depth + 1, ecm);
}
}