1use repose_core::{Modifier, Rect, Semantics, ViewId, ViewKind};
4use slotmap::new_key_type;
5use smallvec::SmallVec;
6
7new_key_type! {
8 pub struct NodeId;
10}
11
12#[derive(Clone)]
14pub struct TreeNode {
15 pub id: NodeId,
17
18 pub view_id: ViewId,
20
21 pub kind: ViewKind,
23
24 pub modifier: Modifier,
26
27 pub semantics: Option<Semantics>,
28
29 pub children: SmallVec<[NodeId; 4]>,
31
32 pub parent: Option<NodeId>,
34
35 pub content_hash: u64,
37
38 pub layout_hash: u64,
40
41 pub measurement_hash: u64,
43
44 pub paint_hash: u64,
46
47 pub semantics_hash: u64,
49
50 pub subtree_hash: u64,
52
53 pub layout_subtree_hash: u64,
54 pub measurement_subtree_hash: u64,
55 pub paint_subtree_hash: u64,
56 pub semantics_subtree_hash: u64,
57
58 pub layout_cache: Option<LayoutCache>,
60
61 pub generation: u64,
63
64 pub user_key: Option<u64>,
66
67 pub depth: u32,
69
70 pub scope_key: Option<String>,
73}
74
75impl TreeNode {
76 pub fn new(
78 id: NodeId,
79 view_id: ViewId,
80 kind: ViewKind,
81 modifier: Modifier,
82 generation: u64,
83 ) -> Self {
84 Self {
85 id,
86 view_id,
87 kind,
88 modifier,
89 semantics: None,
90 children: SmallVec::new(),
91 parent: None,
92 content_hash: 0,
93 layout_hash: 0,
94 measurement_hash: 0,
95 paint_hash: 0,
96 semantics_hash: 0,
97 subtree_hash: 0,
98 layout_subtree_hash: 0,
99 measurement_subtree_hash: 0,
100 paint_subtree_hash: 0,
101 semantics_subtree_hash: 0,
102 layout_cache: None,
103 generation,
104 user_key: None,
105 depth: 0,
106 scope_key: None,
107 }
108 }
109
110 pub fn has_valid_layout(&self) -> bool {
112 self.layout_cache.is_some()
113 }
114
115 pub fn invalidate_layout(&mut self) {
117 self.layout_cache = None;
118 }
119
120 pub fn cached_rect(&self) -> Option<Rect> {
122 self.layout_cache.as_ref().map(|c| c.rect)
123 }
124
125 pub fn set_layout(&mut self, rect: Rect) {
126 self.layout_cache = Some(LayoutCache {
127 rect,
128 screen_rect: Rect::default(),
130 constraints: LayoutConstraints::default(),
131 generation: self.generation,
132 });
133 }
134}
135
136#[derive(Clone, Debug)]
138pub struct LayoutCache {
139 pub rect: Rect,
141
142 pub screen_rect: Rect,
144
145 pub constraints: LayoutConstraints,
147
148 pub generation: u64,
150}
151
152#[derive(Clone, Debug, PartialEq)]
154pub struct LayoutConstraints {
155 pub min_width: f32,
156 pub max_width: f32,
157 pub min_height: f32,
158 pub max_height: f32,
159}
160
161impl Default for LayoutConstraints {
162 fn default() -> Self {
163 Self {
164 min_width: 0.0,
165 max_width: f32::INFINITY,
166 min_height: 0.0,
167 max_height: f32::INFINITY,
168 }
169 }
170}
171
172impl LayoutConstraints {
173 pub fn fixed(width: f32, height: f32) -> Self {
174 Self {
175 min_width: width,
176 max_width: width,
177 min_height: height,
178 max_height: height,
179 }
180 }
181
182 pub fn tight(size: (f32, f32)) -> Self {
183 Self::fixed(size.0, size.1)
184 }
185}
186
187#[derive(Clone, Debug, Default)]
189pub struct TreeStats {
190 pub total_nodes: usize,
191
192 pub dirty_nodes: usize,
193
194 pub reconciled_nodes: usize,
195
196 pub skipped_nodes: usize,
197
198 pub created_nodes: usize,
199
200 pub removed_nodes: usize,
201
202 pub layout_cache_hits: usize,
203
204 pub layout_cache_misses: usize,
205}