diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 123472d23f..d770f4b211 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -65,12 +65,18 @@ namespace Avalonia.Win32 return IntPtr.Zero; } + // Based on https://github.com/dotnet/wpf/blob/master/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L4270-L4337 + // We need to enable parent window before destroying child window to prevent OS from activating a random window behind us. + // This is described here: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enablewindow#remarks + // Our window closed callback will set enabled state to a correct value after child window gets destroyed. + // We need to verify if parent is still alive (perhaps it got destroyed somehow). if (_parent != null && IsWindow(_parent._hwnd)) { var wasActive = GetActiveWindow() == _hwnd; _parent.SetEnabled(true); + // We also need to activate our parent window since again OS might try to activate a window behind if it is not set. if (wasActive) { SetActiveWindow(_parent._hwnd);