Skip to content

Commit 6ca1d3e

Browse files
committed
Fix issue when trying to jump to active window or non existent virtual desktop
1 parent da392d5 commit 6ca1d3e

File tree

7 files changed

+63
-2
lines changed

7 files changed

+63
-2
lines changed

WinJump/Core/VirtualDesktopDefinitions/IVirtualDesktopAPI.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public interface IVirtualDesktopAPI : IDisposable {
2020
/// <returns>0-indexed, where '0' is the first desktop</returns>
2121
int GetCurrentDesktop();
2222

23+
/// <summary>
24+
/// Returns how many virtual desktops there are.
25+
/// </summary>
26+
/// <returns>Virtual desktop count</returns>
27+
public int GetDesktopCount();
28+
2329
/// <summary>
2430
/// Jumps to the virtual desktop.
2531
/// </summary>

WinJump/Core/VirtualDesktopDefinitions/Windows10_17763.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public int GetCurrentDesktop() {
1717
return DesktopManager.GetCurrentDesktopNum();
1818
}
1919

20+
public int GetDesktopCount() {
21+
return DesktopManager.GetDesktopCount();
22+
}
23+
2024
public void JumpToDesktop(int index) {
2125
DesktopManager.SwitchDesktop(index);
2226
}
@@ -110,6 +114,10 @@ internal static int GetCurrentDesktopNum() {
110114
return GetIndex(vd);
111115
}
112116

117+
internal static int GetDesktopCount() {
118+
return VirtualDesktopManagerInternal.GetCount();
119+
}
120+
113121
internal static void MoveCurrentlyFocusedToDesktop(int index) {
114122
int processId;
115123
IntPtr hWnd = GetForegroundWindow();

WinJump/Core/VirtualDesktopDefinitions/Windows11_22000.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public int GetCurrentDesktop() {
1717
return DesktopManager.GetCurrentDesktopNum();
1818
}
1919

20+
public int GetDesktopCount() {
21+
return DesktopManager.GetDesktopCount();
22+
}
23+
2024
public void JumpToDesktop(int index) {
2125
DesktopManager.SwitchDesktop(index);
2226
}
@@ -110,6 +114,10 @@ internal static int GetCurrentDesktopNum() {
110114
return GetIndex(vd);
111115
}
112116

117+
internal static int GetDesktopCount() {
118+
return VirtualDesktopManagerInternal.GetCount(IntPtr.Zero);
119+
}
120+
113121
internal static void MoveCurrentlyFocusedToDesktop(int index) {
114122
int processId;
115123
IntPtr hWnd = GetForegroundWindow();

WinJump/Core/VirtualDesktopDefinitions/Windows11_22621.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public int GetCurrentDesktop() {
1717
return DesktopManager.GetCurrentDesktopNum();
1818
}
1919

20+
public int GetDesktopCount() {
21+
return DesktopManager.GetDesktopCount();
22+
}
23+
2024
public void JumpToDesktop(int index) {
2125
DesktopManager.SwitchDesktop(index);
2226
}
@@ -107,6 +111,10 @@ internal static int GetCurrentDesktopNum() {
107111

108112
return GetIndex(vd);
109113
}
114+
115+
internal static int GetDesktopCount() {
116+
return VirtualDesktopManagerInternal.GetCount(IntPtr.Zero);
117+
}
110118

111119
internal static void MoveCurrentlyFocusedToDesktop(int index) {
112120
int processId;

WinJump/Core/VirtualDesktopDefinitions/Windows11_22621_2215.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public int GetCurrentDesktop() {
1717
return DesktopManager.GetCurrentDesktopNum();
1818
}
1919

20+
public int GetDesktopCount() {
21+
return DesktopManager.GetDesktopCount();
22+
}
23+
2024
public void JumpToDesktop(int index) {
2125
DesktopManager.SwitchDesktop(index);
2226
}
@@ -110,6 +114,10 @@ internal static int GetCurrentDesktopNum() {
110114
return GetIndex(vd);
111115
}
112116

117+
internal static int GetDesktopCount() {
118+
return VirtualDesktopManagerInternal.GetCount();
119+
}
120+
113121
internal static void MoveCurrentlyFocusedToDesktop(int index) {
114122
int processId;
115123
IntPtr hWnd = GetForegroundWindow();

WinJump/Core/VirtualDesktopDefinitions/Windows11_22631_3085.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public int GetCurrentDesktop() {
1717
return DesktopManager.GetCurrentDesktopNum();
1818
}
1919

20+
public int GetDesktopCount() {
21+
return DesktopManager.GetDesktopCount();
22+
}
23+
2024
public void JumpToDesktop(int index) {
2125
DesktopManager.SwitchDesktop(index);
2226
}
@@ -110,6 +114,10 @@ internal static int GetCurrentDesktopNum() {
110114
return GetIndex(vd);
111115
}
112116

117+
internal static int GetDesktopCount() {
118+
return VirtualDesktopManagerInternal.GetCount();
119+
}
120+
113121
internal static void MoveCurrentlyFocusedToDesktop(int index) {
114122
int processId;
115123
IntPtr hWnd = GetForegroundWindow();

WinJump/Core/WinJumpManager.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,24 @@ public void JumpTo(uint index, uint fallback) {
251251
}
252252

253253
public void JumpTo(uint index) {
254+
255+
var allowJump = true;
254256
WrapCall(() => {
255-
api.JumpToDesktop((int) index);
256-
}, true);
257+
// If the desktop is the same as the current one or doesn't exist, don't allow the jump
258+
if(api.GetCurrentDesktop() == index) {
259+
allowJump = false;
260+
}
261+
else if(index >= api.GetDesktopCount())
262+
{
263+
allowJump = false;
264+
}
265+
});
266+
267+
if(allowJump) {
268+
WrapCall(() => {
269+
api.JumpToDesktop((int) index);
270+
}, true);
271+
}
257272
}
258273

259274
public void JumpToNoHack(uint index) {

0 commit comments

Comments
 (0)