Skip to content

Commit

Permalink
Merge pull request #1379 from jkoritzinsky/refcount-scene
Browse files Browse the repository at this point in the history
Make Scene own VisualNodes
  • Loading branch information
jkoritzinsky committed Feb 18, 2018
2 parents 2203b6c + 0d7f577 commit c8a8e82
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/Avalonia.Visuals/Rendering/SceneGraph/IVisualNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@ public interface IVisualNode : IDisposable
/// to hit test children they must be hit tested manually.
/// </remarks>
bool HitTest(Point p);

bool Disposed { get; }
}
}
7 changes: 6 additions & 1 deletion src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ public Scene CloneScene()

public void Dispose()
{
Root.Dispose();
foreach (var node in _index.Values)
{
node.Dispose();
}
}

/// <summary>
Expand Down Expand Up @@ -137,6 +140,8 @@ public void Remove(IVisualNode node)
Contract.Requires<ArgumentNullException>(node != null);

_index.Remove(node.Visual);

node.Dispose();
}

private VisualNode Clone(VisualNode source, IVisualNode parent, Dictionary<IVisual, IVisualNode> index)
Expand Down
22 changes: 12 additions & 10 deletions src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public double Opacity
/// <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);
}
Expand All @@ -135,7 +140,6 @@ public void RemoveChild(IVisualNode child)
{
EnsureChildrenCreated();
_children.Remove(child);
child.Dispose();
}

/// <summary>
Expand All @@ -145,13 +149,13 @@ public void RemoveChild(IVisualNode child)
/// <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>
Expand Down Expand Up @@ -332,13 +336,11 @@ private void EnsureDrawOperationsCreated()
_drawOperationsCloned = false;
}
}

public bool Disposed { get; }

public void Dispose()
{
foreach (var child in Children)
{
child.Dispose();
}
_drawOperationsRefCounter?.Dispose();
}

Expand Down

0 comments on commit c8a8e82

Please sign in to comment.