Skip to content

Commit

Permalink
Add retained paint_children vec
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Nov 29, 2024
1 parent ed24ed5 commit d92fde1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/blitz-dom/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,8 @@ impl Document {
doc.nodes[child_id].layout_parent.set(Some(node_id));
}

*doc.nodes[node_id].layout_children.borrow_mut() = Some(layout_children);
*doc.nodes[node_id].layout_children.borrow_mut() = Some(layout_children.clone());
*doc.nodes[node_id].paint_children.borrow_mut() = Some(layout_children);
// }
}
}
Expand Down
11 changes: 10 additions & 1 deletion packages/blitz-dom/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub struct Node {
pub layout_parent: Cell<Option<usize>>,
/// A separate child list that includes anonymous collections of inline elements
pub layout_children: RefCell<Option<Vec<usize>>>,
/// The same as layout_children, but sorted by z-index
pub paint_children: RefCell<Option<Vec<usize>>>,

/// Node type (Element, TextNode, etc) specific data
pub raw_dom_data: NodeData,
Expand Down Expand Up @@ -101,6 +103,7 @@ impl Node {
children: vec![],
layout_parent: Cell::new(None),
layout_children: RefCell::new(None),
paint_children: RefCell::new(None),

raw_dom_data: data,
stylo_element_data: Default::default(),
Expand Down Expand Up @@ -962,6 +965,12 @@ impl Node {
.unwrap_or(0)
}

pub fn z_index(&self) -> i32 {
self.primary_styles()
.map(|s| s.clone_z_index().integer_or(0))
.unwrap_or(0)
}

/// Takes an (x, y) position (relative to the *parent's* top-left corner) and returns:
/// - None if the position is outside of this node's bounds
/// - Some(HitResult) if the position is within the node but doesn't match any children
Expand All @@ -984,7 +993,7 @@ impl Node {
}

// Call `.hit()` on each child in turn. If any return `Some` then return that value. Else return `Some(self.id).
self.layout_children
self.paint_children
.borrow()
.iter()
.rev()
Expand Down
12 changes: 12 additions & 0 deletions packages/blitz-dom/src/stylo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ impl crate::document::Document {

// Put children back
*self.nodes[node_id].layout_children.borrow_mut() = Some(children);

// Sort paint_children in place
self.nodes[node_id]
.paint_children
.borrow_mut()
.as_mut()
.unwrap()
.sort_by(|left, right| {
let left_node = self.nodes.get(*left).unwrap();
let right_node = self.nodes.get(*right).unwrap();
left_node.z_index().cmp(&right_node.z_index())
})
}
}

Expand Down
18 changes: 5 additions & 13 deletions packages/blitz-renderer-vello/src/renderer/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,19 +742,11 @@ impl ElementCx<'_> {
}
}
fn draw_children(&self, scene: &mut Scene) {
// Sort children by z-index
let mut children = self.node.layout_children.borrow().as_ref().unwrap().clone();
children.sort_by_key(|id| {
self.dom
.get_node(*id)
.unwrap()
.primary_styles()
.map(|s| s.clone_z_index().integer_or(0))
.unwrap_or(0)
});

for child_id in children {
self.render_node(scene, child_id, self.pos);
let paint_children = self.node.paint_children.borrow();
if let Some(children) = &*paint_children {
for child_id in children {
self.render_node(scene, *child_id, self.pos);
}
}
}

Expand Down

0 comments on commit d92fde1

Please sign in to comment.