Jelajahi Sumber

Merge pull request #1379 from jkoritzinsky/refcount-scene

Make Scene own VisualNodes
Jeremy Koritzinsky 8 tahun lalu
induk
melakukan
c8a8e82441

+ 2 - 0
src/Avalonia.Visuals/Rendering/SceneGraph/IVisualNode.cs

@@ -93,5 +93,7 @@ namespace Avalonia.Rendering.SceneGraph
         /// to hit test children they must be hit tested manually.
         /// </remarks>
         bool HitTest(Point p);
+
+        bool Disposed { get; }
     }
 }

+ 6 - 1
src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs

@@ -98,7 +98,10 @@ namespace Avalonia.Rendering.SceneGraph
 
         public void Dispose()
         {
-            Root.Dispose();
+            foreach (var node in _index.Values)
+            {
+                node.Dispose();
+            }
         }
 
         /// <summary>
@@ -137,6 +140,8 @@ namespace Avalonia.Rendering.SceneGraph
             Contract.Requires<ArgumentNullException>(node != null);
 
             _index.Remove(node.Visual);
+
+            node.Dispose();
         }
 
         private VisualNode Clone(VisualNode source, IVisualNode parent, Dictionary<IVisual, IVisualNode> index)

+ 12 - 10
src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs

@@ -113,6 +113,11 @@ namespace Avalonia.Rendering.SceneGraph
         /// <param name="child">The child to add.</param>
         public void AddChild(IVisualNode child)
         {
+            if (child.Disposed)
+            {
+                throw new ObjectDisposedException("Visual node for {node.Visual}");
+            }
+
             EnsureChildrenCreated();
             _children.Add(child);
         }
@@ -135,7 +140,6 @@ namespace Avalonia.Rendering.SceneGraph
         {
             EnsureChildrenCreated();
             _children.Remove(child);
-            child.Dispose();
         }
 
         /// <summary>
@@ -145,13 +149,13 @@ namespace Avalonia.Rendering.SceneGraph
         /// <param name="node">The child to add.</param>
         public void ReplaceChild(int index, IVisualNode node)
         {
-            EnsureChildrenCreated();
-            var old = _children[index];
-            _children[index] = node;
-            if (node != old)
+            if (node.Disposed)
             {
-                old.Dispose(); 
+                throw new ObjectDisposedException("Visual node for {node.Visual}");
             }
+
+            EnsureChildrenCreated();
+            _children[index] = node;
         }
 
         /// <summary>
@@ -332,13 +336,11 @@ namespace Avalonia.Rendering.SceneGraph
                 _drawOperationsCloned = false;
             }
         }
+
+        public bool Disposed { get; }
         
         public void Dispose()
         {
-            foreach (var child in Children)
-            {
-                child.Dispose();
-            }
             _drawOperationsRefCounter?.Dispose();
         }