Skip to content

Commit 6af2b2e

Browse files
committed
Fix Window.MeasureOverride measuring with the old ClientSize (#18338)
1 parent c1b1a1c commit 6af2b2e

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/Avalonia.Controls/Window.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -995,8 +995,13 @@ protected override Size MeasureOverride(Size availableSize)
995995
{
996996
var sizeToContent = SizeToContent;
997997
var clientSize = ClientSize;
998-
var constraint = clientSize;
999998
var maxAutoSize = PlatformImpl?.MaxAutoSizeHint ?? Size.Infinity;
999+
var useAutoWidth = sizeToContent.HasAllFlags(SizeToContent.Width);
1000+
var useAutoHeight = sizeToContent.HasAllFlags(SizeToContent.Height);
1001+
1002+
var constraint = new Size(
1003+
useAutoWidth || double.IsInfinity(availableSize.Width) ? clientSize.Width : availableSize.Width,
1004+
useAutoHeight || double.IsInfinity(availableSize.Height) ? clientSize.Height : availableSize.Height);
10001005

10011006
if (MaxWidth > 0 && MaxWidth < maxAutoSize.Width)
10021007
{
@@ -1007,19 +1012,19 @@ protected override Size MeasureOverride(Size availableSize)
10071012
maxAutoSize = maxAutoSize.WithHeight(MaxHeight);
10081013
}
10091014

1010-
if (sizeToContent.HasAllFlags(SizeToContent.Width))
1015+
if (useAutoWidth)
10111016
{
10121017
constraint = constraint.WithWidth(maxAutoSize.Width);
10131018
}
10141019

1015-
if (sizeToContent.HasAllFlags(SizeToContent.Height))
1020+
if (useAutoHeight)
10161021
{
10171022
constraint = constraint.WithHeight(maxAutoSize.Height);
10181023
}
10191024

10201025
var result = base.MeasureOverride(constraint);
10211026

1022-
if (!sizeToContent.HasAllFlags(SizeToContent.Width))
1027+
if (!useAutoWidth)
10231028
{
10241029
if (!double.IsInfinity(availableSize.Width))
10251030
{
@@ -1031,7 +1036,7 @@ protected override Size MeasureOverride(Size availableSize)
10311036
}
10321037
}
10331038

1034-
if (!sizeToContent.HasAllFlags(SizeToContent.Height))
1039+
if (!useAutoHeight)
10351040
{
10361041
if (!double.IsInfinity(availableSize.Height))
10371042
{

tests/Avalonia.Controls.UnitTests/WindowTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Avalonia.Platform;
66
using Avalonia.Rendering;
77
using Avalonia.Rendering.Composition;
8+
using Avalonia.Threading;
89
using Avalonia.UnitTests;
910
using Moq;
1011
using Xunit;
@@ -688,10 +689,23 @@ public void Child_Should_Be_Measured_With_Width_And_Height_If_SizeToContent_Is_M
688689
Content = child
689690
};
690691

692+
// Verify that the child is initially measured with our Width/Height.
691693
Show(target);
692694

693695
Assert.Equal(1, child.MeasureSizes.Count);
694696
Assert.Equal(new Size(100, 50), child.MeasureSizes[0]);
697+
698+
// Now change the bounds: verify that we are using the new Width/Height, and not the old ClientSize.
699+
child.MeasureSizes.Clear();
700+
child.InvalidateMeasure();
701+
702+
target.Width = 120;
703+
target.Height = 70;
704+
705+
Dispatcher.UIThread.RunJobs();
706+
707+
Assert.Equal(1, child.MeasureSizes.Count);
708+
Assert.Equal(new Size(120, 70), child.MeasureSizes[0]);
695709
}
696710
}
697711

0 commit comments

Comments
 (0)