From e83555498a47258bc569eb5ad59d1dcf36a54897 Mon Sep 17 00:00:00 2001 From: Misaki Date: Sat, 28 Mar 2026 15:48:56 +0900 Subject: [PATCH] fix(dock): address reviewer feedback on window tear-off --- src/Editor/Ghost.Editor/App.xaml.cs | 10 ++++++++++ src/Editor/Ghost.Editor/View/Controls/DockLayout.cs | 13 ++++++++----- .../Ghost.Editor/View/Windows/DockWindow.xaml.cs | 4 +--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Editor/Ghost.Editor/App.xaml.cs b/src/Editor/Ghost.Editor/App.xaml.cs index 5a62383..5e0282b 100644 --- a/src/Editor/Ghost.Editor/App.xaml.cs +++ b/src/Editor/Ghost.Editor/App.xaml.cs @@ -23,6 +23,7 @@ namespace Ghost.Editor; public partial class App : Application { private Window? _window; + private readonly List _secondaryWindows = new(); internal static Window? Window { @@ -39,6 +40,15 @@ public partial class App : Application } } + internal static void AddSecondaryWindow(Window window) + { + if (Current is App app) + { + app._secondaryWindows.Add(window); + window.Closed += (s, e) => app._secondaryWindows.Remove(window); + } + } + internal IHost Host { get; diff --git a/src/Editor/Ghost.Editor/View/Controls/DockLayout.cs b/src/Editor/Ghost.Editor/View/Controls/DockLayout.cs index 8cdf3dc..6d74a72 100644 --- a/src/Editor/Ghost.Editor/View/Controls/DockLayout.cs +++ b/src/Editor/Ghost.Editor/View/Controls/DockLayout.cs @@ -300,12 +300,15 @@ public sealed partial class DockLayout : Control if (_sourceNode != null && _draggedItem != null) { // Remove from current tree - _sourceNode.Items.Remove(_draggedItem); - DockMutationEngine.CleanupEmptyNodes(_sourceNode, Root); + if (_sourceNode.Items.Remove(_draggedItem)) + { + DockMutationEngine.CleanupEmptyNodes(_sourceNode); - // Create new window - var newWindow = new Ghost.Editor.View.Windows.DockWindow(_draggedItem); - newWindow.Activate(); + // Create new window and register it with App to prevent GC + var newWindow = new Ghost.Editor.View.Windows.DockWindow(_draggedItem); + App.AddSecondaryWindow(newWindow); + newWindow.Activate(); + } ClearDragOperationState(); } diff --git a/src/Editor/Ghost.Editor/View/Windows/DockWindow.xaml.cs b/src/Editor/Ghost.Editor/View/Windows/DockWindow.xaml.cs index 00d9fc1..2a89b92 100644 --- a/src/Editor/Ghost.Editor/View/Windows/DockWindow.xaml.cs +++ b/src/Editor/Ghost.Editor/View/Windows/DockWindow.xaml.cs @@ -3,7 +3,7 @@ using WinUIEx; namespace Ghost.Editor.View.Windows; -public sealed partial class DockWindow : WindowEx +internal sealed partial class DockWindow : WindowEx { public DockWindow(object initialTabContent) { @@ -16,7 +16,5 @@ public sealed partial class DockWindow : WindowEx rootGroup.AddChild(panel); PART_DockLayout.Root = rootGroup; - - // Optional: Titlebar setup etc. } }