fix(editor): improve DockContainer robustness and style consistency
- Added cycle detection in AddChild to prevent tree-cycle bugs - Added defensive null validation to AddChild and RemoveChild - Standardized using directives and exception throwing style
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
namespace Ghost.Editor.View.Controls.Docking;
|
namespace Ghost.Editor.View.Controls.Docking;
|
||||||
@@ -20,8 +21,21 @@ public abstract class DockContainer : DockModule
|
|||||||
|
|
||||||
public void AddChild(DockModule module)
|
public void AddChild(DockModule module)
|
||||||
{
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(module);
|
||||||
|
|
||||||
if (module == this)
|
if (module == this)
|
||||||
throw new System.ArgumentException("Cannot add a container to itself.", nameof(module));
|
throw new ArgumentException("Cannot add a container to itself.", nameof(module));
|
||||||
|
|
||||||
|
if (module is DockContainer container)
|
||||||
|
{
|
||||||
|
var current = Owner;
|
||||||
|
while (current != null)
|
||||||
|
{
|
||||||
|
if (current == container)
|
||||||
|
throw new ArgumentException("Cannot add a container that is an ancestor of this container.", nameof(module));
|
||||||
|
current = current.Owner;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (_children.Contains(module))
|
if (_children.Contains(module))
|
||||||
return;
|
return;
|
||||||
@@ -33,6 +47,8 @@ public abstract class DockContainer : DockModule
|
|||||||
|
|
||||||
public void RemoveChild(DockModule module)
|
public void RemoveChild(DockModule module)
|
||||||
{
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(module);
|
||||||
|
|
||||||
if (_children.Remove(module))
|
if (_children.Remove(module))
|
||||||
{
|
{
|
||||||
module.Owner = null;
|
module.Owner = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user