From b4d7d03afd7eb18e871823f24bf8ee8474bf632d Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 30 Jul 2019 17:04:08 +0200 Subject: [PATCH] Constrain to availableSize in MeasureCore. #2431 erroneously removed the `.Constrain(availableSize)` call in `Layoutable.Measure`. Now that the WPF source is available, I can see i that WPF does in fact constrain measure to availableSize and Grid relies on this. Put constraint back in, undo the changes to the controls changed in #2431 (`StackPanel`, `Image`) and update the expected test results based on cross-checks with WPF in https://github.com/wieslawsoltes/WpfUnitTests/pull/1. --- src/Avalonia.Controls/Image.cs | 2 +- src/Avalonia.Controls/StackPanel.cs | 4 +--- src/Avalonia.Layout/Layoutable.cs | 3 +++ .../StackPanelTests.cs | 16 ++++++++-------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Avalonia.Controls/Image.cs b/src/Avalonia.Controls/Image.cs index fa6f5787be..ff6cd482df 100644 --- a/src/Avalonia.Controls/Image.cs +++ b/src/Avalonia.Controls/Image.cs @@ -96,7 +96,7 @@ namespace Avalonia.Controls } } - return result.Constrain(availableSize); + return result; } /// diff --git a/src/Avalonia.Controls/StackPanel.cs b/src/Avalonia.Controls/StackPanel.cs index f378f47e64..bd3441078d 100644 --- a/src/Avalonia.Controls/StackPanel.cs +++ b/src/Avalonia.Controls/StackPanel.cs @@ -229,9 +229,7 @@ namespace Avalonia.Controls stackDesiredSize = stackDesiredSize.WithHeight(stackDesiredSize.Height - (hasVisibleChild ? spacing : 0)); } - // TODO: In WPF `.Constrain(availableSize)` is not used. - //return stackDesiredSize; - return stackDesiredSize.Constrain(availableSize); + return stackDesiredSize; } /// diff --git a/src/Avalonia.Layout/Layoutable.cs b/src/Avalonia.Layout/Layoutable.cs index 662f48ec44..bd248d6d44 100644 --- a/src/Avalonia.Layout/Layoutable.cs +++ b/src/Avalonia.Layout/Layoutable.cs @@ -534,6 +534,9 @@ namespace Avalonia.Layout height = Math.Min(height, MaxHeight); height = Math.Max(height, MinHeight); + width = Math.Min(width, availableSize.Width); + height = Math.Min(height, availableSize.Height); + if (UseLayoutRounding) { var scale = GetLayoutScale(); diff --git a/tests/Avalonia.Controls.UnitTests/StackPanelTests.cs b/tests/Avalonia.Controls.UnitTests/StackPanelTests.cs index 722ad1c8ee..db113f0569 100644 --- a/tests/Avalonia.Controls.UnitTests/StackPanelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/StackPanelTests.cs @@ -210,13 +210,13 @@ namespace Avalonia.Controls.UnitTests new[] { new Rect(0, 0, 50, 10), - new Rect(0, 10, 150, 10), + new Rect(0, 10, 100, 10), new Rect(25, 20, 50, 10), - new Rect(-25, 30, 150, 10), + new Rect(0, 30, 100, 10), new Rect(50, 40, 50, 10), - new Rect(-50, 50, 150, 10), + new Rect(0, 50, 100, 10), new Rect(0, 60, 100, 10), - new Rect(0, 70, 150, 10), + new Rect(0, 70, 100, 10), }, bounds); } @@ -283,13 +283,13 @@ namespace Avalonia.Controls.UnitTests new[] { new Rect(0, 0, 10, 50), - new Rect(10, 0, 10, 150), + new Rect(10, 0, 10, 100), new Rect(20, 25, 10, 50), - new Rect(30, -25, 10, 150), + new Rect(30, 0, 10, 100), new Rect(40, 50, 10, 50), - new Rect(50, -50, 10, 150), + new Rect(50, 0, 10, 100), new Rect(60, 0, 10, 100), - new Rect(70, 0, 10, 150), + new Rect(70, 0, 10, 100), }, bounds); }