Skip to content

Commit 3906559

Browse files
committed
remove _SwitchIVirtualDesktop, animationToDesktopNum replaces goToDesktopNum; MoveWindowToDesktopNum: call this._activateWindowUnder() only if the window to move is the active one; #32
1 parent 8d719d3 commit 3906559

File tree

2 files changed

+45
-57
lines changed

2 files changed

+45
-57
lines changed

_VD.ahk

+39-57
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class VD {
6262
;conditionally assign method to method
6363
this._dll_GetCurrentDesktop:=this._dll_GetCurrentDesktop_Win10
6464
this._dll_GetDesktops:=this._dll_GetDesktops_Win10
65-
this._dll_SwitchDesktop:=this._dll_SwitchDesktop_Win10
6665
this._dll_CreateDesktop:=this._dll_CreateDesktop_Win10
6766
this._dll_GetName:=this._dll_GetName_Win10
6867
this.RegisterDesktopNotifications:=this.RegisterDesktopNotifications_Win10
@@ -75,7 +74,6 @@ class VD {
7574
;conditionally assign method to method
7675
this._dll_GetCurrentDesktop:=this._dll_GetCurrentDesktop_Win11
7776
this._dll_GetDesktops:=this._dll_GetDesktops_Win11
78-
this._dll_SwitchDesktop:=this._dll_SwitchDesktop_Win11
7977
this._dll_CreateDesktop:=this._dll_CreateDesktop_Win11
8078
this._dll_GetName:=this._dll_GetName_Win11
8179
this.RegisterDesktopNotifications:=this.RegisterDesktopNotifications_Win11
@@ -98,7 +96,7 @@ class VD {
9896

9997
this.GetDesktops := this._vtable(this.IVirtualDesktopManagerInternal, 7) ; void GetDesktops(out IObjectArray desktops);
10098
; this.GetAdjacentDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 8) ; int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop);
101-
this.SwitchDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 9) ; void SwitchDesktop(IVirtualDesktop desktop);
99+
; this.SwitchDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 9) ; void SwitchDesktop(IVirtualDesktop desktop);
102100
this.Ptr_CreateDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 10) ; IVirtualDesktop CreateDesktop();
103101
this.Ptr_RemoveDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 11) ; void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback);
104102
this.FindDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 12) ; IVirtualDesktop FindDesktop(ref Guid desktopid);
@@ -107,7 +105,7 @@ class VD {
107105

108106
this.GetDesktops := this._vtable(this.IVirtualDesktopManagerInternal, 7) ; void GetDesktops(IntPtr hWndOrMon, out IObjectArray desktops);
109107
; this.GetAdjacentDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 8) ; int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop);
110-
this.SwitchDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 9) ; void SwitchDesktop(IntPtr hWndOrMon, IVirtualDesktop desktop);
108+
; this.SwitchDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 9) ; void SwitchDesktop(IntPtr hWndOrMon, IVirtualDesktop desktop);
111109
this.Ptr_CreateDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 10) ; IVirtualDesktop CreateDesktop(IntPtr hWndOrMon);
112110
; this.MoveDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 11) ; void MoveDesktop(IVirtualDesktop desktop, IntPtr hWndOrMon, int nIndex);
113111
this.Ptr_RemoveDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 12) ; void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback);
@@ -118,7 +116,7 @@ class VD {
118116
; this.GetAllCurrentDesktops := this._vtable(this.IVirtualDesktopManagerInternal, 7) ; IObjectArray GetAllCurrentDesktops();
119117
this.GetDesktops := this._vtable(this.IVirtualDesktopManagerInternal, 8) ; void GetDesktops(IntPtr hWndOrMon, out IObjectArray desktops);
120118
; this.GetAdjacentDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 9) ; int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop);
121-
this.SwitchDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 10) ; void SwitchDesktop(IntPtr hWndOrMon, IVirtualDesktop desktop);
119+
; this.SwitchDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 10) ; void SwitchDesktop(IntPtr hWndOrMon, IVirtualDesktop desktop);
122120
this.Ptr_CreateDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 11) ; IVirtualDesktop CreateDesktop(IntPtr hWndOrMon);
123121
; this.MoveDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 12) ; void MoveDesktop(IVirtualDesktop desktop, IntPtr hWndOrMon, int nIndex);
124122
this.Ptr_RemoveDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 13) ; void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback);
@@ -183,12 +181,6 @@ class VD {
183181
DllCall(this.GetDesktops, "UPtr", this.IVirtualDesktopManagerInternal, "Ptr", 0, "UPtr*", IObjectArray)
184182
return IObjectArray
185183
}
186-
_dll_SwitchDesktop_Win10(IVirtualDesktop) {
187-
DllCall(this.SwitchDesktop, "ptr", this.IVirtualDesktopManagerInternal, "UPtr", IVirtualDesktop)
188-
}
189-
_dll_SwitchDesktop_Win11(IVirtualDesktop) {
190-
DllCall(this.SwitchDesktop, "ptr", this.IVirtualDesktopManagerInternal, "Ptr", 0, "UPtr", IVirtualDesktop)
191-
}
192184
_dll_CreateDesktop_Win10() {
193185
IVirtualDesktop_ofNewDesktop:=0
194186
DllCall(this.Ptr_CreateDesktop, "UPtr", this.IVirtualDesktopManagerInternal, "Ptr*", IVirtualDesktop_ofNewDesktop)
@@ -244,26 +236,30 @@ class VD {
244236
return this._GetDesktops_Obj().GetCount()
245237
}
246238

247-
goToDesktopNum(desktopNum) {
248-
IVirtualDesktop:=this._GetDesktops_Obj().GetAt(desktopNum)
249-
this._SwitchIVirtualDesktop(IVirtualDesktop)
239+
goToDesktopNum(desktopNum) { ; Lej77 https://github.com/Grabacr07/VirtualDesktop/pull/23#issuecomment-334918711
240+
Gui VD_animation_gui:New, % "-Border -SysMenu +Owner -Caption +HwndVD_animation_gui_hwnd"
241+
IVirtualDesktop := this._GetDesktops_Obj().GetAt(desktopNum)
242+
GetId:=this._vtable(IVirtualDesktop, 4)
243+
VarSetCapacity(GUID_Desktop, 16)
244+
DllCall(GetId, "Ptr", IVirtualDesktop, "Ptr", &GUID_Desktop)
245+
DllCall(this.MoveWindowToDesktop, "Ptr", this.IVirtualDesktopManager, "Ptr", VD_animation_gui_hwnd, "Ptr", &GUID_Desktop)
246+
Gui VD_animation_gui:Show
247+
Sleep 100
248+
Gui VD_animation_gui:Destroy
249+
; "ahk_class TPUtilWindow ahk_exe HxD.exe" instead of "ahk_class WorkerW ahk_exe explorer.exe"
250+
Sleep 50
251+
if (activeHwnd:=WinExist("A")) {
252+
if (!this._isValidWindow(activeHwnd)) {
253+
if (this._activateWindowUnder()==-1) {
254+
WinActivate % "ahk_class WorkerW ahk_exe explorer.exe"
255+
}
256+
}
250257

251-
if (this._isWindowFullScreen("A")) {
252-
timerFunc := ObjBindMethod(this, "_pleaseSwitchDesktop", desktopNum) ;https://www.autohotkey.com/docs/commands/SetTimer.htm#ExampleClass
253-
SetTimer % timerFunc, -50
258+
} else {
259+
if (this._activateWindowUnder()==-1) {
260+
WinActivate % "ahk_class WorkerW ahk_exe explorer.exe"
261+
}
254262
}
255-
256-
}
257-
animationToDesktopNum(desktopNum) { ; Lej77 https://github.com/Grabacr07/VirtualDesktop/pull/23#issuecomment-334918711
258-
Gui VD_animation_gui:New, % "-Border -SysMenu +Owner -Caption +HwndVD_animation_gui_hwnd"
259-
IVirtualDesktop := this._GetDesktops_Obj().GetAt(desktopNum)
260-
GetId:=this._vtable(IVirtualDesktop, 4)
261-
VarSetCapacity(GUID_Desktop, 16)
262-
DllCall(GetId, "Ptr", IVirtualDesktop, "Ptr", &GUID_Desktop)
263-
DllCall(this.MoveWindowToDesktop, "Ptr", this.IVirtualDesktopManager, "Ptr", VD_animation_gui_hwnd, "Ptr", &GUID_Desktop)
264-
Gui VD_animation_gui:Show
265-
Sleep 100
266-
Gui VD_animation_gui:Destroy
267263
}
268264

269265
_getLocalizedWord_Desktop() {
@@ -311,18 +307,27 @@ class VD {
311307
}
312308
}
313309

314-
MoveWindowToDesktopNum(wintitle, desktopNum)
315-
{
310+
MoveWindowToDesktopNum(wintitle, desktopNum) {
316311
found:=this._getFirstValidWindow(wintitle)
317312
if (!found) {
318313
return -1 ;for false
319314
}
320315
theHwnd:=found[1]
321316
thePView:=found[2]
322317

318+
needActivateWindowUnder:=False
319+
if (activeHwnd:=WinExist("A")) {
320+
if (activeHwnd==theHwnd) {
321+
needActivateWindowUnder:=true
322+
}
323+
}
324+
323325
IVirtualDesktop:=this._GetDesktops_Obj().GetAt(desktopNum)
326+
DllCall(this.MoveViewToDesktop, "ptr", this.IVirtualDesktopManagerInternal, "Ptr", thePView, "Ptr", IVirtualDesktop)
324327

325-
this._MoveView_to_IVirtualDesktop(thePView, IVirtualDesktop)
328+
if (needActivateWindowUnder) {
329+
this._activateWindowUnder()
330+
}
326331
}
327332

328333
getRelativeDesktopNum(anchor_desktopNum, relative_count)
@@ -357,8 +362,7 @@ class VD {
357362
this.goToDesktopNum(this.getRelativeDesktopNum(this.getCurrentDesktopNum(), relative_count))
358363
}
359364

360-
MoveWindowToCurrentDesktop(wintitle, activateYourWindow:=true)
361-
{
365+
MoveWindowToCurrentDesktop(wintitle, activateYourWindow:=true) {
362366
found:=this._getFirstValidWindow(wintitle)
363367
if (!found) {
364368
return -1 ;for false
@@ -369,7 +373,7 @@ class VD {
369373
currentDesktopNum:=this.getCurrentDesktopNum()
370374
IVirtualDesktop:=this._GetDesktops_Obj().GetAt(currentDesktopNum)
371375

372-
this._MoveView_to_IVirtualDesktop(thePView, IVirtualDesktop)
376+
DllCall(this.MoveViewToDesktop, "ptr", this.IVirtualDesktopManagerInternal, "Ptr", thePView, "Ptr", IVirtualDesktop)
373377

374378
if (activateYourWindow) {
375379
WinActivate % "ahk_id " theHwnd
@@ -796,28 +800,6 @@ class VD {
796800
;actual methods end
797801

798802
;internal methods start
799-
_MoveView_to_IVirtualDesktop(thePView, IVirtualDesktop) {
800-
DllCall(this.MoveViewToDesktop, "ptr", this.IVirtualDesktopManagerInternal, "Ptr", thePView, "Ptr", IVirtualDesktop)
801-
DllCall(this.MoveViewToDesktop, "ptr", this.IVirtualDesktopManagerInternal, "Ptr", thePView, "Ptr", IVirtualDesktop)
802-
this._activateWindowUnder()
803-
}
804-
_SwitchIVirtualDesktop(IVirtualDesktop) {
805-
;activate taskbar before ;https://github.com/mzomparelli/zVirtualDesktop/issues/59#issuecomment-317613971
806-
WinActivate, ahk_class Shell_TrayWnd
807-
WinWaitActive, ahk_class Shell_TrayWnd
808-
809-
this._dll_SwitchDesktop(IVirtualDesktop)
810-
this._dll_SwitchDesktop(IVirtualDesktop)
811-
this._activateWindowUnder()
812-
}
813-
814-
_pleaseSwitchDesktop(desktopNum) {
815-
;IVirtualDesktop should be calculated again because IVirtualDesktop could have changed
816-
;what we want is the same desktopNum
817-
IVirtualDesktop:=this._GetDesktops_Obj().GetAt(desktopNum)
818-
this._SwitchIVirtualDesktop(IVirtualDesktop)
819-
;this method is goToDesktopNum(), but without the recursion, to prevent recursion
820-
}
821803

822804
_activateWindowUnder() {
823805
; if this doesn't work

notes/discoveries

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vscode active
2+
window with "Show this window on all desktops" below active
3+
^+Right
4+
5+
does NOT switch, window with "Show this window on all desktops" is activated
6+
so I think this is how windows switches desktops, by activating a window on the chosen desktop, though idk about when there is no window, so maybe that's when to use _SwitchIVirtualDesktop, needs further investigation but unwilling until I come back, reading this

0 commit comments

Comments
 (0)