Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions FancyWM/TilingService.Private.cs
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,60 @@ private void OnCurrentDesktopChanged(object? sender, CurrentDesktopChangedEventA
{
Refresh();
InvalidateLayout();

// Restore focus to the previously focused window on this desktop
// Use dispatcher and delay to ensure Windows has completed the desktop transition
_ = m_dispatcher.BeginInvoke(async () =>
{
// Wait for Windows to complete the desktop transition
await Task.Delay(100);

try
{
IntPtr? targetHandle = null;

// First, try to get the tracked focused window for this desktop
lock (m_backend)
{
var focusedNode = m_backend.GetFocus(e.NewDesktop);
if (focusedNode is WindowNode windowNode)
{
var window = windowNode.WindowReference;
if (window.IsAlive && (window.State == WindowState.Restored || window.State == WindowState.Maximized))
{
targetHandle = window.Handle;
}
}
}

// Fallback: if no tracked focus, find any visible window on this desktop
if (targetHandle == null)
{
foreach (var window in m_workspace.GetCurrentDesktopSnapshot())
{
if (window.IsAlive && (window.State == WindowState.Restored || window.State == WindowState.Maximized))
{
targetHandle = window.Handle;
break;
}
}
}

// Attempt to restore focus
if (targetHandle != null)
{
FocusHelper.ForceActivate(targetHandle.Value);
}
}
catch (InvalidWindowReferenceException)
{
// Window no longer exists, ignore
}
catch (Exception)
{
// Focus restoration failed, ignore
}
});
}

private void OnWindowGotFocus(object? sender, WindowFocusChangedEventArgs e)
Expand Down
Loading