-
Notifications
You must be signed in to change notification settings - Fork 2
/
tray.go
68 lines (57 loc) · 1.64 KB
/
tray.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
//go:build linux || windows
// +build linux windows
package app
import (
"context"
"os"
"fyne.io/systray"
"github.com/nervatura/nervatura/service/pkg/icon"
ut "github.com/nervatura/nervatura/service/pkg/utils"
)
// systemTray implements the Nervatura system tray icon and menu
type systemTray struct {
app *App
interrupt chan os.Signal
ctx context.Context
httpDisabled bool
}
func init() {
traySrv = &systemTray{}
}
func (st *systemTray) onReady() {
systray.SetTemplateIcon(icon.Data, icon.Data)
systray.SetTitle("Nervatura")
systray.SetTooltip("Nervatura " + ut.ToString(st.app.config["version"], ""))
mnuConfig := systray.AddMenuItem(ut.GetMessage("admin_configuration_values"), ut.GetMessage("admin_configuration_values"))
mnuAdmin := systray.AddMenuItem(ut.GetMessage("task_admin"), ut.GetMessage("task_admin"))
if st.httpDisabled {
mnuConfig.Disable()
mnuAdmin.Disable()
}
systray.AddSeparator()
mnuExit := systray.AddMenuItem(ut.GetMessage("task_exit"), ut.GetMessage("task_exit"))
go func() {
for {
select {
case <-mnuConfig.ClickedCh:
st.app.onTrayMenu("config")
case <-mnuAdmin.ClickedCh:
st.app.onTrayMenu("admin")
case <-mnuExit.ClickedCh:
systray.Quit()
case <-st.interrupt:
systray.Quit()
case <-st.ctx.Done():
systray.Quit()
}
}
}()
}
// Run initializes GUI and starts the event loop, then invokes the onReady callback.
func (st *systemTray) Run(app *App, interrupt chan os.Signal, ctx context.Context, httpDisabled bool, onExit func()) {
st.app = app
st.interrupt = interrupt
st.ctx = ctx
st.httpDisabled = httpDisabled
systray.Run(st.onReady, onExit)
}