-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimize_To_Tray.ahk
233 lines (200 loc) · 7.6 KB
/
Minimize_To_Tray.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
#SingleInstance Force
Menu, Tray, Icon, %A_ScriptDir%\Lib\Hidden.png
#NoTrayIcon
; CONFIGURATION SECTION: Change the below values as desired.
; This is the maximum number of windows to allow to be hidden (having a
; limit helps performance):
mwt_MaxWindows = 50
; This is the hotkey used to hide the active window:
mwt_Hotkey = !y ; Win+y
; This is the hotkey used to unhide the last hidden window:
mwt_UnHotkey = !u ; Win+u
; If you prefer to have the tray menu empty of all the standard items,
; such as Help and Pause, use N. Otherwise, use Y:
mwt_StandardMenu = Y
; These next few performance settings help to keep the action within the
; #HotkeyModifierTimeout period, and thus avoid the need to release and
; press down the hotkey's modifier if you want to hide more than one
; window in a row. These settings are not needed you choose to have the
; script use the keyboard hook via #InstallKeybdHook or other means:
#HotkeyModifierTimeout 100
SetWinDelay 10
SetKeyDelay 0
; END OF CONFIGURATION SECTION (do not make changes below this point
; unless you want to change the basic functionality of the script).
Hotkey, %mwt_Hotkey%, mwt_Minimize
Hotkey, %mwt_UnHotkey%, mwt_UnMinimize
; If the user terminates the script by any means, unhide all the
; windows first:
OnExit, mwt_RestoreAllThenExit
if mwt_StandardMenu = Y
Menu, Tray, Add
else
{
Menu, Tray, NoStandard
Menu, Tray, Add, Exit and Unhide All, mwt_RestoreAllThenExit
}
Menu, Tray, Add, &Unhide All Hidden Windows, mwt_RestoreAll
Menu, Tray, Add ; Another separator line to make the above more special.
if A_AhkVersion = ; Since it's blank, version is older than 1.0.22.
mwt_MaxLength = 100
else
mwt_MaxLength = 260 ; Reduce this to restrict the width of the menu.
return ; End of auto-execute section.
mwt_Minimize:
if mwt_WindowCount >= %mwt_MaxWindows%
{
MsgBox No more than %mwt_MaxWindows% may be hidden simultaneously.
return
}
; Set the "last found window" to simplify and help performance.
; Since in certain cases it is possible for there to be no active window,
; a timeout has been added:
WinWait, A,, 2
if ErrorLevel <> 0 ; It timed out, so do nothing.
return
; Otherwise, the "last found window" has been set and can now be used:
WinGet, mwt_ActiveID, ID
WinGetTitle, mwt_ActiveTitle
WinGetClass, mwt_ActiveClass
if mwt_ActiveClass in Shell_TrayWnd,Progman
{
MsgBox The desktop and taskbar cannot be hidden.
return
}
; Because hiding the window won't deactivate it, activate the window
; beneath this one (if any). I tried other ways, but they wound up
; activating the task bar. This way sends the active window (which is
; about to be hidden) to the back of the stack, which seems best:
Send, !{esc}
; Hide it only now that WinGetTitle/WinGetClass above have been run (since
; by default, those commands cannot detect hidden windows):
WinHide
; If the title is blank, use the class instead. This serves two purposes:
; 1) A more meaningful name is used as the menu name.
; 2) Allows the menu item to be created (otherwise, blank items wouldn't
; be handled correctly by the various routines below).
if mwt_ActiveTitle =
mwt_ActiveTitle = ahk_class %mwt_ActiveClass%
; Ensure the title is short enough to fit. mwt_ActiveTitle also serves to
; uniquely identify this particular menu item.
StringLeft, mwt_ActiveTitle, mwt_ActiveTitle, %mwt_MaxLength%
; In addition to the tray menu requiring that each menu item name be
; unique, it must also be unique so that we can reliably look it up in
; the array when the window is later unhidden. So make it unique if it
; isn't already:
Loop, %mwt_MaxWindows%
{
if mwt_WindowTitle%A_Index% = %mwt_ActiveTitle%
{
; Match found, so it's not unique.
; First remove the 0x from the hex number to conserve menu space:
StringTrimLeft, mwt_ActiveIDShort, mwt_ActiveID, 2
StringLen, mwt_ActiveIDShortLength, mwt_ActiveIDShort
StringLen, mwt_ActiveTitleLength, mwt_ActiveTitle
mwt_ActiveTitleLength += %mwt_ActiveIDShortLength%
mwt_ActiveTitleLength += 1 ; +1 the 1 space between title & ID.
if mwt_ActiveTitleLength > %mwt_MaxLength%
{
; Since menu item names are limted in length, trim the title
; down to allow just enough room for the Window's Short ID at
; the end of its name:
TrimCount = %mwt_ActiveTitleLength%
TrimCount -= %mwt_MaxLength%
StringTrimRight, mwt_ActiveTitle, mwt_ActiveTitle, %TrimCount%
}
; Build unique title:
mwt_ActiveTitle = %mwt_ActiveTitle% %mwt_ActiveIDShort%
break
}
}
; First, ensure that this ID doesn't already exist in the list, which can
; happen if a particular window was externally unhidden (or its app unhid
; it) and now it's about to be re-hidden:
mwt_AlreadyExists = n
Loop, %mwt_MaxWindows%
{
if mwt_WindowID%A_Index% = %mwt_ActiveID%
{
mwt_AlreadyExists = y
break
}
}
; Add the item to the array and to the menu:
if mwt_AlreadyExists = n
{
Menu, Tray, add, %mwt_ActiveTitle%, RestoreFromTrayMenu
mwt_WindowCount += 1
Loop, %mwt_MaxWindows% ; Search for a free slot.
{
; It should always find a free slot if things are designed right.
if mwt_WindowID%A_Index% = ; An empty slot was found.
{
mwt_WindowID%A_Index% = %mwt_ActiveID%
mwt_WindowTitle%A_Index% = %mwt_ActiveTitle%
break
}
}
}
return
RestoreFromTrayMenu:
Menu, Tray, delete, %A_ThisMenuItem%
; Find window based on its unique title stored as the menu item name:
Loop, %mwt_MaxWindows%
{
if mwt_WindowTitle%A_Index% = %A_ThisMenuItem% ; Match found.
{
StringTrimRight, IDToRestore, mwt_WindowID%A_Index%, 0
WinShow, ahk_id %IDToRestore%
WinActivate ahk_id %IDToRestore% ; Sometimes needed.
mwt_WindowID%A_Index% = ; Make it blank to free up a slot.
mwt_WindowTitle%A_Index% =
mwt_WindowCount -= 1
break
}
}
return
;; This will pop the last minimized window off the stack and unhide it.
mwt_UnMinimize:
;; Make sure there's something to unhide.
if mwt_WindowCount > 0
{
;; Get the id of the last window minimized and unhide it
StringTrimRight, IDToRestore, mwt_WindowID%mwt_WindowCount%, 0
WinShow, ahk_id %IDToRestore%
WinActivate ahk_id %IDToRestore%
;; Get the menu name of the last window minimized and remove it
StringTrimRight, MenuToRemove, mwt_WindowTitle%mwt_WindowCount%, 0
Menu, Tray, delete, %MenuToRemove%
;; clean up our 'arrays' and decrement the window count
mwt_WindowID%mwt_WindowCount% =
mwt_WindowTitle%mwt_WindowCount% =
mwt_WindowCount -= 1
}
return
mwt_RestoreAllThenExit:
Gosub, mwt_RestoreAll
ExitApp ; Do a true exit.
mwt_RestoreAll:
Loop, %mwt_MaxWindows%
{
if mwt_WindowID%A_Index% <>
{
StringTrimRight, IDToRestore, mwt_WindowID%A_Index%, 0
WinShow, ahk_id %IDToRestore%
WinActivate ahk_id %IDToRestore% ; Sometimes needed.
; Do it this way vs. DeleteAll so that the sep. line and first
; item are retained:
StringTrimRight, MenuToRemove, mwt_WindowTitle%A_Index%, 0
Menu, Tray, delete, %MenuToRemove%
mwt_WindowID%A_Index% = ; Make it blank to free up a slot.
mwt_WindowTitle%A_Index% =
mwt_WindowCount -= 1
}
if mwt_WindowCount = 0
break
}
return
!del::
ExitApp
Return