From e6f0c8dbeeebe75a044630d5cba3a185d8612a17 Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Fri, 26 Jan 2024 00:38:06 +0000 Subject: [PATCH] ensure correct parent is set when window owner changes (#14351) Co-authored-by: Max Katz --- src/Avalonia.Controls/Window.cs | 44 ++++++++++++++------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index 44c997f555..988a265655 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -478,16 +478,11 @@ namespace Avalonia.Controls child.CloseInternal(); } - if (Owner is Window owner) - { - owner.RemoveChild(this); - } - - Owner = null; - PlatformImpl?.Dispose(); _showingAsDialog = false; + + Owner = null; } private bool ShouldCancelClose(WindowClosingEventArgs args) @@ -554,11 +549,6 @@ namespace Avalonia.Controls StopRendering(); - if (Owner is Window owner) - { - owner.RemoveChild(this); - } - if (_children.Count > 0) { foreach (var child in _children.ToArray()) @@ -567,10 +557,11 @@ namespace Avalonia.Controls } } - Owner = null; PlatformImpl?.Hide(); IsVisible = false; _shown = false; + + Owner = null; } } @@ -689,13 +680,7 @@ namespace Avalonia.Controls LayoutManager.ExecuteInitialLayoutPass(); - if (PlatformImpl != null && owner?.PlatformImpl is not null) - { - PlatformImpl.SetParent(owner.PlatformImpl); - } - Owner = owner; - owner?.AddChild(this, false); SetWindowStartupLocation(owner); @@ -770,9 +755,7 @@ namespace Avalonia.Controls var result = new TaskCompletionSource(); - PlatformImpl?.SetParent(owner.PlatformImpl!); Owner = owner; - owner.AddChild(this, true); SetWindowStartupLocation(owner); @@ -974,11 +957,6 @@ namespace Avalonia.Controls base.HandleClosed(); - if (Owner is Window owner) - { - owner.RemoveChild(this); - } - Owner = null; } @@ -1031,6 +1009,20 @@ namespace Avalonia.Controls PlatformImpl?.SetSystemDecorations(typedNewValue); } + + if (change.Property == OwnerProperty) + { + var oldParent = change.OldValue as Window; + var newParent = change.NewValue as Window; + + oldParent?.RemoveChild(this); + newParent?.AddChild(this, _showingAsDialog); + + if (PlatformImpl is IWindowImpl impl) + { + impl.SetParent(_showingAsDialog ? newParent?.PlatformImpl! : (newParent?.PlatformImpl ?? null)); + } + } } protected override AutomationPeer OnCreateAutomationPeer()