-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
tray.go
117 lines (105 loc) · 2.12 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
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
//go:build tray
// +build tray
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/getlantern/systray"
)
var TRAY = true
func BuildTagsTray() {
buildTags = append(buildTags, "tray")
}
func RunTray() {
systray.Run(onReady, onExit)
}
func QuitTray() {
systray.Quit()
}
func onExit() {
if RUNNING {
QUIT = true
RESTART <- true
// Safety Sleep (Ensure shutdown tasks get done)
time.Sleep(time.Second)
}
os.Remove(SOCK)
}
func onReady() {
icon, err := localFS.ReadFile("web/favicon.ico")
if err != nil {
log.Fatalf("Failed to load favicon: %v", err)
}
systray.SetIcon(icon)
systray.SetTitle("jfa-go")
mStart := systray.AddMenuItem("Start", "Start jfa-go")
mStop := systray.AddMenuItem("Stop", "Stop jfa-go")
mRestart := systray.AddMenuItem("Restart", "Restart jfa-go")
mOpenLogs := systray.AddMenuItem("Open logs", "Open jfa-go log file.")
as := NewAutostart("jfa-go", "A user management system for Jellyfin", "Run on login", "Run jfa-go on user login.")
mQuit := systray.AddMenuItem("Quit", "Quit jfa-go")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
systray.Quit()
os.Exit(1)
}()
defer func() {
systray.Quit()
}()
RESTART = make(chan bool, 1)
TRAYRESTART = make(chan bool, 1)
go start(false, true)
mStart.Disable()
mStop.Enable()
mRestart.Enable()
go as.HandleCheck()
trayRestart := func() {
if RUNNING {
RESTART <- true
mStop.Disable()
mStart.Enable()
mRestart.Disable()
for {
if !RUNNING {
break
}
}
go start(false, false)
mStart.Disable()
mStop.Enable()
mRestart.Enable()
}
}
for {
select {
case <-mStart.ClickedCh:
if !RUNNING {
go start(false, false)
mStart.Disable()
mStop.Enable()
mRestart.Enable()
}
case <-mStop.ClickedCh:
if RUNNING {
RESTART <- true
mStop.Disable()
mStart.Enable()
mRestart.Disable()
}
case <-TRAYRESTART:
trayRestart()
case <-mRestart.ClickedCh:
trayRestart()
case <-mOpenLogs.ClickedCh:
log.Printf("Opening %s\n", logPath)
OpenFile(logPath)
case <-mQuit.ClickedCh:
systray.Quit()
}
}
}