This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsystray_windows.go
816 lines (721 loc) · 22.2 KB
/
systray_windows.go
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
// +build windows
package systray
import (
"crypto/md5"
"encoding/hex"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
const (
className = "SystrayClass"
windowName = ""
NIM_ADD = 0x00000000
NIM_MODIFY = 0x00000001
NIM_DELETE = 0x00000002
IMAGE_ICON = 1 // Loads an icon
LR_LOADFROMFILE = 0x00000010 // Loads the stand-alone image from the file
LR_DEFAULTSIZE = 0x00000040 // Loads default-size icon for windows(SM_CXICON x SM_CYICON) if cx, cy are set to zero
NIF_ICON = 0x00000002
WM_CLOSE = 0x0010
NIF_TIP = 0x00000004
WM_COMMAND = 0x0111
WM_DESTROY = 0x0002
WM_ENDSESSION = 0x16
WM_RBUTTONUP = 0x0205
WM_LBUTTONUP = 0x0202
WM_LBUTTONDBLCLK = 0x0203
WM_RBUTTONDBLCLK = 0x0206
IDI_APPLICATION = 32512
IDC_ARROW = 32512 // Standard arrow
SW_HIDE = 0
CW_USEDEFAULT = 0x80000000
WS_CAPTION = 0x00C00000
WS_MAXIMIZEBOX = 0x00010000
WS_MINIMIZEBOX = 0x00020000
WS_OVERLAPPED = 0x00000000
WS_SYSMENU = 0x00080000
WS_THICKFRAME = 0x00040000
WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
CS_HREDRAW = 0x0002
CS_VREDRAW = 0x0001
NIF_MESSAGE = 0x00000001
WM_USER = 0x0400
MIM_APPLYTOSUBMENUS = 0x80000000 // Settings apply to the menu and all of its submenus
MIIM_FTYPE = 0x00000100
MIIM_STRING = 0x00000040
MIIM_ID = 0x00000002
MIIM_STATE = 0x00000001
MFT_STRING = 0x00000000
MFS_CHECKED = 0x00000008
MFS_DISABLED = 0x00000003
MFT_SEPARATOR = 0x00000800
MF_BYCOMMAND = 0x00000000
ERROR_SUCCESS syscall.Errno = 0
TPM_BOTTOMALIGN = 0x0020
TPM_LEFTALIGN = 0x0000
MIIM_SUBMENU = 0x00000004
MIIM_BITMAP = 0x00000080
)
// Helpful sources: https://github.com/golang/exp/blob/master/shiny/driver/internal/win32
var (
k32 = windows.NewLazySystemDLL("Kernel32.dll")
s32 = windows.NewLazySystemDLL("Shell32.dll")
u32 = windows.NewLazySystemDLL("User32.dll")
pGetModuleHandle = k32.NewProc("GetModuleHandleW")
pShellNotifyIcon = s32.NewProc("Shell_NotifyIconW")
pCreatePopupMenu = u32.NewProc("CreatePopupMenu")
pCreateWindowEx = u32.NewProc("CreateWindowExW")
pDefWindowProc = u32.NewProc("DefWindowProcW")
pDeleteMenu = u32.NewProc("DeleteMenu")
pDestroyWindow = u32.NewProc("DestroyWindow")
pDispatchMessage = u32.NewProc("DispatchMessageW")
pGetCursorPos = u32.NewProc("GetCursorPos")
pGetMenuItemID = u32.NewProc("GetMenuItemID")
pGetSubMenu = u32.NewProc("GetSubMenu")
pGetMessage = u32.NewProc("GetMessageW")
pInsertMenuItem = u32.NewProc("InsertMenuItemW")
pLoadIcon = u32.NewProc("LoadIconW")
pLoadImage = u32.NewProc("LoadImageW")
pLoadCursor = u32.NewProc("LoadCursorW")
pPostMessage = u32.NewProc("PostMessageW")
pPostQuitMessage = u32.NewProc("PostQuitMessage")
pRegisterClass = u32.NewProc("RegisterClassExW")
pRegisterWindowMessage = u32.NewProc("RegisterWindowMessageW")
pSetForegroundWindow = u32.NewProc("SetForegroundWindow")
pSetMenuInfo = u32.NewProc("SetMenuInfo")
pSetMenuItemInfo = u32.NewProc("SetMenuItemInfoW")
pSetMenuItemBitmaps = u32.NewProc("SetMenuItemBitmaps")
pShowWindow = u32.NewProc("ShowWindow")
pTrackPopupMenu = u32.NewProc("TrackPopupMenu")
pTranslateMessage = u32.NewProc("TranslateMessage")
pUnregisterClass = u32.NewProc("UnregisterClassW")
pUpdateWindow = u32.NewProc("UpdateWindow")
)
// Contains window class information.
// It is used with the RegisterClassEx and GetClassInfoEx functions.
// https://msdn.microsoft.com/en-us/library/ms633577.aspx
type wndClassEx struct {
Size, Style uint32
WndProc uintptr
ClsExtra, WndExtra int32
Instance, Icon, Cursor, Background windows.Handle
MenuName, ClassName *uint16
IconSm windows.Handle
}
// Registers a window class for subsequent use in calls to the CreateWindow or CreateWindowEx function.
// https://msdn.microsoft.com/en-us/library/ms633587.aspx
func (w *wndClassEx) register() error {
w.Size = uint32(unsafe.Sizeof(*w))
res, _, err := pRegisterClass.Call(uintptr(unsafe.Pointer(w)))
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
return nil
}
// Unregisters a window class, freeing the memory required for the class.
// https://msdn.microsoft.com/en-us/library/ms644899.aspx
func (w *wndClassEx) unregister() error {
res, _, err := pUnregisterClass.Call(
uintptr(unsafe.Pointer(w.ClassName)),
uintptr(w.Instance),
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
return nil
}
// Contains information that the system needs to display notifications in the notification area.
// Used by Shell_NotifyIcon.
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb773352(v=vs.85).aspx
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb762159
type notifyIconData struct {
Size uint32
Wnd windows.Handle
ID, Flags, CallbackMessage uint32
Icon windows.Handle
Tip [128]uint16
State, StateMask uint32
Info [256]uint16
Timeout, Version uint32
InfoTitle [64]uint16
InfoFlags uint32
GuidItem windows.GUID
BalloonIcon windows.Handle
}
func (nid *notifyIconData) add() error {
res, _, err := pShellNotifyIcon.Call(
uintptr(NIM_ADD),
uintptr(unsafe.Pointer(nid)),
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
return nil
}
func (nid *notifyIconData) modify() error {
res, _, err := pShellNotifyIcon.Call(
uintptr(NIM_MODIFY),
uintptr(unsafe.Pointer(nid)),
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
return nil
}
func (nid *notifyIconData) delete() error {
res, _, err := pShellNotifyIcon.Call(
uintptr(NIM_DELETE),
uintptr(unsafe.Pointer(nid)),
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
return nil
}
// Contains information about a menu item.
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms647578(v=vs.85).aspx
type menuItemInfo struct {
Size, Mask, Type, State uint32
ID uint32
SubMenu, Checked, Unchecked windows.Handle
ItemData uintptr
TypeData *uint16
Cch uint32
Item windows.Handle
}
// The POINT structure defines the x- and y- coordinates of a point.
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd162805(v=vs.85).aspx
type point struct {
X, Y int32
}
// Contains information about loaded resources
type winTray struct {
instance,
icon,
cursor,
window,
menu windows.Handle
loadedImages map[string]windows.Handle
nid *notifyIconData
wcex *wndClassEx
wmSystrayMessage uint32
wmTaskbarCreated uint32
}
var menus = make(map[int32]windows.Handle)
// Loads an image from file and shows it in tray.
// LoadImage: https://msdn.microsoft.com/en-us/library/windows/desktop/ms648045(v=vs.85).aspx
// Shell_NotifyIcon: https://msdn.microsoft.com/en-us/library/windows/desktop/bb762159(v=vs.85).aspx
func (t *winTray) setIcon(src string) error {
// Save and reuse handles of loaded images
h, ok := t.loadedImages[src]
if !ok {
srcPtr, err := windows.UTF16PtrFromString(src)
if err != nil {
return err
}
res, _, err := pLoadImage.Call(
0,
uintptr(unsafe.Pointer(srcPtr)),
IMAGE_ICON,
0,
0,
LR_LOADFROMFILE|LR_DEFAULTSIZE,
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
h = windows.Handle(res)
t.loadedImages[src] = h
}
t.nid.Icon = h
t.nid.Flags |= NIF_ICON
t.nid.Size = uint32(unsafe.Sizeof(*t.nid))
return t.nid.modify()
}
// Sets tooltip on icon.
// Shell_NotifyIcon: https://msdn.microsoft.com/en-us/library/windows/desktop/bb762159(v=vs.85).aspx
func (t *winTray) setTooltip(src string) error {
b, err := windows.UTF16FromString(src)
if err != nil {
return err
}
copy(t.nid.Tip[:], b[:])
t.nid.Flags |= NIF_TIP
t.nid.Size = uint32(unsafe.Sizeof(*t.nid))
return t.nid.modify()
}
var wt winTray
// WindowProc callback function that processes messages sent to a window.
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633573(v=vs.85).aspx
func (t *winTray) wndProc(hWnd windows.Handle, message uint32, wParam, lParam uintptr) (lResult uintptr) {
switch message {
case WM_COMMAND:
menuId := int32(wParam)
if menuId != -1 {
systrayMenuItemSelected(menuId, false, false)
}
case WM_DESTROY:
// same as WM_ENDSESSION, but throws 0 exit code after all
defer pPostQuitMessage.Call(uintptr(int32(0)))
fallthrough
case WM_ENDSESSION:
if t.nid != nil {
t.nid.delete()
}
systrayExit()
case t.wmSystrayMessage:
switch lParam {
case WM_RBUTTONUP:
rightClickAction()
case WM_LBUTTONUP:
leftClickAction()
case WM_RBUTTONDBLCLK:
rightDblClickAction()
case WM_LBUTTONDBLCLK:
leftDblClickAction()
}
case t.wmTaskbarCreated: // on explorer.exe restarts
t.nid.add()
default:
// Calls the default window procedure to provide default processing for any window messages that an application does not process.
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633572(v=vs.85).aspx
lResult, _, _ = pDefWindowProc.Call(
uintptr(hWnd),
uintptr(message),
wParam,
lParam,
)
}
return
}
func (t *winTray) initInstance() error {
rightClickAction = t.showMenu
leftClickAction = t.showMenu
rightDblClickAction = t.showMenu
leftDblClickAction = t.showMenu
t.wmSystrayMessage = WM_USER + 1
taskbarEventNamePtr, _ := windows.UTF16PtrFromString("TaskbarCreated")
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms644947
res, _, err := pRegisterWindowMessage.Call(
uintptr(unsafe.Pointer(taskbarEventNamePtr)),
)
t.wmTaskbarCreated = uint32(res)
t.loadedImages = make(map[string]windows.Handle)
instanceHandle, _, err := pGetModuleHandle.Call(0)
if instanceHandle == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
t.instance = windows.Handle(instanceHandle)
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms648072(v=vs.85).aspx
iconHandle, _, err := pLoadIcon.Call(0, uintptr(IDI_APPLICATION))
if iconHandle == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
t.icon = windows.Handle(iconHandle)
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms648391(v=vs.85).aspx
cursorHandle, _, err := pLoadCursor.Call(0, uintptr(IDC_ARROW))
if cursorHandle == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
t.cursor = windows.Handle(cursorHandle)
classNamePtr, err := windows.UTF16PtrFromString(className)
if err != nil {
return err
}
windowNamePtr, err := windows.UTF16PtrFromString(windowName)
if err != nil {
return err
}
t.wcex = &wndClassEx{
Style: CS_HREDRAW | CS_VREDRAW,
WndProc: windows.NewCallback(t.wndProc),
Instance: t.instance,
Icon: t.icon,
Cursor: t.cursor,
Background: windows.Handle(6), // (COLOR_WINDOW + 1)
ClassName: classNamePtr,
IconSm: t.icon,
}
if err := t.wcex.register(); err != nil {
return err
}
windowHandle, _, err := pCreateWindowEx.Call(
uintptr(0),
uintptr(unsafe.Pointer(classNamePtr)),
uintptr(unsafe.Pointer(windowNamePtr)),
uintptr(WS_OVERLAPPEDWINDOW),
uintptr(CW_USEDEFAULT),
uintptr(CW_USEDEFAULT),
uintptr(CW_USEDEFAULT),
uintptr(CW_USEDEFAULT),
uintptr(0),
uintptr(0),
uintptr(t.instance),
uintptr(0),
)
if windowHandle == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
t.window = windows.Handle(windowHandle)
_, _, err = pShowWindow.Call(
uintptr(t.window),
uintptr(SW_HIDE),
)
if err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
_, _, err = pUpdateWindow.Call(
uintptr(t.window),
)
if err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
t.nid = ¬ifyIconData{
Wnd: t.window,
ID: 100,
Flags: NIF_MESSAGE,
CallbackMessage: t.wmSystrayMessage,
}
t.nid.Size = uint32(unsafe.Sizeof(*t.nid))
return t.nid.add()
}
func (t *winTray) createMenu() error {
menuHandle, _, err := pCreatePopupMenu.Call()
if menuHandle == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
t.menu = windows.Handle(menuHandle)
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms647575(v=vs.85).aspx
mi := struct {
Size, Mask, Style, Max uint32
Background windows.Handle
ContextHelpID uint32
MenuData uintptr
}{
Mask: MIM_APPLYTOSUBMENUS,
}
mi.Size = uint32(unsafe.Sizeof(mi))
res, _, err := pSetMenuInfo.Call(
uintptr(t.menu),
uintptr(unsafe.Pointer(&mi)),
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
return nil
}
// createSubMenu creates a sub menu and stores
// it in the menus map using the id as key
func createSubMenu(id int32) error {
menuHandle, _, err := pCreatePopupMenu.Call()
if menuHandle == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
menus[id] = windows.Handle(menuHandle)
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms647575(v=vs.85).aspx
mi := struct {
Size, Mask, Style, Max uint32
Background windows.Handle
ContextHelpID uint32
MenuData uintptr
}{
Mask: MIM_APPLYTOSUBMENUS,
}
mi.Size = uint32(unsafe.Sizeof(mi))
res, _, err := pSetMenuInfo.Call(
uintptr(menus[id]),
uintptr(unsafe.Pointer(&mi)),
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
return nil
}
func (t *winTray) addSubmenuToTray(item *MenuItem) error {
titlePtr, err := windows.UTF16PtrFromString(item.title)
if err != nil {
return err
}
mi := menuItemInfo{
Mask: MIIM_FTYPE | MIIM_STRING | MIIM_ID | MIIM_SUBMENU | MIIM_BITMAP,
Type: MFT_STRING,
ID: uint32(item.id),
TypeData: titlePtr,
Cch: uint32(len(item.title)),
SubMenu: menus[item.menuId],
}
mi.Size = uint32(unsafe.Sizeof(mi))
// The return value is the identifier of the specified menu item.
// If the menu item identifier is NULL or if the specified item opens a submenu, the return value is -1.
res, _, err := pGetMenuItemID.Call(uintptr(t.menu), uintptr(item.id))
if int32(res) == -1 {
res, _, err = pInsertMenuItem.Call(
uintptr(t.menu),
uintptr(item.id),
1,
uintptr(unsafe.Pointer(&mi)),
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
} else {
res, _, err = pSetMenuItemInfo.Call(
uintptr(t.menu),
uintptr(item.id),
0,
uintptr(unsafe.Pointer(&mi)),
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
}
return nil
}
func (t *winTray) addOrUpdateMenuItem(item *MenuItem) error {
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms647578(v=vs.85).aspx
var menu windows.Handle
titlePtr, err := windows.UTF16PtrFromString(item.title)
if err != nil {
return err
}
mi := menuItemInfo{
Mask: MIIM_FTYPE | MIIM_STRING | MIIM_ID | MIIM_STATE,
Type: MFT_STRING,
ID: uint32(item.id),
TypeData: titlePtr,
Cch: uint32(len(item.title)),
}
if item.checkable && item.checked {
mi.State |= MFS_CHECKED
}
if item.disabled {
mi.State |= MFS_DISABLED
}
mi.Size = uint32(unsafe.Sizeof(mi))
// if item is a submenu item then get the id of its parent menu
if item.isSubmenuItem {
menu = menus[item.menuId]
} else {
menu = t.menu
}
// The return value is the identifier of the specified menu item.
// If the menu item identifier is NULL or if the specified item opens a submenu, the return value is -1.
res, _, err := pGetMenuItemID.Call(uintptr(menu), uintptr(item.id))
if int32(res) == -1 {
res, _, err = pInsertMenuItem.Call(
uintptr(menu),
uintptr(item.id),
1,
uintptr(unsafe.Pointer(&mi)),
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
} else {
res, _, err = pSetMenuItemInfo.Call(
uintptr(menu),
uintptr(item.id),
0,
uintptr(unsafe.Pointer(&mi)),
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
}
return nil
}
func (t *winTray) addSeparatorMenuItem(menuId int32) error {
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms647578(v=vs.85).aspx
mi := menuItemInfo{
Mask: MIIM_FTYPE | MIIM_ID | MIIM_STATE,
Type: MFT_SEPARATOR,
ID: uint32(menuId),
}
mi.Size = uint32(unsafe.Sizeof(mi))
res, _, err := pInsertMenuItem.Call(
uintptr(t.menu),
uintptr(menuId),
1,
uintptr(unsafe.Pointer(&mi)),
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
return nil
}
func (t *winTray) hideMenuItem(menuId int32) error {
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms647629(v=vs.85).aspx
res, _, err := pDeleteMenu.Call(
uintptr(t.menu),
uintptr(uint32(menuId)),
MF_BYCOMMAND,
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
return nil
}
func (t *winTray) showMenu() error {
p := point{}
res, _, err := pGetCursorPos.Call(uintptr(unsafe.Pointer(&p)))
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
_, _, err = pSetForegroundWindow.Call(uintptr(t.window))
if err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
res, _, err = pTrackPopupMenu.Call(
uintptr(t.menu),
TPM_BOTTOMALIGN|TPM_LEFTALIGN,
uintptr(p.X),
uintptr(p.Y),
0,
uintptr(t.window),
0,
)
if res == 0 && err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
return nil
}
func nativeLoop() error {
if err := wt.initInstance(); err != nil {
return err
}
if err := wt.createMenu(); err != nil {
return err
}
defer func() {
pDestroyWindow.Call(uintptr(wt.window))
wt.wcex.unregister()
}()
go systrayReady()
// Main message pump.
m := struct {
WindowHandle windows.Handle
Message uint32
Wparam uintptr
Lparam uintptr
Time uint32
Pt point
}{}
for {
ret, _, err := pGetMessage.Call(uintptr(unsafe.Pointer(&m)), 0, 0, 0)
if int32(ret) <= 0 {
return err
}
_, _, err = pTranslateMessage.Call(uintptr(unsafe.Pointer(&m)))
if err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
_, _, err = pDispatchMessage.Call(uintptr(unsafe.Pointer(&m)))
if err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
}
}
func quit() error {
if wt.window != 0 {
_, _, err := pPostMessage.Call(
uintptr(wt.window),
WM_CLOSE,
0,
0,
)
if err.(syscall.Errno) != ERROR_SUCCESS {
return err
}
}
return nil
}
// SetIcon sets the systray icon.
// iconBytes should be the content of .ico
func setIcon(iconBytes []byte) error {
bh := md5.Sum(iconBytes)
dataHash := hex.EncodeToString(bh[:])
iconFilePath := filepath.Join(os.TempDir(), "systray_temp_icon_"+dataHash)
if _, err := os.Stat(iconFilePath); os.IsNotExist(err) {
if err := ioutil.WriteFile(iconFilePath, iconBytes, 0644); err != nil {
return err
}
}
return setIconPath(iconFilePath)
}
func setIconPath(path string) error {
return wt.setIcon(path)
}
func setTitle(title string) error {
return nil
}
func setTooltip(tooltip string) error {
return wt.setTooltip(tooltip)
}
func addOrUpdateMenuItem(item *MenuItem) error {
if item.isSeparator {
return addSeparator(item.id)
}
return wt.addOrUpdateMenuItem(item)
}
func addSubmenuToTray(item *MenuItem) error {
return wt.addSubmenuToTray(item)
}
func addSeparator(id int32) error {
return wt.addSeparatorMenuItem(id)
}
func hideMenuItem(item *MenuItem) error {
return wt.hideMenuItem(item.id)
}
func showMenuItem(item *MenuItem) error {
return addOrUpdateMenuItem(item)
}
func addBitmap(bitmapBytes []byte, item *MenuItem) error {
return nil
}
func addBitmapPath(filepath string, item *MenuItem) error {
return nil
}
// SetCustomLeftClickAction set custom function to process left click on icon
// only windows at this moment
func setCustomLeftClickAction(ff ClickActionType) {
leftClickAction = ff
}
// SetCustomRightClickAction set custom function to process right click on icon
// only windows at this moment
func setCustomRightClickAction(ff ClickActionType) {
rightClickAction = ff
}
// SetDefaultLeftClickAction set default function to process left click on icon (show menu)
// only windows at this moment
func setDefaultLeftClickAction() {
leftClickAction = wt.showMenu
}
// SetDefaultRightClickAction set default function to process right click on icon (show menu)
// only windows at this moment
func setDefaultRightClickAction() {
rightClickAction = wt.showMenu
}
// SetCustomLeftDoubleClickAction set custom function to process left click on icon
// only windows at this moment
func setCustomLeftDoubleClickAction(ff ClickActionType) {
leftDblClickAction = ff
}
// SetCustomRightDoubleClickAction set custom function to process right click on icon
// only windows at this moment
func setCustomRightDoubleClickAction(ff ClickActionType) {
rightDblClickAction = ff
}
// SetDefaultLeftDoubleClickAction set default function to process left click on icon (show menu)
// only windows at this moment
func setDefaultLeftDoubleClickAction() {
leftDblClickAction = wt.showMenu
}
// SetDefaultRightDoubleClickAction set default function to process right click on icon (show menu)
// only windows at this moment
func setDefaultRightDoubleClickAction() {
rightDblClickAction = wt.showMenu
}