-
Notifications
You must be signed in to change notification settings - Fork 4
/
LNCHR-Main.ahk
403 lines (298 loc) · 11.1 KB
/
LNCHR-Main.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
#Requires Autohotkey v2.0+
#SingleInstance Force
#WinActivateForce
Suspend 1 ; suspend all hotkeys until loaded
global lngui_props := object() ; stored in an object to allow for easier access in functons (objects are global)
lngui_props.calceqfile := "LNCHR-CalcEqns.txt" ; used ib Funcs
#Include LNCHR-Commands.ahk
#Include LNCHR-Funcs.ahk
#Include QuickTips.ahk
TraySetIcon("rocketlnchr.ico")
SetCapsLockState("AlwaysOff")
toggleCapsLock(){
SetCapsLockState !GetKeyState('CapsLock', 'T')
}
; Allow normal CapsLock functionality to be toggled by Alt+CapsLock, or shift, or crl
!CapsLock::
^CapsLock::
+CapsLock::toggleCapsLock()
; Some flags for commands
UsingMainWorkComputer := A_ComputerName == "A SPECIFIC COMPUTER NAME" ; Global flag for using main work computer, changes title
UsingAnyWorkComputer := InStr(A_ComputerName, "MAYBE YOUR WORK COMPUTER PREFIXES") == 1 ; Global flag for using any work computer for select commands
; some settings
lngui_props.show_commands_tips := True
lngui_props.query_autocomplete := True
; ______________________________________________________________________________ gui properties ___________
; lngui is the launcher gui, pronounced linguini
lngui_props.title := " 🚀 LNCHR" ; restore the title
if UsingMainWorkComputer
lngui_props.title := lngui_props.title " 💼"
lngui_props.memfile := "LNCHR-Memory.ini"
if lngui_props.show_commands_tips
lngui_props.commands := FileRead("HELP-Commands.txt") ; for tooltip prompt when in main
reset_lngui_props(mainoff := "main") { ; reset flags and choose what state you want
lngui_props.state := mainoff ; state of lngui
lngui_props.qfunc := "" ; the function name to be called when in query mode
lngui_props.qclose := True ; close after running query flag
lngui_props.click_exit := False ; click to exit gui (when in main, keep gui open in query)
lngui_props.calced := False ; has calculator been used before?
lngui_props.memind := 0 ; memory index, changes with up/down arrow
lngui_props.memcat := "Calculate" ; memory category, changes with query mode
lngui_props.memarr := [] ; memory array accessed with arrow keys
lngui_props.memstr := "" ; memory string (separated by lines) accessed with arrow keys
}
reset_lngui_props("off") ; initialize flags, set state to off
global key_presses := object() ; log number of keypresses, used for multi-tapping of caps lock
key_presses.caps := 0
is_lngui_state(state) { ; check state of lngui
return (lngui_props.state == state)
}
is_lngui_on() { ; check if gui is on
return ! is_lngui_state("off")
}
set_lngui_state(state) { ; set state of lngui
lngui_props.state := state
}
; ______________________________________________________________________________ make the gui ___________
build_lngui(){
global lngui
lngui := Gui()
acccolor := "FFB900" ; accentcolor
backcolor := "1d1f21" ; background color
common_gui_opts := "xm w400 cc5c8c6 -E0x200"
lngui.Opt("AlwaysOnTop -SysMenu -caption +Border ")
lngui.SetFont("s14", "Verdana")
lngui.Title := lngui_props.title
lngui.BackColor := backcolor
lngui._text := lngui.AddText(common_gui_opts " c" acccolor , lngui_props.title)
lngui._edit := lngui.AddEdit(common_gui_opts " Background" backcolor, "") ; the main input box
lngui._edit.OnEvent("Change", lngui_edit_chng)
lngui._butt := lngui.AddButton("xp-0 yp-0 w0 h0 +default") ; add a hidden button to allow enter to submit contents
lngui._butt.OnEvent("Click", lngui_query_enter) ; see function below
lngui.OnEvent("Escape", close_lngui)
reset_lngui_props("main")
lngui_props.click_exit := True
WinSetTransparent 230, lngui.Hwnd
lngui.Show("autosize")
}
; ______________________________________________________________________________ interfacing with gui input/edit box ___________
add_lngui_input(s:=""){
lngui._edit.value := lngui._edit.value . s
}
set_lngui_input(s:=""){
lngui._edit.value := s
}
get_lngui_input() {
try {
return lngui._edit.value
} catch as e {
}
}
lngui_edit_chng(GuiCtrlObj, Info) {
if is_lngui_state("main") {
lngui_run_commands(get_lngui_input()) ;
lngui_show_command_tips()
} else if lngui_props.query_autocomplete {
lngui_autocomplete()
}
return
}
lngui_show_command_tips() { ; show tooltip with commands
if lngui_props.show_commands_tips {
tip := filter_lines(get_lngui_input(), lngui_props.commands)
ToolTip(tip, 500, 0)
SetTimer(() => ToolTip(), -10000)
}}
; ______________________________________________________________________________ open/close gui ___________
close_lngui(*) {
ToolTip()
reset_lngui_props("off")
lngui.Destroy()
}
lngui_activable(){ ; conditions to be met to allow hotkey
try {
return !InStr(WinGetTitle("A"), "Remote Desktop")
}
return True
}
; Opening and Closing GUI with Caps Lock button
; Single press toggles gui on/off (or closes app that requires double press)
; Double press within 500 ms -> send win+alt+space to open power launcher
; Disable if Remote Desktop is active (to allow other instance of this app take over)
#HotIf lngui_activable() ;; do not push capslock stuff to remote desktop
CapsLock::
{
if is_lngui_state("query") {
if !WinActive(lngui.Hwnd) { ; return focus to lnchr
WinActivate(lngui.Hwnd)
return
}
close_lngui() ; return to main
build_lngui()
return
}
key_presses.caps := key_presses.caps + 1
if key_presses.caps == 1 {
SetTimer(caps_presses_timer, -500) ; set timer on first press
}
if WinActive("PowerToys.PowerLauncher") {
Send "{Esc}"
return
}
if is_lngui_state("off"){
build_lngui()
return
}
if is_lngui_on(){
close_lngui() ; don't return here as a second key press might have been used
}
if (key_presses.caps > 1){ ; do this when double press
Send "#!{Space}" ; win+alt+space, opens MS Power Toys Run
close_lngui()
return
}
return
}
#HotIf
caps_presses_timer() { ; reset cap timer to 0
key_presses.caps := 0
}
#HotIf lngui_props.click_exit ; exit gui by clicking outside of it
LButton::
RButton::
{
MouseGetPos , , &id, &control
if (control = ""){
close_lngui()
}
return
}
#HotIf
; ______________________________________________________________________________ hotkeys while gui is opem ___________
#HotIf is_lngui_on() ; hotkeys for when gui is on
Tab::{
tryrun("C:\Program Files\Everything\Everything.exe")
close_lngui()
return
}
LWin::{
tryrun("ipython")
close_lngui()
return
}
Up::{
lngui_arr_press(1)
return
}
Down::{
lngui_arr_press(-1)
return
}
#HotIf
; ______________________________________________________________________________ query mode ___________
lngui_enable_query(query_title, qfunc) {
ToolTip()
lngui_props.click_exit := False
set_lngui_state("query")
lngui._text.Value := lngui_props.title " 🠖 " query_title
set_lngui_input()
lngui_props.qfunc := [qfunc] ; for some reason, this must be stored in an array, maybe to make new obj?
lngui_props.memcat := query_title
lngui_props.memarr := []
lngui_props.memstr := ""
if InStr(IniRead(lngui_props.memfile), query_title) {
lngui_props.memstr := IniRead(lngui_props.memfile, query_title) "`n"
lngui_props.memarr := StrSplit(lngui_props.memstr, "`n")
}
}
lngui_query_enter(*) { ; gui_call_sub_funcs
if is_lngui_state("query") {
if get_lngui_input() == "clr" { ; type clear to delete memory
IniDelete(lngui_props.memfile, lngui_props.memcat)
} else {
lngui_props.qfunc[1](get_lngui_input())
if lngui_props.memcat != "Calculate" {
IniWrite(get_lngui_input(), lngui_props.memfile, lngui_props.memcat)
}
}
}
if lngui_props.qclose ; by default, close after query
close_lngui()
}
lngui_enable_calc(){
lngui_props.qclose := False ; don't close gui after submission
if ! lngui_props.calced {
lngui_enable_query("Calculate", Calculate)
lngui_calctext := lngui.AddText("yp+30 xm w220 cc5c8c6 -E0x200 vCalcText ", " ")
}
lngui_props.calced := True
lngui_props.memcat := "Calculate"
lngui.Show("autosize")
}
set_calc_text(result) {
lngui["CalcText"].Value := " = " result
}
lngui_arr_press(ud) {
if is_lngui_state("main") {
input := get_lngui_input()
if input {
lngui_run_commands(input " ") ; try to activate query by adding space, eg. o ↓ goes through outlook history
}
if not is_lngui_state("query") {
lngui_enable_calc() ; if was not put into query, assume calc enables
}
} else {
lngui_props.memind -= ud
set_lngui_input(lngui_get_query_mem(lngui_props.memcat, lngui_props.memind))
move_caret(0,-1)
}
}
lngui_get_query_mem(cat, ind) { ; retrieve memory based on index
if lngui_props.memarr.Length == 0 {
return ""
}
if ind < 1 {
ind := 1
}
if ind > lngui_props.memarr.Length {
ind := lngui_props.memarr.Length
}
lngui_props.memind := ind
return lngui_props.memarr[ind]
}
lngui_autocomplete() {
if ((GetKeyState("Delete", "P")) || (GetKeyState("Backspace", "P")))
return ; dont autocomplete on delete or backspace
input := get_lngui_input() ; stash current input
res := filter_lines(input, lngui_props.memstr, True) ;
if res == "" or input == res or StrLen(input) > StrLen(res)
return ; don't autocomplete if exact match or no match
currCaret := SendMessage(0xB0,0,0,lngui._edit.hwnd) & 0xFFFF ; stash caret pos
set_lngui_input(res) ; set to the suggest word
move_caret(currCaret,-1) ; highlight from what was originally entered to end
}
; ______________________________________________________________________________ utils ___________
move_caret(s:=-1, e:=s) { ; move caret from start to end, -1 means end of input
if s < 0 {
s := StrLen(lngui._edit.value) + 1 - s
}
if e < 0 {
e := StrLen(lngui._edit.value) + 1 - e
}
SendMessage(0xB1, s, e,, lngui._edit.Hwnd)
}
filter_lines(q, s, one:=False) { ; used to filter lines from a string "s", each line starting with "q"
if q == ""
return ""
prepend := ""
loop Parse q { ; iterate through characters
s := RegExReplace(s, "m)^" prepend "[^" A_LoopField "].*\R", "") ; remove lines where character doesn't match
prepend := prepend "." ; adjusts position for the next character
}
if s == ""
return s
if one
s := StrSplit(s,["`r","`n"])[1]
return s
}
Suspend 0 ; re-enable hot keys