Skip to content

Commit 8251d79

Browse files
grokysdanwalmsley
authored andcommitted
Merge pull request AvaloniaUI#8095 from sn4k3/master
Respect Window MaxWidth and MaxHeight when using any SizeToContent to Auto
1 parent d2f3210 commit 8251d79

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Avalonia.Controls/Window.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,15 @@ protected override Size MeasureOverride(Size availableSize)
918918
var constraint = clientSize;
919919
var maxAutoSize = PlatformImpl?.MaxAutoSizeHint ?? Size.Infinity;
920920

921+
if (MaxWidth > 0 && MaxWidth < maxAutoSize.Width)
922+
{
923+
maxAutoSize = maxAutoSize.WithWidth(MaxWidth);
924+
}
925+
if (MaxHeight > 0 && MaxHeight < maxAutoSize.Height)
926+
{
927+
maxAutoSize = maxAutoSize.WithHeight(MaxHeight);
928+
}
929+
921930
if (sizeToContent.HasAllFlags(SizeToContent.Width))
922931
{
923932
constraint = constraint.WithWidth(maxAutoSize.Width);

tests/Avalonia.Controls.UnitTests/WindowTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,27 @@ public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_WidthAn
719719
}
720720
}
721721

722+
[Fact]
723+
public void MaxWidth_And_MaxHeight_Should_Be_Respected_With_SizeToContent_WidthAndHeight()
724+
{
725+
using (UnitTestApplication.Start(TestServices.StyledWindow))
726+
{
727+
var child = new ChildControl();
728+
729+
var target = new Window()
730+
{
731+
SizeToContent = SizeToContent.WidthAndHeight,
732+
MaxWidth = 300,
733+
MaxHeight = 700,
734+
Content = child,
735+
};
736+
737+
Show(target);
738+
739+
Assert.Equal(new[] { new Size(300, 700) }, child.MeasureSizes);
740+
}
741+
}
742+
722743
[Fact]
723744
public void SizeToContent_Should_Not_Be_Lost_On_Show()
724745
{

0 commit comments

Comments
 (0)