From 6ca1d3e90377be0fd2956bd1a6acf0938eb5f41e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigtryggur=20=C3=93marsson?= Date: Fri, 2 Aug 2024 15:05:34 +0200 Subject: [PATCH] Fix issue when trying to jump to active window or non existent virtual desktop --- .../IVirtualDesktopAPI.cs | 6 ++++++ .../Windows10_17763.cs | 8 ++++++++ .../Windows11_22000.cs | 8 ++++++++ .../Windows11_22621.cs | 8 ++++++++ .../Windows11_22621_2215.cs | 8 ++++++++ .../Windows11_22631_3085.cs | 8 ++++++++ WinJump/Core/WinJumpManager.cs | 19 +++++++++++++++++-- 7 files changed, 63 insertions(+), 2 deletions(-) diff --git a/WinJump/Core/VirtualDesktopDefinitions/IVirtualDesktopAPI.cs b/WinJump/Core/VirtualDesktopDefinitions/IVirtualDesktopAPI.cs index 743adf5..b729cbc 100644 --- a/WinJump/Core/VirtualDesktopDefinitions/IVirtualDesktopAPI.cs +++ b/WinJump/Core/VirtualDesktopDefinitions/IVirtualDesktopAPI.cs @@ -20,6 +20,12 @@ public interface IVirtualDesktopAPI : IDisposable { /// 0-indexed, where '0' is the first desktop int GetCurrentDesktop(); + /// + /// Returns how many virtual desktops there are. + /// + /// Virtual desktop count + public int GetDesktopCount(); + /// /// Jumps to the virtual desktop. /// diff --git a/WinJump/Core/VirtualDesktopDefinitions/Windows10_17763.cs b/WinJump/Core/VirtualDesktopDefinitions/Windows10_17763.cs index 77d1b38..ac5505f 100644 --- a/WinJump/Core/VirtualDesktopDefinitions/Windows10_17763.cs +++ b/WinJump/Core/VirtualDesktopDefinitions/Windows10_17763.cs @@ -17,6 +17,10 @@ public int GetCurrentDesktop() { return DesktopManager.GetCurrentDesktopNum(); } + public int GetDesktopCount() { + return DesktopManager.GetDesktopCount(); + } + public void JumpToDesktop(int index) { DesktopManager.SwitchDesktop(index); } @@ -110,6 +114,10 @@ internal static int GetCurrentDesktopNum() { return GetIndex(vd); } + internal static int GetDesktopCount() { + return VirtualDesktopManagerInternal.GetCount(); + } + internal static void MoveCurrentlyFocusedToDesktop(int index) { int processId; IntPtr hWnd = GetForegroundWindow(); diff --git a/WinJump/Core/VirtualDesktopDefinitions/Windows11_22000.cs b/WinJump/Core/VirtualDesktopDefinitions/Windows11_22000.cs index 4b2541d..5e5fb06 100644 --- a/WinJump/Core/VirtualDesktopDefinitions/Windows11_22000.cs +++ b/WinJump/Core/VirtualDesktopDefinitions/Windows11_22000.cs @@ -17,6 +17,10 @@ public int GetCurrentDesktop() { return DesktopManager.GetCurrentDesktopNum(); } + public int GetDesktopCount() { + return DesktopManager.GetDesktopCount(); + } + public void JumpToDesktop(int index) { DesktopManager.SwitchDesktop(index); } @@ -110,6 +114,10 @@ internal static int GetCurrentDesktopNum() { return GetIndex(vd); } + internal static int GetDesktopCount() { + return VirtualDesktopManagerInternal.GetCount(IntPtr.Zero); + } + internal static void MoveCurrentlyFocusedToDesktop(int index) { int processId; IntPtr hWnd = GetForegroundWindow(); diff --git a/WinJump/Core/VirtualDesktopDefinitions/Windows11_22621.cs b/WinJump/Core/VirtualDesktopDefinitions/Windows11_22621.cs index 7861b08..6197e03 100644 --- a/WinJump/Core/VirtualDesktopDefinitions/Windows11_22621.cs +++ b/WinJump/Core/VirtualDesktopDefinitions/Windows11_22621.cs @@ -17,6 +17,10 @@ public int GetCurrentDesktop() { return DesktopManager.GetCurrentDesktopNum(); } + public int GetDesktopCount() { + return DesktopManager.GetDesktopCount(); + } + public void JumpToDesktop(int index) { DesktopManager.SwitchDesktop(index); } @@ -107,6 +111,10 @@ internal static int GetCurrentDesktopNum() { return GetIndex(vd); } + + internal static int GetDesktopCount() { + return VirtualDesktopManagerInternal.GetCount(IntPtr.Zero); + } internal static void MoveCurrentlyFocusedToDesktop(int index) { int processId; diff --git a/WinJump/Core/VirtualDesktopDefinitions/Windows11_22621_2215.cs b/WinJump/Core/VirtualDesktopDefinitions/Windows11_22621_2215.cs index 0dc91d9..f76fe97 100644 --- a/WinJump/Core/VirtualDesktopDefinitions/Windows11_22621_2215.cs +++ b/WinJump/Core/VirtualDesktopDefinitions/Windows11_22621_2215.cs @@ -17,6 +17,10 @@ public int GetCurrentDesktop() { return DesktopManager.GetCurrentDesktopNum(); } + public int GetDesktopCount() { + return DesktopManager.GetDesktopCount(); + } + public void JumpToDesktop(int index) { DesktopManager.SwitchDesktop(index); } @@ -110,6 +114,10 @@ internal static int GetCurrentDesktopNum() { return GetIndex(vd); } + internal static int GetDesktopCount() { + return VirtualDesktopManagerInternal.GetCount(); + } + internal static void MoveCurrentlyFocusedToDesktop(int index) { int processId; IntPtr hWnd = GetForegroundWindow(); diff --git a/WinJump/Core/VirtualDesktopDefinitions/Windows11_22631_3085.cs b/WinJump/Core/VirtualDesktopDefinitions/Windows11_22631_3085.cs index 5df5eaa..9e612dd 100644 --- a/WinJump/Core/VirtualDesktopDefinitions/Windows11_22631_3085.cs +++ b/WinJump/Core/VirtualDesktopDefinitions/Windows11_22631_3085.cs @@ -17,6 +17,10 @@ public int GetCurrentDesktop() { return DesktopManager.GetCurrentDesktopNum(); } + public int GetDesktopCount() { + return DesktopManager.GetDesktopCount(); + } + public void JumpToDesktop(int index) { DesktopManager.SwitchDesktop(index); } @@ -110,6 +114,10 @@ internal static int GetCurrentDesktopNum() { return GetIndex(vd); } + internal static int GetDesktopCount() { + return VirtualDesktopManagerInternal.GetCount(); + } + internal static void MoveCurrentlyFocusedToDesktop(int index) { int processId; IntPtr hWnd = GetForegroundWindow(); diff --git a/WinJump/Core/WinJumpManager.cs b/WinJump/Core/WinJumpManager.cs index b7e8260..d4dbb86 100644 --- a/WinJump/Core/WinJumpManager.cs +++ b/WinJump/Core/WinJumpManager.cs @@ -251,9 +251,24 @@ public void JumpTo(uint index, uint fallback) { } public void JumpTo(uint index) { + + var allowJump = true; WrapCall(() => { - api.JumpToDesktop((int) index); - }, true); + // If the desktop is the same as the current one or doesn't exist, don't allow the jump + if(api.GetCurrentDesktop() == index) { + allowJump = false; + } + else if(index >= api.GetDesktopCount()) + { + allowJump = false; + } + }); + + if(allowJump) { + WrapCall(() => { + api.JumpToDesktop((int) index); + }, true); + } } public void JumpToNoHack(uint index) {