forked from alex2108/systray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgo_windows.go
115 lines (97 loc) · 2.13 KB
/
cgo_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
package systray
/*
#cgo windows CFLAGS: -DWIN32
#include "systray.h"
*/
import "C"
import (
"fmt"
"io/ioutil"
"os"
"syscall"
"unsafe"
)
func nativeLoop() {
C.nativeLoop()
}
var prevIconPath string
func quit() {
os.Remove(prevIconPath)
C.quit()
}
func SetIcon(iconBytes []byte) {
f, err := ioutil.TempFile("", "systray_temp_icon")
if err != nil {
log.Errorf("Unable to create temp icon: %v", err)
return
}
defer f.Close()
_, err = f.Write(iconBytes)
if err != nil {
log.Errorf("Unable to write icon to temp file %v: %v", f.Name(), f)
return
}
iconPath := f.Name()
// Need to close file before we load it to make sure contents is flushed.
f.Close()
name, err := syscall.UTF16PtrFromString(f.Name())
if err != nil {
log.Errorf("Unable to convert name to string pointer: %v", err)
return
}
C.setIcon((*C.wchar_t)(unsafe.Pointer(name)), 0)
// remove old icon file
os.Remove(prevIconPath)
prevIconPath = iconPath
}
// SetTitle sets the systray title, only available on Mac.
func SetTitle(title string) {
t, err := syscall.UTF16PtrFromString(title)
if err != nil {
panic(err)
}
C.setTitle((*C.wchar_t)(unsafe.Pointer(t)))
}
// SetTooltip sets the systray tooltip to display on mouse hover of the tray icon,
// only available on Mac and Windows.
func SetTooltip(tooltip string) {
t, err := syscall.UTF16PtrFromString(tooltip)
if err != nil {
panic(err)
}
C.setTooltip((*C.wchar_t)(unsafe.Pointer(t)))
}
func addOrUpdateMenuItem(item *MenuItem) {
var disabled C.short = 0
if item.disabled {
disabled = 1
}
var checked C.short = 0
if item.checked {
checked = 1
}
title, err := syscall.UTF16PtrFromString(item.title)
if err != nil {
panic(err)
}
tooltip, err := syscall.UTF16PtrFromString(item.tooltip)
if err != nil {
panic(err)
}
C.add_or_update_menu_item(
C.int(item.id),
(*C.wchar_t)(unsafe.Pointer(title)),
(*C.wchar_t)(unsafe.Pointer(tooltip)),
disabled,
checked,
)
}
//export systray_ready
func systray_ready() {
fmt.Println("systray ready")
systrayReady()
}
//export systray_menu_item_selected
func systray_menu_item_selected(cId C.int) {
systrayMenuItemSelected(int32(cId))
}