Browse Source

Make a new base class for both Row/ColumnDefinitions.

Jumar Macato 6 years ago
parent
commit
2b03824811

+ 1 - 26
src/Avalonia.Controls/ColumnDefinitions.cs

@@ -11,7 +11,7 @@ namespace Avalonia.Controls
     /// <summary>
     /// A collection of <see cref="ColumnDefinition"/>s.
     /// </summary>
-    public class ColumnDefinitions : AvaloniaList<ColumnDefinition>
+    public class ColumnDefinitions : DefinitionList<ColumnDefinition>
     {
         /// <summary>
         /// Initializes a new instance of the <see cref="ColumnDefinitions"/> class.
@@ -23,31 +23,6 @@ namespace Avalonia.Controls
             this.TrackItemPropertyChanged(delegate { IsDirty = true; });
         }
 
-        internal bool IsDirty { get; set; } = true;
-        internal Grid Parent { get; set; }
-
-        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
-        {
-            foreach (var nI in this.Select((d, i) => (d, i)))
-                nI.d._parentIndex = nI.i;
-
-            foreach (var nD in e.NewItems?.Cast<DefinitionBase>()
-                            ?? Enumerable.Empty<DefinitionBase>())
-            {
-                nD.Parent = this.Parent;
-                nD.OnEnterParentTree();
-            }
-
-            foreach (var oD in e.OldItems?.Cast<DefinitionBase>()
-                            ?? Enumerable.Empty<DefinitionBase>())
-            {
-                oD.Parent = null;
-                oD.OnExitParentTree();
-            }
-
-            IsDirty = true;
-        }
-
         /// <summary>
         /// Initializes a new instance of the <see cref="ColumnDefinitions"/> class.
         /// </summary>

+ 1 - 1
src/Avalonia.Controls/DefinitionBase.cs

@@ -315,7 +315,7 @@ namespace Avalonia.Controls
             }
             set
             {
-                Debug.Assert(value >= -1 && _parentIndex != value);
+                Debug.Assert(value >= -1);
                 _parentIndex = value;
             }
         }

+ 56 - 0
src/Avalonia.Controls/DefinitionList.cs

@@ -0,0 +1,56 @@
+// Copyright (c) The Avalonia Project. All rights reserved.
+// Licensed under the MIT license. See licence.md file in the project root for full license information.
+
+using System;
+using System.Collections.Specialized;
+using System.Linq;
+using Avalonia.Collections;
+
+namespace Avalonia.Controls
+{
+    public abstract class DefinitionList<T> : AvaloniaList<T> where T : DefinitionBase
+    {
+        internal bool IsDirty = true;
+        private Grid _parent;
+
+        internal Grid Parent
+        {
+            get => _parent;
+            set => SetParent(value);
+        }
+
+
+        private void SetParent(Grid value)
+        {
+            _parent = value;
+
+            foreach (var pair in this.Select((definitions, index) => (definitions, index)))
+            {
+                pair.definitions.Parent = value;
+                pair.definitions.Index = pair.index;
+            }
+        }
+
+        internal void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
+        {
+            foreach (var nI in this.Select((d, i) => (d, i)))
+                nI.d._parentIndex = nI.i;
+
+            foreach (var nD in e.NewItems?.Cast<DefinitionBase>()
+                            ?? Enumerable.Empty<DefinitionBase>())
+            {
+                nD.Parent = this.Parent;
+                nD.OnEnterParentTree();
+            }
+
+            foreach (var oD in e.OldItems?.Cast<DefinitionBase>()
+                            ?? Enumerable.Empty<DefinitionBase>())
+            {
+                oD.Parent = null;
+                oD.OnExitParentTree();
+            }
+
+            IsDirty = true;
+        }
+    }
+}

+ 4 - 4
src/Avalonia.Controls/Grid.cs

@@ -933,7 +933,7 @@ namespace Avalonia.Controls
                 {
                     if (extData.DefinitionsU == null)
                     {
-                        extData.DefinitionsU = new DefinitionBase[] { new ColumnDefinition() };
+                        extData.DefinitionsU = new DefinitionBase[] { new ColumnDefinition() { Parent = this } };
                     }
                 }
                 else
@@ -942,7 +942,7 @@ namespace Avalonia.Controls
                     {
                         //  if column definitions collection is empty
                         //  mockup array with one column
-                        extData.DefinitionsU = new DefinitionBase[] { new ColumnDefinition() };
+                        extData.DefinitionsU = new DefinitionBase[] { new ColumnDefinition() { Parent = this } };
                     }
                     else
                     {
@@ -974,7 +974,7 @@ namespace Avalonia.Controls
                 {
                     if (extData.DefinitionsV == null)
                     {
-                        extData.DefinitionsV = new DefinitionBase[] { new RowDefinition() };
+                        extData.DefinitionsV = new DefinitionBase[] { new RowDefinition() { Parent = this } };
                     }
                 }
                 else
@@ -983,7 +983,7 @@ namespace Avalonia.Controls
                     {
                         //  if row definitions collection is empty
                         //  mockup array with one row
-                        extData.DefinitionsV = new DefinitionBase[] { new RowDefinition() };
+                        extData.DefinitionsV = new DefinitionBase[] { new RowDefinition() { Parent = this } };
                     }
                     else
                     {                       

+ 1 - 27
src/Avalonia.Controls/RowDefinitions.cs

@@ -1,7 +1,6 @@
 // Copyright (c) The Avalonia Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
-using System.Collections.Specialized;
 using System.Linq;
 using Avalonia.Collections;
 
@@ -10,7 +9,7 @@ namespace Avalonia.Controls
     /// <summary>
     /// A collection of <see cref="RowDefinition"/>s.
     /// </summary>
-    public class RowDefinitions : AvaloniaList<RowDefinition>
+    public class RowDefinitions : DefinitionList<RowDefinition>
     {
         /// <summary>
         /// Initializes a new instance of the <see cref="RowDefinitions"/> class.
@@ -22,31 +21,6 @@ namespace Avalonia.Controls
             this.TrackItemPropertyChanged(delegate { IsDirty = true; });
         }
 
-        internal bool IsDirty { get; set; } = true;
-        internal Grid Parent { get; set; }
-
-        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
-        {
-            foreach (var nI in this.Select((d, i) => (d, i)))
-                nI.d._parentIndex = nI.i;
-
-            foreach (var nD in e.NewItems?.Cast<DefinitionBase>()
-                            ?? Enumerable.Empty<DefinitionBase>())
-            {
-                nD.Parent = this.Parent;
-                nD.OnEnterParentTree();
-            }
-
-            foreach (var oD in e.OldItems?.Cast<DefinitionBase>()
-                            ?? Enumerable.Empty<DefinitionBase>())
-            {
-                oD.Parent = null;
-                oD.OnExitParentTree();
-            }
-
-            IsDirty = true;
-        }
-
         /// <summary>
         /// Initializes a new instance of the <see cref="RowDefinitions"/> class.
         /// </summary>