forked from gedoor/ahkLauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launchMenu.ahk
245 lines (209 loc) · 6.25 KB
/
launchMenu.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
#Requires AutoHotkey v2.0
#SingleInstance Ignore
#Include lib\AppUtils.ahk
#Include lib\DirTreeMenu.ahk
#Include lib\LauncherJumpList.ahk
#Include lib\ThemeUtils.ahk
#Include lib\WatchFolder.ahk
#Include lib\DescriptionUtils.ahk
AppUtils.SetCurrentProcessExplicitAppUserModelID(AppUserModelID)
;@Ahk2Exe-SetMainIcon res\launcher.ico
TraySetIcon("res\launcher.ico")
KeyHistory(0)
CoordMode "Mouse", "Screen"
CoordMode "Menu", "Screen"
DetectHiddenWindows True
Persistent true
launcherLnk := A_ScriptDir "\launchDir.lnk"
if !FileExist(launcherLnk) {
AppUtils.SelectLaunchDir()
if not FileExist(launcherLnk) {
ExitApp
}
}
try {
FileGetShortcut launcherLnk, &launcherPath
} catch {
try FileDelete(launcherLnk)
MsgBox("导航文件夹出错,请重新选择!")
ExitApp
}
showOnLoaded := false
InitArg()
WM_INITMENUPOPUP := 0x0117
WM_MENURBUTTONUP := 0x0122
WM_UNINITMENUPOPUP := 0x0125
WM_MENUSELECT := 0x11F
OnMessage(WM_INITMENUPOPUP, MenuShowCallback)
OnMessage(WM_MENURBUTTONUP, MenuRButtonUpCallback)
OnMessage(WM_UNINITMENUPOPUP, HideToolTip)
OnMessage(WM_MENUSELECT, HideToolTip)
OnMessage(AppMsgNum, AppMsgCallback)
A_IconTip := "导航菜单"
A_TrayMenu.Delete()
A_TrayMenu.Add("Config", (*) => Run(A_AhkPath " Config.ahk"))
A_TrayMenu.Default := "1&"
A_TrayMenu.Add("Reload", (*) => Reload())
A_TrayMenu.Add("Exit", (*) => ExitApp())
A_TrayMenu.Add()
A_TrayMenu.Add("SelectLaunchDir", SelectLaunchDir)
A_TrayMenu.Add("OpenLaunchDir", (*) => Run(launcherLnk))
A_TrayMenu.Add("OpenAppDir", (*) => Run("explorer " A_ScriptDir))
A_TrayMenu.Add()
dpiZom := A_ScreenDPI / 96
IconSize := Integer(32 * dpiZom)
BulidLauncherMenu()
if showOnLoaded {
ShowLauncherMenu()
}
watchLauncerDir := WatchFolder(launcherPath, "LaunchChangeCallback", true, 0x00000013)
Run '"' A_AhkPath '" "' A_ScriptDir '\Autorun.ahk"'
return
InitArg() {
for arg in A_Args {
if arg = "show" {
global showOnLoaded
showOnLoaded := true
return
}
}
}
BulidLauncherMenu() {
try {
launcTree := DirTreeMenu.getDirTree(launcherPath)
} catch TimeoutError as err {
MsgBox(err.Message)
return
}
global launcherMenu
global scriptMenu
launcherMenu := DirTreeMenu.createMenu(launcTree, IconSize, LauncherMenuCallback)
loadAhkScript := IniRead(configIni, "config", "loadAhkScript", 0)
if (loadAhkScript) {
scriptMenu := Menu()
scriptMenu.DefineProp("data", { Value: Array() })
loop files A_ScriptDir "\ux\*.ahk", "F"
{
menuName := SubStr(A_LoopFileName, 1, StrLen(A_LoopFileName) - 4)
scriptMenu.Add(menuName, LauncherMenuCallback)
scriptMenu.data.Push({ path: A_LoopFileFullPath, name: menuName })
}
launcherMenu.Add("AhkScript", scriptMenu)
launcherMenu.SetIcon("AhkScript", A_AhkPath, 1, IconSize)
}
A_TrayMenu.Add("Launcher", launcherMenu)
}
AppMsgCallback(wParam, lParam, *) {
switch wParam {
case 1111:
ShowLauncherMenu()
case 1112:
BulidLauncherMenu()
}
}
ShowLauncherMenu() {
if not IsSet(launcherMenu) {
global showOnLoaded
showOnLoaded := true
return
}
global dpiZom
global IconSize
nowDpiZom := A_ScreenDPI / 96
if nowDpiZom != dpiZom {
dpiZom := nowDpiZom
IconSize := Integer(32 * dpiZom)
BulidLauncherMenu()
ShowLauncherMenu()
return
}
MouseGetPos(&mouseX, &mouseY)
; default menu pos to mouse pos
menu_x := mouseX, menu_y := mouseY
;获取活动区域
MonitorGetWorkArea(, &wLeft, &wTop, &wRight, &wBottom)
menuW := 80 * dpiZom
if mouseX > menuW {
menu_x := mouseX - menuW
}
if mouseY > wBottom {
menu_y := wBottom
}
launcherMenu.Show(menu_x, menu_y)
}
MenuShowCallback(wParam, lParam, *) {
if wParam = A_TrayMenu.Handle {
ThemeUtils.darkMenuMode(ThemeUtils.SysIsDarkMode)
} else if IsSet(launcherMenu) && wParam = launcherMenu.Handle {
ThemeUtils.darkMenuMode(ThemeUtils.SysIsDarkMode)
} else if IsSet(scriptMenu) && wParam = scriptMenu.Handle {
for item in scriptMenu.data {
if WinExist(item.path " - AutoHotkey") {
scriptMenu.SetIcon(item.name, "%SystemRoot%\system32\shell32.dll", 295)
} else {
scriptMenu.SetIcon(item.name, "*")
}
}
}
}
LauncherMenuCallback(ItemName, ItemPos, MyMenu) {
rPath := MyMenu.data[ItemPos].path
if not DirExist("recent") {
DirCreate("recent")
}
if (rPath ~= ".*?.ahk$") {
Run('"' A_AhkPath '" "' rPath '"')
return
}
try {
Run(rPath)
FileCreateShortcut(rPath, "recent\" ItemName)
} catch {
FileGetShortcut(rPath, &outTarget, &outWrkDir, &outArgs)
try {
Run(outTarget " " outArgs, outWrkDir)
FileCreateShortcut(outTarget, "recent\" ItemName, outWrkDir, outArgs)
} catch {
try {
pf64 := EnvGet("ProgramW6432")
_outTarget64 := StrReplace(outTarget, A_ProgramFiles, pf64, , , 1)
Run(_outTarget64 " " outArgs, outWrkDir)
FileCreateShortcut(_outTarget64, "recent\" ItemName, outWrkDir, outArgs)
} catch {
MsgBox("运行" rPath "失败." A_LastError)
}
}
}
LauncherJumpList.up(AppUserModelID)
}
MenuRButtonUpCallback(wParam, lParam, *) {
menuItem := DirTreeMenu.findMenu(launcherMenu.data, lParam, wParam)
if menuItem {
path := menuItem.path
if path ~= ".*?.lnk$" {
FileGetShortcut path, &path
}
if GetKeyState("Control", "P") {
Run("explorer /select,`"" menuItem.path "`"")
} else {
ToolTip(path "`n按住Ctrl右击打开文件夹")
}
} else if lParam = scriptMenu.Handle {
ahkPath := scriptMenu.data[wParam + 1].path
description := DescriptionUtils.getAhkDescription(ahkPath)
if description {
ToolTip(description)
}
}
}
LaunchChangeCallback(path, notifications) {
BulidLauncherMenu()
}
SelectLaunchDir(*) {
if AppUtils.SelectLaunchDir() {
Reload
}
}
HideToolTip(*) {
ToolTip()
}