fix(docking): improve container consistency and re-parenting

This commit is contained in:
2026-03-28 21:46:38 +09:00
parent 47ffc01524
commit ea4d1084e9
2 changed files with 36 additions and 2 deletions

View File

@@ -13,11 +13,22 @@ public abstract class DockContainer : DockModule
private void OnChildrenChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
{
// Reset is tricky because e.OldItems is null.
// We should have handled this by clearing owners before Clear() was called,
// or by using a custom collection that notifies before clearing.
// For now, we'll just log or handle it if we can.
}
if (e.OldItems != null)
{
foreach (DockModule module in e.OldItems)
{
module.Owner = null;
if (module.Owner == this)
{
module.Owner = null;
}
}
}
@@ -25,6 +36,12 @@ public abstract class DockContainer : DockModule
{
foreach (DockModule module in e.NewItems)
{
// Re-parenting: if it has an owner, detach it
if (module.Owner != null && module.Owner != this)
{
module.Owner.Children.Remove(module);
}
module.Owner = this;
// module.Root = Root;
}
@@ -32,6 +49,23 @@ public abstract class DockContainer : DockModule
OnChildrenUpdated();
}
public void AddChild(DockModule module)
{
if (Children.Contains(module))
return;
Children.Add(module);
}
public void Clear()
{
foreach (var child in Children)
{
child.Owner = null;
}
Children.Clear();
}
protected virtual void OnChildrenUpdated() { }
}

View File

@@ -10,7 +10,7 @@ public abstract class DockModule : Control
public void Detach()
{
// Owner?.Children.Remove(this);
Owner?.Children.Remove(this);
Owner = null;
}
}