fix(dock): encapsulate Children collection and polish tests

This commit is contained in:
2026-03-28 12:45:55 +09:00
parent 3ea4260405
commit 87217337b7
2 changed files with 8 additions and 7 deletions

View File

@@ -15,16 +15,19 @@ public partial class DockGroupNode : DockNode
[ObservableProperty] [ObservableProperty]
public partial Orientation Orientation { get; set; } public partial Orientation Orientation { get; set; }
private readonly ObservableCollection<DockNode> _children = new();
/// <summary> /// <summary>
/// Gets the collection of child nodes. /// Gets the collection of child nodes.
/// </summary> /// </summary>
public ObservableCollection<DockNode> Children { get; } = new(); public ReadOnlyObservableCollection<DockNode> Children { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DockGroupNode"/> class. /// Initializes a new instance of the <see cref="DockGroupNode"/> class.
/// </summary> /// </summary>
public DockGroupNode() public DockGroupNode()
{ {
Children = new ReadOnlyObservableCollection<DockNode>(_children);
Orientation = Orientation.Horizontal; Orientation = Orientation.Horizontal;
} }
@@ -35,7 +38,6 @@ public partial class DockGroupNode : DockNode
/// <exception cref="ArgumentNullException">Thrown if node is null.</exception> /// <exception cref="ArgumentNullException">Thrown if node is null.</exception>
/// <exception cref="InvalidOperationException">Thrown if adding the node would create a cycle or if adding self.</exception> /// <exception cref="InvalidOperationException">Thrown if adding the node would create a cycle or if adding self.</exception>
public void AddChild(DockNode node) public void AddChild(DockNode node)
{ {
ArgumentNullException.ThrowIfNull(node); ArgumentNullException.ThrowIfNull(node);
@@ -44,7 +46,7 @@ public partial class DockGroupNode : DockNode
throw new InvalidOperationException("Cannot add a node to itself."); throw new InvalidOperationException("Cannot add a node to itself.");
} }
if (Children.Contains(node)) if (_children.Contains(node))
{ {
return; return;
} }
@@ -66,7 +68,7 @@ public partial class DockGroupNode : DockNode
} }
node.Parent = this; node.Parent = this;
Children.Add(node); _children.Add(node);
} }
/// <summary> /// <summary>
@@ -75,7 +77,7 @@ public partial class DockGroupNode : DockNode
/// <param name="node">The node to remove.</param> /// <param name="node">The node to remove.</param>
public void RemoveChild(DockNode node) public void RemoveChild(DockNode node)
{ {
if (Children.Remove(node)) if (_children.Remove(node))
{ {
node.Parent = null; node.Parent = null;
} }

View File

@@ -1,5 +1,4 @@
using Ghost.Editor.Core.Controls.Internal.Docking; using Ghost.Editor.Core.Controls.Internal.Docking;
using Microsoft.UI.Xaml.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Ghost.UnitTest; namespace Ghost.UnitTest;
@@ -50,7 +49,7 @@ public class DockingModelTest
{ {
thrown = true; thrown = true;
} }
Assert.IsTrue(thrown); Assert.IsTrue(thrown, "Should have thrown InvalidOperationException due to cycle.");
} }
[TestMethod] [TestMethod]