forked from Ixiko/AHK-libs-and-classes-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_ExplorerTool.ahk
317 lines (220 loc) · 6.85 KB
/
class_ExplorerTool.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
Class ExplorerTool {
SetViewMode( pMode := 0 ) {
Static viewModes := {"Icon": 1, "SmallIcon":2, "List":3, "Details":4, "Thumbs":5, "Tile":6, "ThumbStrip":7}
activeSFV := this.GetActiveShellFolderView()
If (activeSFV == "")
Return
If (viewModes[pMode] != "")
pMode := viewModes[pMode]
If ((pMode < 1) || (pMode > 7))
Return
activeSFV.CurrentViewMode := pMode
}
CreateNewFolder( pName := "New Folder" ) {
activeSFV := this.GetActiveShellFolderView()
If (activeSFV == "")
Return
newFolderNameX := pName
Loop {
isAlreadyExists := activeSFV.Folder.ParseName(newFolderNameX)
If (isAlreadyExists == "") {
activeSFV.Folder.NewFolder(newFolderNameX)
Break
}
newFolderNameX := Format("{1} ({2})", newFolderName, A_Index)
}
newFolderAsItem := activeSFV.Folder.ParseName(newFolderNameX)
activeSFV.SelectItem(newFolderAsItem, 3|4|8|16)
}
Class LiveQuery {
Static __Parent := ""
Static guiControls := ""
Static activeSFV := ""
Static focusHandler := ""
Init() {
If (this.__Parent == "") {
parentClassName := RegexReplace(this.__Class, "\.[^\.]+$", "")
this.__Parent := %parentClassName%
}
If (this.focusHandler == "") {
this.focusHandler := ObjBindMethod(this, "GuiCheckFocus", this)
}
If (this.guiControls != "")
Return
ETGuiWindowTitle := "ExplorerTool Filter Query"
ETGuiTextHint := "Enter query. You can use wildcards * ? #, ""quotes"", pipe|bar,"
. "`n" . "size filters >10k <10m and folder keys keys /d /D"
Gui, ETGui: New , HWNDhETGuiWindow , % ETGuiWindowTitle
Gui, ETGui: Add, Text , w500 HWNDhETGuiText , % ETGuiTextHint
Gui, ETGui: Add, Edit , w500 HWNDhETGuiEdit , % ""
Gui, ETGui: +ToolWindow
this.guiControls := {}
this.guiControls["Window"] := hETGuiWindow
this.guiControls["Edit" ] := hETGuiEdit
Gui, % ("ETGui:+Label" . this.__Class . ".GuiOn")
editHandler := ObjBindMethod(this, "GuiOnEdit", this)
GuiControl, ETGui: +g, % hETGuiEdit, % editHandler
enterHandler := ObjBindMethod(this, "GuiOnClose", this)
HotKey, IfWinActive, % ("ahk_id " . this.guiControls["Window"])
HotKey, Enter, % enterHandler
HotKey, If
}
GuiOnClose() {
Gui, ETGui: Hide
}
GuiOnEscape() {
Gui, ETGui: Hide
}
GuiCheckFocus() {
If (WinActive("A") != this.guiControls["Window"]) {
this.GuiOnClose()
Return
}
focusHandler := this.focusHandler
SetTimer, % focusHandler, -250
}
GuiOnEdit() {
GuiControlGet, queryText,, % this.guiControls["Edit"]
this.__Parent.SelectFiltered(this.activeSFV, queryText)
}
Show() {
this.Init()
this.activeSFV := this.__Parent.GetActiveShellFolderView()
If (this.activeSFV == "")
Return
Gui, ETGui: Show, Y0
GuiControl,, % this.guiControls["Edit"]
GuiControl, Focus, % this.guiControls["Edit"]
WinWaitActive % "ahk_id " this.guiControls["Window"]
this.GuiCheckFocus()
}
}
SimpleQuery() {
activeSFV := this.GetActiveShellFolderView()
If (activeSFV == "")
Return
InputBox, query, % "Enter filter query"
, % "You can use wildcards *, ? and # for digits"
. "`nInclude folders with /d or select only folders with /D"
. "`nFilter size with >100k and <100m"
. "`nUse quotes if query ""contains spaces"""
. "`nSelect multiple|several variants with pipe"
,,,, , 0
If (ErrorLevel == 1)
Return
this.SelectFiltered(activeSFV, query)
}
GetActiveShellFolderView() {
activeWindowHWND := WinActive("A")
comShell := ComObjCreate("Shell.Application")
comShellWindows := comShell.Windows()
comActiveShellView := ""
Loop % comShellWindows.Count {
comShellWindow := comShellWindows.Item(A_Index-1)
If (comShellWindow.HWND == activeWindowHWND) {
comActiveShellView := comShellWindow.Document
Break
}
}
Return comActiveShellView
}
SelectFiltered(pSFView, pQuery) {
Critical
If (pSFView == "")
Return
filter := this.Filter.Prepare(pQuery)
comFolderItems := pSFView.Folder.Items
Loop % comFolderItems.Count {
item := comFolderItems.Item(A_Index-1)
itemMatch := this.Filter.Match(item, filter)
pSFView.SelectItem(item, itemMatch)
}
}
Class Filter {
Static escapeChars := Format("([{1}])", RegexReplace("^$.+\()[]{}", "(.)", "\$1"))
Static sizeFactors := {B:1, K:1024, M:1024**2, G:1024**3}
Static sizeRegexp := "Oi)^(<|>)(\d+)([bkmg]?)$"
Prepare( pQueryText ) {
filter := {tokens: []}
queryDequote := StrSplit(pQueryText, """")
Loop % queryDequote.Length() {
newToken := queryDequote[A_Index]
newToken := RegexReplace(newToken, this.escapeChars, "\$1")
If (A_Index & 1 == 0) {
filter.tokens.Push(newToken)
Continue
}
tokens := StrSplit(newToken, " ")
For i, token In tokens {
If (token == "")
Continue
token := StrReplace(token, "?", ".")
token := StrReplace(token, "*", ".*?")
token := StrReplace(token, "#", "\d")
filter.tokens.Push(token)
}
}
filter.folders := 0
filter.sizeMin := -1
filter.sizeMax := -1
tokensCount := filter.tokens.Length()
Loop % tokensCount {
tokenIndex := tokensCount + 1 - A_Index
token := filter.tokens[tokenIndex]
If (token ~= "i)^/d$") {
filter.folders := (token == "/d") ? 1 : 2
filter.tokens.RemoveAt(tokenIndex)
Continue
}
RegexMatch(token, this.sizeRegexp, tokenSize)
If (tokenSize) {
sizeFactor := tokenSize[3] ? this.sizeFactors[tokenSize[3]] : 1
totalSize := Floor(tokenSize[2]) * sizeFactor
If (tokenSize[1] == ">") {
filter.sizeMin := totalSize
} Else {
filter.sizeMax := totalSize
}
filter.tokens.RemoveAt(tokenIndex)
}
}
Return filter
}
Match( pItem, pFilter ) {
itemIsFolder := (pItem.IsFolder && (pItem.Size == 0))
If (((pFilter.folders == 0) && (itemIsFolder == True))
|| ((pFilter.folders == 2) && (itemIsFolder == False))
|| ((pFilter.sizeMin != -1) && (pItem.Size < pFilter.sizeMin))
|| ((pFilter.sizeMax != -1) && (pItem.Size > pFilter.sizeMax)))
Return False
If ((pFilter.tokens.Length() == 0)
&& (pFilter.sizeMin == -1)
&& (pFilter.sizeMax == -1))
Return False
itemName := pItem.Name
For i, token In pFilter.tokens {
If ((itemName ~= ("i)" . token)) == 0) {
Return False
}
}
Return True
}
}
}
#If WinActive("ahk_class ExploreWClass") || WinActive("ahk_class CabinetWClass")
^q::
; ExplorerTool.SimpleQuery()
ExplorerTool.LiveQuery.Show()
Return
^n::
ExplorerTool.CreateNewFolder()
Return
^1::
ExplorerTool.SetViewMode("List")
Return
^2::
ExplorerTool.SetViewMode("Details")
Return
^3::
ExplorerTool.SetViewMode("Thumbs")
Return