Repository navigation
Expand file tree
/
Copy pathtray_windows.go
More file actions
executable file
·541 lines (470 loc) · 12.3 KB
/
Copy pathtray_windows.go
File metadata and controls
executable file
·541 lines (470 loc) · 12.3 KB
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package systray
import (
"errors"
"path/filepath"
"syscall"
"unsafe"
)
type _Systray struct {
iconPath string
id uint32
mhwnd uintptr
hwnd uintptr
popupMenu uintptr
menuItemCallbacks []CallbackInfo
lclick func()
rclick func()
dclick func()
}
func (p *_Systray) destroy() {}
func (p *_Systray) Stop() error {
nid := NOTIFYICONDATA{
UID: p.id,
HWnd: HWND(p.hwnd),
}
nid.CbSize = uint32(unsafe.Sizeof(nid))
ret, _, _ := Shell_NotifyIcon.Call(NIM_DELETE, uintptr(unsafe.Pointer(&nid)))
if ret == 0 {
return errors.New("shell notify delete failed")
}
ret, _, _ = PostMessage.Call(p.hwnd, WM_QUIT, 0, 0)
if ret == 0 {
println("PostMessage failed")
return nil
}
return nil
}
func (p *_Systray) Show(file string, hint string) error {
err := p.SetIcon(file)
if err != nil {
return err
}
err = p.SetTooltip(hint)
if err != nil {
return err
}
return p.SetVisible(true)
}
func (p *_Systray) OnClick(fun func()) {
p.lclick = fun
p.rclick = fun
p.dclick = fun
}
func (p *_Systray) SetTooltip(tooltip string) error {
nid := NOTIFYICONDATA{
UID: p.id,
HWnd: HWND(p.hwnd),
}
nid.CbSize = uint32(unsafe.Sizeof(nid))
nid.UFlags = NIF_TIP
copy(nid.SzTip[:], syscall.StringToUTF16(tooltip))
ret, _, _ := Shell_NotifyIcon.Call(NIM_MODIFY, uintptr(unsafe.Pointer(&nid)))
if ret == 0 {
return errors.New("shell notify tooltip failed")
}
return nil
}
func (p *_Systray) SetVisible(visible bool) error {
nid := NOTIFYICONDATA{
UID: p.id,
HWnd: HWND(p.hwnd),
}
nid.CbSize = uint32(unsafe.Sizeof(nid))
nid.UFlags = NIF_STATE
nid.DwStateMask = NIS_HIDDEN
if !visible {
nid.DwState = NIS_HIDDEN
}
ret, _, _ := Shell_NotifyIcon.Call(NIM_MODIFY, uintptr(unsafe.Pointer(&nid)))
if ret == 0 {
return errors.New("shell notify tooltip failed")
}
return nil
}
func (p *_Systray) SetIcon(file string) error {
path := filepath.Join(p.iconPath, file)
path, err := filepath.Abs(path)
if err != nil {
return err
}
icon, err := NewIconFromFile(path)
if err != nil {
return err
}
hicon := HICON(icon)
nid := NOTIFYICONDATA{
UID: p.id,
HWnd: HWND(p.hwnd),
}
nid.CbSize = uint32(unsafe.Sizeof(nid))
nid.UFlags = NIF_ICON
if hicon == 0 {
nid.HIcon = 0
} else {
nid.HIcon = hicon
}
ret, _, _ := Shell_NotifyIcon.Call(NIM_MODIFY, uintptr(unsafe.Pointer(&nid)))
if ret == 0 {
return errors.New("shell notify icon failed")
}
return nil
}
func (p *_Systray) WinProc(hwnd HWND, msg uint32, wparam uintptr, lparam uintptr) uintptr {
switch msg {
case NotifyIconMessageId:
switch lparam {
case WM_LBUTTONDBLCLK:
p.dclick()
case WM_LBUTTONUP:
p.lclick()
if len(p.menuItemCallbacks) > 0 {
p.displaySystrayMenu()
}
case WM_RBUTTONUP:
p.rclick()
//case WM_LBUTTONDOWN:
// p.lclick()
}
case WM_COMMAND:
cmdMsgId := int(wparam & 0xffff)
switch cmdMsgId {
// *********************************************************
// Insert other commands we want to specifically handle here
// *********************************************************
default:
// See if this matches one of our menu item callbacks
if cmdMsgId >= MenuButtonBaseMessageId && cmdMsgId < (MenuButtonBaseMessageId+len(p.menuItemCallbacks)) {
itemIndex := cmdMsgId - MenuButtonBaseMessageId
p.menuItemCallbacks[itemIndex].Callback()
}
}
}
result, _, _ := DefWindowProc.Call(uintptr(hwnd), uintptr(msg), wparam, lparam)
return result
}
func (p *_Systray) Run() error {
hwnd := p.mhwnd
var msg MSG
for {
rt, _, _ := GetMessage.Call(uintptr(unsafe.Pointer(&msg)), 0, 0, 0)
switch int(rt) {
case 0:
return nil
case -1:
return errors.New("run failed")
}
is, _, _ := IsDialogMessage.Call(hwnd, uintptr(unsafe.Pointer(&msg)))
if is == 0 {
TranslateMessage.Call(uintptr(unsafe.Pointer(&msg)))
DispatchMessage.Call(uintptr(unsafe.Pointer(&msg)))
}
}
return nil
}
func _NewSystray(iconPath string, clientPath string) *_Systray {
tray, err := _NewSystrayEx(iconPath)
if err != nil {
panic(err)
}
return tray
}
func _NewSystrayEx(iconPath string) (*_Systray, error) {
ni := &_Systray{iconPath, 0, 0, 0, 0, make([]CallbackInfo, 0, 10), func() {}, func() {}, func() {}}
MainClassName := "MainForm"
RegisterWindow(MainClassName, ni.WinProc)
mhwnd, _, _ := CreateWindowEx.Call(
WS_EX_CONTROLPARENT,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(MainClassName))),
0,
WS_OVERLAPPEDWINDOW|WS_CLIPSIBLINGS,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
0,
0)
if mhwnd == 0 {
return nil, errors.New("create main win failed")
}
NotifyIconClassName := "NotifyIconForm"
RegisterWindow(NotifyIconClassName, ni.WinProc)
hwnd, _, _ := CreateWindowEx.Call(
0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(NotifyIconClassName))),
0,
0,
0,
0,
0,
0,
uintptr(HWND_MESSAGE),
0,
0,
0)
if hwnd == 0 {
return nil, errors.New("create notify win failed")
}
nid := NOTIFYICONDATA{
HWnd: HWND(hwnd),
UFlags: NIF_MESSAGE | NIF_STATE,
DwState: NIS_HIDDEN,
DwStateMask: NIS_HIDDEN,
UCallbackMessage: NotifyIconMessageId,
}
nid.CbSize = uint32(unsafe.Sizeof(nid))
ret, _, _ := Shell_NotifyIcon.Call(NIM_ADD, uintptr(unsafe.Pointer(&nid)))
if ret == 0 {
return nil, errors.New("shell notify create failed")
}
nid.UVersion = NOTIFYICON_VERSION
ret, _, _ = Shell_NotifyIcon.Call(NIM_SETVERSION, uintptr(unsafe.Pointer(&nid)))
if ret == 0 {
return nil, errors.New("shell notify version failed")
}
ni.id = nid.UID
ni.mhwnd = mhwnd
ni.hwnd = hwnd
return ni, nil
}
func NewIconFromFile(filePath string) (uintptr, error) {
absFilePath, err := filepath.Abs(filePath)
if err != nil {
return 0, err
}
hicon, _, _ := LoadImage.Call(
0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(absFilePath))),
IMAGE_ICON,
0,
0,
LR_DEFAULTSIZE|LR_LOADFROMFILE)
if hicon == 0 {
return 0, errors.New("load image failed: " + filePath)
}
return hicon, nil
}
func RegisterWindow(name string, proc WindowProc) error {
hinst, _, _ := GetModuleHandle.Call(0)
if hinst == 0 {
return errors.New("get module handle failed")
}
hicon, _, _ := LoadIcon.Call(0, uintptr(IDI_APPLICATION))
if hicon == 0 {
return errors.New("load icon failed")
}
hcursor, _, _ := LoadCursor.Call(0, uintptr(IDC_ARROW))
if hcursor == 0 {
return errors.New("load cursor failed")
}
var wc WNDCLASSEX
wc.CbSize = uint32(unsafe.Sizeof(wc))
wc.LpfnWndProc = syscall.NewCallback(proc)
wc.HInstance = HINSTANCE(hinst)
wc.HIcon = HICON(hicon)
wc.HCursor = HCURSOR(hcursor)
wc.HbrBackground = COLOR_BTNFACE + 1
wc.LpszClassName = syscall.StringToUTF16Ptr(name)
atom, _, _ := RegisterClassEx.Call(uintptr(unsafe.Pointer(&wc)))
if atom == 0 {
return errors.New("register class failed")
}
return nil
}
// TODO: Resolve tab vs space
func (p *_Systray) AddSystrayMenuItems(items []CallbackInfo) {
// Add callbacks to our list, mapping them to the id range dynamically
for _, info := range items {
p.menuItemCallbacks = append(p.menuItemCallbacks, info)
}
}
func (p *_Systray) ClearSystrayMenuItems() {
p.menuItemCallbacks = make([]CallbackInfo, 0, 10)
}
func (p *_Systray) displaySystrayMenu() {
var ret uintptr
var err uintptr
menu, _, _ := CreatePopupMenu.Call()
p.popupMenu = menu
for index, callbackInfo := range p.menuItemCallbacks {
// First callback is MenuButtonBaseMessageId+0, second is MenuButtonBaseMessageId+1, etc.
itemID := MenuButtonBaseMessageId + index
flags := MF_STRING
if callbackInfo.Disabled {
flags = flags | MF_GRAYED
}
if callbackInfo.Checked {
flags = flags | MF_CHECKED
}
ret, err, _ = AppendMenu.Call(menu, uintptr(flags), uintptr(itemID), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(callbackInfo.ItemName))))
if ret == 0 {
println("AppendMenu failed", err)
return
}
}
var pos POINT
ret, _, _ = GetCursorPos.Call(uintptr(unsafe.Pointer(&pos)))
if ret == 0 {
println("GetCursorPos failed")
return
}
ret, _, _ = SetForegroundWindow.Call(p.hwnd)
if ret == 0 {
println("SetForegroundWindow failed")
return
}
ret, _, _ = TrackPopupMenu.Call(menu,
TPM_LEFTALIGN,
uintptr(pos.X),
uintptr(pos.Y),
0,
p.hwnd,
0)
if ret == 0 {
println("TrackPopupMenu failed")
return
}
ret, _, _ = PostMessage.Call(p.hwnd, WM_NULL, 0, 0)
if ret == 0 {
println("PostMessage failed")
return
}
}
type WindowProc func(hwnd HWND, msg uint32, wparam, lparam uintptr) uintptr
type NOTIFYICONDATA struct {
CbSize uint32
HWnd HWND
UID uint32
UFlags uint32
UCallbackMessage uint32
HIcon HICON
SzTip [128]uint16
DwState uint32
DwStateMask uint32
SzInfo [256]uint16
UVersion uint32
SzInfoTitle [64]uint16
DwInfoFlags uint32
GuidItem GUID
}
type GUID struct {
Data1 uint32
Data2 uint16
Data3 uint16
Data4 [8]byte
}
type WNDCLASSEX struct {
CbSize uint32
Style uint32
LpfnWndProc uintptr
CbClsExtra int32
CbWndExtra int32
HInstance HINSTANCE
HIcon HICON
HCursor HCURSOR
HbrBackground HBRUSH
LpszMenuName *uint16
LpszClassName *uint16
HIconSm HICON
}
type MSG struct {
HWnd HWND
Message uint32
WParam uintptr
LParam uintptr
Time uint32
Pt POINT
}
type POINT struct {
X, Y int32
}
type (
HANDLE uintptr
HINSTANCE HANDLE
HCURSOR HANDLE
HICON HANDLE
HWND HANDLE
HMENU HANDLE
HBITMAP HANDLE
HGDIOBJ HANDLE
HBRUSH HGDIOBJ
)
const (
WM_LBUTTONDOWN = 0x0201
WM_LBUTTONUP = 0x0202
WM_LBUTTONDBLCLK = 0x0203
WM_RBUTTONUP = 0x0205
WM_USER = 0x0400
WM_TRAYICON = WM_USER + 69
WS_EX_APPWINDOW = 0x00040000
WS_OVERLAPPEDWINDOW = 0X00000000 | 0X00C00000 | 0X00080000 | 0X00040000 | 0X00020000 | 0X00010000
CW_USEDEFAULT = 0x80000000
NIM_ADD = 0x00000000
NIM_MODIFY = 0x00000001
NIM_DELETE = 0x00000002
NIM_SETVERSION = 0x00000004
NIF_MESSAGE = 0x00000001
NIF_ICON = 0x00000002
NIF_TIP = 0x00000004
NIF_STATE = 0x00000008
NIS_HIDDEN = 0x00000001
IMAGE_BITMAP = 0
IMAGE_ICON = 1
LR_LOADFROMFILE = 0x00000010
LR_DEFAULTSIZE = 0x00000040
IDC_ARROW = 32512
COLOR_WINDOW = 5
COLOR_BTNFACE = 15
GWLP_USERDATA = -21
WS_CLIPSIBLINGS = 0X04000000
WS_EX_CONTROLPARENT = 0X00010000
HWND_MESSAGE = ^HWND(2)
NOTIFYICON_VERSION = 3
// New stuff for menus
TPM_LEFTALIGN = 0x0000
WM_NULL = 0x0000
WM_QUIT = 0x0012
MIIM_FTYPE = 0x0100
MIIM_ID = 0x0002
MIIM_STRING = 0x0040
MFT_STRING = 0x0000
MF_STRING = 0x0000
MF_GRAYED = 0x0001
WM_COMMAND = 0x0111
MF_CHECKED = 0x0008
MF_UNCHECKED = 0x0000
IDI_APPLICATION = 32512
WM_APP = 32768
NotifyIconMessageId = WM_APP + iota
MenuButtonBaseMessageId = WM_APP + 1024
)
var (
kernel32 = syscall.MustLoadDLL("kernel32")
GetModuleHandle = kernel32.MustFindProc("GetModuleHandleW")
GetConsoleWindow = kernel32.MustFindProc("GetConsoleWindow")
GetLastError = kernel32.MustFindProc("GetLastError")
shell32 = syscall.MustLoadDLL("shell32.dll")
Shell_NotifyIcon = shell32.MustFindProc("Shell_NotifyIconW")
user32 = syscall.MustLoadDLL("user32.dll")
GetMessage = user32.MustFindProc("GetMessageW")
IsDialogMessage = user32.MustFindProc("IsDialogMessageW")
TranslateMessage = user32.MustFindProc("TranslateMessage")
DispatchMessage = user32.MustFindProc("DispatchMessageW")
ShowWindow = user32.MustFindProc("ShowWindow")
UpdateWindow = user32.MustFindProc("UpdateWindow")
DefWindowProc = user32.MustFindProc("DefWindowProcW")
RegisterClassEx = user32.MustFindProc("RegisterClassExW")
GetDesktopWindow = user32.MustFindProc("GetDesktopWindow")
CreateWindowEx = user32.MustFindProc("CreateWindowExW")
LoadImage = user32.MustFindProc("LoadImageW")
LoadIcon = user32.MustFindProc("LoadIconW")
LoadCursor = user32.MustFindProc("LoadCursorW")
// New stuff for menus
PostMessage = user32.MustFindProc("PostMessageW")
GetCursorPos = user32.MustFindProc("GetCursorPos")
CreatePopupMenu = user32.MustFindProc("CreatePopupMenu")
TrackPopupMenu = user32.MustFindProc("TrackPopupMenu")
SetForegroundWindow = user32.MustFindProc("SetForegroundWindow")
AppendMenu = user32.MustFindProc("AppendMenuW")
)