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.go
445 lines (380 loc) · 10.7 KB
/
systray.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
/*
Package systray is a cross-platform Go library to place an icon and menu in the
notification area. Based on https://github.com/getlantern/systray and provides few improvements:
* Windows system calls instead of custom DLL
* Caching icon files
* Allows to set direct path to icon file
* Error handling
Supports Windows, Mac OSX and Linux currently.
Methods can be called from any goroutine except Run(), which should be called
at the very beginning of main() to lock at main thread.
*/
package systray
import (
"errors"
"os"
"runtime"
"sort"
"sync"
"sync/atomic"
)
const (
ItemDefault = 0
ItemSeparator byte = 1 << iota
ItemChecked
ItemCheckable
ItemDisabled
)
type ClickActionType func() error
var (
hasStarted = int32(0)
hasQuit = int32(0)
leftClickAction ClickActionType
rightClickAction ClickActionType
leftDblClickAction ClickActionType
rightDblClickAction ClickActionType
)
// MenuItem is used to keep track each menu item of systray
// Don't create it directly, use the one systray.AddMenuItem() returned
type MenuItem struct {
// ClickedCh is the channel which will be notified when the menu item is clicked
clickedCh chan struct{}
// id uniquely identify a menu item, not supposed to be modified
id int32
// menuId uniquely identify a menu the menu item belogns to
menuId int32
// title is the text shown on menu item
title string
// tooltip is the text shown when pointing to menu item
tooltip string
isSeparator bool
isSubmenuItem bool
isSubmenu bool
checkable bool
checked bool
disabled bool
}
var (
systrayReady func()
systrayExit func()
menuItems = make(map[int32]*MenuItem)
menuItemsLock sync.RWMutex
iconTitle string
iconTooltip string
iconPath string
iconData []byte
idSequence = int32(-1)
)
// Run initializes GUI and starts the event loop, then invokes the onReady
// callback.
// It blocks until systray.Quit() is called.
// Should be called at the very beginning of main() to lock at main thread.
func Run(onReady func(), onExit func()) error {
runtime.LockOSThread()
systrayReady = func() {
atomic.StoreInt32(&hasStarted, 1)
initMenu()
if onReady != nil {
onReady()
}
}
// onExit runs in the event loop to make sure it has time to
// finish before the process terminates
if onExit == nil {
onExit = func() {}
}
systrayExit = onExit
return nativeLoop()
}
// Quit the systray
func Quit() error {
if atomic.LoadInt32(&hasStarted) == 1 && atomic.CompareAndSwapInt32(&hasQuit, 0, 1) {
return quit()
}
return nil
}
// AddMenuItem adds menu item with designated title and tooltip, returning a channel
// that notifies whenever that menu item is clicked.
//
// It can be safely invoked from different goroutines.
func AddMenuItem(title, tooltip string, flags byte) *MenuItem {
menuItemsLock.Lock()
defer menuItemsLock.Unlock()
if ItemSeparator&flags != 0 {
// remove other flags if separator
flags = ItemSeparator
} else if ItemCheckable&flags == 0 {
// remove "checked" flag if not checkable
flags &= ^ItemChecked
}
item := &MenuItem{
clickedCh: make(chan struct{}),
id: atomic.AddInt32(&idSequence, 1),
title: title,
tooltip: tooltip,
isSubmenuItem: false,
isSubmenu: false,
isSeparator: ItemSeparator&flags != 0,
checkable: ItemCheckable&flags != 0,
checked: ItemChecked&flags != 0,
disabled: ItemDisabled&flags != 0,
}
menuItems[item.id] = item
if atomic.LoadInt32(&hasStarted) == 1 {
item.update()
}
return item
}
// AddSubMenu adds a sub menu to the systray and
// and returns an id to access to accesss the menu
func AddSubMenu(title string) *MenuItem {
subMenuId := atomic.AddInt32(&idSequence, 1)
createSubMenu(subMenuId)
item := &MenuItem{
clickedCh: make(chan struct{}),
id: atomic.AddInt32(&idSequence, 1),
title: title,
menuId: subMenuId,
isSubmenuItem: false,
isSubmenu: true,
}
if atomic.LoadInt32(&hasStarted) == 1 {
addSubmenuToTray(item)
}
return item
}
func (sitem *MenuItem) AddSubMenuItem(title, tooltip string, flags byte) *MenuItem {
menuItemsLock.Lock()
defer menuItemsLock.Unlock()
if ItemSeparator&flags != 0 {
// remove other flags if separator
flags = ItemSeparator
} else if ItemCheckable&flags == 0 {
// remove "checked" flag if not checkable
flags &= ^ItemChecked
}
item := &MenuItem{
clickedCh: make(chan struct{}),
id: atomic.AddInt32(&idSequence, 1),
title: title,
tooltip: tooltip,
menuId: sitem.menuId,
isSeparator: ItemSeparator&flags != 0,
isSubmenuItem: true,
isSubmenu: false,
checkable: ItemCheckable&flags != 0,
checked: ItemChecked&flags != 0,
disabled: ItemDisabled&flags != 0,
}
menuItems[item.id] = item
if atomic.LoadInt32(&hasStarted) == 1 {
item.update()
}
return item
}
func (item *MenuItem) AddBitmap(bitmapByte []byte) error {
menuItemsLock.Lock()
defer menuItemsLock.Unlock()
if atomic.LoadInt32(&hasStarted) == 1 {
return addBitmap(bitmapByte, item)
}
return errors.New("Could not set bitmap on menuitem")
}
func (item *MenuItem) AddBitmapPath(filepath string) error {
menuItemsLock.Lock()
defer menuItemsLock.Unlock()
if atomic.LoadInt32(&hasStarted) == 1 {
return addBitmapPath(filepath, item)
}
return errors.New("Could not set bitmap on menuitem")
}
// AddSeparator adds a separator bar to the menu.
func AddSeparator() *MenuItem {
return AddMenuItem("", "", ItemSeparator)
}
// SetTitle set the text to display on a menu item.
func (item *MenuItem) SetTitle(title string) error {
item.title = title
return item.update()
}
// SetTooltip set the tooltip to show when mouse hover.
func (item *MenuItem) SetTooltip(tooltip string) error {
item.tooltip = tooltip
return item.update()
}
// Disabled checkes if the menu item is disabled.
func (item *MenuItem) Disabled() bool {
return item.disabled
}
// Enable a menu item regardless if it's previously enabled or not.
func (item *MenuItem) Enable() error {
item.disabled = false
return item.update()
}
// Disable a menu item regardless if it's previously disabled or not.
func (item *MenuItem) Disable() error {
item.disabled = true
return item.update()
}
// Hide hides a menu item.
func (item *MenuItem) Hide() error {
return hideMenuItem(item)
}
// Show shows a previously hidden menu item.
func (item *MenuItem) Show() error {
return showMenuItem(item)
}
// Checked returns if the menu item has a check mark.
func (item *MenuItem) Checked() bool {
return item.checked
}
// Check a menu item regardless if it's previously checked or not.
func (item *MenuItem) Check() error {
if !item.checkable {
return errors.New("item must be checkable")
}
item.checked = true
return item.update()
}
// Uncheck a menu item regardless if it's previously unchecked or not.
func (item *MenuItem) Uncheck() error {
if !item.checkable {
return errors.New("item must be checkable")
}
item.checked = false
return item.update()
}
// update propogates changes on a menu item to systray.
func (item *MenuItem) update() error {
return addOrUpdateMenuItem(item)
}
func (item *MenuItem) OnClickCh() <-chan struct{} {
return item.clickedCh
}
// SetIcon sets the systray icon.
// iconBytes should be the content of *.ico for windows and *.ico/*.jpg/*.png
// for other platforms.
// On windows and linux it creates reusable temporary file with icon content. This file
func SetIcon(iconBytes []byte) error {
if atomic.LoadInt32(&hasStarted) == 0 {
iconData = iconBytes
return nil
}
return setIcon(iconBytes)
}
// SetIconPath sets icon by direct path.
// It should be *.ico for windows and *.ico/*.jpg/*.png for other platforms.
func SetIconPath(path string) error {
if _, err := os.Stat(path); err != nil {
return err
}
if atomic.LoadInt32(&hasStarted) == 0 {
iconPath = path
return nil
}
return setIconPath(path)
}
// SetTitle sets the systray title, only available on Mac.
func SetTitle(title string) error {
if atomic.LoadInt32(&hasStarted) == 0 {
iconTitle = title
return nil
}
return setTitle(title)
}
// SetTooltip sets the systray tooltip to display on mouse hover of the tray icon,
// only available on Mac and Windows.
func SetTooltip(tooltip string) error {
if atomic.LoadInt32(&hasStarted) == 0 {
iconTooltip = tooltip
return nil
}
return setTooltip(tooltip)
}
func initMenu() {
menuItemsLock.RLock()
defer menuItemsLock.RUnlock()
if iconPath != "" {
SetIconPath(iconPath)
} else if iconData != nil {
SetIcon(iconData)
}
if iconTitle != "" {
SetTitle(iconTitle)
}
if iconTooltip != "" {
SetTooltip(iconTooltip)
}
keys := make([]int, len(menuItems))
i := 0
for k := range menuItems {
keys[i] = int(k)
i++
}
sort.Ints(keys)
for _, k := range keys {
menuItems[int32(k)].update()
}
}
func systrayMenuItemSelected(id int32, forceState bool, newState bool) {
menuItemsLock.RLock()
defer menuItemsLock.RUnlock()
item, ok := menuItems[id]
if !ok {
return
}
if item.checkable {
if forceState {
item.checked = newState
} else {
// toggle state
item.checked = !item.checked
item.update()
}
}
select {
case item.clickedCh <- struct{}{}:
// in case no one waiting for the channel
default:
}
}
// SetCustomLeftClickAction set custom function to process left click on icon
// only windows at this moment
func SetCustomLeftClickAction(ff ClickActionType) {
setCustomLeftClickAction(ff)
}
// SetCustomRightClickAction set custom function to process right click on icon
// only windows at this moment
func SetCustomRightClickAction(ff ClickActionType) {
setCustomRightClickAction(ff)
}
// SetDefaultLeftClickAction set defualt function to process left click on icon (show menu)
// only windows at this moment
func SetDefaultLeftClickAction() {
setDefaultLeftClickAction()
}
// SetDefaultRightClickAction set defualt function to process right click on icon (show menu)
// only windows at this moment
func SetDefaultRightClickAction() {
setDefaultRightClickAction()
}
// SetCustomLeftDoubleClickAction set custom function to process left click on icon
// only windows at this moment
func SetCustomLeftDoubleClickAction(ff ClickActionType) {
setCustomLeftDoubleClickAction(ff)
}
// SetCustomRightDoubleClickAction set custom function to process right click on icon
// only windows at this moment
func SetCustomRightDoubleClickAction(ff ClickActionType) {
setCustomRightDoubleClickAction(ff)
}
// SetDefaultLeftDoubleClickAction set defualt function to process left click on icon (show menu)
// only windows at this moment
func SetDefaultLeftDoubleClickAction() {
setDefaultLeftDoubleClickAction()
}
// SetDefaultRightDoubleClickAction set defualt function to process right click on icon (show menu)
// only windows at this moment
func SetDefaultRightDoubleClickAction() {
setDefaultRightDoubleClickAction()
}