-
Notifications
You must be signed in to change notification settings - Fork 0
/
desktop.ahk
414 lines (346 loc) · 14.1 KB
/
desktop.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#Requires AutoHotkey v2.0-a
#SingleInstance Force ; The script will Reload if launched while already running
KeyHistory 0 ; Ensures user privacy when debugging is not needed
SetWorkingDir A_ScriptDir ; Ensures a consistent starting directory
SendMode "Input" ; Recommended for new scripts due to its superior speed and reliability
SetKeyDelay 75
; Globals
DesktopCount := 2 ; Windows starts with 2 desktops at boot
CurrentDesktop := 1 ; Desktop count is 1-indexed (Microsoft numbers them this way)
LastOpenedDesktop := 1
NextDesktop := CurrentDesktop ; For loop desktop
DesktopMiniCount := 2 ; keep desktop mini count at script boot.
DesktopBeforeScriptBoot := -1 ; this param will keep desktop location before script run. desktop mini count will create new desktop while switch to new desktop.
; desktop associate with background picture
AutoAssociateBackgroundWithDesktop := false
BackgroundPicPaths := [".\bgPic\1.jpg", ".\bgPic\2.jpg"]
; DLL 预加载dll库
hVirtualDesktopAccessor := DllCall("LoadLibrary", "Str", A_ScriptDir . "\VirtualDesktopAccessor.dll", "Ptr")
; DllCall 参数解释:方法,参数1类型,参数1参数值 ,参数2类型,参数2参数值,。。。。 返回值类型
global IsWindowOnDesktopNumberProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "IsWindowOnDesktopNumber", "Ptr")
global MoveWindowToDesktopNumberProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "MoveWindowToDesktopNumber", "Ptr")
global IsPinnedWindowProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "IsPinnedWindow", "Ptr")
global PinWindowProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "PinWindow", "Ptr")
global UnPinWindowProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "UnPinWindow", "Ptr")
global IsPinnedAppProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "IsPinnedApp", "Ptr")
global PinAppProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "PinApp", "Ptr")
global UnPinAppProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "UnPinApp", "Ptr")
; Main
mapDesktopsFromRegistry()
OutputDebug "loading] desktops: " . DesktopCount "current:" . CurrentDesktop
initDesktopMiniCount()
; return
;
; This function examines the registry to build an accurate list of the current virtual desktops and which one we're currently on.
; Current desktop UUID appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\1\VirtualDesktops
; List of desktops appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops
; On Windows 11 the current desktop UUID appears to be in the same location
; On previous versions in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\1\VirtualDesktops
mapDesktopsFromRegistry()
{
global CurrentDesktop, DesktopCount, DesktopBeforeScriptBoot, NextDesktop
; Get the current desktop UUID. Length should be 32 always, but there's no guarantee this couldn't change in a later Windows release so we check.
IdLength := 32
SessionId := getSessionId()
if (SessionId) {
CurrentDesktopId := RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops", 'CurrentVirtualDesktop')
if A_LastError != 0 {
; 这个注册表内容,在win11上没发现。
CurrentDesktopId := RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\" . SessionId . "\VirtualDesktops\CurrentVirtualDesktop")
}
if (CurrentDesktopId) {
IdLength := StrLen(CurrentDesktopId)
}
}
; Get a list of the UUIDs for all virtual desktops on the system
DesktopList := RegRead('HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops', 'VirtualDesktopIDs')
if (DesktopList) {
DesktopListLength := StrLen(DesktopList)
; Figure out how many virtual desktops there are
DesktopCount := floor(DesktopListLength / IdLength)
}
else {
DesktopCount := 1
}
; Parse the REG_DATA string that stores the array of UUID's for virtual desktops in the registry.
i := 0
while (CurrentDesktopId and i < DesktopCount) {
StartPos := (i * IdLength) + 1
DesktopIter := SubStr(DesktopList, StartPos, IdLength)
OutputDebug "The iterator is pointing at " . DesktopIter . "and count is" i
; Break out if we find a match in the list. If we didn't find anything, keep the
; old guess and pray we're still correct :-D.
if (DesktopIter = CurrentDesktopId) {
CurrentDesktop := i + 1
OutputDebug "Current desktop number is " . CurrentDesktop . "with an ID of" DesktopIter
break
}
i++
}
DesktopBeforeScriptBoot := CurrentDesktop
NextDesktop := CurrentDesktop + 1
NextDesktop := (NextDesktop > DesktopCount ? NextDesktop - DesktopCount : NextDesktop)
}
;
; This functions finds out ID of current session.
;
getSessionId()
{
try {
ProcessId := DllCall("GetCurrentProcessId", "UInt")
OutputDebug " Current Process Id:" . ProcessId
} catch Error as e {
OutputDebug "Error getting current process id:" . e.Message
return
}
SessionId := 0
try {
; 注意,Uint* ,后面是一个VarRef 即,&value
DllCall("ProcessIdToSessionId", "UInt", ProcessId, "UInt*", &SessionId)
OutputDebug "Current Session Id: " SessionId
} catch Error as e {
OutputDebug "Error getting session id:" e.Message
return
}
return SessionId
}
_switchDesktopToTarget(targetDesktop)
{
; Globals variables should have been updated via updateGlobalVariables() prior to entering this function
global CurrentDesktop, DesktopCount, LastOpenedDesktop, NextDesktop
; Don't attempt to switch to an invalid desktop
if (targetDesktop > DesktopCount || targetDesktop < 1 || targetDesktop == CurrentDesktop) {
OutputDebug "[invalid] target:" targetDesktop "current:" CurrentDesktop
return
}
LastOpenedDesktop := CurrentDesktop
; Fixes the issue of active windows in intermediate desktops capturing the switch shortcut and therefore delaying or stopping the switching sequence. This also fixes the flashing window button after switching in the taskbar. More info: https://github.com/pmb6tz/windows-desktop-switcher/pull/19
WinActivate "ahk_class Shell_TrayWnd"
; Go right until we reach the desktop we want
while (CurrentDesktop < targetDesktop) {
Send "{LWin down}{LCtrl down}{Right down}{LWin up}{LCtrl up}{Right up}"
CurrentDesktop++
OutputDebug "[right] target:" targetDesktop "current:" CurrentDesktop
}
; Go left until we reach the desktop we want
while (CurrentDesktop > targetDesktop) {
Send "{LWin down}{LCtrl down}{Left down}{Lwin up}{LCtrl up}{Left up}"
CurrentDesktop--
OutputDebug "[left] target: " targetDesktop "current:" CurrentDesktop
}
NextDesktop := CurrentDesktop + 1
NextDesktop := (NextDesktop > DesktopCount ? NextDesktop - DesktopCount : NextDesktop)
; Makes the WinActivate fix less intrusive
Sleep 50
focusTheForemostWindow(targetDesktop)
; associate with background picture
changeBackgroundWithDesktopId()
}
updateGlobalVariables()
{
; Re-generate the list of desktops and where we fit in that. We do this because
; the user may have switched desktops via some other means than the script.
mapDesktopsFromRegistry()
}
switchDesktopByNumber(targetDesktop)
{
global CurrentDesktop, DesktopCount
updateGlobalVariables()
_switchDesktopToTarget(targetDesktop)
}
switchDesktopToLastOpened()
{
global CurrentDesktop, DesktopCount, LastOpenedDesktop
updateGlobalVariables()
_switchDesktopToTarget(LastOpenedDesktop)
}
switchDesktopToNext()
{
global CurrentDesktop, DesktopCount, LastOpenedDesktop, NextDesktop
updateGlobalVariables()
_switchDesktopToTarget(NextDesktop)
}
switchDesktopToRight()
{
global CurrentDesktop, DesktopCount
updateGlobalVariables()
_switchDesktopToTarget(CurrentDesktop == DesktopCount ? 1 : CurrentDesktop + 1)
}
switchDesktopToLeft()
{
global CurrentDesktop, DesktopCount
updateGlobalVariables()
_switchDesktopToTarget(CurrentDesktop == 1 ? DesktopCount : CurrentDesktop - 1)
}
focusTheForemostWindow(targetDesktop) {
foremostWindowId := getForemostWindowIdOnDesktop(targetDesktop)
if foremostWindowId != "" && isWindowNonMinimized(foremostWindowId) {
WinActivate "ahk_id" . foremostWindowId
}
}
isWindowNonMinimized(windowId) {
return WinGetMinMax("ahk_id" . windowId) == -1
}
; ;测试
; ^!2:: getForemostWindowIdOnDesktop(1)
getForemostWindowIdOnDesktop(n)
{
n := n - 1 ; Desktops start at 0, while in script it's 1
; winIDList contains a list of windows IDs ordered from the top to the bottom for each desktop.
winIDList := WinGetList()
for windowID in winIDList
{
windowIsOnDesktop := DllCall(IsWindowOnDesktopNumberProc, "UInt", windowID, "UInt", n)
; Select the first (and foremost) window which is in the specified desktop.
if (windowIsOnDesktop == 1) {
return windowID
}
}
}
ToBase(n, b) {
return (n < b ? "" : ToBase(n // b, b)) . ((d := Mod(n, b)) < 10 ? d : Chr(d + 55))
}
MoveCurrentWindowToDesktop(desktopNumber) {
activeHwnd := WinExist("A")
DllCall(MoveWindowToDesktopNumberProc, "UInt", activeHwnd, "UInt", desktopNumber - 1)
switchDesktopByNumber(desktopNumber)
}
MoveCurrentWindowToLastOpened() {
global CurrentDesktop, DesktopCount, LastOpenedDesktop
activeHwnd := WinExist("A")
DllCall(MoveWindowToDesktopNumberProc, "UInt", activeHwnd, "UInt", LastOpenedDesktop - 1)
switchDesktopByNumber(LastOpenedDesktop)
}
MoveCurrentWindowToNext() {
global CurrentDesktop, DesktopCount, LastOpenedDesktop, NextDesktop
activeHwnd := WinExist("A")
DllCall(MoveWindowToDesktopNumberProc, "UInt", activeHwnd, "UInt", NextDesktop - 1)
switchDesktopByNumber(NextDesktop)
}
;
; This function creates a new virtual desktop and switches to it
;
createVirtualDesktop()
{
global CurrentDesktop, DesktopCount
Send "#^d"
DesktopCount++
CurrentDesktop := DesktopCount
OutputDebug "[create] desktops:" . DesktopCount . "current:" . CurrentDesktop
}
;
; This function deletes the current virtual desktop
;
deleteVirtualDesktop()
{
global CurrentDesktop, DesktopCount, LastOpenedDesktop
Send "#^{F4}"
if (LastOpenedDesktop >= CurrentDesktop) {
LastOpenedDesktop--
}
DesktopCount--
CurrentDesktop--
OutputDebug "[delete] desktops:" . DesktopCount . "current:" . CurrentDesktop
}
changeBackgroundWithDesktopId()
{
global CurrentDesktop, BackgroundPicPaths, AutoAssociateBackgroundWithDesktop
filePath := BackgroundPicPaths[CurrentDesktop]
isRelative := (substr(filePath, 1, 1) == ".")
if (isRelative) {
filePath := (A_WorkingDir . substr(filePath, 2))
}
if (AutoAssociateBackgroundWithDesktop and filePath and FileExist(filePath)) {
DllCall("SystemParametersInfo", "UInt", 0x14, "UInt", 0, "Str", filePath, "UInt", 1)
}
}
initDesktopMiniCount()
{
global DeskTopMiniCount, DesktopCount, CurrentDesktop, DesktopBeforeScriptBoot
i := DeskTopMiniCount - DesktopCount
j := i
while (i-- > 0) {
createVirtualDesktop()
OutputDebug "[initCount] DeskTopMiniCount: " . DeskTopMiniCount . "desktops:" . DesktopCount . "current:" . CurrentDesktop
}
if (j > 0 && DesktopBeforeScriptBoot > 0) {
_switchDesktopToTarget(DesktopBeforeScriptBoot > DesktopCount ? DesktopCount : DesktopBeforeScriptBoot)
}
}
_GetCurrentWindowID() {
return WinGetID("A")
}
_GetCurrentWindowTitle() {
return WinGetTitle("A")
}
_GetCurrentWindowProcess() {
return WinGetProcessName("A")
}
OnTogglePinOnTopPress() {
_notif(_GetCurrentWindowProcess(), "Toggled 'Pin On Top'")
WinSetAlwaysOnTop -1, "A"
}
OnTogglePinWindowPress() {
windowID := _GetCurrentWindowID()
processName := _GetCurrentWindowProcess()
if (_GetIsWindowPinned(windowID)) {
_UnpinWindow(windowID)
_notif(processName, "Unpinned Window")
}
else {
_PinWindow(windowID)
_notif(processName, "Pinned Window")
}
}
OnTogglePinAppPress() {
windowID := _GetCurrentWindowID()
processName := _GetCurrentWindowProcess()
if (_GetIsAppPinned(windowID)) {
_UnpinApp(windowID)
_notif(processName, "Unpinned App")
}
else {
_PinApp(windowID)
_notif(processName, "Pinned App")
}
}
_PinWindow(windowID := "") {
_CallWindowProc(PinWindowProc, windowID)
}
_UnpinWindow(windowID := "") {
_CallWindowProc(UnpinWindowProc, windowID)
}
_GetIsWindowPinned(windowID := "") {
return _CallWindowProc(IsPinnedWindowProc, windowID)
}
_PinApp(windowID := "") {
_CallWindowProc(PinAppProc, windowID)
}
_UnpinApp(windowID := "") {
_CallWindowProc(UnpinAppProc, windowID)
}
_GetIsAppPinned(windowID := "") {
return _CallWindowProc(IsPinnedAppProc, windowID)
}
_CallWindowProc(proc, window := "") {
if (window == "") {
window := _GetCurrentWindowID()
}
return DllCall(proc, "UInt", window)
}
_notif(txt, title := "") {
HideTrayTip()
title := _TruncateString(title, 100)
TrayTip txt, title, "Iconi Mute"
}
HideTrayTip() {
TrayTip ; 尝试以普通的方式隐藏它.
if SubStr(A_OSVersion, 1, 3) = "10." {
A_IconHidden := true
Sleep 200 ; 可能有必要调整 sleep 的时间.
A_IconHidden := false
}
}
_TruncateString(string := "", n := 10) {
return (StrLen(string) > n ? SubStr(string, 1, n - 3) . "..." : string)
}