Browse Source

Fix Grid shared scope not being updated when resizing row/column definitions.

Dariusz Komosinski 6 years ago
parent
commit
9c5e688958

+ 3 - 1
src/Avalonia.Controls/ColumnDefinition.cs

@@ -31,7 +31,9 @@ namespace Avalonia.Controls
         /// </summary>
         static ColumnDefinition()
         {
-            AffectsParentMeasure(WidthProperty, MinWidthProperty, MaxWidthProperty);
+            AffectsParentMeasure(MinWidthProperty, MaxWidthProperty);
+
+            WidthProperty.Changed.AddClassHandler<DefinitionBase>(OnUserSizePropertyChanged);
         }
 
         /// <summary>

+ 30 - 3
src/Avalonia.Controls/DefinitionBase.cs

@@ -7,9 +7,6 @@ using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Diagnostics;
-
-using Avalonia;
-using Avalonia.Collections;
 using Avalonia.Utilities;
 
 namespace Avalonia.Controls
@@ -118,6 +115,36 @@ namespace Avalonia.Controls
             }
         }
 
+        /// <remarks>
+        /// This method needs to be internal to be accessable from derived classes.
+        /// </remarks>
+        internal static void OnUserSizePropertyChanged(DefinitionBase definition, AvaloniaPropertyChangedEventArgs e)
+        {
+            if (definition.Parent == null)
+            {
+                return;
+            }
+
+            if (definition._sharedState != null)
+            {
+                definition._sharedState.Invalidate();
+            }
+            else
+            {
+                GridUnitType oldUnitType = ((GridLength)e.OldValue).GridUnitType;
+                GridUnitType newUnitType = ((GridLength)e.NewValue).GridUnitType;
+
+                if (oldUnitType != newUnitType)
+                {
+                    definition.Parent.Invalidate();
+                }
+                else
+                {
+                    definition.Parent.InvalidateMeasure();
+                }
+            }
+        }
+
         /// <summary>
         /// Returns <c>true</c> if this definition is a part of shared group.
         /// </summary>

+ 3 - 1
src/Avalonia.Controls/RowDefinition.cs

@@ -31,7 +31,9 @@ namespace Avalonia.Controls
         /// </summary>
         static RowDefinition()
         {
-            AffectsParentMeasure(HeightProperty, MaxHeightProperty, MinHeightProperty);
+            AffectsParentMeasure(MaxHeightProperty, MinHeightProperty);
+
+            HeightProperty.Changed.AddClassHandler<DefinitionBase>(OnUserSizePropertyChanged);
         }
 
         /// <summary>

+ 35 - 0
tests/Avalonia.Controls.UnitTests/GridTests.cs

@@ -1172,6 +1172,41 @@ namespace Avalonia.Controls.UnitTests
             Assert.Equal(0, grids[0].ColumnDefinitions[0].ActualWidth);
         }
 
+        [Fact]
+        public void Size_Group_Definition_Resizes_Are_Tracked()
+        {
+            var grids = new[] {
+                CreateGrid(("A", new GridLength(5, GridUnitType.Pixel)), (null, new GridLength())),
+                CreateGrid(("A", new GridLength(5, GridUnitType.Pixel)), (null, new GridLength())) };
+            var scope = new Grid();
+            foreach (var xgrids in grids)
+                scope.Children.Add(xgrids);
+
+            var root = new Grid();
+            root.UseLayoutRounding = false;
+            root.SetValue(Grid.IsSharedSizeScopeProperty, true);
+            root.Children.Add(scope);
+
+            root.Measure(new Size(50, 50));
+            root.Arrange(new Rect(new Point(), new Point(50, 50)));
+
+            PrintColumnDefinitions(grids[0]);
+            Assert.Equal(5, grids[0].ColumnDefinitions[0].ActualWidth);
+            Assert.Equal(5, grids[1].ColumnDefinitions[0].ActualWidth);
+
+            grids[0].ColumnDefinitions[0].Width = new GridLength(10, GridUnitType.Pixel);
+
+            foreach (Grid grid in grids)
+            {
+                grid.Measure(new Size(50, 50));
+                grid.Arrange(new Rect(new Point(), new Point(50, 50)));
+            }
+
+            PrintColumnDefinitions(grids[0]);
+            Assert.Equal(10, grids[0].ColumnDefinitions[0].ActualWidth);
+            Assert.Equal(10, grids[1].ColumnDefinitions[0].ActualWidth);
+        }
+
         [Fact]
         public void Collection_Changes_Are_Tracked()
         {