Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement indexed sort for visual children. #6271

Merged
merged 4 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,25 @@ private static void Update(DrawingContext context, Scene scene, VisualNode node,
}
else if (visualChildren.Count > 1)
{
var sortedChildren = new IVisual[visualChildren.Count];
visualChildren.CopyTo(sortedChildren, 0);
var count = visualChildren.Count;
var sortedChildren = new (IVisual visual, int index)[count];

Array.Sort(sortedChildren, ZIndexComparer.ComparisonInstance);
for (var i = 0; i < count; i++)
{
sortedChildren[i] = (visualChildren[i], i);
}

// Regular Array.Sort is unstable, we need to provide indices as well to avoid reshuffling elements.
Array.Sort(sortedChildren, (lhs, rhs) =>
{
var result = ZIndexComparer.Instance.Compare(lhs.visual, rhs.visual);

return result == 0 ? lhs.index.CompareTo(rhs.index) : result;
});

foreach (var child in sortedChildren)
{
var childNode = GetOrCreateChildNode(scene, child, node);
var childNode = GetOrCreateChildNode(scene, child.Item1, node);
Update(context, scene, (VisualNode)childNode, clip, forceRecurse);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,34 @@ public void Should_Respect_ZIndex()
}
}

[Fact]
public void Should_Respect_Uniform_ZIndex()
{
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
{
Panel panel;

var tree = new TestRoot
{
Child = panel = new Panel()
};

for (var i = 0; i < 128; i++)
{
panel.Children.Add(new Border());
}

var result = new Scene(tree);
var sceneBuilder = new SceneBuilder();
sceneBuilder.UpdateAll(result);

var panelNode = result.FindNode(tree.Child);
var expected = panel.Children.ToArray();
var actual = panelNode.Children.OfType<IVisualNode>().Select(x => x.Visual).ToArray();
Assert.Equal(expected, actual);
}
}

[Fact]
public void ClipBounds_Should_Be_In_Global_Coordinates()
{
Expand Down