diff --git a/src/Avalonia.Controls/Grid.cs b/src/Avalonia.Controls/Grid.cs index 2e15b07463..a93a2324b2 100644 --- a/src/Avalonia.Controls/Grid.cs +++ b/src/Avalonia.Controls/Grid.cs @@ -1335,8 +1335,9 @@ namespace Avalonia.Controls sizeToDistribute -= newMinSize; } - // sanity check: requested size must all be distributed - Debug.Assert(MathUtilities.IsZero(sizeToDistribute)); + // sanity check: requested size must all be distributed, up to a rounding residual + // relative to the requested size + Debug.Assert(MathUtilities.AreClose(requestedSize - sizeToDistribute, requestedSize)); } else if (requestedSize <= rangeMaxSize) { @@ -1379,8 +1380,9 @@ namespace Avalonia.Controls sizeToDistribute -= (tempDefinition.MinSize - preferredSize); } - // sanity check: requested size must all be distributed - Debug.Assert(MathUtilities.IsZero(sizeToDistribute)); + // sanity check: requested size must all be distributed, up to a rounding residual + // relative to the requested size + Debug.Assert(MathUtilities.AreClose(requestedSize - sizeToDistribute, requestedSize)); } else { diff --git a/tests/Avalonia.Controls.UnitTests/GridTests.cs b/tests/Avalonia.Controls.UnitTests/GridTests.cs index 3a9a8cdaee..ea685aa34c 100644 --- a/tests/Avalonia.Controls.UnitTests/GridTests.cs +++ b/tests/Avalonia.Controls.UnitTests/GridTests.cs @@ -2121,6 +2121,32 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(new Rect(0, 0, 60, 100), target.Children[0].Bounds); } + [Fact] + public void Grid_Controls_With_Spacing_With_Span_Allows_A_Rounding_Residual() + { + // Distributing the spanned height into the three star rows leaves a residual of + // about 2.8e-14, which failed the Debug sanity check in EnsureMinSizeInDefinitionRange. + var target = new Grid + { + RowSpacing = 12, + RowDefinitions = RowDefinitions.Parse("*,*,*,*,*,*,*"), + Children = + { + new Border + { + Height = 540.8571428571429, + [Grid.RowProperty] = 4, + [Grid.RowSpanProperty] = 3 + } + }, + }; + + target.Measure(new Size(1000, 1278)); + + // The child's height plus the four spacings outside its span, layout rounded + Assert.Equal(589, target.DesiredSize.Height); + } + [Fact] public void Grid_Controls_With_Spacing_With_Span_And_SharedSize() {