1use super::*;
2
3impl DisplayList {
5 pub fn reset(&mut self, viewport: Size, scale_factor: f32) {
13 self.get_mut_commands().clear();
14 self.set_viewport(viewport);
15 self.set_scale_factor(scale_factor.max(0.5));
16 self.set_portal_split_index(0);
17 }
18
19 pub fn push(&mut self, command: DrawCommand) {
26 self.get_mut_commands().push(command);
27 }
28
29 #[must_use]
39 pub fn len(&self) -> usize {
40 self.get_commands().len()
41 }
42
43 #[must_use]
53 pub fn is_empty(&self) -> bool {
54 self.get_commands().is_empty()
55 }
56
57 fn mix_hash(hash: &mut u64, value: u64) {
64 *hash ^= value;
65 *hash = hash.wrapping_mul(FNV1A_PRIME);
66 }
67
68 #[must_use]
78 pub fn fast_hash(&self) -> u64 {
79 let mut hash: u64 = FNV1A_OFFSET_BASIS;
80 Self::mix_hash(&mut hash, self.get_commands().len() as u64);
81 Self::mix_hash(&mut hash, self.get_viewport().get_width().to_bits() as u64);
82 Self::mix_hash(&mut hash, self.get_viewport().get_height().to_bits() as u64);
83 Self::mix_hash(&mut hash, self.get_scale_factor().to_bits() as u64);
84 for cmd in self.get_commands().iter() {
85 match cmd {
86 DrawCommand::Clear(c) => {
87 Self::mix_hash(&mut hash, 1);
88 Self::mix_hash(&mut hash, u32::from_le_bytes(c.to_rgba_bytes()) as u64);
89 }
90 DrawCommand::FillRoundedRect {
91 rect,
92 color,
93 border,
94 ..
95 } => {
96 Self::mix_hash(&mut hash, 2);
97 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
98 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
99 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
100 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
101 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
102 Self::mix_hash(&mut hash, border.get_width().to_bits() as u64);
103 }
104 DrawCommand::LinearGradientRect { rect, gradient, .. } => {
105 Self::mix_hash(&mut hash, 3);
106 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
107 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
108 Self::mix_hash(
109 &mut hash,
110 u32::from_le_bytes(gradient.get_start_color().to_rgba_bytes()) as u64,
111 );
112 }
113 DrawCommand::BoxShadow { rect, shadow, .. } => {
114 Self::mix_hash(&mut hash, 4);
115 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
116 Self::mix_hash(&mut hash, shadow.get_blur_radius().to_bits() as u64);
117 }
118 DrawCommand::BackdropBlur {
119 rect, blur_radius, ..
120 } => {
121 Self::mix_hash(&mut hash, 15);
122 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
123 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
124 Self::mix_hash(&mut hash, blur_radius.to_bits() as u64);
125 }
126 DrawCommand::TextRun { rect, text, style } => {
127 Self::mix_hash(&mut hash, 5);
128 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
129 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
130 Self::mix_hash(&mut hash, text.len() as u64);
131 for byte in text.as_bytes().iter().take(16).copied() {
132 Self::mix_hash(&mut hash, byte as u64);
133 }
134 Self::mix_hash(
135 &mut hash,
136 u32::from_le_bytes(style.get_color().to_rgba_bytes()) as u64,
137 );
138 }
139 DrawCommand::TextCaret {
140 text_rect,
141 text,
142 color,
143 ..
144 } => {
145 Self::mix_hash(&mut hash, 13);
146 Self::mix_hash(&mut hash, text_rect.get_x().to_bits() as u64);
147 Self::mix_hash(&mut hash, text_rect.get_y().to_bits() as u64);
148 Self::mix_hash(&mut hash, text.len() as u64);
149 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
150 }
151 DrawCommand::Image { rect, buffer, .. } => {
152 Self::mix_hash(&mut hash, 6);
153 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
154 Self::mix_hash(&mut hash, buffer.get_width() as u64);
155 Self::mix_hash(&mut hash, buffer.get_height() as u64);
156 }
157 DrawCommand::Line {
158 from,
159 to,
160 color,
161 width,
162 } => {
163 Self::mix_hash(&mut hash, 7);
164 Self::mix_hash(&mut hash, from.get_x().to_bits() as u64);
165 Self::mix_hash(&mut hash, to.get_y().to_bits() as u64);
166 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
167 Self::mix_hash(&mut hash, width.to_bits() as u64);
168 }
169 DrawCommand::Circle {
170 center,
171 radius,
172 color,
173 } => {
174 Self::mix_hash(&mut hash, 8);
175 Self::mix_hash(&mut hash, center.get_x().to_bits() as u64);
176 Self::mix_hash(&mut hash, center.get_y().to_bits() as u64);
177 Self::mix_hash(&mut hash, radius.to_bits() as u64);
178 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
179 }
180 DrawCommand::Triangle { p0, p1, p2, color } => {
181 Self::mix_hash(&mut hash, 9);
182 Self::mix_hash(&mut hash, p0.get_x().to_bits() as u64);
183 Self::mix_hash(&mut hash, p1.get_y().to_bits() as u64);
184 Self::mix_hash(&mut hash, p2.get_x().to_bits() as u64);
185 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
186 }
187 DrawCommand::SpriteRegion {
188 rect,
189 src_rect,
190 tint,
191 ..
192 } => {
193 Self::mix_hash(&mut hash, 10);
194 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
195 Self::mix_hash(&mut hash, src_rect.get_x().to_bits() as u64);
196 Self::mix_hash(&mut hash, u32::from_le_bytes(tint.to_rgba_bytes()) as u64);
197 }
198 DrawCommand::PushClip(r) => {
199 Self::mix_hash(&mut hash, 11);
200 Self::mix_hash(&mut hash, r.get_x().to_bits() as u64);
201 Self::mix_hash(&mut hash, r.get_y().to_bits() as u64);
202 }
203 DrawCommand::PushRoundedClip { rect, radius } => {
204 Self::mix_hash(&mut hash, 14);
205 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
206 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
207 Self::mix_hash(&mut hash, radius.max_radius().to_bits() as u64);
208 }
209 DrawCommand::PopClip => {
210 Self::mix_hash(&mut hash, 12);
211 }
212 }
213 }
214 hash
215 }
216
217 #[must_use]
227 pub fn base_layer_hash(&self) -> u64 {
228 let split: usize = self.get_portal_split_index().min(self.get_commands().len());
229 let mut hash: u64 = FNV1A_OFFSET_BASIS;
230 Self::mix_hash(&mut hash, split as u64);
231 Self::mix_hash(&mut hash, self.get_viewport().get_width().to_bits() as u64);
232 Self::mix_hash(&mut hash, self.get_viewport().get_height().to_bits() as u64);
233 Self::mix_hash(&mut hash, self.get_scale_factor().to_bits() as u64);
234 for cmd in self.get_commands()[..split].iter() {
235 match cmd {
236 DrawCommand::Clear(c) => {
237 Self::mix_hash(&mut hash, 1);
238 Self::mix_hash(&mut hash, u32::from_le_bytes(c.to_rgba_bytes()) as u64);
239 }
240 DrawCommand::FillRoundedRect {
241 rect,
242 color,
243 radius,
244 border,
245 } => {
246 Self::mix_hash(&mut hash, 2);
247 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
248 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
249 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
250 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
251 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
252 Self::mix_hash(&mut hash, radius.get_top_left().to_bits() as u64);
253 Self::mix_hash(&mut hash, radius.get_top_right().to_bits() as u64);
254 Self::mix_hash(&mut hash, radius.get_bottom_right().to_bits() as u64);
255 Self::mix_hash(&mut hash, radius.get_bottom_left().to_bits() as u64);
256 Self::mix_hash(&mut hash, border.get_width().to_bits() as u64);
257 Self::mix_hash(
258 &mut hash,
259 u32::from_le_bytes(border.get_color().to_rgba_bytes()) as u64,
260 );
261 }
262 DrawCommand::LinearGradientRect {
263 rect,
264 gradient,
265 radius,
266 } => {
267 Self::mix_hash(&mut hash, 3);
268 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
269 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
270 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
271 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
272 Self::mix_hash(&mut hash, gradient.get_angle_deg().to_bits() as u64);
273 Self::mix_hash(
274 &mut hash,
275 u32::from_le_bytes(gradient.get_start_color().to_rgba_bytes()) as u64,
276 );
277 Self::mix_hash(
278 &mut hash,
279 u32::from_le_bytes(gradient.get_end_color().to_rgba_bytes()) as u64,
280 );
281 Self::mix_hash(&mut hash, radius.max_radius().to_bits() as u64);
282 }
283 DrawCommand::BoxShadow {
284 rect,
285 radius,
286 shadow,
287 } => {
288 Self::mix_hash(&mut hash, 4);
289 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
290 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
291 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
292 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
293 Self::mix_hash(&mut hash, radius.max_radius().to_bits() as u64);
294 Self::mix_hash(&mut hash, shadow.get_offset_x().to_bits() as u64);
295 Self::mix_hash(&mut hash, shadow.get_offset_y().to_bits() as u64);
296 Self::mix_hash(&mut hash, shadow.get_blur_radius().to_bits() as u64);
297 Self::mix_hash(&mut hash, shadow.get_spread_radius().to_bits() as u64);
298 Self::mix_hash(
299 &mut hash,
300 u32::from_le_bytes(shadow.get_color().to_rgba_bytes()) as u64,
301 );
302 }
303 DrawCommand::BackdropBlur {
304 rect,
305 radius,
306 blur_radius,
307 } => {
308 Self::mix_hash(&mut hash, 15);
309 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
310 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
311 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
312 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
313 Self::mix_hash(&mut hash, radius.max_radius().to_bits() as u64);
314 Self::mix_hash(&mut hash, blur_radius.to_bits() as u64);
315 }
316 DrawCommand::TextRun { rect, text, style } => {
317 Self::mix_hash(&mut hash, 5);
318 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
319 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
320 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
321 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
322 Self::mix_hash(&mut hash, style.get_font_size().to_bits() as u64);
323 Self::mix_hash(&mut hash, style.get_family() as u64);
324 Self::mix_hash(&mut hash, style.get_weight() as u64);
325 Self::mix_hash(&mut hash, u64::from(style.get_italic()));
326 Self::mix_hash(&mut hash, style.get_align() as u64);
327 Self::mix_hash(
328 &mut hash,
329 u32::from_le_bytes(style.get_color().to_rgba_bytes()) as u64,
330 );
331 Self::mix_hash(&mut hash, text.len() as u64);
332 for byte in text.as_bytes().iter().copied() {
333 Self::mix_hash(&mut hash, byte as u64);
334 }
335 }
336 DrawCommand::TextCaret {
337 text_rect,
338 content_rect,
339 text,
340 style,
341 caret_height,
342 color,
343 multiline,
344 } => {
345 Self::mix_hash(&mut hash, 13);
346 Self::mix_hash(&mut hash, text_rect.get_x().to_bits() as u64);
347 Self::mix_hash(&mut hash, text_rect.get_y().to_bits() as u64);
348 Self::mix_hash(&mut hash, content_rect.get_width().to_bits() as u64);
349 Self::mix_hash(&mut hash, caret_height.to_bits() as u64);
350 Self::mix_hash(&mut hash, style.get_font_size().to_bits() as u64);
351 Self::mix_hash(&mut hash, u64::from(*multiline));
352 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
353 Self::mix_hash(&mut hash, text.len() as u64);
354 for byte in text.as_bytes().iter().copied() {
355 Self::mix_hash(&mut hash, byte as u64);
356 }
357 }
358 DrawCommand::Image {
359 rect,
360 buffer,
361 radius,
362 } => {
363 Self::mix_hash(&mut hash, 6);
364 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
365 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
366 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
367 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
368 Self::mix_hash(&mut hash, buffer.get_width() as u64);
369 Self::mix_hash(&mut hash, buffer.get_height() as u64);
370 Self::mix_hash(&mut hash, buffer.get_pixels().as_ptr() as usize as u64);
371 Self::mix_hash(&mut hash, radius.max_radius().to_bits() as u64);
372 }
373 DrawCommand::Line {
374 from,
375 to,
376 color,
377 width,
378 } => {
379 Self::mix_hash(&mut hash, 7);
380 Self::mix_hash(&mut hash, from.get_x().to_bits() as u64);
381 Self::mix_hash(&mut hash, from.get_y().to_bits() as u64);
382 Self::mix_hash(&mut hash, to.get_x().to_bits() as u64);
383 Self::mix_hash(&mut hash, to.get_y().to_bits() as u64);
384 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
385 Self::mix_hash(&mut hash, width.to_bits() as u64);
386 }
387 DrawCommand::Circle {
388 center,
389 radius,
390 color,
391 } => {
392 Self::mix_hash(&mut hash, 8);
393 Self::mix_hash(&mut hash, center.get_x().to_bits() as u64);
394 Self::mix_hash(&mut hash, center.get_y().to_bits() as u64);
395 Self::mix_hash(&mut hash, radius.to_bits() as u64);
396 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
397 }
398 DrawCommand::Triangle { p0, p1, p2, color } => {
399 Self::mix_hash(&mut hash, 9);
400 Self::mix_hash(&mut hash, p0.get_x().to_bits() as u64);
401 Self::mix_hash(&mut hash, p0.get_y().to_bits() as u64);
402 Self::mix_hash(&mut hash, p1.get_x().to_bits() as u64);
403 Self::mix_hash(&mut hash, p1.get_y().to_bits() as u64);
404 Self::mix_hash(&mut hash, p2.get_x().to_bits() as u64);
405 Self::mix_hash(&mut hash, p2.get_y().to_bits() as u64);
406 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
407 }
408 DrawCommand::SpriteRegion {
409 rect,
410 buffer,
411 src_rect,
412 tint,
413 } => {
414 Self::mix_hash(&mut hash, 10);
415 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
416 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
417 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
418 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
419 Self::mix_hash(&mut hash, buffer.get_pixels().as_ptr() as usize as u64);
420 Self::mix_hash(&mut hash, src_rect.get_x().to_bits() as u64);
421 Self::mix_hash(&mut hash, src_rect.get_y().to_bits() as u64);
422 Self::mix_hash(&mut hash, u32::from_le_bytes(tint.to_rgba_bytes()) as u64);
423 }
424 DrawCommand::PushClip(r) => {
425 Self::mix_hash(&mut hash, 11);
426 Self::mix_hash(&mut hash, r.get_x().to_bits() as u64);
427 Self::mix_hash(&mut hash, r.get_y().to_bits() as u64);
428 Self::mix_hash(&mut hash, r.get_width().to_bits() as u64);
429 Self::mix_hash(&mut hash, r.get_height().to_bits() as u64);
430 }
431 DrawCommand::PushRoundedClip { rect, radius } => {
432 Self::mix_hash(&mut hash, 14);
433 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
434 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
435 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
436 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
437 Self::mix_hash(&mut hash, radius.get_top_left().to_bits() as u64);
438 Self::mix_hash(&mut hash, radius.get_top_right().to_bits() as u64);
439 Self::mix_hash(&mut hash, radius.get_bottom_right().to_bits() as u64);
440 Self::mix_hash(&mut hash, radius.get_bottom_left().to_bits() as u64);
441 }
442 DrawCommand::PopClip => {
443 Self::mix_hash(&mut hash, 12);
444 }
445 }
446 }
447 if hash == 0 { 1 } else { hash }
448 }
449}
450
451impl PaintFragmentCache {
453 pub fn clear(&mut self) {
459 let Self {
460 has_last_scene,
461 last_has_canvas,
462 slots,
463 ..
464 } = self;
465 *has_last_scene = false;
466 *last_has_canvas = false;
467 for slot in slots.iter_mut() {
468 *slot = None;
469 }
470 }
471
472 #[must_use]
482 pub fn stats(&self) -> (u64, u64, u64, u64) {
483 (
484 self.get_whole_list_hits(),
485 self.get_fragment_hits(),
486 self.get_fragment_misses(),
487 self.get_fragment_evictions(),
488 )
489 }
490
491 #[inline]
497 fn ensure_slots(&mut self) {
498 let Self { slots, .. } = self;
499 if slots.len() != PAINT_FRAGMENT_CACHE_SLOTS {
500 slots.resize_with(PAINT_FRAGMENT_CACHE_SLOTS, || None);
501 }
502 }
503}
504
505impl PaintPass {
507 #[inline]
517 #[must_use]
518 fn text_selection_sig(tree: &SceneTree) -> u64 {
519 let Some(sel) = tree.text_selection() else {
520 return 0;
521 };
522 if sel.is_empty() {
523 return 0;
524 }
525 let (start, end): (usize, usize) = sel.ordered_range();
526 let nid: NodeId = sel.get_node_id();
527 let mut h: u64 = ((nid.get_index() as u64) << 32) | (nid.get_generation() as u64);
528 h = (h ^ (start as u64)).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
529 h = (h ^ (end as u64)).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
530 if h == 0 { 1 } else { h }
531 }
532
533 #[inline(always)]
540 fn fx_fold(hash: &mut u64, word: u64) {
541 *hash = (*hash ^ word).wrapping_mul(PAINT_FRAGMENT_FX_MULT);
542 }
543
544 #[inline(always)]
551 fn fx_fold_rect(hash: &mut u64, rect: Rect) {
552 let lo: u64 = ((rect.get_x().to_bits() as u64) << 32) | (rect.get_y().to_bits() as u64);
553 let hi: u64 =
554 ((rect.get_width().to_bits() as u64) << 32) | (rect.get_height().to_bits() as u64);
555 Self::fx_fold(hash, lo);
556 Self::fx_fold(hash, hi);
557 }
558
559 #[inline]
566 fn fx_fold_paint(hash: &mut u64, paint: &PaintStyle) {
567 let bg_bits: u64 = paint
568 .background_color()
569 .map(|c: Color| u32::from_le_bytes(c.to_rgba_bytes()) as u64 | (1_u64 << 32))
570 .unwrap_or(0);
571 Self::fx_fold(hash, bg_bits);
572 if let Some(grad) = paint.background_gradient() {
573 let g0: u64 = u32::from_le_bytes(grad.get_start_color().to_rgba_bytes()) as u64;
574 let g1: u64 = u32::from_le_bytes(grad.get_end_color().to_rgba_bytes()) as u64;
575 Self::fx_fold(hash, (g0 << 32) | g1);
576 Self::fx_fold(hash, grad.get_angle_deg().to_bits() as u64 | (1_u64 << 33));
577 }
578 let border: Border = paint.get_border();
579 let b_word: u64 = ((border.get_width().to_bits() as u64) << 32)
580 | (u32::from_le_bytes(border.get_color().to_rgba_bytes()) as u64);
581 Self::fx_fold(hash, b_word);
582 let r: CornerRadius = paint.get_radius();
583 let r0: u64 =
584 ((r.get_top_left().to_bits() as u64) << 32) | (r.get_top_right().to_bits() as u64);
585 let r1: u64 = ((r.get_bottom_right().to_bits() as u64) << 32)
586 | (r.get_bottom_left().to_bits() as u64);
587 Self::fx_fold(hash, r0);
588 Self::fx_fold(hash, r1);
589 Self::fx_fold(hash, paint.get_opacity().to_bits() as u64);
590 if let Some(sh) = paint.box_shadow() {
591 let s0: u64 =
592 ((sh.get_offset_x().to_bits() as u64) << 32) | (sh.get_offset_y().to_bits() as u64);
593 let s1: u64 = ((sh.get_blur_radius().to_bits() as u64) << 32)
594 | (sh.get_spread_radius().to_bits() as u64);
595 Self::fx_fold(hash, s0);
596 Self::fx_fold(hash, s1);
597 Self::fx_fold(
598 hash,
599 u32::from_le_bytes(sh.get_color().to_rgba_bytes()) as u64 | (1_u64 << 34),
600 );
601 }
602 }
603
604 #[inline]
612 fn fx_fold_text(hash: &mut u64, text_style: &TextStyle, text: &str) {
613 let m0: u64 = ((text_style.get_font_size().to_bits() as u64) << 32)
614 | (text_style.get_line_height().to_bits() as u64);
615 let m1: u64 = ((text_style.get_letter_spacing().to_bits() as u64) << 32)
616 | (u32::from_le_bytes(text_style.get_color().to_rgba_bytes()) as u64);
617 let flags: u64 = (text_style.get_family() as u64)
618 | ((text_style.get_weight() as u64) << 8)
619 | ((text_style.get_align() as u64) << 16)
620 | ((text_style.get_overflow() as u64) << 24)
621 | (u64::from(text_style.get_italic()) << 32);
622 Self::fx_fold(hash, m0);
623 Self::fx_fold(hash, m1);
624 Self::fx_fold(hash, flags);
625 let bytes: &[u8] = text.as_bytes();
626 Self::fx_fold(hash, bytes.len() as u64);
627 let (chunks, rem): (&[[u8; 8]], &[u8]) = bytes.as_chunks::<8>();
628 for chunk in chunks.iter() {
629 Self::fx_fold(hash, u64::from_ne_bytes(*chunk));
630 }
631 if !rem.is_empty() {
632 let mut tail: u64 = 0;
633 for (idx, &b) in rem.iter().enumerate() {
634 tail |= (b as u64) << (idx * 8);
635 }
636 Self::fx_fold(hash, tail);
637 }
638 }
639
640 #[inline]
654 #[must_use]
655 fn fingerprint_shallow_container(
656 tree: &SceneTree,
657 node_id: NodeId,
658 node: &SceneNode,
659 active_hover_target: Option<NodeId>,
660 selection_node: Option<NodeId>,
661 ) -> Option<(u64, u64)> {
662 let children: &[NodeId] = node.get_children().as_slice();
663 if children.is_empty()
664 || children.len() > PAINT_FRAGMENT_MAX_CHILDREN
665 || tree.backdrop_blur_radius(node_id) > 0.0
666 {
667 return None;
668 }
669 let style: &Style = node.style_ref();
670 let transform: Transform2D = style.paint.get_transform();
671 let scroll: Point = node.layout_box.get_scroll_offset();
672 let mut geom_h: u64 = children.len() as u64;
673 Self::fx_fold_rect(&mut geom_h, node.layout_box.get_bounds());
674 Self::fx_fold(
675 &mut geom_h,
676 ((transform.get_translate_x().to_bits() as u64) << 32)
677 | (transform.get_translate_y().to_bits() as u64),
678 );
679 Self::fx_fold(
680 &mut geom_h,
681 ((transform.get_scale_x().to_bits() as u64) << 32)
682 | (transform.get_scale_y().to_bits() as u64),
683 );
684 Self::fx_fold(
685 &mut geom_h,
686 ((scroll.get_x().to_bits() as u64) << 32) | (scroll.get_y().to_bits() as u64),
687 );
688 let mut style_h: u64 =
689 ((node_id.get_index() as u64) << 32) | (node_id.get_generation() as u64);
690 Self::fx_fold_paint(&mut style_h, &style.paint);
691 for &cid in children.iter() {
692 if active_hover_target == Some(cid)
693 || selection_node == Some(cid)
694 || tree.backdrop_blur_radius(cid) > 0.0
695 {
696 return None;
697 }
698 let child: &SceneNode = tree.get_node(cid)?;
699 if !child.get_children().is_empty() {
700 return None;
701 }
702 let c_inter: InteractionState = child.get_interaction();
703 let c_active: bool = c_inter.get_hovered() || c_inter.get_pressed();
704 if c_active || c_inter.get_focused() {
705 return None;
706 }
707 let c_style: &Style = child.style_ref();
708 if c_style.layout.get_position() == Position::Fixed {
709 return None;
710 }
711 let c_tag: u64 = match child.get_kind() {
712 NodeKind::View | NodeKind::Fragment => 1,
713 NodeKind::Text(text) => {
714 Self::fx_fold_text(&mut style_h, &c_style.text, text.as_str());
715 Self::fx_fold_rect(&mut geom_h, child.layout_box.get_content_bounds());
716 2
717 }
718 NodeKind::Image(buf) => {
719 Self::fx_fold(
720 &mut style_h,
721 ((buf.get_width() as u64) << 32) | (buf.get_height() as u64),
722 );
723 Self::fx_fold(&mut style_h, buf.get_pixels().as_ptr() as usize as u64);
724 3
725 }
726 NodeKind::Portal | NodeKind::Canvas | NodeKind::Input(_) | NodeKind::ScrollView => {
727 return None;
728 }
729 };
730 let c_id_bits: u64 = ((cid.get_index() as u64) << 32) | (cid.get_generation() as u64);
731 Self::fx_fold(&mut geom_h, c_id_bits);
732 Self::fx_fold_rect(&mut geom_h, child.layout_box.get_bounds());
733 let ct: Transform2D = c_style.paint.get_transform();
734 Self::fx_fold(
735 &mut geom_h,
736 ((ct.get_translate_x().to_bits() as u64) << 32)
737 | (ct.get_translate_y().to_bits() as u64),
738 );
739 Self::fx_fold(
740 &mut geom_h,
741 ((ct.get_scale_x().to_bits() as u64) << 32) | (ct.get_scale_y().to_bits() as u64),
742 );
743 let c_lay_bits: u64 = (c_tag << 40)
744 | ((c_style.layout.get_display() as u64) << 32)
745 | ((c_style.layout.get_overflow() as u64) << 24)
746 | ((c_style.layout.get_z_index() as u32) as u64);
747 Self::fx_fold(&mut style_h, c_lay_bits);
748 Self::fx_fold_paint(&mut style_h, &c_style.paint);
749 }
750 Some((geom_h, style_h))
751 }
752
753 #[inline]
765 #[must_use]
766 fn translate_command(cmd: &DrawCommand, dx: f32, dy: f32) -> DrawCommand {
767 match cmd {
768 DrawCommand::Clear(c) => DrawCommand::Clear(*c),
769 DrawCommand::FillRoundedRect {
770 rect,
771 color,
772 radius,
773 border,
774 } => DrawCommand::FillRoundedRect {
775 rect: Rect::new(
776 rect.get_x() + dx,
777 rect.get_y() + dy,
778 rect.get_width(),
779 rect.get_height(),
780 ),
781 color: *color,
782 radius: *radius,
783 border: *border,
784 },
785 DrawCommand::LinearGradientRect {
786 rect,
787 gradient,
788 radius,
789 } => DrawCommand::LinearGradientRect {
790 rect: Rect::new(
791 rect.get_x() + dx,
792 rect.get_y() + dy,
793 rect.get_width(),
794 rect.get_height(),
795 ),
796 gradient: *gradient,
797 radius: *radius,
798 },
799 DrawCommand::BoxShadow {
800 rect,
801 radius,
802 shadow,
803 } => DrawCommand::BoxShadow {
804 rect: Rect::new(
805 rect.get_x() + dx,
806 rect.get_y() + dy,
807 rect.get_width(),
808 rect.get_height(),
809 ),
810 radius: *radius,
811 shadow: *shadow,
812 },
813 DrawCommand::BackdropBlur {
814 rect,
815 radius,
816 blur_radius,
817 } => DrawCommand::BackdropBlur {
818 rect: Rect::new(
819 rect.get_x() + dx,
820 rect.get_y() + dy,
821 rect.get_width(),
822 rect.get_height(),
823 ),
824 radius: *radius,
825 blur_radius: *blur_radius,
826 },
827 DrawCommand::TextRun { rect, text, style } => DrawCommand::TextRun {
828 rect: Rect::new(
829 rect.get_x() + dx,
830 rect.get_y() + dy,
831 rect.get_width(),
832 rect.get_height(),
833 ),
834 text: text.clone(),
835 style: *style,
836 },
837 DrawCommand::TextCaret {
838 text_rect,
839 content_rect,
840 text,
841 style,
842 caret_height,
843 color,
844 multiline,
845 } => DrawCommand::TextCaret {
846 text_rect: Rect::new(
847 text_rect.get_x() + dx,
848 text_rect.get_y() + dy,
849 text_rect.get_width(),
850 text_rect.get_height(),
851 ),
852 content_rect: Rect::new(
853 content_rect.get_x() + dx,
854 content_rect.get_y() + dy,
855 content_rect.get_width(),
856 content_rect.get_height(),
857 ),
858 text: text.clone(),
859 style: *style,
860 caret_height: *caret_height,
861 color: *color,
862 multiline: *multiline,
863 },
864 DrawCommand::Image {
865 rect,
866 buffer,
867 radius,
868 } => DrawCommand::Image {
869 rect: Rect::new(
870 rect.get_x() + dx,
871 rect.get_y() + dy,
872 rect.get_width(),
873 rect.get_height(),
874 ),
875 buffer: buffer.clone(),
876 radius: *radius,
877 },
878 DrawCommand::Line {
879 from,
880 to,
881 color,
882 width,
883 } => DrawCommand::Line {
884 from: Point::new(from.get_x() + dx, from.get_y() + dy),
885 to: Point::new(to.get_x() + dx, to.get_y() + dy),
886 color: *color,
887 width: *width,
888 },
889 DrawCommand::Circle {
890 center,
891 radius,
892 color,
893 } => DrawCommand::Circle {
894 center: Point::new(center.get_x() + dx, center.get_y() + dy),
895 radius: *radius,
896 color: *color,
897 },
898 DrawCommand::Triangle { p0, p1, p2, color } => DrawCommand::Triangle {
899 p0: Point::new(p0.get_x() + dx, p0.get_y() + dy),
900 p1: Point::new(p1.get_x() + dx, p1.get_y() + dy),
901 p2: Point::new(p2.get_x() + dx, p2.get_y() + dy),
902 color: *color,
903 },
904 DrawCommand::SpriteRegion {
905 rect,
906 buffer,
907 src_rect,
908 tint,
909 } => DrawCommand::SpriteRegion {
910 rect: Rect::new(
911 rect.get_x() + dx,
912 rect.get_y() + dy,
913 rect.get_width(),
914 rect.get_height(),
915 ),
916 buffer: buffer.clone(),
917 src_rect: *src_rect,
918 tint: *tint,
919 },
920 DrawCommand::PushClip(rect) => DrawCommand::PushClip(Rect::new(
921 rect.get_x() + dx,
922 rect.get_y() + dy,
923 rect.get_width(),
924 rect.get_height(),
925 )),
926 DrawCommand::PushRoundedClip { rect, radius } => DrawCommand::PushRoundedClip {
927 rect: Rect::new(
928 rect.get_x() + dx,
929 rect.get_y() + dy,
930 rect.get_width(),
931 rect.get_height(),
932 ),
933 radius: *radius,
934 },
935 DrawCommand::PopClip => DrawCommand::PopClip,
936 }
937 }
938
939 pub fn build(
949 tree: &SceneTree,
950 viewport: Size,
951 scale_factor: f32,
952 clear_color: Color,
953 display_list: &mut DisplayList,
954 ) {
955 let mut saw_canvas: bool = false;
956 Self::build_inner(
957 tree,
958 viewport,
959 scale_factor,
960 clear_color,
961 display_list,
962 None,
963 &mut saw_canvas,
964 );
965 }
966
967 #[inline(always)]
977 #[must_use]
978 fn pack_opt_node_id(node: Option<NodeId>) -> u64 {
979 match node {
980 Some(id) => ((id.get_index() as u64) << 32) | (id.get_generation() as u64),
981 None => u64::MAX,
982 }
983 }
984
985 pub fn build_retained(
997 tree: &SceneTree,
998 viewport: Size,
999 scale_factor: f32,
1000 clear_color: Color,
1001 layout_changed: bool,
1002 display_list: &mut DisplayList,
1003 cache: &mut PaintFragmentCache,
1004 ) {
1005 let clamped_scale: f32 = scale_factor.max(0.5);
1006 let focus: FocusManager = tree.get_focus();
1007 let sig: PaintSceneSignature = PaintSceneSignature {
1008 tree_ptr: (tree as *const SceneTree) as usize,
1009 tree_revision: tree.get_revision(),
1010 viewport_w_bits: viewport.get_width().to_bits(),
1011 viewport_h_bits: viewport.get_height().to_bits(),
1012 scale_bits: clamped_scale.to_bits(),
1013 clear_rgba: u32::from_le_bytes(clear_color.to_rgba_bytes()),
1014 focused_packed: Self::pack_opt_node_id(focus.focused_node()),
1015 hovered_packed: Self::pack_opt_node_id(focus.hovered_node()),
1016 pressed_packed: Self::pack_opt_node_id(focus.pressed_node()),
1017 caret_visible: tree.get_caret_visible(),
1018 text_sel_sig: Self::text_selection_sig(tree),
1019 };
1020 if !layout_changed
1021 && !cache.last_has_canvas
1022 && !display_list.is_empty()
1023 && cache.has_last_scene
1024 && cache.last_scene_sig == sig
1025 {
1026 cache.whole_list_hits = cache.whole_list_hits.wrapping_add(1);
1027 return;
1028 }
1029 cache.ensure_slots();
1030 let mut saw_canvas: bool = false;
1031 Self::build_inner(
1032 tree,
1033 viewport,
1034 clamped_scale,
1035 clear_color,
1036 display_list,
1037 Some(cache),
1038 &mut saw_canvas,
1039 );
1040 cache.last_has_canvas = saw_canvas;
1041 cache.has_last_scene = true;
1042 cache.last_scene_sig = sig;
1043 }
1044
1045 fn collect_portal_ancestors(tree: &SceneTree, out_ancestors: &mut Vec<NodeId>) {
1052 out_ancestors.clear();
1053 let portal_nodes: &[NodeId] = tree.portal_nodes();
1054 if portal_nodes.is_empty() {
1055 return;
1056 }
1057 for &pid in portal_nodes.iter() {
1058 let mut cur: Option<NodeId> = tree.get_node(pid).and_then(SceneNode::parent_id);
1059 while let Some(anc) = cur {
1060 if out_ancestors.contains(&anc) {
1061 break;
1062 }
1063 out_ancestors.push(anc);
1064 cur = tree.get_node(anc).and_then(SceneNode::parent_id);
1065 }
1066 }
1067 if out_ancestors.len() > 16 {
1068 out_ancestors.sort_unstable_by_key(|id: &NodeId| {
1069 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
1070 });
1071 }
1072 }
1073
1074 #[inline]
1085 #[must_use]
1086 fn is_portal_ancestor(ancestors: &[NodeId], node_id: NodeId) -> bool {
1087 if ancestors.is_empty() {
1088 return false;
1089 }
1090 if ancestors.len() <= 16 {
1091 return ancestors.contains(&node_id);
1092 }
1093 let target: u64 = ((node_id.get_index() as u64) << 32) | (node_id.get_generation() as u64);
1094 ancestors
1095 .binary_search_by_key(&target, |id: &NodeId| {
1096 ((id.get_index() as u64) << 32) | (id.get_generation() as u64)
1097 })
1098 .is_ok()
1099 }
1100
1101 #[inline]
1117 #[must_use]
1118 fn conservative_self_vis_rect(
1119 node: &SceneNode,
1120 bounds: Rect,
1121 paint: &PaintStyle,
1122 focused: bool,
1123 total_off_phys: (f32, f32),
1124 scale: f32,
1125 inv_scale: f32,
1126 ) -> Rect {
1127 let mut min_x: f32 = bounds.get_x() - 1.0;
1128 let mut min_y: f32 = bounds.get_y() - 1.0;
1129 let mut max_x: f32 = bounds.right() + 1.0;
1130 let mut max_y: f32 = bounds.bottom() + 1.0;
1131 if let Some(shadow) = paint.box_shadow()
1132 && shadow.get_color().get_a() > 0
1133 {
1134 let spread: f32 = shadow.get_spread_radius().max(0.0);
1135 let blur_phys: f32 = (shadow.get_blur_radius().max(0.5) * scale).min(96.0);
1136 let sigma_phys: f32 = (blur_phys * 0.5).max(0.75);
1137 let blur_pad: f32 = (sigma_phys * 3.0).ceil() * inv_scale + 2.0 * inv_scale;
1138 let pad: f32 = spread + blur_pad;
1139 let sx0: f32 = bounds.get_x() + shadow.get_offset_x() - pad;
1140 let sy0: f32 = bounds.get_y() + shadow.get_offset_y() - pad;
1141 let sx1: f32 = bounds.right() + shadow.get_offset_x() + pad;
1142 let sy1: f32 = bounds.bottom() + shadow.get_offset_y() + pad;
1143 min_x = min_x.min(sx0);
1144 min_y = min_y.min(sy0);
1145 max_x = max_x.max(sx1);
1146 max_y = max_y.max(sy1);
1147 }
1148 if focused && let Some(ring) = paint.focus_ring_border() {
1149 let rw: f32 = ring.get_width().max(0.0) + 1.0;
1150 min_x = min_x.min(bounds.get_x() - rw);
1151 min_y = min_y.min(bounds.get_y() - rw);
1152 max_x = max_x.max(bounds.right() + rw);
1153 max_y = max_y.max(bounds.bottom() + rw);
1154 }
1155 if matches!(node.get_kind(), NodeKind::Text(_)) {
1156 let content_b: Rect = node.layout_box.get_content_bounds();
1157 let cx0: f32 = ((content_b.get_x() * scale).round() + total_off_phys.0) * inv_scale;
1158 let cy0: f32 = ((content_b.get_y() * scale).round() + total_off_phys.1) * inv_scale;
1159 let pad: f32 = node.style_ref().text.get_font_size() * 0.5 + 2.0;
1160 min_x = min_x.min(cx0 - pad);
1161 min_y = min_y.min(cy0 - pad);
1162 max_x = max_x.max(cx0 + content_b.get_width() + pad);
1163 max_y = max_y.max(cy0 + content_b.get_height() + pad);
1164 }
1165 Rect::new(
1166 min_x,
1167 min_y,
1168 (max_x - min_x).max(0.0),
1169 (max_y - min_y).max(0.0),
1170 )
1171 }
1172
1173 fn visit_culled_portal_child(
1182 tree: &SceneTree,
1183 child_id: NodeId,
1184 parent_opacity: f32,
1185 scratch: &mut PaintScratch<'_>,
1186 ) {
1187 let Some(child) = tree.get_node(child_id) else {
1188 return;
1189 };
1190 let style: &Style = child.style_ref();
1191 let layout: &LayoutStyle = &style.layout;
1192 if layout.get_display() == Display::None {
1193 return;
1194 }
1195 let is_portal: bool = matches!(child.get_kind(), NodeKind::Portal)
1196 || layout.get_position() == Position::Fixed;
1197 if is_portal {
1198 if let Some(portal_queue) = scratch.portals.as_deref_mut() {
1199 portal_queue.push((layout.get_z_index(), portal_queue.len(), child_id));
1200 }
1201 return;
1202 }
1203 if !Self::is_portal_ancestor(scratch.portal_ancestors, child_id) {
1204 return;
1205 }
1206 let hovered: bool = child.get_interaction().get_hovered();
1207 let paint_opacity: f32 = if hovered {
1208 child
1209 .hover_paint()
1210 .map(|hp: PaintStyle| hp.get_opacity())
1211 .unwrap_or_else(|| style.paint.get_opacity())
1212 } else {
1213 style.paint.get_opacity()
1214 };
1215 let next_opacity: f32 = (parent_opacity * paint_opacity).clamp(0.0, 1.0);
1216 if next_opacity <= 0.0 {
1217 return;
1218 }
1219 Self::collect_portals_in_culled_subtree(tree, child_id, next_opacity, scratch);
1220 }
1221
1222 fn collect_portals_in_culled_subtree(
1231 tree: &SceneTree,
1232 node_id: NodeId,
1233 parent_opacity: f32,
1234 scratch: &mut PaintScratch<'_>,
1235 ) {
1236 let Some(node) = tree.get_node(node_id) else {
1237 return;
1238 };
1239 let raw_children: &[NodeId] = node.get_children().as_slice();
1240 if raw_children.is_empty() {
1241 return;
1242 }
1243 let needs_z_sort: bool = raw_children.len() > 1
1244 && raw_children.iter().copied().any(|cid: NodeId| {
1245 tree.get_node(cid)
1246 .map(|c: &SceneNode| c.style_ref().layout.get_z_index() != 0)
1247 .unwrap_or(false)
1248 });
1249 if !needs_z_sort {
1250 for child_id in raw_children.iter().copied() {
1251 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
1252 }
1253 } else {
1254 let sort_start: usize = scratch.z_sort.len();
1255 for (idx, cid) in raw_children.iter().copied().enumerate() {
1256 if let Some(c) = tree.get_node(cid) {
1257 scratch
1258 .z_sort
1259 .push((c.style_ref().layout.get_z_index(), idx, cid));
1260 }
1261 }
1262 let sort_end: usize = scratch.z_sort.len();
1263 scratch.z_sort[sort_start..sort_end].sort_by(
1264 |a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
1265 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
1266 },
1267 );
1268 for slot_idx in sort_start..sort_end {
1269 let child_id: NodeId = scratch.z_sort[slot_idx].2;
1270 Self::visit_culled_portal_child(tree, child_id, parent_opacity, scratch);
1271 }
1272 scratch.z_sort.truncate(sort_start);
1273 }
1274 }
1275
1276 fn build_inner(
1288 tree: &SceneTree,
1289 viewport: Size,
1290 scale_factor: f32,
1291 clear_color: Color,
1292 display_list: &mut DisplayList,
1293 mut fragments: Option<&mut PaintFragmentCache>,
1294 saw_canvas: &mut bool,
1295 ) {
1296 display_list.reset(viewport, scale_factor);
1297 display_list.push(DrawCommand::Clear(clear_color));
1298 let viewport_rect: Rect = Rect::new(0.0, 0.0, viewport.get_width(), viewport.get_height());
1299 let active_hover_target: Option<NodeId> = Self::resolve_active_hover_target(tree);
1300 let mut portals: Vec<(i32, usize, NodeId)> = take(&mut display_list.portals_scratch);
1301 let mut z_sort: Vec<(i32, usize, NodeId)> = take(&mut display_list.z_sort_scratch);
1302 let mut portal_ancestors: Vec<NodeId> = take(&mut display_list.portal_ancestors_scratch);
1303 portals.clear();
1304 z_sort.clear();
1305 Self::collect_portal_ancestors(tree, &mut portal_ancestors);
1306 {
1307 let mut scratch: PaintScratch<'_> = PaintScratch {
1308 portals: Some(&mut portals),
1309 z_sort: &mut z_sort,
1310 portal_ancestors: portal_ancestors.as_slice(),
1311 fragments: fragments.as_deref_mut(),
1312 saw_canvas,
1313 };
1314 Self::emit_node(
1315 tree,
1316 tree.get_root(),
1317 (Point::new(0.0, 0.0), 1.0),
1318 viewport_rect,
1319 active_hover_target,
1320 &mut scratch,
1321 display_list,
1322 );
1323 }
1324 display_list.set_portal_split_index(display_list.len());
1325 if portals.len() > 1 {
1326 portals.sort_by(|a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
1327 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
1328 });
1329 }
1330 {
1331 let mut portal_scratch: PaintScratch<'_> = PaintScratch {
1332 portals: None,
1333 z_sort: &mut z_sort,
1334 portal_ancestors: &[],
1335 fragments,
1336 saw_canvas,
1337 };
1338 for &(_, _, portal_id) in portals.iter() {
1339 Self::emit_node(
1340 tree,
1341 portal_id,
1342 (Point::new(0.0, 0.0), 1.0),
1343 viewport_rect,
1344 active_hover_target,
1345 &mut portal_scratch,
1346 display_list,
1347 );
1348 }
1349 }
1350 portals.clear();
1351 z_sort.clear();
1352 portal_ancestors.clear();
1353 display_list.portals_scratch = portals;
1354 display_list.z_sort_scratch = z_sort;
1355 display_list.portal_ancestors_scratch = portal_ancestors;
1356 }
1357
1358 #[must_use]
1368 fn resolve_active_hover_target(tree: &SceneTree) -> Option<NodeId> {
1369 let mut cursor: Option<NodeId> = tree.get_focus().hovered_node();
1370 while let Some(id) = cursor {
1371 let Some(node) = tree.get_node(id) else {
1372 break;
1373 };
1374 let style: &Style = node.style_ref();
1375 let paint: &PaintStyle = &style.paint;
1376 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
1377 || style.layout.get_position() == Position::Fixed;
1378 let is_transparent_canvas: bool =
1379 matches!(node.get_kind(), NodeKind::Canvas) && paint.background_color().is_none();
1380 if !is_scrim && !is_transparent_canvas {
1381 let is_interactive: bool = paint.get_cursor() == CursorIcon::Pointer
1382 || node
1383 .get_handlers()
1384 .handler_for(&UiEventKind::Click(Point::default()))
1385 .is_some();
1386 if is_interactive {
1387 return Some(id);
1388 }
1389 }
1390 cursor = node.parent_id();
1391 }
1392 None
1393 }
1394
1395 fn interactive_tint(base: Option<Color>, hovered: bool, pressed: bool) -> Option<Color> {
1407 if !hovered && !pressed {
1408 return base;
1409 }
1410 let delta: i32 = if pressed { 24 } else { 14 };
1411 match base {
1412 Some(c) if c.get_a() > 0 => {
1413 let luma: u32 =
1414 (c.get_r() as u32) * 299 + (c.get_g() as u32) * 587 + (c.get_b() as u32) * 114;
1415 if luma < 120_000 {
1416 let lift: i32 = if pressed { 30 } else { 20 };
1417 Some(Color::new(
1418 (c.get_r() as i32 + lift).clamp(0, 255) as u8,
1419 (c.get_g() as i32 + lift).clamp(0, 255) as u8,
1420 (c.get_b() as i32 + lift).clamp(0, 255) as u8,
1421 c.get_a(),
1422 ))
1423 } else {
1424 Some(Color::new(
1425 (c.get_r() as i32 - delta).clamp(0, 255) as u8,
1426 (c.get_g() as i32 - delta).clamp(0, 255) as u8,
1427 (c.get_b() as i32 - delta).clamp(0, 255) as u8,
1428 c.get_a(),
1429 ))
1430 }
1431 }
1432 _ => Some(Color::rgba(113, 113, 122, if pressed { 42 } else { 26 })),
1433 }
1434 }
1435
1436 fn emit_node(
1448 tree: &SceneTree,
1449 node_id: NodeId,
1450 offset_and_opacity: (Point, f32),
1451 cull_rect: Rect,
1452 active_hover_target: Option<NodeId>,
1453 scratch: &mut PaintScratch<'_>,
1454 display_list: &mut DisplayList,
1455 ) {
1456 let (offset_phys, parent_opacity): (Point, f32) = offset_and_opacity;
1457 let Some(node) = tree.get_node(node_id) else {
1458 return;
1459 };
1460 let style: &Style = node.style_ref();
1461 let layout: &LayoutStyle = &style.layout;
1462 if layout.get_display() == Display::None {
1463 return;
1464 }
1465 if let Some(portal_queue) = scratch.portals.as_deref_mut()
1466 && (matches!(node.get_kind(), NodeKind::Portal)
1467 || layout.get_position() == Position::Fixed)
1468 {
1469 portal_queue.push((layout.get_z_index(), portal_queue.len(), node_id));
1470 return;
1471 }
1472 let interaction: InteractionState = node.get_interaction();
1473 let hovered: bool = interaction.get_hovered();
1474 let pressed: bool = interaction.get_pressed();
1475 if !hovered && (parent_opacity * style.paint.get_opacity()) <= 0.0 {
1476 return;
1477 }
1478 let mut paint_owned: Option<PaintStyle> = if hovered { node.hover_paint() } else { None };
1479 let prelim_paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
1480 let opacity: f32 = (parent_opacity * prelim_paint.get_opacity()).clamp(0.0, 1.0);
1481 if opacity <= 0.0 {
1482 return;
1483 }
1484 let scale: f32 = display_list.get_scale_factor().max(0.5);
1485 let inv_scale: f32 = 1.0 / scale;
1486 let raw_bounds: Rect = node.layout_box.get_bounds();
1487 let transform: Transform2D = prelim_paint.get_transform();
1488 let tx_phys: f32 = (transform.get_translate_x() * scale).round();
1489 let ty_phys: f32 = (transform.get_translate_y() * scale).round();
1490 let total_off_x: f32 = offset_phys.get_x() + tx_phys;
1491 let total_off_y: f32 = offset_phys.get_y() + ty_phys;
1492 let raw_x0: f32 = (raw_bounds.get_x() * scale).round();
1493 let raw_y0: f32 = (raw_bounds.get_y() * scale).round();
1494 let raw_x1: f32 = (raw_bounds.right() * scale).round();
1495 let raw_y1: f32 = (raw_bounds.bottom() * scale).round();
1496 let static_w_phys: f32 = (raw_x1 - raw_x0).max(0.0);
1497 let static_h_phys: f32 = (raw_y1 - raw_y0).max(0.0);
1498 let scaled_w_phys: f32 = (static_w_phys * transform.get_scale_x()).round().max(0.0);
1499 let scaled_h_phys: f32 = (static_h_phys * transform.get_scale_y()).round().max(0.0);
1500 let scale_dx_phys: f32 = ((static_w_phys - scaled_w_phys) * 0.5).round();
1501 let scale_dy_phys: f32 = ((static_h_phys - scaled_h_phys) * 0.5).round();
1502 let bounds: Rect = Rect::new(
1503 (raw_x0 + total_off_x + scale_dx_phys) * inv_scale,
1504 (raw_y0 + total_off_y + scale_dy_phys) * inv_scale,
1505 scaled_w_phys * inv_scale,
1506 scaled_h_phys * inv_scale,
1507 );
1508 let clipped: bool = matches!(layout.get_overflow(), Overflow::Hidden | Overflow::Scroll);
1509 let clipped_cull: Option<Rect> = if clipped {
1510 cull_rect.intersect(bounds)
1511 } else {
1512 Some(cull_rect)
1513 };
1514 let focused: bool = interaction.get_focused();
1515 let self_vis_rect: Rect = Self::conservative_self_vis_rect(
1516 node,
1517 bounds,
1518 prelim_paint,
1519 focused,
1520 (total_off_x, total_off_y),
1521 scale,
1522 inv_scale,
1523 );
1524 let self_visible: bool = bounds.get_width() > 0.0
1525 && bounds.get_height() > 0.0
1526 && self_vis_rect.intersect(cull_rect).is_some();
1527 if clipped && clipped_cull.is_none() {
1528 if self_visible {
1529 if let Some(mut shadow) = prelim_paint.box_shadow() {
1530 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
1531 display_list.push(DrawCommand::BoxShadow {
1532 rect: bounds,
1533 radius: prelim_paint.get_radius(),
1534 shadow,
1535 });
1536 }
1537 if focused && let Some(mut ring) = prelim_paint.focus_ring_border() {
1538 ring.set_color(ring.get_color().multiply_alpha(opacity));
1539 let ring_w: f32 = ring.get_width();
1540 let ring_rect: Rect = Rect::new(
1541 bounds.get_x() - ring_w,
1542 bounds.get_y() - ring_w,
1543 bounds.get_width() + ring_w * 2.0,
1544 bounds.get_height() + ring_w * 2.0,
1545 );
1546 display_list.push(DrawCommand::FillRoundedRect {
1547 rect: ring_rect,
1548 color: COLOR_TRANSPARENT,
1549 radius: CornerRadius::all(prelim_paint.get_radius().max_radius() + ring_w),
1550 border: ring,
1551 });
1552 }
1553 }
1554 if scratch.portals.is_some()
1555 && Self::is_portal_ancestor(scratch.portal_ancestors, node_id)
1556 {
1557 Self::collect_portals_in_culled_subtree(tree, node_id, opacity, scratch);
1558 }
1559 return;
1560 }
1561 let raw_children: &[NodeId] = node.get_children().as_slice();
1562 if !self_visible && raw_children.is_empty() {
1563 return;
1564 }
1565 let next_cull: Rect = clipped_cull.unwrap_or(cull_rect);
1566 let hover_visual: bool = hovered && active_hover_target == Some(node_id);
1567 if self_visible
1568 && (hover_visual || pressed)
1569 && paint_owned.is_none()
1570 && (hovered || node.hover_paint().is_none())
1571 {
1572 let base_paint: &PaintStyle = &style.paint;
1573 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
1574 || layout.get_position() == Position::Fixed;
1575 let is_transparent_canvas: bool = matches!(node.get_kind(), NodeKind::Canvas)
1576 && base_paint.background_color().is_none();
1577 if !is_scrim && !is_transparent_canvas {
1578 let is_interactive: bool = base_paint.get_cursor() == CursorIcon::Pointer
1579 || node
1580 .get_handlers()
1581 .handler_for(&UiEventKind::Click(Point::default()))
1582 .is_some();
1583 if is_interactive {
1584 let mut tinted: PaintStyle = *base_paint;
1585 tinted.set_background(Self::interactive_tint(
1586 tinted.background_color(),
1587 hover_visual,
1588 pressed,
1589 ));
1590 paint_owned = Some(tinted);
1591 }
1592 }
1593 }
1594 let paint: &PaintStyle = paint_owned.as_ref().unwrap_or(&style.paint);
1595 let mut frag_capture: Option<(usize, usize, u64, u64)> = None;
1596 if self_visible
1597 && scratch.fragments.is_some()
1598 && scratch.portals.is_some()
1599 && matches!(node.get_kind(), NodeKind::View)
1600 && layout.get_overflow() == Overflow::Hidden
1601 && !hovered
1602 && !pressed
1603 && !interaction.get_focused()
1604 && active_hover_target != Some(node_id)
1605 {
1606 let vis_x_in: bool = self_vis_rect.get_x() >= cull_rect.get_x()
1607 && self_vis_rect.right() <= cull_rect.right();
1608 let vis_y_in: bool = self_vis_rect.get_y() >= cull_rect.get_y()
1609 && self_vis_rect.bottom() <= cull_rect.bottom();
1610 if vis_x_in && vis_y_in {
1611 let sel_node: Option<NodeId> = tree
1612 .text_selection()
1613 .filter(|s: &SelectionState| !s.is_empty())
1614 .map(|s: SelectionState| s.get_node_id());
1615 if let Some((geom_hash, style_text_hash)) = Self::fingerprint_shallow_container(
1616 tree,
1617 node_id,
1618 node,
1619 active_hover_target,
1620 sel_node,
1621 ) {
1622 let slot_idx: usize =
1623 (node_id.get_index() as usize) & (PAINT_FRAGMENT_CACHE_SLOTS - 1);
1624 let scale_bits: u32 = scale.to_bits();
1625 let opacity_bits: u32 = parent_opacity.to_bits();
1626 let dyadic_12: bool = scale == 1.0 || scale == 2.0;
1627 let dyadic_scale: bool = dyadic_12 || scale == 4.0;
1628 if let Some(cache) = scratch.fragments.as_deref_mut()
1629 && let Some(cached) = cache.slots.get(slot_idx).and_then(Option::as_ref)
1630 && cached.node_id == node_id
1631 && cached.scale_bits == scale_bits
1632 && cached.opacity_bits == opacity_bits
1633 && cached.geom_hash == geom_hash
1634 && cached.style_text_hash == style_text_hash
1635 {
1636 if cached.offset_phys == offset_phys {
1637 display_list
1638 .get_mut_commands()
1639 .extend_from_slice(cached.commands.as_slice());
1640 cache.fragment_hits = cache.fragment_hits.wrapping_add(1);
1641 return;
1642 }
1643 if dyadic_scale {
1644 let dx: f32 =
1645 (offset_phys.get_x() - cached.offset_phys.get_x()) * inv_scale;
1646 let dy: f32 =
1647 (offset_phys.get_y() - cached.offset_phys.get_y()) * inv_scale;
1648 let dst: &mut Vec<DrawCommand> = display_list.get_mut_commands();
1649 dst.reserve(cached.commands.len());
1650 for cmd in cached.commands.iter() {
1651 dst.push(Self::translate_command(cmd, dx, dy));
1652 }
1653 cache.fragment_hits = cache.fragment_hits.wrapping_add(1);
1654 return;
1655 }
1656 }
1657 frag_capture = Some((display_list.len(), slot_idx, geom_hash, style_text_hash));
1658 }
1659 }
1660 }
1661 if self_visible {
1662 let blur_r: f32 = tree.backdrop_blur_radius(node_id);
1663 if blur_r > 0.0 {
1664 display_list.push(DrawCommand::BackdropBlur {
1665 rect: bounds,
1666 radius: paint.get_radius(),
1667 blur_radius: blur_r,
1668 });
1669 }
1670 if let Some(mut shadow) = paint.box_shadow() {
1671 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
1672 display_list.push(DrawCommand::BoxShadow {
1673 rect: bounds,
1674 radius: paint.get_radius(),
1675 shadow,
1676 });
1677 }
1678 if let Some(mut gradient) = paint.background_gradient() {
1679 gradient.set_start_color(gradient.get_start_color().multiply_alpha(opacity));
1680 gradient.set_end_color(gradient.get_end_color().multiply_alpha(opacity));
1681 display_list.push(DrawCommand::LinearGradientRect {
1682 rect: bounds,
1683 gradient,
1684 radius: paint.get_radius(),
1685 });
1686 } else if let Some(bg) = paint.background_color() {
1687 let mut border: Border = paint.get_border();
1688 border.set_color(border.get_color().multiply_alpha(opacity));
1689 display_list.push(DrawCommand::FillRoundedRect {
1690 rect: bounds,
1691 color: bg.multiply_alpha(opacity),
1692 radius: paint.get_radius(),
1693 border,
1694 });
1695 } else if paint.get_border().get_width() > 0.0 {
1696 let mut border: Border = paint.get_border();
1697 border.set_color(border.get_color().multiply_alpha(opacity));
1698 display_list.push(DrawCommand::FillRoundedRect {
1699 rect: bounds,
1700 color: COLOR_TRANSPARENT,
1701 radius: paint.get_radius(),
1702 border,
1703 });
1704 }
1705 if interaction.get_focused()
1706 && let Some(mut ring) = paint.focus_ring_border()
1707 {
1708 ring.set_color(ring.get_color().multiply_alpha(opacity));
1709 let ring_w: f32 = ring.get_width();
1710 let ring_rect: Rect = Rect::new(
1711 bounds.get_x() - ring_w,
1712 bounds.get_y() - ring_w,
1713 bounds.get_width() + ring_w * 2.0,
1714 bounds.get_height() + ring_w * 2.0,
1715 );
1716 display_list.push(DrawCommand::FillRoundedRect {
1717 rect: ring_rect,
1718 color: COLOR_TRANSPARENT,
1719 radius: CornerRadius::all(paint.get_radius().max_radius() + ring_w),
1720 border: ring,
1721 });
1722 }
1723 match node.get_kind() {
1724 NodeKind::Text(text) => {
1725 let mut text_style: TextStyle = style.text;
1726 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
1727 let content_b: Rect = node.layout_box.get_content_bounds();
1728 let cx0: f32 = (content_b.get_x() * scale).round();
1729 let cy0: f32 = (content_b.get_y() * scale).round();
1730 let shifted_content: Rect = Rect::new(
1731 (cx0 + total_off_x) * inv_scale,
1732 (cy0 + total_off_y) * inv_scale,
1733 content_b.get_width(),
1734 content_b.get_height(),
1735 );
1736 if let Some(sel) = tree.text_selection()
1737 && sel.get_node_id() == node_id
1738 && !sel.is_empty()
1739 {
1740 let sel_color: Color = tree
1741 .effective_selection_color(node_id)
1742 .multiply_alpha(opacity);
1743 let sel_rects: Vec<Rect> = compute_text_selection_rects(
1744 text.as_str(),
1745 text_style,
1746 shifted_content,
1747 sel.ordered_range(),
1748 );
1749 for sel_rect in sel_rects.into_iter() {
1750 display_list.push(DrawCommand::FillRoundedRect {
1751 rect: sel_rect,
1752 color: sel_color,
1753 radius: CornerRadius::all(2.0),
1754 border: Border::default(),
1755 });
1756 }
1757 }
1758 display_list.push(DrawCommand::TextRun {
1759 rect: shifted_content,
1760 text: text.clone(),
1761 style: text_style,
1762 });
1763 }
1764 NodeKind::Input(input_state) => {
1765 let mut text_style: TextStyle = style.text;
1766 let is_empty_value: bool = input_state.get_value().is_empty();
1767 let is_focused: bool = interaction.get_focused();
1768 let rendered_text: String = if is_empty_value {
1769 text_style.set_color(text_style.get_color().multiply_alpha(opacity * 0.55));
1770 input_state.get_placeholder().clone()
1771 } else if input_state.get_password() {
1772 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
1773 "*".repeat(input_state.get_value().chars().count())
1774 } else {
1775 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
1776 input_state.get_value().clone()
1777 };
1778 let content_b: Rect = node.layout_box.get_content_bounds();
1779 let cx0: f32 = (content_b.get_x() * scale).round();
1780 let cy0: f32 = (content_b.get_y() * scale).round();
1781 let shifted_content: Rect = Rect::new(
1782 (cx0 + total_off_x) * inv_scale,
1783 (cy0 + total_off_y) * inv_scale,
1784 content_b.get_width(),
1785 content_b.get_height(),
1786 );
1787 let line_h: f32 =
1788 (text_style.get_font_size() * text_style.get_line_height()).max(1.0);
1789 let text_rect: Rect = if input_state.get_multiline() {
1790 shifted_content
1791 } else {
1792 text_style.set_overflow(TextOverflow::Clip);
1793 let v_off_phys: f32 =
1794 (((shifted_content.get_height() - line_h) * 0.5).max(0.0) * scale)
1795 .round();
1796 let text_y: f32 = (cy0 + total_off_y + v_off_phys) * inv_scale;
1797 Rect::new(
1798 shifted_content.get_x(),
1799 text_y,
1800 shifted_content.get_width(),
1801 line_h,
1802 )
1803 };
1804 let run_rect: Rect =
1805 if is_empty_value && is_focused && !rendered_text.is_empty() {
1806 let clearance_phys: f32 =
1807 (PLACEHOLDER_FOCUSED_CARET_CLEARANCE_PX * scale).round();
1808 let clearance_logical: f32 = clearance_phys * inv_scale;
1809 Rect::new(
1810 text_rect.get_x() + clearance_logical,
1811 text_rect.get_y(),
1812 (text_rect.get_width() - clearance_logical).max(0.0),
1813 text_rect.get_height(),
1814 )
1815 } else {
1816 text_rect
1817 };
1818 let show_caret: bool = is_focused && tree.get_caret_visible();
1819 let caret_text: String = if show_caret && !is_empty_value {
1820 rendered_text.clone()
1821 } else {
1822 String::new()
1823 };
1824 display_list.push(DrawCommand::PushClip(shifted_content));
1825 display_list.push(DrawCommand::TextRun {
1826 rect: run_rect,
1827 text: rendered_text,
1828 style: text_style,
1829 });
1830 if show_caret {
1831 let caret_h: f32 = (text_style.get_font_size() * 1.15)
1832 .min(shifted_content.get_height().max(12.0));
1833 display_list.push(DrawCommand::TextCaret {
1834 text_rect,
1835 content_rect: shifted_content,
1836 text: caret_text,
1837 style: text_style,
1838 caret_height: caret_h,
1839 color: style.text.get_color().multiply_alpha(opacity),
1840 multiline: input_state.get_multiline(),
1841 });
1842 }
1843 display_list.push(DrawCommand::PopClip);
1844 }
1845 NodeKind::Image(buffer) => {
1846 display_list.push(DrawCommand::Image {
1847 rect: bounds,
1848 buffer: buffer.clone(),
1849 radius: paint.get_radius(),
1850 });
1851 }
1852 NodeKind::Canvas => {
1853 *scratch.saw_canvas = true;
1854 if let Some(callback) = node.canvas_callback()
1855 && let Ok(mut func) = callback.try_borrow_mut()
1856 {
1857 let canvas_bw: f32 = paint.get_border().get_width();
1858 let canvas_clip_rect: Rect = if canvas_bw > 0.0 {
1859 Rect::new(
1860 bounds.get_x() + canvas_bw,
1861 bounds.get_y() + canvas_bw,
1862 (bounds.get_width() - canvas_bw * 2.0).max(0.0),
1863 (bounds.get_height() - canvas_bw * 2.0).max(0.0),
1864 )
1865 } else {
1866 bounds
1867 };
1868 let canvas_max_r: f32 = (bounds.get_width() * 0.5)
1869 .min(bounds.get_height() * 0.5)
1870 .max(0.0);
1871 let canvas_raw_r: CornerRadius = paint.get_radius();
1872 let canvas_inner_r: CornerRadius = CornerRadius::new(
1873 (canvas_raw_r.get_top_left().min(canvas_max_r) - canvas_bw).max(0.0),
1874 (canvas_raw_r.get_top_right().min(canvas_max_r) - canvas_bw).max(0.0),
1875 (canvas_raw_r.get_bottom_right().min(canvas_max_r) - canvas_bw)
1876 .max(0.0),
1877 (canvas_raw_r.get_bottom_left().min(canvas_max_r) - canvas_bw).max(0.0),
1878 );
1879 if canvas_inner_r.max_radius() > 0.25 {
1880 display_list.push(DrawCommand::PushRoundedClip {
1881 rect: canvas_clip_rect,
1882 radius: canvas_inner_r,
1883 });
1884 } else {
1885 display_list.push(DrawCommand::PushClip(canvas_clip_rect));
1886 }
1887 func(bounds, display_list);
1888 display_list.push(DrawCommand::PopClip);
1889 }
1890 }
1891 NodeKind::View | NodeKind::ScrollView | NodeKind::Portal | NodeKind::Fragment => {}
1892 }
1893 }
1894 let vp: Size = display_list.get_viewport();
1895 let show_scrollbar: bool = vp.get_width().min(vp.get_height()) >= 540.0;
1896 let wants_scrollbar: bool = show_scrollbar
1897 && layout.get_overflow() == Overflow::Scroll
1898 && bounds.get_height() > 24.0;
1899 let emit_clip: bool = clipped && (!raw_children.is_empty() || wants_scrollbar);
1900 let clip_push_idx: usize = display_list.len();
1901 if emit_clip {
1902 let bw: f32 = paint.get_border().get_width();
1903 let clip_rect: Rect = if bw > 0.0 {
1904 Rect::new(
1905 bounds.get_x() + bw,
1906 bounds.get_y() + bw,
1907 (bounds.get_width() - bw * 2.0).max(0.0),
1908 (bounds.get_height() - bw * 2.0).max(0.0),
1909 )
1910 } else {
1911 bounds
1912 };
1913 let max_r: f32 = (bounds.get_width() * 0.5)
1914 .min(bounds.get_height() * 0.5)
1915 .max(0.0);
1916 let raw_r: CornerRadius = paint.get_radius();
1917 let inner_r: CornerRadius = CornerRadius::new(
1918 (raw_r.get_top_left().min(max_r) - bw).max(0.0),
1919 (raw_r.get_top_right().min(max_r) - bw).max(0.0),
1920 (raw_r.get_bottom_right().min(max_r) - bw).max(0.0),
1921 (raw_r.get_bottom_left().min(max_r) - bw).max(0.0),
1922 );
1923 if inner_r.max_radius() > 0.25 {
1924 display_list.push(DrawCommand::PushRoundedClip {
1925 rect: clip_rect,
1926 radius: inner_r,
1927 });
1928 } else {
1929 display_list.push(DrawCommand::PushClip(clip_rect));
1930 }
1931 }
1932 if !raw_children.is_empty() {
1933 let raw_node_scroll: Point = node.layout_box.get_scroll_offset();
1934 let scroll_x_phys: f32 = (raw_node_scroll.get_x() * scale).round();
1935 let scroll_y_phys: f32 = (raw_node_scroll.get_y() * scale).round();
1936 let child_offset_phys: Point =
1937 Point::new(total_off_x - scroll_x_phys, total_off_y - scroll_y_phys);
1938 let needs_z_sort: bool = raw_children.len() > 1
1939 && raw_children.iter().copied().any(|cid: NodeId| {
1940 tree.get_node(cid)
1941 .map(|c: &SceneNode| c.style_ref().layout.get_z_index() != 0)
1942 .unwrap_or(false)
1943 });
1944 if !needs_z_sort {
1945 for child_id in raw_children.iter().copied() {
1946 Self::emit_node(
1947 tree,
1948 child_id,
1949 (child_offset_phys, opacity),
1950 next_cull,
1951 active_hover_target,
1952 scratch,
1953 display_list,
1954 );
1955 }
1956 } else {
1957 let sort_start: usize = scratch.z_sort.len();
1958 for (idx, cid) in raw_children.iter().copied().enumerate() {
1959 if let Some(c) = tree.get_node(cid) {
1960 scratch
1961 .z_sort
1962 .push((c.style_ref().layout.get_z_index(), idx, cid));
1963 }
1964 }
1965 let sort_end: usize = scratch.z_sort.len();
1966 scratch.z_sort[sort_start..sort_end].sort_by(
1967 |a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
1968 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
1969 },
1970 );
1971 for slot_idx in sort_start..sort_end {
1972 let child_id: NodeId = scratch.z_sort[slot_idx].2;
1973 Self::emit_node(
1974 tree,
1975 child_id,
1976 (child_offset_phys, opacity),
1977 next_cull,
1978 active_hover_target,
1979 scratch,
1980 display_list,
1981 );
1982 }
1983 scratch.z_sort.truncate(sort_start);
1984 }
1985 }
1986 if wants_scrollbar {
1987 let lbox: &LayoutBox = &node.layout_box;
1988 let border_w: f32 = paint.get_border().get_width();
1989 let inner_x: f32 = bounds.get_x() + border_w;
1990 let inner_y: f32 = bounds.get_y() + border_w;
1991 let inner_w: f32 = (bounds.get_width() - border_w * 2.0).max(1.0);
1992 let inner_h: f32 = (bounds.get_height() - border_w * 2.0).max(1.0);
1993 let view_w: f32 = lbox.get_content_bounds().get_width().max(1.0);
1994 let view_h: f32 = lbox.get_content_bounds().get_height().max(1.0);
1995 let (scroll_y, total_h): (f32, f32) = match node.virtual_scroll_metrics() {
1996 Some((vy, vh)) => (vy, vh),
1997 None => (
1998 lbox.get_scroll_offset().get_y(),
1999 lbox.get_content_size().get_height(),
2000 ),
2001 };
2002 let scroll_x: f32 = lbox.get_scroll_offset().get_x();
2003 let total_w: f32 = lbox.get_content_size().get_width();
2004 let max_scroll_y: f32 = (total_h - view_h).max(0.0);
2005 let max_scroll_x: f32 = (total_w - view_w).max(0.0);
2006 let has_v_bar: bool = max_scroll_y > 1.0;
2007 let has_h_bar: bool = max_scroll_x > 1.0 && bounds.get_width() > 24.0;
2008 let corner_inset: f32 = if has_v_bar && has_h_bar { 8.0 } else { 0.0 };
2009 if has_v_bar {
2010 let track_margin: f32 = 3.0;
2011 let track_w: f32 = if hovered { 6.0 } else { 4.0 };
2012 let track_x: f32 = (inner_x + inner_w - track_w - 2.0).max(inner_x);
2013 let track_y: f32 = inner_y + track_margin;
2014 let track_h: f32 = (inner_h - track_margin * 2.0 - corner_inset).max(16.0);
2015 let ratio: f32 = (view_h / total_h.max(view_h)).clamp(0.0, 1.0);
2016 let min_thumb_h: f32 = 20.0_f32.min(track_h);
2017 let thumb_h: f32 = (track_h * ratio).clamp(min_thumb_h, track_h);
2018 let scroll_frac: f32 = (scroll_y / max_scroll_y).clamp(0.0, 1.0);
2019 let thumb_y: f32 = track_y + (track_h - thumb_h) * scroll_frac;
2020 let track_x_snapped: f32 = (track_x * scale).round() * inv_scale;
2021 let track_y_snapped: f32 = (track_y * scale).round() * inv_scale;
2022 let track_w_snapped: f32 = (track_w * scale).round().max(1.0) * inv_scale;
2023 let track_h_snapped: f32 = (track_h * scale).round().max(1.0) * inv_scale;
2024 let thumb_y_snapped: f32 = (thumb_y * scale).round() * inv_scale;
2025 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
2026 if hovered {
2027 display_list.push(DrawCommand::FillRoundedRect {
2028 rect: Rect::new(
2029 track_x_snapped,
2030 track_y_snapped,
2031 track_w_snapped,
2032 track_h_snapped,
2033 ),
2034 color: Color::rgba(113, 113, 122, 24).multiply_alpha(opacity),
2035 radius: CornerRadius::all(3.0),
2036 border: Border::default(),
2037 });
2038 }
2039 let thumb_alpha: u8 = if hovered { 180 } else { 112 };
2040 display_list.push(DrawCommand::FillRoundedRect {
2041 rect: Rect::new(
2042 track_x_snapped,
2043 thumb_y_snapped,
2044 track_w_snapped,
2045 thumb_h_snapped,
2046 ),
2047 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
2048 radius: CornerRadius::all(track_w * 0.5),
2049 border: Border::default(),
2050 });
2051 }
2052 if has_h_bar {
2053 let track_margin: f32 = 3.0;
2054 let track_h: f32 = if hovered { 6.0 } else { 4.0 };
2055 let track_y: f32 = (inner_y + inner_h - track_h - 2.0).max(inner_y);
2056 let track_x: f32 = inner_x + track_margin;
2057 let track_w: f32 = (inner_w - track_margin * 2.0 - corner_inset).max(16.0);
2058 let ratio: f32 = (view_w / total_w.max(view_w)).clamp(0.0, 1.0);
2059 let min_thumb_w: f32 = 20.0_f32.min(track_w);
2060 let thumb_w: f32 = (track_w * ratio).clamp(min_thumb_w, track_w);
2061 let scroll_frac: f32 = (scroll_x / max_scroll_x).clamp(0.0, 1.0);
2062 let thumb_x: f32 = track_x + (track_w - thumb_w) * scroll_frac;
2063 let track_x_snapped: f32 = (track_x * scale).round() * inv_scale;
2064 let track_y_snapped: f32 = (track_y * scale).round() * inv_scale;
2065 let track_w_snapped: f32 = (track_w * scale).round().max(1.0) * inv_scale;
2066 let track_h_snapped: f32 = (track_h * scale).round().max(1.0) * inv_scale;
2067 let thumb_x_snapped: f32 = (thumb_x * scale).round() * inv_scale;
2068 let thumb_w_snapped: f32 = (thumb_w * scale).round().max(1.0) * inv_scale;
2069 if hovered {
2070 display_list.push(DrawCommand::FillRoundedRect {
2071 rect: Rect::new(
2072 track_x_snapped,
2073 track_y_snapped,
2074 track_w_snapped,
2075 track_h_snapped,
2076 ),
2077 color: Color::rgba(113, 113, 122, 24).multiply_alpha(opacity),
2078 radius: CornerRadius::all(3.0),
2079 border: Border::default(),
2080 });
2081 }
2082 let thumb_alpha: u8 = if hovered { 180 } else { 112 };
2083 display_list.push(DrawCommand::FillRoundedRect {
2084 rect: Rect::new(
2085 thumb_x_snapped,
2086 track_y_snapped,
2087 thumb_w_snapped,
2088 track_h_snapped,
2089 ),
2090 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
2091 radius: CornerRadius::all(track_h * 0.5),
2092 border: Border::default(),
2093 });
2094 }
2095 }
2096 if emit_clip {
2097 if display_list.len() == clip_push_idx + 1 {
2098 let _: Option<DrawCommand> = display_list.get_mut_commands().pop();
2099 } else {
2100 display_list.push(DrawCommand::PopClip);
2101 }
2102 }
2103 if let Some((cmd_start, slot_idx, geom_hash, style_text_hash)) = frag_capture
2104 && let Some(cache) = scratch.fragments.as_deref_mut()
2105 && let Some(slot_ref) = cache.slots.get_mut(slot_idx)
2106 {
2107 let cmd_slice: &[DrawCommand] = &display_list.get_commands()[cmd_start..];
2108 cache.fragment_misses = cache.fragment_misses.wrapping_add(1);
2109 if let Some(existing) = slot_ref.as_mut() {
2110 if existing.node_id != node_id {
2111 cache.fragment_evictions = cache.fragment_evictions.wrapping_add(1);
2112 }
2113 existing.node_id = node_id;
2114 existing.scale_bits = scale.to_bits();
2115 existing.opacity_bits = parent_opacity.to_bits();
2116 existing.offset_phys = offset_phys;
2117 existing.geom_hash = geom_hash;
2118 existing.style_text_hash = style_text_hash;
2119 existing.commands.clear();
2120 existing.commands.extend_from_slice(cmd_slice);
2121 } else {
2122 *slot_ref = Some(RetainedPaintFragment {
2123 node_id,
2124 scale_bits: scale.to_bits(),
2125 opacity_bits: parent_opacity.to_bits(),
2126 offset_phys,
2127 geom_hash,
2128 style_text_hash,
2129 commands: cmd_slice.to_vec(),
2130 });
2131 }
2132 }
2133 }
2134}