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