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 std::{cell::RefCell, collections::BTreeMap};
use dces::prelude::*;
use crate::{
render::RenderContext2D, theming::*, tree::Tree, utils::prelude::*, widget_base::mark_as_dirty,
};
use super::{component, component_try_mut, Layout};
#[derive(Default)]
pub struct AbsoluteLayout {
desired_size: RefCell<DirtySize>,
}
impl AbsoluteLayout {
pub fn new() -> Self {
AbsoluteLayout::default()
}
}
impl Layout for AbsoluteLayout {
fn measure(
&self,
render_context_2_d: &mut RenderContext2D,
entity: Entity,
ecm: &mut EntityComponentManager<Tree, StringComponentStore>,
layouts: &BTreeMap<Entity, Box<dyn Layout>>,
theme: &Theme,
) -> DirtySize {
let window = ecm.entity_store().root();
if let Ok(bounds) = ecm.component_store().get::<Rectangle>("bounds", window) {
self.desired_size
.borrow_mut()
.set_size(bounds.width(), bounds.height());
}
for index in 0..ecm.entity_store().children[&entity].len() {
let child = ecm.entity_store().children[&entity][index];
if let Some(child_layout) = layouts.get(&child) {
let dirty = child_layout
.measure(render_context_2_d, child, ecm, layouts, theme)
.dirty()
|| self.desired_size.borrow().dirty();
self.desired_size.borrow_mut().set_dirty(dirty);
}
}
*self.desired_size.borrow()
}
fn arrange(
&self,
render_context_2_d: &mut RenderContext2D,
_parent_size: (f64, f64),
entity: Entity,
ecm: &mut EntityComponentManager<Tree, StringComponentStore>,
layouts: &BTreeMap<Entity, Box<dyn Layout>>,
theme: &Theme,
) -> (f64, f64) {
if component::<Visibility>(ecm, entity, "visibility") == Visibility::Collapsed {
self.desired_size.borrow_mut().set_size(0.0, 0.0);
return (0.0, 0.0);
}
if let Some(bounds) = component_try_mut::<Rectangle>(ecm, entity, "bounds") {
bounds.set_width(self.desired_size.borrow().width());
bounds.set_height(self.desired_size.borrow().height());
}
mark_as_dirty("bounds", entity, ecm);
for index in 0..ecm.entity_store().children[&entity].len() {
let child = ecm.entity_store().children[&entity][index];
if let Some(child_layout) = layouts.get(&child) {
child_layout.arrange(
render_context_2_d,
(
self.desired_size.borrow().width(),
self.desired_size.borrow().height(),
),
child,
ecm,
layouts,
theme,
);
}
}
self.desired_size.borrow_mut().set_dirty(false);
self.desired_size.borrow().size()
}
}
impl Into<Box<dyn Layout>> for AbsoluteLayout {
fn into(self) -> Box<dyn Layout> {
Box::new(self)
}
}