From 27ee4e00cd77c6c9754d0cc36a15f4daaf4a2350 Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Sat, 24 Oct 2020 20:38:56 +0200 Subject: [PATCH] Add a bunch of comments to explain what is going on in win32 closed handler. --- src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs | 6 ++++++ 1 file changed, 6 insertions(+) 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);