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 PaintPass {
432 pub fn build(
442 tree: &SceneTree,
443 viewport: Size,
444 scale_factor: f32,
445 clear_color: Color,
446 display_list: &mut DisplayList,
447 ) {
448 display_list.reset(viewport, scale_factor);
449 display_list.push(DrawCommand::Clear(clear_color));
450 let viewport_rect: Rect = Rect::new(0.0, 0.0, viewport.get_width(), viewport.get_height());
451 let active_hover_target: Option<NodeId> = Self::resolve_active_hover_target(tree);
452 let mut portals: Vec<(i32, usize, NodeId)> = Vec::new();
453 Self::emit_node(
454 tree,
455 tree.get_root(),
456 (Point::new(0.0, 0.0), 1.0),
457 viewport_rect,
458 active_hover_target,
459 Some(&mut portals),
460 display_list,
461 );
462 display_list.set_portal_split_index(display_list.len());
463 portals.sort_by(|a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
464 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
465 });
466 for (_, _, portal_id) in portals.into_iter() {
467 Self::emit_node(
468 tree,
469 portal_id,
470 (Point::new(0.0, 0.0), 1.0),
471 viewport_rect,
472 active_hover_target,
473 None,
474 display_list,
475 );
476 }
477 }
478
479 #[must_use]
489 fn resolve_active_hover_target(tree: &SceneTree) -> Option<NodeId> {
490 let mut cursor: Option<NodeId> = tree.get_focus().hovered_node();
491 while let Some(id) = cursor {
492 let Some(node) = tree.get_node(id) else {
493 break;
494 };
495 let style: Style = node.get_style();
496 let paint: PaintStyle = style.get_paint();
497 let is_scrim: bool = matches!(node.get_kind(), NodeKind::Portal)
498 || style.get_layout().get_position() == Position::Fixed;
499 let is_transparent_canvas: bool =
500 matches!(node.get_kind(), NodeKind::Canvas) && paint.background_color().is_none();
501 let is_interactive: bool = paint.get_cursor() == CursorIcon::Pointer
502 || node
503 .get_handlers()
504 .handler_for(&UiEventKind::Click(Point::default()))
505 .is_some();
506 if !is_scrim && !is_transparent_canvas && is_interactive {
507 return Some(id);
508 }
509 cursor = node.parent_id();
510 }
511 None
512 }
513
514 fn interactive_tint(base: Option<Color>, hovered: bool, pressed: bool) -> Option<Color> {
526 if !hovered && !pressed {
527 return base;
528 }
529 let delta: i32 = if pressed { 24 } else { 14 };
530 match base {
531 Some(c) if c.get_a() > 0 => {
532 let luma: u32 =
533 (c.get_r() as u32) * 299 + (c.get_g() as u32) * 587 + (c.get_b() as u32) * 114;
534 if luma < 120_000 {
535 let lift: i32 = if pressed { 30 } else { 20 };
536 Some(Color::new(
537 (c.get_r() as i32 + lift).clamp(0, 255) as u8,
538 (c.get_g() as i32 + lift).clamp(0, 255) as u8,
539 (c.get_b() as i32 + lift).clamp(0, 255) as u8,
540 c.get_a(),
541 ))
542 } else {
543 Some(Color::new(
544 (c.get_r() as i32 - delta).clamp(0, 255) as u8,
545 (c.get_g() as i32 - delta).clamp(0, 255) as u8,
546 (c.get_b() as i32 - delta).clamp(0, 255) as u8,
547 c.get_a(),
548 ))
549 }
550 }
551 _ => Some(Color::rgba(113, 113, 122, if pressed { 42 } else { 26 })),
552 }
553 }
554
555 fn emit_node(
567 tree: &SceneTree,
568 node_id: NodeId,
569 offset_and_opacity: (Point, f32),
570 cull_rect: Rect,
571 active_hover_target: Option<NodeId>,
572 mut portals: Option<&mut Vec<(i32, usize, NodeId)>>,
573 display_list: &mut DisplayList,
574 ) {
575 let (offset_phys, parent_opacity): (Point, f32) = offset_and_opacity;
576 let Some(node) = tree.get_node(node_id) else {
577 return;
578 };
579 let style: Style = node.get_style();
580 let layout: LayoutStyle = style.get_layout();
581 if layout.get_display() == Display::None {
582 return;
583 }
584 if let Some(portal_queue) = portals.as_deref_mut()
585 && (matches!(node.get_kind(), NodeKind::Portal)
586 || layout.get_position() == Position::Fixed)
587 {
588 portal_queue.push((layout.get_z_index(), portal_queue.len(), node_id));
589 return;
590 }
591 let hovered: bool = node.get_interaction().get_hovered();
592 let pressed: bool = node.get_interaction().get_pressed();
593 let mut paint: PaintStyle = if hovered {
594 node.hover_paint().unwrap_or(style.get_paint())
595 } else {
596 style.get_paint()
597 };
598 let is_scrim: bool =
599 matches!(node.get_kind(), NodeKind::Portal) || layout.get_position() == Position::Fixed;
600 let is_interactive: bool = paint.get_cursor() == CursorIcon::Pointer
601 || node
602 .get_handlers()
603 .handler_for(&UiEventKind::Click(Point::default()))
604 .is_some();
605 let is_transparent_canvas: bool =
606 matches!(node.get_kind(), NodeKind::Canvas) && paint.background_color().is_none();
607 let hover_visual: bool = hovered && active_hover_target == Some(node_id);
608 if is_interactive
609 && !is_scrim
610 && !is_transparent_canvas
611 && node.hover_paint().is_none()
612 && (hover_visual || pressed)
613 {
614 paint.set_background(Self::interactive_tint(
615 paint.background_color(),
616 hover_visual,
617 pressed,
618 ));
619 }
620 let opacity: f32 = (parent_opacity * paint.get_opacity()).clamp(0.0, 1.0);
621 if opacity <= 0.0 {
622 return;
623 }
624 let scale: f32 = display_list.get_scale_factor().max(0.5);
625 let inv_scale: f32 = 1.0 / scale;
626 let raw_bounds: Rect = node.get_layout_box().get_bounds();
627 let transform: Transform2D = paint.get_transform();
628 let tx_phys: f32 = (transform.get_translate_x() * scale).round();
629 let ty_phys: f32 = (transform.get_translate_y() * scale).round();
630 let total_off_x: f32 = offset_phys.get_x() + tx_phys;
631 let total_off_y: f32 = offset_phys.get_y() + ty_phys;
632 let raw_x0: f32 = (raw_bounds.get_x() * scale).round();
633 let raw_y0: f32 = (raw_bounds.get_y() * scale).round();
634 let raw_x1: f32 = (raw_bounds.right() * scale).round();
635 let raw_y1: f32 = (raw_bounds.bottom() * scale).round();
636 let static_w_phys: f32 = (raw_x1 - raw_x0).max(0.0);
637 let static_h_phys: f32 = (raw_y1 - raw_y0).max(0.0);
638 let scaled_w_phys: f32 = (static_w_phys * transform.get_scale_x()).round().max(0.0);
639 let scaled_h_phys: f32 = (static_h_phys * transform.get_scale_y()).round().max(0.0);
640 let scale_dx_phys: f32 = ((static_w_phys - scaled_w_phys) * 0.5).round();
641 let scale_dy_phys: f32 = ((static_h_phys - scaled_h_phys) * 0.5).round();
642 let bounds: Rect = Rect::new(
643 (raw_x0 + total_off_x + scale_dx_phys) * inv_scale,
644 (raw_y0 + total_off_y + scale_dy_phys) * inv_scale,
645 scaled_w_phys * inv_scale,
646 scaled_h_phys * inv_scale,
647 );
648 let clipped: bool = matches!(layout.get_overflow(), Overflow::Hidden | Overflow::Scroll);
649 let next_cull: Rect = if clipped {
650 match cull_rect.intersect(bounds) {
651 Some(r) => r,
652 None => return,
653 }
654 } else {
655 cull_rect
656 };
657 let halo: f32 = if paint.box_shadow().is_some() {
658 36.0
659 } else if paint.focus_ring_border().is_some() {
660 8.0
661 } else {
662 2.0
663 };
664 let self_vis_rect: Rect = Rect::new(
665 bounds.get_x() - halo,
666 bounds.get_y() - halo,
667 bounds.get_width() + halo * 2.0,
668 bounds.get_height() + halo * 2.0,
669 );
670 let self_visible: bool = bounds.get_width() > 0.0
671 && bounds.get_height() > 0.0
672 && self_vis_rect.intersect(cull_rect).is_some();
673 if self_visible {
674 if let Some(mut shadow) = paint.box_shadow() {
675 shadow.set_color(shadow.get_color().multiply_alpha(opacity));
676 display_list.push(DrawCommand::BoxShadow {
677 rect: bounds,
678 radius: paint.get_radius(),
679 shadow,
680 });
681 }
682 if let Some(mut gradient) = paint.background_gradient() {
683 gradient.set_start_color(gradient.get_start_color().multiply_alpha(opacity));
684 gradient.set_end_color(gradient.get_end_color().multiply_alpha(opacity));
685 display_list.push(DrawCommand::LinearGradientRect {
686 rect: bounds,
687 gradient,
688 radius: paint.get_radius(),
689 });
690 } else if let Some(bg) = paint.background_color() {
691 let mut border: Border = paint.get_border();
692 border.set_color(border.get_color().multiply_alpha(opacity));
693 display_list.push(DrawCommand::FillRoundedRect {
694 rect: bounds,
695 color: bg.multiply_alpha(opacity),
696 radius: paint.get_radius(),
697 border,
698 });
699 } else if paint.get_border().get_width() > 0.0 {
700 let mut border: Border = paint.get_border();
701 border.set_color(border.get_color().multiply_alpha(opacity));
702 display_list.push(DrawCommand::FillRoundedRect {
703 rect: bounds,
704 color: COLOR_TRANSPARENT,
705 radius: paint.get_radius(),
706 border,
707 });
708 }
709 if node.get_interaction().get_focused()
710 && let Some(mut ring) = paint.focus_ring_border()
711 {
712 ring.set_color(ring.get_color().multiply_alpha(opacity));
713 let ring_w: f32 = ring.get_width();
714 let ring_rect: Rect = Rect::new(
715 bounds.get_x() - ring_w,
716 bounds.get_y() - ring_w,
717 bounds.get_width() + ring_w * 2.0,
718 bounds.get_height() + ring_w * 2.0,
719 );
720 display_list.push(DrawCommand::FillRoundedRect {
721 rect: ring_rect,
722 color: COLOR_TRANSPARENT,
723 radius: CornerRadius::all(paint.get_radius().max_radius() + ring_w),
724 border: ring,
725 });
726 }
727 match node.get_kind() {
728 NodeKind::Text(text) => {
729 let mut text_style: TextStyle = style.get_text();
730 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
731 let content_b: Rect = node.get_layout_box().get_content_bounds();
732 let cx0: f32 = (content_b.get_x() * scale).round();
733 let cy0: f32 = (content_b.get_y() * scale).round();
734 let shifted_content: Rect = Rect::new(
735 (cx0 + total_off_x) * inv_scale,
736 (cy0 + total_off_y) * inv_scale,
737 content_b.get_width(),
738 content_b.get_height(),
739 );
740 display_list.push(DrawCommand::TextRun {
741 rect: shifted_content,
742 text: text.clone(),
743 style: text_style,
744 });
745 }
746 NodeKind::Input(input_state) => {
747 let mut text_style: TextStyle = style.get_text();
748 let rendered_text: String = if input_state.get_value().is_empty() {
749 text_style.set_color(text_style.get_color().multiply_alpha(opacity * 0.55));
750 input_state.get_placeholder().clone()
751 } else if input_state.get_password() {
752 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
753 "*".repeat(input_state.get_value().chars().count())
754 } else {
755 text_style.set_color(text_style.get_color().multiply_alpha(opacity));
756 input_state.get_value().clone()
757 };
758 let content_b: Rect = node.get_layout_box().get_content_bounds();
759 let cx0: f32 = (content_b.get_x() * scale).round();
760 let cy0: f32 = (content_b.get_y() * scale).round();
761 let shifted_content: Rect = Rect::new(
762 (cx0 + total_off_x) * inv_scale,
763 (cy0 + total_off_y) * inv_scale,
764 content_b.get_width(),
765 content_b.get_height(),
766 );
767 let line_h: f32 =
768 (text_style.get_font_size() * text_style.get_line_height()).max(1.0);
769 let text_rect: Rect = if input_state.get_multiline() {
770 shifted_content
771 } else {
772 text_style.set_overflow(TextOverflow::Clip);
773 let v_off_phys: f32 =
774 (((shifted_content.get_height() - line_h) * 0.5).max(0.0) * scale)
775 .round();
776 let text_y: f32 = (cy0 + total_off_y + v_off_phys) * inv_scale;
777 Rect::new(
778 shifted_content.get_x(),
779 text_y,
780 shifted_content.get_width(),
781 line_h,
782 )
783 };
784 display_list.push(DrawCommand::PushClip(shifted_content));
785 display_list.push(DrawCommand::TextRun {
786 rect: text_rect,
787 text: rendered_text.clone(),
788 style: text_style,
789 });
790 if node.get_interaction().get_focused() && tree.get_caret_visible() {
791 let caret_h: f32 = (text_style.get_font_size() * 1.15)
792 .min(shifted_content.get_height().max(12.0));
793 let caret_text: String = if input_state.get_value().is_empty() {
794 String::new()
795 } else {
796 rendered_text
797 };
798 display_list.push(DrawCommand::TextCaret {
799 text_rect,
800 content_rect: shifted_content,
801 text: caret_text,
802 style: text_style,
803 caret_height: caret_h,
804 color: style.get_text().get_color().multiply_alpha(opacity),
805 multiline: input_state.get_multiline(),
806 });
807 }
808 display_list.push(DrawCommand::PopClip);
809 }
810 NodeKind::Image(buffer) => {
811 display_list.push(DrawCommand::Image {
812 rect: bounds,
813 buffer: buffer.clone(),
814 radius: paint.get_radius(),
815 });
816 }
817 NodeKind::Canvas => {
818 if let Some(callback) = node.canvas_callback()
819 && let Ok(mut func) = callback.try_borrow_mut()
820 {
821 let canvas_bw: f32 = paint.get_border().get_width();
822 let canvas_clip_rect: Rect = if canvas_bw > 0.0 {
823 Rect::new(
824 bounds.get_x() + canvas_bw,
825 bounds.get_y() + canvas_bw,
826 (bounds.get_width() - canvas_bw * 2.0).max(0.0),
827 (bounds.get_height() - canvas_bw * 2.0).max(0.0),
828 )
829 } else {
830 bounds
831 };
832 let canvas_max_r: f32 = (bounds.get_width() * 0.5)
833 .min(bounds.get_height() * 0.5)
834 .max(0.0);
835 let canvas_raw_r: CornerRadius = paint.get_radius();
836 let canvas_inner_r: CornerRadius = CornerRadius::new(
837 (canvas_raw_r.get_top_left().min(canvas_max_r) - canvas_bw).max(0.0),
838 (canvas_raw_r.get_top_right().min(canvas_max_r) - canvas_bw).max(0.0),
839 (canvas_raw_r.get_bottom_right().min(canvas_max_r) - canvas_bw)
840 .max(0.0),
841 (canvas_raw_r.get_bottom_left().min(canvas_max_r) - canvas_bw).max(0.0),
842 );
843 if canvas_inner_r.max_radius() > 0.25 {
844 display_list.push(DrawCommand::PushRoundedClip {
845 rect: canvas_clip_rect,
846 radius: canvas_inner_r,
847 });
848 } else {
849 display_list.push(DrawCommand::PushClip(canvas_clip_rect));
850 }
851 func(bounds, display_list);
852 display_list.push(DrawCommand::PopClip);
853 }
854 }
855 NodeKind::View | NodeKind::ScrollView | NodeKind::Portal | NodeKind::Fragment => {}
856 }
857 }
858 if clipped {
859 let bw: f32 = paint.get_border().get_width();
860 let clip_rect: Rect = if bw > 0.0 {
861 Rect::new(
862 bounds.get_x() + bw,
863 bounds.get_y() + bw,
864 (bounds.get_width() - bw * 2.0).max(0.0),
865 (bounds.get_height() - bw * 2.0).max(0.0),
866 )
867 } else {
868 bounds
869 };
870 let max_r: f32 = (bounds.get_width() * 0.5)
871 .min(bounds.get_height() * 0.5)
872 .max(0.0);
873 let raw_r: CornerRadius = paint.get_radius();
874 let inner_r: CornerRadius = CornerRadius::new(
875 (raw_r.get_top_left().min(max_r) - bw).max(0.0),
876 (raw_r.get_top_right().min(max_r) - bw).max(0.0),
877 (raw_r.get_bottom_right().min(max_r) - bw).max(0.0),
878 (raw_r.get_bottom_left().min(max_r) - bw).max(0.0),
879 );
880 if inner_r.max_radius() > 0.25 {
881 display_list.push(DrawCommand::PushRoundedClip {
882 rect: clip_rect,
883 radius: inner_r,
884 });
885 } else {
886 display_list.push(DrawCommand::PushClip(clip_rect));
887 }
888 }
889 let raw_node_scroll: Point = node.get_layout_box().get_scroll_offset();
890 let scroll_x_phys: f32 = (raw_node_scroll.get_x() * scale).round();
891 let scroll_y_phys: f32 = (raw_node_scroll.get_y() * scale).round();
892 let child_offset_phys: Point =
893 Point::new(total_off_x - scroll_x_phys, total_off_y - scroll_y_phys);
894 let raw_children: &[NodeId] = node.get_children().as_slice();
895 let needs_z_sort: bool = raw_children.len() > 1
896 && raw_children.iter().copied().any(|cid: NodeId| {
897 tree.get_node(cid)
898 .map(|c: &SceneNode| c.get_style().get_layout().get_z_index() != 0)
899 .unwrap_or(false)
900 });
901 if !needs_z_sort {
902 for child_id in raw_children.iter().copied() {
903 Self::emit_node(
904 tree,
905 child_id,
906 (child_offset_phys, opacity),
907 next_cull,
908 active_hover_target,
909 portals.as_deref_mut(),
910 display_list,
911 );
912 }
913 } else {
914 let mut sorted_children: Vec<(i32, usize, NodeId)> = raw_children
915 .iter()
916 .copied()
917 .enumerate()
918 .filter_map(|(idx, cid): (usize, NodeId)| {
919 tree.get_node(cid)
920 .map(|c: &SceneNode| (c.get_style().get_layout().get_z_index(), idx, cid))
921 })
922 .collect();
923 sorted_children.sort_by(|a: &(i32, usize, NodeId), b: &(i32, usize, NodeId)| {
924 a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1))
925 });
926 for (_, _, child_id) in sorted_children.into_iter() {
927 Self::emit_node(
928 tree,
929 child_id,
930 (child_offset_phys, opacity),
931 next_cull,
932 active_hover_target,
933 portals.as_deref_mut(),
934 display_list,
935 );
936 }
937 }
938 let vp: Size = display_list.get_viewport();
939 let show_scrollbar: bool = vp.get_width().min(vp.get_height()) >= 540.0;
940 if show_scrollbar && layout.get_overflow() == Overflow::Scroll && bounds.get_height() > 24.0
941 {
942 let lbox: LayoutBox = node.get_layout_box();
943 let border_w: f32 = paint.get_border().get_width();
944 let inner_x: f32 = bounds.get_x() + border_w;
945 let inner_y: f32 = bounds.get_y() + border_w;
946 let inner_w: f32 = (bounds.get_width() - border_w * 2.0).max(1.0);
947 let inner_h: f32 = (bounds.get_height() - border_w * 2.0).max(1.0);
948 let view_h: f32 = lbox.get_content_bounds().get_height().max(1.0);
949 let (scroll_y, total_h): (f32, f32) = match node.virtual_scroll_metrics() {
950 Some((vy, vh)) => (vy, vh),
951 None => (
952 lbox.get_scroll_offset().get_y(),
953 lbox.get_content_size().get_height(),
954 ),
955 };
956 let max_scroll_y: f32 = (total_h - view_h).max(0.0);
957 if max_scroll_y > 1.0 {
958 let track_margin: f32 = 3.0;
959 let track_w: f32 = if hovered { 6.0 } else { 4.0 };
960 let track_x: f32 = (inner_x + inner_w - track_w - 2.0).max(inner_x);
961 let track_y: f32 = inner_y + track_margin;
962 let track_h: f32 = (inner_h - track_margin * 2.0).max(16.0);
963 let ratio: f32 = (view_h / total_h.max(view_h)).clamp(0.0, 1.0);
964 let min_thumb_h: f32 = 20.0_f32.min(track_h);
965 let thumb_h: f32 = (track_h * ratio).clamp(min_thumb_h, track_h);
966 let scroll_frac: f32 = (scroll_y / max_scroll_y).clamp(0.0, 1.0);
967 let thumb_y: f32 = track_y + (track_h - thumb_h) * scroll_frac;
968 let track_x_snapped: f32 = (track_x * scale).round() * inv_scale;
969 let track_y_snapped: f32 = (track_y * scale).round() * inv_scale;
970 let track_w_snapped: f32 = (track_w * scale).round().max(1.0) * inv_scale;
971 let track_h_snapped: f32 = (track_h * scale).round().max(1.0) * inv_scale;
972 let thumb_y_snapped: f32 = (thumb_y * scale).round() * inv_scale;
973 let thumb_h_snapped: f32 = (thumb_h * scale).round().max(1.0) * inv_scale;
974 if hovered {
975 display_list.push(DrawCommand::FillRoundedRect {
976 rect: Rect::new(
977 track_x_snapped,
978 track_y_snapped,
979 track_w_snapped,
980 track_h_snapped,
981 ),
982 color: Color::rgba(113, 113, 122, 24).multiply_alpha(opacity),
983 radius: CornerRadius::all(3.0),
984 border: Border::default(),
985 });
986 }
987 let thumb_alpha: u8 = if hovered { 180 } else { 112 };
988 display_list.push(DrawCommand::FillRoundedRect {
989 rect: Rect::new(
990 track_x_snapped,
991 thumb_y_snapped,
992 track_w_snapped,
993 thumb_h_snapped,
994 ),
995 color: Color::rgba(113, 113, 122, thumb_alpha).multiply_alpha(opacity),
996 radius: CornerRadius::all(track_w * 0.5),
997 border: Border::default(),
998 });
999 }
1000 }
1001 if clipped {
1002 display_list.push(DrawCommand::PopClip);
1003 }
1004 }
1005}