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

Make UniformGrid account for layout rounding. #17700

Closed
wants to merge 2 commits into from
Closed
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
42 changes: 30 additions & 12 deletions src/Avalonia.Controls/Primitives/UniformGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,45 @@ protected override Size MeasureOverride(Size availableSize)

protected override Size ArrangeOverride(Size finalSize)
{
var x = FirstColumn;
var y = 0;

var width = finalSize.Width / _columns;
var height = finalSize.Height / _rows;
var columnIndex = FirstColumn;
var columnWidth = finalSize.Width / _columns;
var rowIndex = 0;
var rowHeight = finalSize.Height / _rows;
var x = 0.0;
var y = 0.0;
var nextY = 0.0;

foreach (var child in Children)
{
if (!child.IsVisible)
if (child.IsVisible)
{
continue;
}
// Layout scaling may cause the child to take a different size than the one
// requested. Layout each each child with it's top-left aligned against the
// previous column/row bounds and request the bottom-right to be placed in
// the ideal position.
var topLeft = new Point(x, y);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't rely on the previous child's bounds here: it might effectively be completely different (try any child with non-stretch alignment) from the cell's size, breaking the uniformity.

Example:

<UniformGrid Columns="2">
  <Border Width="100" Height="100" Background="Blue" />
  <Border Width="100" Height="100" Background="Gray" />
  <Border Width="50" Height="100" Background="Blue" HorizontalAlignment="Left" />
  <Border Width="50" Height="100" Background="Gray" HorizontalAlignment="Left" />
</UniformGrid>

It might even be out of bounds of the UniformGrid (if the children are larger than their container), causing an invalid rectangle to be passed to Arrange, and an InvalidOperationException.

Example:

<UniformGrid Width="50" Height="50">
  <Border Width="100" Height="100" />
  <Border Width="100" Height="100" />
</UniformGrid>

var bottomRight = new Point((columnIndex + 1) * columnWidth, (rowIndex + 1) * rowHeight);

child.Arrange(new Rect(topLeft, bottomRight));

child.Arrange(new Rect(x * width, y * height, width, height));
x = child.Bounds.Right;
nextY = Math.Max(nextY, child.Bounds.Bottom);
}
else
{
x += columnWidth;
nextY = Math.Max(nextY, rowHeight);
}

x++;
columnIndex++;

if (x >= _columns)
if (columnIndex >= _columns)
{
x = 0;
y++;
y = nextY;
nextY = 0;
columnIndex = 0;
rowIndex++;
}
}

Expand Down
71 changes: 71 additions & 0 deletions tests/Avalonia.Controls.UnitTests/Primitives/UniformGridTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Controls.Primitives;
using Avalonia.UnitTests;
using Xunit;

namespace Avalonia.Controls.UnitTests.Primitives
Expand Down Expand Up @@ -140,5 +141,75 @@ public void Not_Visible_Children_Are_Ignored()
// 2 * 2 grid
Assert.Equal(new Size(2 * 50, 2 * 70), target.Bounds.Size);
}

[Fact]
public void Children_Do_Not_Overlap_With_125_Percent_Scaling_1()
{
// Issue #17699
var target = new UniformGrid
{
Columns = 2,
Children =
{
new Border(),
new Border(),
new Border(),
new Border(),
}
};

var root = new TestRoot
{
LayoutScaling = 1.25,
Child = new Border
{
Width = 100,
Height = 100,
Child = target,
}
};

root.ExecuteInitialLayoutPass();

Assert.Equal(new(0, 0, 50.4, 50.4), target.Children[0].Bounds);
Assert.Equal(new(50.4, 0, 49.6, 50.4), target.Children[1].Bounds);
Assert.Equal(new(0, 50.4, 50.4, 49.6), target.Children[2].Bounds);
Assert.Equal(new(50.4, 50.4, 49.6, 49.6), target.Children[3].Bounds);
}

[Fact]
public void Children_Do_Not_Overlap_With_125_Percent_Scaling_2()
{
// Issue #17699
var target = new UniformGrid
{
Columns = 4,
Children =
{
new Border(),
new Border(),
new Border(),
new Border(),
}
};

var root = new TestRoot
{
LayoutScaling = 1.25,
Child = new Border
{
Width = 100,
Height = 100,
Child = target,
}
};

root.ExecuteInitialLayoutPass();

Assert.Equal(new(0, 0, 25.6, 100), target.Children[0].Bounds);
Assert.Equal(new(25.6, 0, 24.8, 100), target.Children[1].Bounds);
Assert.Equal(new(50.4, 0, 24.8, 100), target.Children[2].Bounds);
Assert.Equal(new(75.2, 0, 24.8, 100), target.Children[3].Bounds);
}
}
}
2 changes: 1 addition & 1 deletion tests/Avalonia.UnitTests/TestRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public TestRoot(bool useGlobalStyles, Control child)
internal ILayoutManager LayoutManager { get; set; }
ILayoutManager ILayoutRoot.LayoutManager => LayoutManager;

public double RenderScaling => 1;
public double RenderScaling => LayoutScaling;

internal IRenderer Renderer { get; set; }
internal IHitTester HitTester { get; set; }
Expand Down
Loading