1use crate::*;
2
3pub struct PaintCx<'a, C: Context> {
5 bump: &'a BumpArena,
6 context: &'a mut C,
7}
8
9impl<'a, C: Context> PaintCx<'a, C> {
10 pub fn context(&self) -> &C {
11 self.context
12 }
13
14 pub fn context_mut(&mut self) -> &mut C {
15 self.context
16 }
17
18 pub fn scratch(&self) -> &'a ScratchBuffer {
19 &self.bump.storage
20 }
21}
22
23pub fn insert_measure<C: Context, R, F>(tree: &mut Tree<C>, node: NodeId, measure: F)
28where
29 R: Into<Fit<Size<C::Scalar>>>,
30 F: Fn(&mut C, Constraints<C::Scalar>) -> R + 'static,
31{
32 fn run<C: Context, R, F>(
33 bump: &BumpArena,
34 id: BumpId,
35 context: &mut C,
36 bounds: Constraints<C::Scalar>,
37 ) -> Fit<Size<C::Scalar>>
38 where
39 R: Into<Fit<Size<C::Scalar>>>,
40 F: Fn(&mut C, Constraints<C::Scalar>) -> R + 'static,
41 {
42 (bump.load::<F>(id))(context, bounds).into()
43 }
44
45 let id = StoredMeasureId::new(tree.measures.len());
46 let node = &mut tree.nodes[node.index()];
47 tree.measures.push(StoredMeasure {
49 run: run::<C, R, F>,
50 bump_id: tree.bump.store(measure),
51 next: node.first_measure,
52 });
53 node.first_measure = id;
54}
55
56pub fn insert_paint<C: Context, F>(tree: &mut Tree<C>, node: NodeId, paint: F)
57where
58 F: Fn(&mut PaintCx<'_, C>, Rect<C::Scalar>) + 'static,
59{
60 fn run<C: Context, F>(bump: &BumpArena, id: BumpId, context: &mut C, area: Rect<C::Scalar>)
61 where
62 F: Fn(&mut PaintCx<'_, C>, Rect<C::Scalar>) + 'static,
63 {
64 (bump.load::<F>(id))(&mut PaintCx { bump, context }, area);
65 }
66
67 let id = StoredPaintId::new(tree.paints.len());
68 let bump_id = tree.bump.store(paint);
69 let node = &mut tree.nodes[node.index()];
70 let next = if let Some(last) = node.last_paint.index() {
72 let first = tree.paints[last].next;
73 tree.paints[last].next = id;
74 first
75 } else {
76 id
77 };
78 tree.paints.push(StoredPaint {
79 run: run::<C, F>,
80 bump_id,
81 next,
82 });
83 node.last_paint = id;
84}
85
86pub fn resolve_order<C: Context>(tree: &mut Tree<C>, bump: &BumpArena) {
87 tree.paint_order.clear();
88 if !tree.has_custom_order {
89 return;
90 }
91 let count = tree.nodes.len();
92 let z_indices = &tree.z_indices;
93 let z_index = |index: usize| z_indices.get(index).copied().unwrap_or(0);
94 if !tree
95 .nodes
96 .iter()
97 .enumerate()
98 .any(|(index, node)| z_index(index) != 0 || visual_parent(&tree.aux, node) != node.parent)
99 {
100 return;
101 }
102
103 let mut scratch = bump.storage.alloc(count);
104 scratch.resize(count, PaintLinks::default());
105 let links = &mut scratch[..count];
106 for index in 1..count {
108 let parent = visual_parent(&tree.aux, &tree.nodes[index]).index();
109 links[index].next_sibling = links[parent].first_child;
110 links[parent].first_child = index as u32;
111 }
112
113 tree.paint_order.resize(count, NodeId::new(0));
115 let order = &mut tree.paint_order[..count];
116 let mut pending = count - 1;
117 for output in 0..count {
118 debug_assert!(pending >= output && pending < count);
119 let parent = order[pending];
120 pending += 1;
121 order[output] = parent;
122 let start = pending;
123 let mut child = links[parent.index()].first_child;
124 let mut needs_sort = false;
125 while child != 0 {
126 let index = child as usize;
127 child = links[index].next_sibling;
128 needs_sort = needs_sort || z_index(index) != 0;
129 pending -= 1;
130 debug_assert!(pending > output);
131 order[pending] = NodeId::new(index);
132 }
133 let children = &mut order[pending..start];
134 if needs_sort {
135 children.sort_unstable_by_key(|id| (z_index(id.index()), id.index()));
136 }
137 }
138 debug_assert_eq!(pending, count);
139}
140
141pub fn resolve_clips<C: Context>(tree: &mut Tree<C>) {
142 tree.interaction.clips.clear();
143 tree.resolved_clips.clear();
144 if !tree.has_clips {
145 return;
146 }
147 let count = tree.nodes.len();
148 let nodes = &tree.nodes[..count];
149 let placements = &tree.placements[..count];
150 let aux = &tree.aux;
151 tree.resolved_clips.reserve(count);
152 for index in 0..count {
153 let parent = if index == 0 {
154 ResolvedClipId::NONE
155 } else {
156 tree.resolved_clips[visual_parent(aux, &nodes[index]).index()]
157 };
158 let Some(radius) = nodes[index].aux.index().and_then(|index| aux[index].clip) else {
159 tree.resolved_clips.push(parent);
160 continue;
161 };
162 let area = placements[index];
163 let bounds = parent
164 .index()
165 .map_or(tree.screen, |clip| tree.interaction.clips[clip].bounds)
166 .intersection(area)
167 .unwrap_or_default();
168 let id = ResolvedClipId::new(tree.interaction.clips.len());
169 let depth = parent
170 .index()
171 .map_or(1, |parent| tree.interaction.clips[parent].depth + 1);
172 tree.interaction.clips.push(ResolvedClip {
173 parent,
174 depth,
175 area,
176 radius,
177 bounds,
178 });
179 tree.resolved_clips.push(id);
180 }
181}
182
183pub fn paint<C: Context>(
184 tree: &Tree<C>,
185 bump: &BumpArena,
186 context: &mut C,
187 mut push_clip: impl FnMut(&mut C, Rect<C::Scalar>, Corners<C::Scalar>),
188 mut pop_clip: impl FnMut(&mut C),
189) {
190 let mut active = bump.storage.alloc::<ResolvedClipId>(tree.interaction.clips.len());
192 let mut set = |context: &mut C, target: ResolvedClipId| {
193 if active.last().map_or(u32::MAX, |clip| clip.0) == target.0 {
194 return;
195 }
196 let mut common = target;
197 while let Some(index) = common.index() {
198 let depth = tree.interaction.clips[index].depth as usize;
199 if depth <= active.len() && active[depth - 1].0 == common.0 {
200 break;
201 }
202 common = tree.interaction.clips[index].parent;
203 }
204 while active.last().map_or(u32::MAX, |clip| clip.0) != common.0 {
205 active.pop();
206 pop_clip(context);
207 }
208 let start = active.len();
209 let mut clip = target;
210 while clip.0 != common.0 {
211 active.push(clip);
212 clip = tree.interaction.clips[clip.index().unwrap()].parent;
213 }
214 active[start..].reverse();
215 for &clip in &active[start..] {
216 let stored = tree.interaction.clips[clip.index().unwrap()];
217 push_clip(context, stored.area, stored.radius);
218 }
219 };
220 let count = if tree.paint_order.is_empty() {
221 tree.nodes.len()
222 } else {
223 tree.paint_order.len()
224 };
225 for index in 0..count {
226 let node = if tree.paint_order.is_empty() {
227 index
228 } else {
229 tree.paint_order[index].index()
230 };
231 let Some(last) = tree.nodes[node].last_paint.index() else {
232 continue;
233 };
234 let area = tree.placements[node];
235 let resolved_clip = if tree.resolved_clips.is_empty() {
236 ResolvedClipId::NONE
237 } else {
238 tree.resolved_clips[node]
239 };
240 let bounds = resolved_clip
242 .index()
243 .map_or(tree.screen, |clip| tree.interaction.clips[clip].bounds);
244 if bounds.width <= C::Scalar::ZERO || bounds.height <= C::Scalar::ZERO {
245 continue;
246 }
247 set(context, resolved_clip);
248 let mut index = tree.paints[last].next.0 as usize;
249 loop {
250 let stored = tree.paints[index];
251 (stored.run)(bump, stored.bump_id, context, area);
252 if index == last {
253 break;
254 }
255 index = stored.next.0 as usize;
256 }
257 }
258 set(context, ResolvedClipId::NONE);
259}
260
261#[derive(Clone, Copy, Default)]
262struct PaintLinks {
263 first_child: u32,
264 next_sibling: u32,
265}