Skip to content

Commit

Permalink
TaffyTree::set_children: Remove from previous parent (#749)
Browse files Browse the repository at this point in the history
* TaffyTree::set_children: Remove from previous parent

* added test

---------

Co-authored-by: Nico Burns <[email protected]>
  • Loading branch information
SpecificProtagonist and nicoburns authored Dec 12, 2024
1 parent e20aa21 commit f37efc5
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/tree/taffy_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,12 @@ impl<NodeContext> TaffyTree<NodeContext> {
}

// Build up relation node <-> child
for child in children {
self.parents[(*child).into()] = Some(parent);
for &child in children {
// Remove child from previous parent
if let Some(previous_parent) = self.parents[child.into()] {
self.remove_child(previous_parent, child).unwrap();
}
self.parents[child.into()] = Some(parent);
}

let parent_children = &mut self.children[parent_key];
Expand Down Expand Up @@ -1234,4 +1238,16 @@ mod tests {
assert_eq!(layout.location.x, 10f32);
assert_eq!(layout.location.y, 30f32);
}

#[test]
fn set_children_reparents() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let child = taffy.new_leaf(Style::default()).unwrap();
let old_parent = taffy.new_with_children(Style::default(), &[child]).unwrap();

let new_parent = taffy.new_leaf(Style::default()).unwrap();
taffy.set_children(new_parent, &[child]).unwrap();

assert!(taffy.children(old_parent).unwrap().is_empty());
}
}

0 comments on commit f37efc5

Please sign in to comment.