-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.pb
518 lines (497 loc) · 24.1 KB
/
main.pb
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
IncludeFile "../pb-macos-globalhotkeys/ghk.pbi"
IncludeFile "const.pb"
EnableExplicit
Define statusBar.i,statusItem.i,i.l
Define application.i = CocoaMessage(0,0,"NSApplication sharedApplication")
Define workspace.i = CocoaMessage(0,0,"NSWorkspace sharedWorkspace")
Define ev.i
Define editExistent.b = #False
Define updateCheckThread.i
Define updateVer.s = #myVer
Define updateDetails.s = ""
Define gadgetState.l
Define testRunResult.testRunResults
Define activeSelector.i = -1
Define previousHotkey.s
Define cur.NSPoint
Define setWorkdirWithCD.b = #False
Define.i wndW,wndH,wndX,wndY
IncludeFile "helpers.pb"
IncludeFile "proc.pb"
IncludeFile "applescript.pb"
initResources()
globalHK::Init()
OpenWindow(#wnd,#PB_Ignore,#PB_Ignore,0,0,#myName,#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
CocoaMessage(0,CocoaMessage(0,WindowID(#wnd),"standardWindowButton:",#NSWindowButtonMinimize),"setHidden:",#YES)
CocoaMessage(0,CocoaMessage(0,WindowID(#wnd),"standardWindowButton:",#NSWindowButtonMaximize),"setHidden:",#YES)
PanelGadget(#gadTabs,5,0,590,300)
CocoaMessage(0,GadgetID(#gadTabs),"setFocusRingType:",1)
CocoaMessage(0,WindowID(#wnd),"setAutorecalculatesKeyViewLoop:",#NO)
AddGadgetItem(#gadTabs,0,"Shortcuts")
ListIconGadget(#gadShortcuts,5,0,560,220,"Shortcut",80,#PB_ListIcon_CheckBoxes)
SetGadgetAttribute(#gadShortcuts,#PB_ListIcon_List,#True)
CocoaMessage(0,GadgetID(#gadShortcuts),"setFocusRingType:",1)
AddGadgetColumn(#gadShortcuts,2,"Action",120)
AddGadgetColumn(#gadShortcuts,3,"Command",1)
AddGadgetColumn(#gadShortcuts,4,"Workdir",1)
ListIconGadgetHideColumn(#gadShortcuts,0,#True)
ButtonImageGadget(#gadAdd,460,222,36,34,ImageID(#resAdd))
ButtonImageGadget(#gadEdit,496,222,36,34,ImageID(#resEdit))
ButtonImageGadget(#gadDel,532,222,36,34,ImageID(#resDel))
ButtonImageGadget(#gadTest,460,222,36,34,ImageID(#resTest))
ButtonImageGadget(#gadApply,496,222,36,34,ImageID(#resApply))
ButtonImageGadget(#gadCancel,532,222,36,34,ImageID(#resCancel))
ButtonImageGadget(#gadUp,2,222,36,34,ImageID(#resUp))
ButtonImageGadget(#gadDown,38,222,36,34,ImageID(#resDown))
TextGadget(#gadTestNote,210,230,244,20,"Don't forget to do a test run -->",#PB_Text_Right)
CocoaMessage(0,GadgetID(#gadAdd),"setFocusRingType:",1)
CocoaMessage(0,GadgetID(#gadEdit),"setFocusRingType:",1)
CocoaMessage(0,GadgetID(#gadDel),"setFocusRingType:",1)
CocoaMessage(0,GadgetID(#gadApply),"setFocusRingType:",1)
CocoaMessage(0,GadgetID(#gadCancel),"setFocusRingType:",1)
CocoaMessage(0,GadgetID(#gadUp),"setFocusRingType:",1)
CocoaMessage(0,GadgetID(#gadDown),"setFocusRingType:",1)
CocoaMessage(0,GadgetID(#gadAdd),"setBordered:",0)
CocoaMessage(0,GadgetID(#gadEdit),"setBordered:",0)
CocoaMessage(0,GadgetID(#gadDel),"setBordered:",0)
CocoaMessage(0,GadgetID(#gadTest),"setBordered:",0)
CocoaMessage(0,GadgetID(#gadApply),"setBordered:",0)
CocoaMessage(0,GadgetID(#gadCancel),"setBordered:",0)
CocoaMessage(0,GadgetID(#gadUp),"setBordered:",0)
CocoaMessage(0,GadgetID(#gadDown),"setBordered:",0)
CreatePopupMenu(#columnMenu)
MenuItem(#columnMenuShortcut,"Shortcut")
MenuItem(#columnMenuAction,"Action")
MenuItem(#columnMenuCommand,"Command")
MenuItem(#columnMenuWorkdir,"Workdir")
MenuBar()
MenuItem(#columnMenuReset,"Reset")
AddGadgetItem(#gadTabs,1,"Preferences")
TextGadget(#gadPrefShellCap,10,12,45,20,"Shell:")
StringGadget(#gadPrefShell,55,10,210,20,"")
ButtonGadget(#gadPrefShellDefault,270,9,95,25,"default")
TextGadget(#gadPrefShellNote,10,45,370,200,~"Keep in mind that you have to set a correct $PATH variable in your shell config (~/.bash_profile or ~/.zshrc, etc).\n\nYou can use the test run functionality when you create new shortcut to check if everything works fine.")
CocoaMessage(0,GadgetID(#gadPrefShell),"setFocusRingType:",1)
FrameGadget(#gadPrefFrame,380,0,180,250,"")
CheckBoxGadget(#gadPrefStatusBar,390,10,160,20,"Show icon in status bar")
CocoaMessage(0,GadgetID(#gadPrefStatusBar),"setFocusRingType:",1)
CheckBoxGadget(#gadPrefAutostart,390,35,160,20,"Start on login")
CocoaMessage(0,GadgetID(#gadPrefAutostart),"setFocusRingType:",1)
CheckBoxGadget(#gadPrefCheckUpdate,390,60,160,20,"Check for updates")
CocoaMessage(0,GadgetID(#gadPrefCheckUpdate),"setFocusRingType:",1)
CheckBoxGadget(#gadPrefPopulateMenu,390,85,160,20,"Show actions in menu")
CocoaMessage(0,GadgetID(#gadPrefPopulateMenu),"setFocusRingType:",1)
CheckBoxGadget(#gadPrefShowHtk,390,110,160,20,"Show hotkeys in menu")
CocoaMessage(0,GadgetID(#gadPrefShowHtk),"setFocusRingType:",1)
CheckBoxGadget(#gadPrefSetWorkdirCD,390,135,160,20,"Set workdir with `cd`")
CocoaMessage(0,GadgetID(#gadPrefSetWorkdirCD),"setFocusRingType:",1)
GadgetToolTip(#gadPrefStatusBar,"Show/hide status bar icon. If hidden, you can open preferences dialog by launching iCHS app again.")
GadgetToolTip(#gadPrefAutostart,"Ugh... I wonder what this does...")
GadgetToolTip(#gadPrefCheckUpdate,"Enable/disable automatic updates checking every 24 hours.")
GadgetToolTip(#gadPrefPopulateMenu,"Show/hide all enabled actions in status bar menu.")
GadgetToolTip(#gadPrefShowHtk,"Show/hide hotkeys bound to each action in status bar menu.")
GadgetToolTip(#gadPrefSetWorkdirCD,"Enable to try to set workdir by sending `cd $workdir` to shell as a first line. Useful if you want to use environment variables in your workdir param, otherwise only ~ will be expanded. Has no effect at all if shell is not defined.")
AddGadgetItem(#gadTabs,2,"About")
ImageGadget(#gadLogo,68,25,64,64,ImageID(#resLogo))
TextGadget(#gadNameVer,0,103,200,60,#myName + ~"\n" + #myVer,#PB_Text_Center)
TextGadget(#gadCopyright,45,162,70,20,"created by")
HyperLinkGadget(#gadWebDeveloper,107,163,100,20,"deseven",#linkColorHighlighted)
TextGadget(#gadCopyrightIcon,45,176,70,20,"icons by")
HyperLinkGadget(#gadWebDesigner,94,177,100,20,"denboroda",#linkColorHighlighted)
SetGadgetColor(#gadWebDeveloper,#PB_Gadget_FrontColor,#linkColor)
SetGadgetColor(#gadWebDesigner,#PB_Gadget_FrontColor,#linkColor)
SetGadgetFont(#gadNameVer,FontID(#resBigFont))
SetGadgetFont(#gadCopyright,FontID(#resNormalFont))
SetGadgetFont(#gadWebDeveloper,FontID(#resNormalFont))
SetGadgetFont(#gadCopyrightIcon,FontID(#resNormalFont))
SetGadgetFont(#gadWebDesigner,FontID(#resNormalFont))
HyperLinkGadget(#gadWebGitHub,60,203,120,20,"[GitHub repo]",#linkColorHighlighted)
HyperLinkGadget(#gadWebTelegram,52,220,120,20,"[Telegram group]",#linkColorHighlighted)
SetGadgetColor(#gadWebGitHub,#PB_Gadget_FrontColor,#linkColor)
SetGadgetColor(#gadWebTelegram,#PB_Gadget_FrontColor,#linkColor)
EditorGadget(#gadLicense,205,5,360,245,#PB_Editor_ReadOnly|#PB_Editor_WordWrap)
AddGadgetItem(#gadLicense,-1,#LICENSE)
SetActiveGadget(#gadShortcuts)
DisableGadget(#gadEdit,#True) : DisableGadget(#gadDel,#True)
DisableGadget(#gadUp,#True) : DisableGadget(#gadDown,#True)
HideGadget(#gadTest,#True) : HideGadget(#gadApply,#True) : HideGadget(#gadCancel,#True) : HideGadget(#gadTestNote,#True)
GadgetToolTip(#gadAdd,"Add new shortcut")
GadgetToolTip(#gadEdit,"Edit selected shortcut")
GadgetToolTip(#gadDel,"Delete selected shortcut")
GadgetToolTip(#gadApply,"Apply changes")
GadgetToolTip(#gadCancel,"Cancel")
GadgetToolTip(#gadTest,"Perform a test run")
GadgetToolTip(#gadUp,"Move selected gadget up")
GadgetToolTip(#gadDown,"Move selected gadget down")
CloseGadgetList()
StickyWindow(#wnd,#True)
settings()
registerShortcuts()
;If #True
If Not CountGadgetItems(#gadShortcuts)
; a hack to add a special gadget state column
AddGadgetItem(#gadShortcuts,0,~"-\n-\n-\n-")
SetGadgetItemState(#gadShortcuts,0,#PB_ListIcon_Checked)
SetGadgetItemImage(#gadShortcuts,i,ImageID(#resDisabled))
ClearGadgetItems(#gadShortcuts)
wndState(#show)
Else
wndState(#hide)
EndIf
setColumnsState()
WindowBounds(#wnd,600,300,1280,720)
BindEvent(#PB_Event_SizeWindow,@windowHandler())
If wndX = #PB_Ignore
ResizeWindow(#wnd,WindowX(#wnd)-(wndW/2),wndY,wndW,wndH)
Else
ResizeWindow(#wnd,wndX,wndY,wndW,wndH)
EndIf
If GetGadgetState(#gadPrefCheckUpdate) = #PB_Checkbox_Checked
updateCheckThread = CreateThread(@checkUpdateAsync(),#updateCheckInterval*60*1000)
EndIf
Define class = CocoaMessage(0,WindowID(#wnd),"class")
Define selector = sel_registerName_("performKeyEquivalent:")
class_addMethod_(class,selector,@keyHandler(),"v@:@")
selector = sel_registerName_("flagsChanged:")
class_addMethod_(class,selector,@keyHandler(),"v@:@")
Repeat
ev = WaitWindowEvent(1000)
Select ev
Case #PB_Event_SizeWindow,#PB_Event_MoveWindow ; those do not fire in realtime
If WindowWidth(#wnd) <> wndW Or WindowHeight(#wnd) <> wndH Or WindowX(#wnd) <> wndX Or WindowY(#wnd) <> wndY
wndW = WindowWidth(#wnd)
wndH = WindowHeight(#wnd)
wndX = WindowX(#wnd)
wndY = WindowY(#wnd)
settings(#True)
EndIf
Case #PB_Event_RightClick
If GetGadgetState(#gadTabs) = 0
If (WindowMouseX(#wnd) >= GadgetX(#gadShortcuts)) And (WindowMouseX(#wnd) <= GadgetX(#gadShortcuts) + GadgetWidth(#gadShortcuts)) And
(WindowMouseY(#wnd) >= GadgetY(#gadShortcuts)) And (WindowMouseY(#wnd) <= GadgetY(#gadShortcuts) + GadgetHeight(#gadShortcuts))
DisplayPopupMenu(#columnMenu,WindowID(#wnd))
EndIf
EndIf
Case #PB_Event_Menu
Select EventMenu()
Case #columnMenuShortcut
ToggleMenuItemState(#columnMenu,#columnMenuShortcut)
Case #columnMenuAction
ToggleMenuItemState(#columnMenu,#columnMenuAction)
Case #columnMenuCommand
ToggleMenuItemState(#columnMenu,#columnMenuCommand)
Case #columnMenuWorkdir
ToggleMenuItemState(#columnMenu,#columnMenuWorkdir)
Case #columnMenuReset
SetGadgetItemAttribute(#gadShortcuts,0,#PB_ListIcon_ColumnWidth,80,0)
SetGadgetItemAttribute(#gadShortcuts,0,#PB_ListIcon_ColumnWidth,100,1)
SetGadgetItemAttribute(#gadShortcuts,0,#PB_ListIcon_ColumnWidth,120,2)
SetGadgetItemAttribute(#gadShortcuts,0,#PB_ListIcon_ColumnWidth,100,3)
SetMenuItemState(#columnMenu,#columnMenuShortcut,#True)
SetMenuItemState(#columnMenu,#columnMenuAction,#True)
SetMenuItemState(#columnMenu,#columnMenuCommand,#True)
SetMenuItemState(#columnMenu,#columnMenuWorkdir,#False)
setColumnsState()
CocoaMessage(0,GadgetID(#gadShortcuts),"sizeLastColumnToFit")
settings(#True)
EndSelect
If EventMenu() >= #columnMenuShortcut And EventMenu() <= #columnMenuWorkdir
setColumnsState()
settings(#True)
EndIf
Case #PB_Event_Gadget
Select EventGadget()
Case #gadShortcuts
Select EventType()
Case #PB_EventType_Change
If GetGadgetState(#gadShortcuts) <> -1
DisableGadget(#gadEdit,#False) : DisableGadget(#gadDel,#False)
recalcUpDown()
registerShortcuts()
settings(#True)
Else
DisableGadget(#gadEdit,#True) : DisableGadget(#gadDel,#True)
DisableGadget(#gadUp,#True) : DisableGadget(#gadDown,#True)
EndIf
Case #PB_EventType_LeftClick
cur\x = WindowMouseX(#wnd)
cur\y = WindowHeight(#wnd) - WindowMouseY(#wnd)
CocoaMessage(@cur,GadgetID(#gadShortcuts),"convertPoint:@",@cur,"fromView:",0)
Define selectedColumn.i = CocoaMessage(0,GadgetID(#gadShortcuts),"columnAtPoint:@",@cur)
If selectedColumn = 1
If GetGadgetItemState(#gadShortcuts,GetGadgetState(#gadShortcuts)) = #PB_ListIcon_Checked|#PB_ListIcon_Selected
SetGadgetItemState(#gadShortcuts,GetGadgetState(#gadShortcuts),#PB_ListIcon_Selected)
Else
SetGadgetItemState(#gadShortcuts,GetGadgetState(#gadShortcuts),#PB_ListIcon_Selected|#PB_ListIcon_Checked)
EndIf
PostEvent(#PB_Event_Gadget,#wnd,#gadShortcuts,#PB_EventType_Change)
;Debug "Selected column: " + Str(selectedColumn)
EndIf
Case #PB_EventType_LeftDoubleClick
If GetGadgetState(#gadShortcuts) <> -1
editingExistentMode()
EndIf
EndSelect
Case #gadWebDeveloper
RunProgram("open","http://d7.wtf","")
Case #gadWebDesigner
RunProgram("open","https://dribbble.com/denboroda","")
Case #gadWebGitHub
RunProgram("open","https://github.com/deseven/icanhazshortcut","")
Case #gadWebTelegram
RunProgram("open","https://t.me/icanhazshortcut","")
Case #gadAdd
editingMode()
Case #gadEdit
editingExistentMode()
Case #gadDel
i = GetGadgetState(#gadShortcuts)
RemoveGadgetItem(#gadShortcuts,i)
registerShortcuts()
settings(#True)
If CountGadgetItems(#gadShortcuts) > i
SetGadgetState(#gadShortcuts,i)
Else
SetGadgetState(#gadShortcuts,CountGadgetItems(#gadShortcuts)-1)
EndIf
recalcUpDown()
Case #gadTest
If Len(GetGadgetText(#gadCommand))
testRun(GetGadgetText(#gadCommand),GetGadgetText(#gadWorkdir))
Define testRunMessage.s = "Command: " + GetGadgetText(#gadCommand) + ~"\n" +
"Shell: " + GetGadgetText(#gadPrefShell) + ~"\n" +
"Exit code: " + Str(testRunResult\exitCode) + ~"\n\n"
If Len(testRunResult\stdout)
testRunMessage + ~"[stdout]\n" + testRunResult\stdout + ~"\n\n"
EndIf
If Len(testRunResult\stderr)
testRunMessage + ~"[stderr]\n" + testRunResult\stderr + ~"\n\n"
EndIf
If testRunResult\aborted
testRunMessage + ~"Program execution was aborted!"
ElseIf testRunResult\exitCode = -1
testRunMessage + "Failed to run!"
Else
testRunMessage + "Action executed successfully!"
EndIf
MessageRequester(#myName + " - test run",testRunMessage)
Else
MessageRequester(#myName,"Please define your command first.")
EndIf
Case #gadApply
If Len(GetGadgetText(#gadShortcutSelector)) > 0 And Len(GetGadgetText(#gadCommand)) > 0 And GetGadgetText(#gadShortcutSelector) <> #pressInvite And GetGadgetText(#gadShortcutSelector) <> #enterInvite
If editExistent
gadgetState = GetGadgetItemState(#gadShortcuts,GetGadgetState(#gadShortcuts)) - #PB_ListIcon_Selected
AddGadgetItem(#gadShortcuts,GetGadgetState(#gadShortcuts),GetGadgetText(#gadShortcutSelector) + ~"\n" +
GetGadgetText(#gadAction) + ~"\n" +
GetGadgetText(#gadCommand) + ~"\n" +
GetGadgetText(#gadWorkdir))
If gadgetState = #PB_ListIcon_Checked
SetGadgetItemState(#gadShortcuts,GetGadgetState(#gadShortcuts),gadgetState + #PB_ListIcon_Selected)
EndIf
RemoveGadgetItem(#gadShortcuts,GetGadgetState(#gadShortcuts)+1)
Else
AddGadgetItem(#gadShortcuts,-1,GetGadgetText(#gadShortcutSelector) + ~"\n" +
GetGadgetText(#gadAction) + ~"\n" +
GetGadgetText(#gadCommand) + ~"\n" +
GetGadgetText(#gadWorkdir))
SetGadgetItemState(#gadShortcuts,CountGadgetItems(#gadShortcuts)-1,#PB_ListIcon_Checked)
EndIf
registerShortcuts()
settings(#True)
viewingMode()
Else
MessageRequester(#myName,"Please define your shortcut and command first.")
EndIf
Case #gadCancel
viewingMode()
Case #gadUp
gadgetState = GetGadgetItemState(#gadShortcuts,GetGadgetState(#gadShortcuts)) - #PB_ListIcon_Selected
AddGadgetItem(#gadShortcuts,GetGadgetState(#gadShortcuts)-1,GetGadgetItemText(#gadShortcuts,GetGadgetState(#gadShortcuts),0) + ~"\n" +
GetGadgetItemText(#gadShortcuts,GetGadgetState(#gadShortcuts),1) + ~"\n" +
GetGadgetItemText(#gadShortcuts,GetGadgetState(#gadShortcuts),2) + ~"\n" +
GetGadgetItemText(#gadShortcuts,GetGadgetState(#gadShortcuts),3))
RemoveGadgetItem(#gadShortcuts,GetGadgetState(#gadShortcuts)+1)
SetGadgetState(#gadShortcuts,GetGadgetState(#gadShortcuts)-1)
If gadgetState = #PB_ListIcon_Checked
SetGadgetItemState(#gadShortcuts,GetGadgetState(#gadShortcuts),#PB_ListIcon_Checked + #PB_ListIcon_Selected)
EndIf
registerShortcuts()
settings(#True)
Case #gadDown
gadgetState = GetGadgetItemState(#gadShortcuts,GetGadgetState(#gadShortcuts)) - #PB_ListIcon_Selected
AddGadgetItem(#gadShortcuts,GetGadgetState(#gadShortcuts)+2,GetGadgetItemText(#gadShortcuts,GetGadgetState(#gadShortcuts),0) + ~"\n" +
GetGadgetItemText(#gadShortcuts,GetGadgetState(#gadShortcuts),1) + ~"\n" +
GetGadgetItemText(#gadShortcuts,GetGadgetState(#gadShortcuts),2) + ~"\n" +
GetGadgetItemText(#gadShortcuts,GetGadgetState(#gadShortcuts),3))
RemoveGadgetItem(#gadShortcuts,GetGadgetState(#gadShortcuts))
SetGadgetState(#gadShortcuts,GetGadgetState(#gadShortcuts)+1)
If gadgetState = #PB_ListIcon_Checked
SetGadgetItemState(#gadShortcuts,GetGadgetState(#gadShortcuts),#PB_ListIcon_Checked + #PB_ListIcon_Selected)
EndIf
registerShortcuts()
settings(#True)
Case #gadShortcutSelector
activateSelector(#gadShortcutSelector)
Case #gadPrefPopulateMenu
settings(#True) : registerShortcuts()
If GetGadgetState(#gadPrefPopulateMenu) = #PB_Checkbox_Checked
DisableGadget(#gadPrefShowHtk,#False)
Else
DisableGadget(#gadPrefShowHtk,#True)
EndIf
Case #gadPrefShowHtk
settings(#True) : registerShortcuts()
Case #gadPrefShell
If EventType() = #PB_EventType_Change : settings(#True) : EndIf
Case #gadPrefAutostart
If GetGadgetState(#gadPrefAutostart) = #PB_Checkbox_Checked
If Not enableLoginItem(#myID,#True)
SetGadgetState(#gadPrefAutostart,#PB_Checkbox_Unchecked)
MessageRequester(#myName,#errorMsg + "applying autostart")
Else
settings(#True)
EndIf
Else
If Not enableLoginItem(#myID,#False)
SetGadgetState(#gadPrefAutostart,#PB_Checkbox_Checked)
MessageRequester(#myName,#errorMsg + "disabling autostart")
Else
settings(#True)
EndIf
EndIf
Case #gadPrefCheckUpdate
settings(#True)
If GetGadgetState(#gadPrefCheckUpdate) = #PB_Checkbox_Checked
If Not IsThread(updateCheckThread)
updateCheckThread = CreateThread(@checkUpdateAsync(),#updateCheckInterval*60*1000)
EndIf
Else
If IsThread(updateCheckThread)
KillThread(updateCheckThread)
EndIf
EndIf
Case #gadPrefStatusBar
settings(#True)
buildMenu()
Case #gadPrefSetWorkdirCD
If GetGadgetState(#gadPrefSetWorkdirCD) = #PB_Checkbox_Checked
setWorkdirWithCD = #True
Else
setWorkdirWithCD = #False
EndIf
settings(#True)
Case #gadPrefShellDefault
SetGadgetText(#gadPrefShell,"/bin/bash -l")
PostEvent(#PB_Event_Gadget,#wnd,#gadPrefShell,#PB_EventType_Change)
Case #gadActionHelp1
SetGadgetText(#gadAction,~"open Finder")
SetGadgetText(#gadCommand,~"open -a Finder")
Case #gadActionHelp2
SetGadgetText(#gadAction,~"make a screenshot")
SetGadgetText(#gadCommand,~"screencapture -i -r -t png \"$HOME/screenshot.png\"")
Case #gadActionHelp3
SetGadgetText(#gadAction,~"say current date")
SetGadgetText(#gadCommand,~"say `date \"+Current date is %Y-%m-%d\"`")
Case #gadActionHelp4
SetGadgetText(#gadAction,~"save clipboard contents")
SetGadgetText(#gadCommand,~"pbpaste >> \"$HOME/clipboard.log\"")
Case #gadActionHelp5
SetGadgetText(#gadAction,~"set clipboard contents")
SetGadgetText(#gadCommand,~"echo \"test\" | pbcopy")
Case #gadActionHelp6
SetGadgetText(#gadAction,~"lock screen")
SetGadgetText(#gadCommand,~"pmset displaysleepnow")
EndSelect
Case #PB_Event_LeftClick
deactivateSelector()
Case #PB_Event_CloseWindow
If IsGadget(#gadShortcutSelectorCap)
viewingMode()
EndIf
wndState(#hide)
settings(#True)
Case #evDisableShortcut,#evEnableShortcut,#evToggleShortcut
If EventData()
Define shortcut.s = PeekS(EventData())
For i = 0 To CountGadgetItems(#gadShortcuts)-1
If GetGadgetItemText(#gadShortcuts,i,0) = shortcut
If ev = #evDisableShortcut
SetGadgetItemState(#gadShortcuts,i,0)
ElseIf ev = #evEnableShortcut
SetGadgetItemState(#gadShortcuts,i,#PB_ListIcon_Checked)
Else
If GetGadgetItemState(#gadShortcuts,i) >= #PB_ListIcon_Checked
PostEvent(#evDisableShortcutID,0,0,0,i+1)
Else
PostEvent(#evEnableShortcutID,0,0,0,i+1)
EndIf
EndIf
registerShortcuts()
settings(#True)
Break
EndIf
Next
EndIf
Case #evDisableAction,#evEnableAction,#evToggleAction
If EventData()
Define action.s = PeekS(EventData())
For i = 0 To CountGadgetItems(#gadShortcuts)-1
If GetGadgetItemText(#gadShortcuts,i,1) = action
If ev = #evDisableAction
SetGadgetItemState(#gadShortcuts,i,0)
ElseIf ev = #evEnableAction
SetGadgetItemState(#gadShortcuts,i,#PB_ListIcon_Checked)
Else
If GetGadgetItemState(#gadShortcuts,i) >= #PB_ListIcon_Checked
PostEvent(#evDisableShortcutID,0,0,0,i+1)
Else
PostEvent(#evEnableShortcutID,0,0,0,i+1)
EndIf
EndIf
registerShortcuts()
settings(#True)
Break
EndIf
Next
EndIf
Case #evDisableShortcutID
If EventData() And CountGadgetItems(#gadShortcuts) >= EventData()
SetGadgetItemState(#gadShortcuts,EventData()-1,0)
registerShortcuts()
settings(#True)
EndIf
Case #evEnableShortcutID
If EventData() And CountGadgetItems(#gadShortcuts) >= EventData()
SetGadgetItemState(#gadShortcuts,EventData()-1,#PB_ListIcon_Checked)
registerShortcuts()
settings(#True)
EndIf
Case #evToggleShortcutID
If EventData() And CountGadgetItems(#gadShortcuts) >= EventData()
If GetGadgetItemState(#gadShortcuts,EventData()-1) >= #PB_ListIcon_Checked
PostEvent(#evDisableShortcutID,0,0,0,EventData())
Else
PostEvent(#evEnableShortcutID,0,0,0,EventData())
EndIf
EndIf
Case #evUpdateArrival
wndState(#True)
Select MessageRequesterEx(#myName + ", new version is available!","Found new version " + updateVer + ~"\n\nChangelog:\n" + updateDetails + ~"\n\nDo you want to download it?","note",1,"Download new version","Remind me later","Skip this version")
Case 1
RunProgram("open",#updateDownloadUrl,"")
die()
Case 2
updateVer = #myVer
EndSelect
wndState(#False)
settings(#True)
Case #evNoUpdateFound
wndState(#True)
MessageRequester(#myName,"You have the latest stable version!")
wndState(#False)
Case #evUpdateConfig
settings(#True)
EndSelect
ForEver
die()