forked from xilp/systray
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtray_darwin.go
executable file
·228 lines (193 loc) · 5.12 KB
/
tray_darwin.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
package systray
import (
"path/filepath"
"runtime"
"unsafe"
)
/*
#cgo linux pkg-config: gtk+-3.0
#cgo linux CFLAGS: -DLINUX
#cgo windows CFLAGS: -DWIN32
#cgo darwin CFLAGS: -DDARWIN -x objective-c
#cgo darwin LDFLAGS: -framework Cocoa
#include <stdlib.h>
typedef uint64_t ManagerId;
void runApplication(const char *title, const char *initialIcon, const char *initialHint, ManagerId manager);
void stopApplication(void);
void addSystrayMenuItem(const char *item, ManagerId, unsigned int index, unsigned char enabled, unsigned char checked);
void clearSystrayMenuItems(void);
void setIcon(const char *path);
void setHint(const char *hint);
*/
import "C"
type _Systray struct {
iconPath string
currentIcon string
currentHint string
isExiting bool
isCreated bool
menuItemCallbacks []CallbackInfo
lclick func()
rclick func()
dclick func()
refId refId
}
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: iconPath,
currentIcon: "",
currentHint: "",
isExiting: false,
isCreated: false,
menuItemCallbacks: make([]CallbackInfo, 0, 10),
lclick: func() {},
rclick: func() {},
dclick: func() {},
}
// Register and track a reference to this instance
ni.refId = gSystrays.Add(ni)
return ni, nil
}
func (p *_Systray) destroy() {
if p != nil {
gSystrays.Decref(p.refId)
}
}
func (p *_Systray) Stop() error {
C.stopApplication()
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 {
p.currentHint = tooltip
if p.isCreated {
cTooltip := C.CString(tooltip)
defer C.free(unsafe.Pointer(cTooltip))
C.setHint(cTooltip)
}
return nil
}
func (p *_Systray) SetIcon(file string) error {
p.currentIcon = file
if p.isCreated {
cFile := C.CString(file)
defer C.free(unsafe.Pointer(cFile))
C.setIcon(cFile)
}
return nil
}
func (p *_Systray) SetVisible(visible bool) error {
// Does this have any meaning for darwin?
return nil
}
func (p *_Systray) Run() error {
cTitle := C.CString(p.currentHint)
defer C.free(unsafe.Pointer(cTitle))
cIconPath := C.CString(filepath.Join(p.iconPath, p.currentIcon))
defer C.free(unsafe.Pointer(cIconPath))
println("Running main loop on systray", p)
// Enter the main loop - this calls [NSApplication run] internally, which *must*
// execute on the main thread.
// We call LockOSThread() here just in case, but, really, call it earlier!
runtime.LockOSThread()
C.runApplication(cTitle, cIconPath, cTitle, C.ManagerId(p.refId))
runtime.UnlockOSThread()
// If reached, user clicked Exit
p.isExiting = true
return nil
}
func (p *_Systray) insertMenuItem(info CallbackInfo, index int) {
// TODO - insert item into array at desired index
p.menuItemCallbacks = append(p.menuItemCallbacks, info)
}
func (p *_Systray) appendMenuItem(info CallbackInfo) {
index := len(p.menuItemCallbacks)
p.menuItemCallbacks = append(p.menuItemCallbacks, info)
if p.isCreated {
p.addItemToNativeMenu(info, index)
}
}
func (p *_Systray) addItemToNativeMenu(info CallbackInfo, index int) {
cItemName := C.CString(info.ItemName)
defer C.free(unsafe.Pointer(cItemName))
cIndex := C.uint(index)
var cEnabled C.uchar
if info.Disabled || info.Callback == nil {
cEnabled = C.uchar(0)
} else {
cEnabled = C.uchar(1)
}
var cChecked C.uchar
if info.Checked {
cChecked = C.uchar(1)
} else {
cChecked = C.uchar(0)
}
C.addSystrayMenuItem(cItemName, C.ManagerId(p.refId), cIndex, cEnabled, cChecked)
}
func (p *_Systray) AddSystrayMenuItems(items []CallbackInfo) {
for _, info := range items {
p.appendMenuItem(info)
}
}
func (p *_Systray) ClearSystrayMenuItems() {
p.menuItemCallbacks = make([]CallbackInfo, 0, 10)
if p.isCreated {
C.clearSystrayMenuItems()
}
}
func (p *_Systray) handleMenuClick(index int) {
println("Want to handle menu click for index", index)
if index >= 0 && index < len(p.menuItemCallbacks) {
p.menuItemCallbacks[index].Callback()
}
}
/*
* C API to provide hooks back into Go. Without the ability to pass Go function
* pointers into C functions, the C code needs to know a priori about these
* hooks.
*/
//export menuClickCallback
func menuClickCallback(id refId, index int) {
manager, ok := gSystrays.Get(id)
if !ok {
return
}
manager.handleMenuClick(index)
}
//export menuCreatedCallback
func menuCreatedCallback(id refId) {
manager, ok := gSystrays.Get(id)
if !ok {
return
}
manager.isCreated = true
// Add all previously registered callbacks to the menu
for idx, info := range manager.menuItemCallbacks {
println("Adding callback for", info.ItemName)
manager.addItemToNativeMenu(info, idx)
}
manager.SetTooltip(manager.currentHint)
}