Pārlūkot izejas kodu

add some unit tests and tidy relative panel

Dan Walmsley 5 gadi atpakaļ
vecāks
revīzija
be55373811

+ 31 - 2
src/Avalonia.Controls/RelativePanel.cs

@@ -60,12 +60,26 @@ namespace Avalonia.Controls
             {
             {
                 child.Measure(availableSize);
                 child.Measure(availableSize);
             }
             }
+
+            Rect bounds = new Rect();
+
             foreach (var item in CalculateLocations(availableSize))
             foreach (var item in CalculateLocations(availableSize))
             {
             {
                 if (item.Item2.Size.Width < item.Item1.DesiredSize.Width || item.Item2.Size.Height < item.Item1.DesiredSize.Height)
                 if (item.Item2.Size.Width < item.Item1.DesiredSize.Width || item.Item2.Size.Height < item.Item1.DesiredSize.Height)
                     item.Item1.Measure(item.Item2.Size);
                     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;
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -79,9 +93,24 @@ namespace Avalonia.Controls
         /// <returns>The actual size used.</returns>
         /// <returns>The actual size used.</returns>
         protected override Size ArrangeOverride(Size finalSize)
         protected override Size ArrangeOverride(Size finalSize)
         {
         {
+            Rect bounds = new Rect();
+
             foreach (var item in CalculateLocations(finalSize))
             foreach (var item in CalculateLocations(finalSize))
+            {
                 item.Item1.Arrange(item.Item2);
                 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<Tuple<ILayoutable, Rect>> CalculateLocations(Size finalSize)
         private IEnumerable<Tuple<ILayoutable, Rect>> CalculateLocations(Size finalSize)

+ 32 - 0
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);
+        }
+    }
+}