From be553738112175b1d291650a87fb32ac8fc7e2b5 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Tue, 7 Jul 2020 15:52:30 -0300 Subject: [PATCH] add some unit tests and tidy relative panel --- src/Avalonia.Controls/RelativePanel.cs | 33 +++++++++++++++++-- .../RelativePanelTests.cs | 32 ++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/Avalonia.Controls.UnitTests/RelativePanelTests.cs diff --git a/src/Avalonia.Controls/RelativePanel.cs b/src/Avalonia.Controls/RelativePanel.cs index cf7fa212fd..73f668bbe9 100644 --- a/src/Avalonia.Controls/RelativePanel.cs +++ b/src/Avalonia.Controls/RelativePanel.cs @@ -60,12 +60,26 @@ namespace Avalonia.Controls { child.Measure(availableSize); } + + Rect bounds = new Rect(); + foreach (var item in CalculateLocations(availableSize)) { if (item.Item2.Size.Width < item.Item1.DesiredSize.Width || item.Item2.Size.Height < item.Item1.DesiredSize.Height) item.Item1.Measure(item.Item2.Size); + + if(item.Item2.Right > bounds.Width) + { + bounds = bounds.WithWidth(item.Item2.Right); + } + + if(item.Item2.Bottom > bounds.Height) + { + bounds = bounds.WithHeight(item.Item2.Bottom); + } } - return base.MeasureOverride(availableSize); + + return bounds.Size; } /// @@ -79,9 +93,24 @@ namespace Avalonia.Controls /// The actual size used. protected override Size ArrangeOverride(Size finalSize) { + Rect bounds = new Rect(); + foreach (var item in CalculateLocations(finalSize)) + { item.Item1.Arrange(item.Item2); - return finalSize; + + if (item.Item2.Right > bounds.Width) + { + bounds = bounds.WithWidth(item.Item2.Right); + } + + if (item.Item2.Bottom > bounds.Height) + { + bounds = bounds.WithHeight(item.Item2.Bottom); + } + } + + return bounds.Size; } private IEnumerable> CalculateLocations(Size finalSize) diff --git a/tests/Avalonia.Controls.UnitTests/RelativePanelTests.cs b/tests/Avalonia.Controls.UnitTests/RelativePanelTests.cs new file mode 100644 index 0000000000..7a2036687e --- /dev/null +++ b/tests/Avalonia.Controls.UnitTests/RelativePanelTests.cs @@ -0,0 +1,32 @@ +using Avalonia.Controls.Shapes; +using Xunit; + +namespace Avalonia.Controls.UnitTests +{ + public class RelativePanelTests + { + [Fact] + public void Lays_Out_1_Child_Below_the_other() + { + var rect1 = new Rectangle { Height = 20, Width = 20 }; + var rect2 = new Rectangle { Height = 20, Width = 20 }; + + var target = new RelativePanel + { + Children = + { + rect1, rect2 + } + }; + + RelativePanel.SetAlignLeftWithPanel(rect1 , true); + RelativePanel.SetBelow(rect2, rect1); + target.Measure(new Size(400, 400)); + target.Arrange(new Rect(target.DesiredSize)); + + Assert.Equal(new Size(20, 40), target.Bounds.Size); + Assert.Equal(new Rect(0, 0, 20, 20), target.Children[0].Bounds); + Assert.Equal(new Rect(0, 20, 20, 20), target.Children[1].Bounds); + } + } +}