From 97389713697e0089c7b8b65deda62943b57d814f Mon Sep 17 00:00:00 2001 From: Misaki Date: Sat, 28 Mar 2026 22:52:34 +0900 Subject: [PATCH] fix(docking): address code quality issues in DockingLayout and FloatingWindow --- .../View/Controls/Docking/DockingLayout.cs | 63 ++++++++----------- .../View/Controls/Docking/FloatingWindow.cs | 9 +++ 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/Editor/Ghost.Editor/View/Controls/Docking/DockingLayout.cs b/src/Editor/Ghost.Editor/View/Controls/Docking/DockingLayout.cs index 5eae0c4..88be133 100644 --- a/src/Editor/Ghost.Editor/View/Controls/Docking/DockingLayout.cs +++ b/src/Editor/Ghost.Editor/View/Controls/Docking/DockingLayout.cs @@ -30,6 +30,7 @@ public class DockingLayout : Control private Canvas? _overlayCanvas; private DockRegionHighlight? _highlight; + private readonly List _floatingWindows = new(); public DockingLayout() { @@ -113,16 +114,36 @@ public class DockingLayout : Control private void SplitGroup(DockGroup targetGroup, DockDocument doc, DockTarget target) { var parentPanel = targetGroup.Owner as DockPanel; + + if (parentPanel == null) + { + throw new InvalidOperationException("targetGroup must be owned by a DockPanel to be split."); + } + var newGroup = new DockGroup(); newGroup.AddChild(doc); var orientation = (target == DockTarget.Left || target == DockTarget.Right) ? Orientation.Horizontal : Orientation.Vertical; - if (parentPanel == null) + int index = parentPanel.Children.IndexOf(targetGroup); + + if (parentPanel.Orientation == orientation) { - // targetGroup is the root + // Same orientation, just insert + if (target == DockTarget.Left || target == DockTarget.Top) + { + parentPanel.InsertChild(index, newGroup); + } + else + { + parentPanel.InsertChild(index + 1, newGroup); + } + } + else + { + // Different orientation, need a new sub-panel var newPanel = new DockPanel { Orientation = orientation }; - RootPanel = newPanel; + parentPanel.ReplaceChild(targetGroup, newPanel); if (target == DockTarget.Left || target == DockTarget.Top) { @@ -135,40 +156,6 @@ public class DockingLayout : Control newPanel.AddChild(newGroup); } } - else - { - int index = parentPanel.Children.IndexOf(targetGroup); - - if (parentPanel.Orientation == orientation) - { - // Same orientation, just insert - if (target == DockTarget.Left || target == DockTarget.Top) - { - parentPanel.InsertChild(index, newGroup); - } - else - { - parentPanel.InsertChild(index + 1, newGroup); - } - } - else - { - // Different orientation, need a new sub-panel - var newPanel = new DockPanel { Orientation = orientation }; - parentPanel.ReplaceChild(targetGroup, newPanel); - - if (target == DockTarget.Left || target == DockTarget.Top) - { - newPanel.AddChild(newGroup); - newPanel.AddChild(targetGroup); - } - else - { - newPanel.AddChild(targetGroup); - newPanel.AddChild(newGroup); - } - } - } } private static DockGroup? FindFirstDockGroup(DockContainer container) @@ -262,6 +249,8 @@ public class DockingLayout : Control internal void CreateFloatingWindow(DockDocument doc) { var window = new FloatingWindow(doc); + _floatingWindows.Add(window); + window.Closed += (s, e) => _floatingWindows.Remove(window); window.Activate(); } } diff --git a/src/Editor/Ghost.Editor/View/Controls/Docking/FloatingWindow.cs b/src/Editor/Ghost.Editor/View/Controls/Docking/FloatingWindow.cs index 289595e..e4e1a97 100644 --- a/src/Editor/Ghost.Editor/View/Controls/Docking/FloatingWindow.cs +++ b/src/Editor/Ghost.Editor/View/Controls/Docking/FloatingWindow.cs @@ -2,10 +2,19 @@ using Microsoft.UI.Xaml; namespace Ghost.Editor.View.Controls.Docking; +/// +/// A floating window that contains a docking layout. +/// public class FloatingWindow : Window { + /// + /// Initializes a new instance of the class with the specified document. + /// + /// The document to display in the floating window. public FloatingWindow(DockDocument document) { + ArgumentNullException.ThrowIfNull(document); + var layout = new DockingLayout(); var group = new DockGroup(); group.AddChild(document);