Skip to content

Commit

Permalink
Handle reparenting controls in SceneBuilder.
Browse files Browse the repository at this point in the history
Renamed `VisualNode.SortChildren` -> `UpdateChildren` and make it remove nodes for controls that are no longer children.
  • Loading branch information
grokys committed Sep 17, 2019
1 parent 249e994 commit db8751d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Avalonia.Visuals/Rendering/DeferredRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ private void UpdateScene()
foreach (var visual in _recalculateChildren)
{
var node = scene.FindNode(visual);
((VisualNode)node)?.SortChildren(scene);
((VisualNode)node)?.UpdateChildren(scene);
}

_recalculateChildren.Clear();
Expand Down
15 changes: 12 additions & 3 deletions src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ public void ReplaceDrawOperation(int index, IRef<IDrawOperation> operation)

/// <summary>
/// Sorts the <see cref="Children"/> collection according to the order of the visual's
/// children and their z-index.
/// children and their z-index and removes controls that are no longer children.
/// </summary>
/// <param name="scene">The scene that the node is a part of.</param>
public void SortChildren(Scene scene)
public void UpdateChildren(Scene scene)
{
if (_children == null || _children.Count <= 1)
if (_children == null || _children.Count == 0)
{
return;
}
Expand All @@ -193,9 +193,12 @@ public void SortChildren(Scene scene)
keys.Add(((long)zIndex << 32) + i);
}

var toRemove = _children.ToList();

keys.Sort();
_children.Clear();


foreach (var i in keys)
{
var child = Visual.VisualChildren[(int)(i & 0xffffffff)];
Expand All @@ -204,8 +207,14 @@ public void SortChildren(Scene scene)
if (node != null)
{
_children.Add(node);
toRemove.Remove(node);
}
}

foreach (var node in toRemove)
{
scene.Remove(node);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ public void Should_Update_When_Control_Moved()
moveFromNode = (VisualNode)scene.FindNode(moveFrom);
moveToNode = (VisualNode)scene.FindNode(moveTo);

moveFromNode.SortChildren(scene);
moveToNode.SortChildren(scene);
moveFromNode.UpdateChildren(scene);
moveToNode.UpdateChildren(scene);
sceneBuilder.Update(scene, moveFrom);
sceneBuilder.Update(scene, moveTo);
sceneBuilder.Update(scene, moveMe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void SortChildren_Does_Not_Throw_On_Null_Children()
var node = new VisualNode(Mock.Of<IVisual>(), null);
var scene = new Scene(Mock.Of<IVisual>());

node.SortChildren(scene);
node.UpdateChildren(scene);
}
}
}

0 comments on commit db8751d

Please sign in to comment.