From 07ee89fadafb679e525c209eb10a46df366186f6 Mon Sep 17 00:00:00 2001 From: Dinys Monvoisin Date: Mon, 2 Feb 2026 22:10:24 +1100 Subject: [PATCH] fix-autofocus-new-windows --- FancyWM/TilingService.Private.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/FancyWM/TilingService.Private.cs b/FancyWM/TilingService.Private.cs index d750468..f8fcf22 100644 --- a/FancyWM/TilingService.Private.cs +++ b/FancyWM/TilingService.Private.cs @@ -1045,6 +1045,28 @@ private void OnWindowAdded(object? sender, WindowChangedEventArgs e) } InvalidateLayout(); + + // Auto-focus the newly registered window + // Use async pattern with delay to ensure Windows has fully initialized the window + _ = m_dispatcher.BeginInvoke(async () => + { + await Task.Delay(100); + try + { + if (e.Source.IsAlive && (e.Source.State == WindowState.Restored || e.Source.State == WindowState.Maximized)) + { + FocusHelper.ForceActivate(e.Source.Handle); + } + } + catch (InvalidWindowReferenceException) + { + // Window no longer exists, ignore + } + catch (Exception) + { + // Focus activation failed, ignore + } + }, System.Windows.Threading.DispatcherPriority.Background); }, System.Windows.Threading.DispatcherPriority.DataBind); } }