From 43c679f9045bdbcb6b34cb80d88224f5d0822afa Mon Sep 17 00:00:00 2001 From: Will Davies Date: Thu, 15 Dec 2022 10:06:38 -0600 Subject: [PATCH] Better fix to foreground window setting issue --- WinJump/Program.cs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/WinJump/Program.cs b/WinJump/Program.cs index 0f87827..fcee889 100644 --- a/WinJump/Program.cs +++ b/WinJump/Program.cs @@ -11,7 +11,10 @@ internal static class Program { private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] - private static extern IntPtr GetTopWindow(); + private static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + private static extern bool IsWindow(IntPtr hWnd); [STAThread] public static void Main() { @@ -50,6 +53,7 @@ public static void Main() { // Credit https://stackoverflow.com/a/21684059/4779937 private sealed class STAThread : IDisposable { + private IntPtr[] LastActiveWindow = new IntPtr[10]; private readonly VirtualDesktopWrapper vdw = VirtualDesktopManager.Create(); public STAThread() { @@ -73,12 +77,20 @@ public void JumpTo(int index) { if (ctx == null) throw new ObjectDisposedException("STAThread"); ctx.Send((_) => { + // Before we go to a new Window, save the foreground Window + LastActiveWindow[vdw.GetDesktop()] = GetForegroundWindow(); + vdw.JumpTo(index); // Give it just a little time to let the desktop settle - Thread.Sleep(10); - IntPtr ptr = GetTopWindow(); - if (ptr != IntPtr.Zero) { - SetForegroundWindow(ptr); + Thread.Sleep(50); + + if(LastActiveWindow[index] != IntPtr.Zero) { + // Check if the window still exists (it might have been closed) + if (IsWindow(LastActiveWindow[index])) { + SetForegroundWindow(LastActiveWindow[index]); + } else { + LastActiveWindow[index] = IntPtr.Zero; + } } }, null); }