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::PopClip => {
196 Self::mix_hash(&mut hash, 12);
197 }
198 }
199 }
200 hash
201 }
202
203 #[must_use]
213 pub fn base_layer_hash(&self) -> u64 {
214 let split: usize = self.get_portal_split_index().min(self.get_commands().len());
215 let mut hash: u64 = FNV1A_OFFSET_BASIS;
216 Self::mix_hash(&mut hash, split as u64);
217 Self::mix_hash(&mut hash, self.get_viewport().get_width().to_bits() as u64);
218 Self::mix_hash(&mut hash, self.get_viewport().get_height().to_bits() as u64);
219 Self::mix_hash(&mut hash, self.get_scale_factor().to_bits() as u64);
220 for cmd in self.get_commands()[..split].iter() {
221 match cmd {
222 DrawCommand::Clear(c) => {
223 Self::mix_hash(&mut hash, 1);
224 Self::mix_hash(&mut hash, u32::from_le_bytes(c.to_rgba_bytes()) as u64);
225 }
226 DrawCommand::FillRoundedRect {
227 rect,
228 color,
229 radius,
230 border,
231 } => {
232 Self::mix_hash(&mut hash, 2);
233 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
234 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
235 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
236 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
237 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
238 Self::mix_hash(&mut hash, radius.get_top_left().to_bits() as u64);
239 Self::mix_hash(&mut hash, radius.get_top_right().to_bits() as u64);
240 Self::mix_hash(&mut hash, radius.get_bottom_right().to_bits() as u64);
241 Self::mix_hash(&mut hash, radius.get_bottom_left().to_bits() as u64);
242 Self::mix_hash(&mut hash, border.get_width().to_bits() as u64);
243 Self::mix_hash(
244 &mut hash,
245 u32::from_le_bytes(border.get_color().to_rgba_bytes()) as u64,
246 );
247 }
248 DrawCommand::LinearGradientRect {
249 rect,
250 gradient,
251 radius,
252 } => {
253 Self::mix_hash(&mut hash, 3);
254 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
255 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
256 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
257 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
258 Self::mix_hash(&mut hash, gradient.get_angle_deg().to_bits() as u64);
259 Self::mix_hash(
260 &mut hash,
261 u32::from_le_bytes(gradient.get_start_color().to_rgba_bytes()) as u64,
262 );
263 Self::mix_hash(
264 &mut hash,
265 u32::from_le_bytes(gradient.get_end_color().to_rgba_bytes()) as u64,
266 );
267 Self::mix_hash(&mut hash, radius.max_radius().to_bits() as u64);
268 }
269 DrawCommand::BoxShadow {
270 rect,
271 radius,
272 shadow,
273 } => {
274 Self::mix_hash(&mut hash, 4);
275 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
276 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
277 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
278 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
279 Self::mix_hash(&mut hash, radius.max_radius().to_bits() as u64);
280 Self::mix_hash(&mut hash, shadow.get_offset_x().to_bits() as u64);
281 Self::mix_hash(&mut hash, shadow.get_offset_y().to_bits() as u64);
282 Self::mix_hash(&mut hash, shadow.get_blur_radius().to_bits() as u64);
283 Self::mix_hash(&mut hash, shadow.get_spread_radius().to_bits() as u64);
284 Self::mix_hash(
285 &mut hash,
286 u32::from_le_bytes(shadow.get_color().to_rgba_bytes()) as u64,
287 );
288 }
289 DrawCommand::TextRun { rect, text, style } => {
290 Self::mix_hash(&mut hash, 5);
291 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
292 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
293 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
294 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
295 Self::mix_hash(&mut hash, style.get_font_size().to_bits() as u64);
296 Self::mix_hash(&mut hash, style.get_family() as u64);
297 Self::mix_hash(&mut hash, style.get_weight() as u64);
298 Self::mix_hash(&mut hash, u64::from(style.get_italic()));
299 Self::mix_hash(&mut hash, style.get_align() as u64);
300 Self::mix_hash(
301 &mut hash,
302 u32::from_le_bytes(style.get_color().to_rgba_bytes()) as u64,
303 );
304 Self::mix_hash(&mut hash, text.len() as u64);
305 for byte in text.as_bytes().iter().copied() {
306 Self::mix_hash(&mut hash, byte as u64);
307 }
308 }
309 DrawCommand::TextCaret {
310 text_rect,
311 content_rect,
312 text,
313 style,
314 caret_height,
315 color,
316 multiline,
317 } => {
318 Self::mix_hash(&mut hash, 13);
319 Self::mix_hash(&mut hash, text_rect.get_x().to_bits() as u64);
320 Self::mix_hash(&mut hash, text_rect.get_y().to_bits() as u64);
321 Self::mix_hash(&mut hash, content_rect.get_width().to_bits() as u64);
322 Self::mix_hash(&mut hash, caret_height.to_bits() as u64);
323 Self::mix_hash(&mut hash, style.get_font_size().to_bits() as u64);
324 Self::mix_hash(&mut hash, u64::from(*multiline));
325 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
326 Self::mix_hash(&mut hash, text.len() as u64);
327 for byte in text.as_bytes().iter().copied() {
328 Self::mix_hash(&mut hash, byte as u64);
329 }
330 }
331 DrawCommand::Image {
332 rect,
333 buffer,
334 radius,
335 } => {
336 Self::mix_hash(&mut hash, 6);
337 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
338 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
339 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
340 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
341 Self::mix_hash(&mut hash, buffer.get_width() as u64);
342 Self::mix_hash(&mut hash, buffer.get_height() as u64);
343 Self::mix_hash(&mut hash, buffer.get_pixels().as_ptr() as usize as u64);
344 Self::mix_hash(&mut hash, radius.max_radius().to_bits() as u64);
345 }
346 DrawCommand::Line {
347 from,
348 to,
349 color,
350 width,
351 } => {
352 Self::mix_hash(&mut hash, 7);
353 Self::mix_hash(&mut hash, from.get_x().to_bits() as u64);
354 Self::mix_hash(&mut hash, from.get_y().to_bits() as u64);
355 Self::mix_hash(&mut hash, to.get_x().to_bits() as u64);
356 Self::mix_hash(&mut hash, to.get_y().to_bits() as u64);
357 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
358 Self::mix_hash(&mut hash, width.to_bits() as u64);
359 }
360 DrawCommand::Circle {
361 center,
362 radius,
363 color,
364 } => {
365 Self::mix_hash(&mut hash, 8);
366 Self::mix_hash(&mut hash, center.get_x().to_bits() as u64);
367 Self::mix_hash(&mut hash, center.get_y().to_bits() as u64);
368 Self::mix_hash(&mut hash, radius.to_bits() as u64);
369 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
370 }
371 DrawCommand::Triangle { p0, p1, p2, color } => {
372 Self::mix_hash(&mut hash, 9);
373 Self::mix_hash(&mut hash, p0.get_x().to_bits() as u64);
374 Self::mix_hash(&mut hash, p0.get_y().to_bits() as u64);
375 Self::mix_hash(&mut hash, p1.get_x().to_bits() as u64);
376 Self::mix_hash(&mut hash, p1.get_y().to_bits() as u64);
377 Self::mix_hash(&mut hash, p2.get_x().to_bits() as u64);
378 Self::mix_hash(&mut hash, p2.get_y().to_bits() as u64);
379 Self::mix_hash(&mut hash, u32::from_le_bytes(color.to_rgba_bytes()) as u64);
380 }
381 DrawCommand::SpriteRegion {
382 rect,
383 buffer,
384 src_rect,
385 tint,
386 } => {
387 Self::mix_hash(&mut hash, 10);
388 Self::mix_hash(&mut hash, rect.get_x().to_bits() as u64);
389 Self::mix_hash(&mut hash, rect.get_y().to_bits() as u64);
390 Self::mix_hash(&mut hash, rect.get_width().to_bits() as u64);
391 Self::mix_hash(&mut hash, rect.get_height().to_bits() as u64);
392 Self::mix_hash(&mut hash, buffer.get_pixels().as_ptr() as usize as u64);
393 Self::mix_hash(&mut hash, src_rect.get_x().to_bits() as u64);
394 Self::mix_hash(&mut hash, src_rect.get_y().to_bits() as u64);
395 Self::mix_hash(&mut hash, u32::from_le_bytes(tint.to_rgba_bytes()) as u64);
396 }
397 DrawCommand::PushClip(r) => {
398 Self::mix_hash(&mut hash, 11);
399 Self::mix_hash(&mut hash, r.get_x().to_bits() as u64);
400 Self::mix_hash(&mut hash, r.get_y().to_bits() as u64);
401 Self::mix_hash(&mut hash, r.get_width().to_bits() as u64);
402 Self::mix_hash(&mut hash, r.get_height().to_bits() as u64);
403 }
404 DrawCommand::PopClip => {
405 Self::mix_hash(&mut hash, 12);
406 }
407 }
408 }
409 if hash == 0 { 1 } else { hash }
410 }
411}
412
413impl PaintPass {
415 pub fn build(
425 tree: &SceneTree,
426 viewport: Size,
427 scale_factor: f32,
428 clear_color: Color,
429 display_list: &mut DisplayList,
430 ) {
431 display_list.reset(viewport, scale_factor);
432 display_list.push(DrawCommand::Clear(clear_color));
433 let viewport_rect: Rect = Rect::new(0.0, 0.0, viewport.get_width(), viewport.get_height());
434 let active_hover_target: Option<NodeId> = Self::resolve_active_hover_target(tree);
435 let mut portals: Vec<(i32, usize, NodeId)> = Vec::new();
436 Self::emit_node(
437 tree,
438 tree.get_root(),
439 (Point::new(0.0, 0.0), 1.0),
440 viewport_rect,
441 active_hover_target,
442 Some(&mut portals),
443 display_list,
444 );
445 display_list.set_portal_split_index(display_list.len());
446 portals.sort_by(|a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
447 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
448 });
449 for (_, _, portal_id) in portals.into_iter() {
450 Self::emit_node(
451 tree,
452 portal_id,
453 (Point::new(0.0, 0.0), 1.0),
454 viewport_rect,
455 active_hover_target,
456 None,
457 display_list,
458 );
459 }
460 }
461
462 #[must_use]
472 fn resolve_active_hover_target(tree: &SceneTree) -> Option<NodeId> {
473 let mut cursor: Option<NodeId> = tree.get_focus().hovered_node();
474 while let Some(id) = cursor {
475 let Some(node) = tree.get_node(id) else {
476 break;
477 };
478 let style: Style = node.get_style();
479 let paint: PaintStyle = style.get_paint();
480 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
481 || style.get_layout().get_position() == Position::Fixed;
482 let is_transparent_canvas: bool =
483 matches!(node.get_kind(), NodeKind::Canvas) && paint.background_color().is_none();
484 let is_interactive: bool = paint.get_cursor() == CursorIcon::Pointer
485 || node
486 .get_handlers()
487 .handler_for(&UiEventKind::Click(Point::default()))
488 .is_some();
489 if !is_scrim && !is_transparent_canvas && is_interactive {
490 return Some(id);
491 }
492 cursor = node.parent_id();
493 }
494 None
495 }
496
497 fn interactive_tint(base: Option<Color>, hovered: bool, pressed: bool) -> Option<Color> {
509 if !hovered && !pressed {
510 return base;
511 }
512 let delta: i32 = if pressed { 24 } else { 14 };
513 match base {
514 Some(c) if c.get_a() > 0 => {
515 let luma: u32 =
516 (c.get_r() as u32) * 299 + (c.get_g() as u32) * 587 + (c.get_b() as u32) * 114;
517 if luma < 120_000 {
518 let lift: i32 = if pressed { 30 } else { 20 };
519 Some(Color::new(
520 (c.get_r() as i32 + lift).clamp(0, 255) as u8,
521 (c.get_g() as i32 + lift).clamp(0, 255) as u8,
522 (c.get_b() as i32 + lift).clamp(0, 255) as u8,
523 c.get_a(),
524 ))
525 } else {
526 Some(Color::new(
527 (c.get_r() as i32 - delta).clamp(0, 255) as u8,
528 (c.get_g() as i32 - delta).clamp(0, 255) as u8,
529 (c.get_b() as i32 - delta).clamp(0, 255) as u8,
530 c.get_a(),
531 ))
532 }
533 }
534 _ => Some(Color::rgba(113, 113, 122, if pressed { 42 } else { 26 })),
535 }
536 }
537
538 fn emit_node(
550 tree: &SceneTree,
551 node_id: NodeId,
552 scroll_and_opacity: (Point, f32),
553 cull_rect: Rect,
554 active_hover_target: Option<NodeId>,
555 mut portals: Option<&mut Vec<(i32, usize, NodeId)>>,
556 display_list: &mut DisplayList,
557 ) {
558 let (scroll_offset, parent_opacity): (Point, f32) = scroll_and_opacity;
559 let Some(node) = tree.get_node(node_id) else {
560 return;
561 };
562 let style: Style = node.get_style();
563 let layout: LayoutStyle = style.get_layout();
564 if layout.get_display() == Display::None {
565 return;
566 }
567 if let Some(portal_queue) = portals.as_deref_mut()
568 && (matches!(node.get_kind(), NodeKind::Portal)
569 || layout.get_position() == Position::Fixed)
570 {
571 portal_queue.push((layout.get_z_index(), portal_queue.len(), node_id));
572 return;
573 }
574 let hovered: bool = node.get_interaction().get_hovered();
575 let pressed: bool = node.get_interaction().get_pressed();
576 let mut paint: PaintStyle = if hovered {
577 node.hover_paint().unwrap_or(style.get_paint())
578 } else {
579 style.get_paint()
580 };
581 let is_scrim: bool =
582 matches!(node.get_kind(), NodeKind::Portal) || layout.get_position() == Position::Fixed;
583 let is_interactive: bool = paint.get_cursor() == CursorIcon::Pointer
584 || node
585 .get_handlers()
586 .handler_for(&UiEventKind::Click(Point::default()))
587 .is_some();
588 let is_transparent_canvas: bool =
589 matches!(node.get_kind(), NodeKind::Canvas) && paint.background_color().is_none();
590 let hover_visual: bool = hovered && active_hover_target == Some(node_id);
591 if is_interactive
592 && !is_scrim
593 && !is_transparent_canvas
594 && node.hover_paint().is_none()
595 && (hover_visual || pressed)
596 {
597 paint.set_background(Self::interactive_tint(
598 paint.background_color(),
599 hover_visual,
600 pressed,
601 ));
602 }
603 let opacity: f32 = (parent_opacity * paint.get_opacity()).clamp(0.0, 1.0);
604 if opacity <= 0.0 {
605 return;
606 }
607 let raw_bounds: Rect = node.get_layout_box().get_bounds();
608 let transform: Transform2D = paint.get_transform();
609 let bounds: Rect = Rect::new(
610 raw_bounds.get_x() - scroll_offset.get_x() + transform.get_translate_x(),
611 raw_bounds.get_y() - scroll_offset.get_y() + transform.get_translate_y(),
612 raw_bounds.get_width() * transform.get_scale_x(),
613 raw_bounds.get_height() * transform.get_scale_y(),
614 );
615 let clipped: bool = matches!(layout.get_overflow(), Overflow::Hidden | Overflow::Scroll);
616 let next_cull: Rect = if clipped {
617 match cull_rect.intersect(bounds) {
618 Some(r) => r,
619 None => return,
620 }
621 } else {
622 cull_rect
623 };
624 let halo: f32 = if paint.box_shadow().is_some() {
625 36.0
626 } else if paint.focus_ring_border().is_some() {
627 8.0
628 } else {
629 2.0
630 };
631 let self_vis_rect: Rect = Rect::new(
632 bounds.get_x() - halo,
633 bounds.get_y() - halo,
634 bounds.get_width() + halo * 2.0,
635 bounds.get_height() + halo * 2.0,
636 );
637 let self_visible: bool = bounds.get_width() > 0.0
638 && bounds.get_height() > 0.0
639 && self_vis_rect.intersect(cull_rect).is_some();
640 if self_visible {
641 if let Some(mut shadow) = paint.box_shadow() {
642 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
643 display_list.push(DrawCommand::BoxShadow {
644 rect: bounds,
645 radius: paint.get_radius(),
646 shadow,
647 });
648 }
649 if let Some(mut gradient) = paint.background_gradient() {
650 gradient.set_start_color(gradient.get_start_color().multiply_alpha(opacity));
651 gradient.set_end_color(gradient.get_end_color().multiply_alpha(opacity));
652 display_list.push(DrawCommand::LinearGradientRect {
653 rect: bounds,
654 gradient,
655 radius: paint.get_radius(),
656 });
657 } else if let Some(bg) = paint.background_color() {
658 let mut border: Border = paint.get_border();
659 border.set_color(border.get_color().multiply_alpha(opacity));
660 display_list.push(DrawCommand::FillRoundedRect {
661 rect: bounds,
662 color: bg.multiply_alpha(opacity),
663 radius: paint.get_radius(),
664 border,
665 });
666 } else if paint.get_border().get_width() > 0.0 {
667 let mut border: Border = paint.get_border();
668 border.set_color(border.get_color().multiply_alpha(opacity));
669 display_list.push(DrawCommand::FillRoundedRect {
670 rect: bounds,
671 color: COLOR_TRANSPARENT,
672 radius: paint.get_radius(),
673 border,
674 });
675 }
676 if node.get_interaction().get_focused()
677 && let Some(mut ring) = paint.focus_ring_border()
678 {
679 ring.set_color(ring.get_color().multiply_alpha(opacity));
680 let ring_w: f32 = ring.get_width();
681 let ring_rect: Rect = Rect::new(
682 bounds.get_x() - ring_w,
683 bounds.get_y() - ring_w,
684 bounds.get_width() + ring_w * 2.0,
685 bounds.get_height() + ring_w * 2.0,
686 );
687 display_list.push(DrawCommand::FillRoundedRect {
688 rect: ring_rect,
689 color: COLOR_TRANSPARENT,
690 radius: CornerRadius::all(paint.get_radius().max_radius() + ring_w),
691 border: ring,
692 });
693 }
694 match node.get_kind() {
695 NodeKind::Text(text) => {
696 let mut text_style: TextStyle = style.get_text();
697 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
698 let content_b: Rect = node.get_layout_box().get_content_bounds();
699 let shifted_content: Rect = Rect::new(
700 content_b.get_x() - scroll_offset.get_x() + transform.get_translate_x(),
701 content_b.get_y() - scroll_offset.get_y() + transform.get_translate_y(),
702 content_b.get_width(),
703 content_b.get_height(),
704 );
705 display_list.push(DrawCommand::TextRun {
706 rect: shifted_content,
707 text: text.clone(),
708 style: text_style,
709 });
710 }
711 NodeKind::Input(input_state) => {
712 let mut text_style: TextStyle = style.get_text();
713 let rendered_text: String = if input_state.get_value().is_empty() {
714 text_style.set_color(text_style.get_color().multiply_alpha(opacity * 0.55));
715 input_state.get_placeholder().clone()
716 } else if input_state.get_password() {
717 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
718 "*".repeat(input_state.get_value().chars().count())
719 } else {
720 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
721 input_state.get_value().clone()
722 };
723 let content_b: Rect = node.get_layout_box().get_content_bounds();
724 let shifted_content: Rect = Rect::new(
725 content_b.get_x() - scroll_offset.get_x() + transform.get_translate_x(),
726 content_b.get_y() - scroll_offset.get_y() + transform.get_translate_y(),
727 content_b.get_width(),
728 content_b.get_height(),
729 );
730 let line_h: f32 =
731 (text_style.get_font_size() * text_style.get_line_height()).max(1.0);
732 let text_rect: Rect = if input_state.get_multiline() {
733 shifted_content
734 } else {
735 text_style.set_overflow(TextOverflow::Clip);
736 let text_y: f32 = shifted_content.get_y()
737 + ((shifted_content.get_height() - line_h) * 0.5).max(0.0);
738 Rect::new(
739 shifted_content.get_x(),
740 text_y,
741 shifted_content.get_width(),
742 line_h,
743 )
744 };
745 display_list.push(DrawCommand::PushClip(shifted_content));
746 display_list.push(DrawCommand::TextRun {
747 rect: text_rect,
748 text: rendered_text.clone(),
749 style: text_style,
750 });
751 if node.get_interaction().get_focused() && tree.get_caret_visible() {
752 let caret_h: f32 = (text_style.get_font_size() * 1.15)
753 .min(shifted_content.get_height().max(12.0));
754 let caret_text: String = if input_state.get_value().is_empty() {
755 String::new()
756 } else {
757 rendered_text
758 };
759 display_list.push(DrawCommand::TextCaret {
760 text_rect,
761 content_rect: shifted_content,
762 text: caret_text,
763 style: text_style,
764 caret_height: caret_h,
765 color: style.get_text().get_color().multiply_alpha(opacity),
766 multiline: input_state.get_multiline(),
767 });
768 }
769 display_list.push(DrawCommand::PopClip);
770 }
771 NodeKind::Image(buffer) => {
772 display_list.push(DrawCommand::Image {
773 rect: bounds,
774 buffer: buffer.clone(),
775 radius: paint.get_radius(),
776 });
777 }
778 NodeKind::Canvas => {
779 if let Some(callback) = node.canvas_callback()
780 && let Ok(mut func) = callback.try_borrow_mut()
781 {
782 display_list.push(DrawCommand::PushClip(bounds));
783 func(bounds, display_list);
784 display_list.push(DrawCommand::PopClip);
785 }
786 }
787 NodeKind::View | NodeKind::ScrollView | NodeKind::Portal | NodeKind::Fragment => {}
788 }
789 }
790 if clipped {
791 let bw: f32 = paint.get_border().get_width();
792 let clip_rect: Rect = if bw > 0.0 {
793 Rect::new(
794 bounds.get_x() + bw,
795 bounds.get_y() + bw,
796 (bounds.get_width() - bw * 2.0).max(0.0),
797 (bounds.get_height() - bw * 2.0).max(0.0),
798 )
799 } else {
800 bounds
801 };
802 display_list.push(DrawCommand::PushClip(clip_rect));
803 }
804 let scale: f32 = display_list.get_scale_factor().max(0.5);
805 let raw_node_scroll: Point = node.get_layout_box().get_scroll_offset();
806 let snap_scroll_x: f32 = (raw_node_scroll.get_x() * scale).round() / scale;
807 let snap_scroll_y: f32 = (raw_node_scroll.get_y() * scale).round() / scale;
808 let child_scroll: Point = Point::new(
809 scroll_offset.get_x() + snap_scroll_x,
810 scroll_offset.get_y() + snap_scroll_y,
811 );
812 let raw_children: &[NodeId] = node.get_children().as_slice();
813 let needs_z_sort: bool = raw_children.len() > 1
814 && raw_children.iter().copied().any(|cid: NodeId| {
815 tree.get_node(cid)
816 .map(|c: &SceneNode| c.get_style().get_layout().get_z_index() != 0)
817 .unwrap_or(false)
818 });
819 if !needs_z_sort {
820 for child_id in raw_children.iter().copied() {
821 Self::emit_node(
822 tree,
823 child_id,
824 (child_scroll, opacity),
825 next_cull,
826 active_hover_target,
827 portals.as_deref_mut(),
828 display_list,
829 );
830 }
831 } else {
832 let mut sorted_children: Vec<(i32, usize, NodeId)> = raw_children
833 .iter()
834 .copied()
835 .enumerate()
836 .filter_map(|(idx, cid): (usize, NodeId)| {
837 tree.get_node(cid)
838 .map(|c: &SceneNode| (c.get_style().get_layout().get_z_index(), idx, cid))
839 })
840 .collect();
841 sorted_children.sort_by(|a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
842 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
843 });
844 for (_, _, child_id) in sorted_children.into_iter() {
845 Self::emit_node(
846 tree,
847 child_id,
848 (child_scroll, opacity),
849 next_cull,
850 active_hover_target,
851 portals.as_deref_mut(),
852 display_list,
853 );
854 }
855 }
856 let vp: Size = display_list.get_viewport();
857 let show_scrollbar: bool = vp.get_width().min(vp.get_height()) >= 540.0;
858 if show_scrollbar && layout.get_overflow() == Overflow::Scroll && bounds.get_height() > 24.0
859 {
860 let lbox: LayoutBox = node.get_layout_box();
861 let border_w: f32 = paint.get_border().get_width();
862 let inner_x: f32 = bounds.get_x() + border_w;
863 let inner_y: f32 = bounds.get_y() + border_w;
864 let inner_w: f32 = (bounds.get_width() - border_w * 2.0).max(1.0);
865 let inner_h: f32 = (bounds.get_height() - border_w * 2.0).max(1.0);
866 let view_h: f32 = lbox.get_content_bounds().get_height().max(1.0);
867 let (scroll_y, total_h): (f32, f32) = match node.virtual_scroll_metrics() {
868 Some((vy, vh)) => (vy, vh),
869 None => (
870 lbox.get_scroll_offset().get_y(),
871 lbox.get_content_size().get_height(),
872 ),
873 };
874 let max_scroll_y: f32 = (total_h - view_h).max(0.0);
875 if max_scroll_y > 1.0 {
876 let track_margin: f32 = 3.0;
877 let track_w: f32 = if hovered { 6.0 } else { 4.0 };
878 let track_x: f32 = (inner_x + inner_w - track_w - 2.0).max(inner_x);
879 let track_y: f32 = inner_y + track_margin;
880 let track_h: f32 = (inner_h - track_margin * 2.0).max(16.0);
881 let ratio: f32 = (view_h / total_h.max(view_h)).clamp(0.0, 1.0);
882 let min_thumb_h: f32 = 20.0_f32.min(track_h);
883 let thumb_h: f32 = (track_h * ratio).clamp(min_thumb_h, track_h);
884 let scroll_frac: f32 = (scroll_y / max_scroll_y).clamp(0.0, 1.0);
885 let thumb_y: f32 = track_y + (track_h - thumb_h) * scroll_frac;
886 if hovered {
887 display_list.push(DrawCommand::FillRoundedRect {
888 rect: Rect::new(track_x, track_y, track_w, track_h),
889 color: Color::rgba(113, 113, 122, 24).multiply_alpha(opacity),
890 radius: CornerRadius::all(3.0),
891 border: Border::default(),
892 });
893 }
894 let thumb_alpha: u8 = if hovered { 180 } else { 112 };
895 display_list.push(DrawCommand::FillRoundedRect {
896 rect: Rect::new(track_x, thumb_y, track_w, thumb_h),
897 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
898 radius: CornerRadius::all(track_w * 0.5),
899 border: Border::default(),
900 });
901 }
902 }
903 if clipped {
904 display_list.push(DrawCommand::PopClip);
905 }
906 }
907}