From 408ca45772e8b85c5fa2d8f16811053b0293c5d1 Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Thu, 27 Mar 2025 04:33:19 -0400 Subject: [PATCH] dismiss popups when toplevel deactivates in embedded scenarios (#18527) --- src/Avalonia.Controls/Primitives/Popup.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/Popup.cs b/src/Avalonia.Controls/Primitives/Popup.cs index 87962846aa..8d8bedb8ff 100644 --- a/src/Avalonia.Controls/Primitives/Popup.cs +++ b/src/Avalonia.Controls/Primitives/Popup.cs @@ -14,6 +14,7 @@ using Avalonia.Metadata; using Avalonia.Platform; using Avalonia.VisualTree; using Avalonia.Media; +using Avalonia.Interactivity; namespace Avalonia.Controls.Primitives { @@ -460,13 +461,13 @@ namespace Avalonia.Controls.Primitives SubscribeToEventHandler>(placementTarget, TargetDetached, (x, handler) => x.DetachedFromVisualTree += handler, (x, handler) => x.DetachedFromVisualTree -= handler).DisposeWith(handlerCleanup); - + if (topLevel is Window window && window.PlatformImpl != null) { SubscribeToEventHandler(window, WindowDeactivated, (x, handler) => x.Deactivated += handler, (x, handler) => x.Deactivated -= handler).DisposeWith(handlerCleanup); - + SubscribeToEventHandler(window.PlatformImpl, WindowLostFocus, (x, handler) => x.LostFocus += handler, (x, handler) => x.LostFocus -= handler).DisposeWith(handlerCleanup); @@ -500,6 +501,12 @@ namespace Avalonia.Controls.Primitives (x, handler) => x.Closed -= handler).DisposeWith(handlerCleanup); } } + else if (topLevel is { } tl && tl.PlatformImpl is ITopLevelImpl pimpl) + { + SubscribeToEventHandler(pimpl, TopLevelLostPlatformFocus, + (x, handler) => x.LostFocus += handler, + (x, handler) => x.LostFocus -= handler).DisposeWith(handlerCleanup); + } InputManager.Instance?.Process.Subscribe(ListenForNonClientClick).DisposeWith(handlerCleanup); @@ -963,6 +970,14 @@ namespace Avalonia.Controls.Primitives } } + private void TopLevelLostPlatformFocus() + { + if (IsLightDismissEnabled) + { + Close(); + } + } + private void PlacementTargetTransformChanged(Visual v, Matrix? matrix) { if (_openState is not null)