2023-02-02 13:42:15 +00:00
|
|
|
//go:build tray
|
2021-05-16 15:23:28 +00:00
|
|
|
// +build tray
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2023-06-11 18:48:03 +00:00
|
|
|
"time"
|
2021-05-16 15:23:28 +00:00
|
|
|
|
|
|
|
"github.com/getlantern/systray"
|
|
|
|
)
|
|
|
|
|
|
|
|
var TRAY = true
|
|
|
|
|
|
|
|
func RunTray() {
|
|
|
|
systray.Run(onReady, onExit)
|
|
|
|
}
|
|
|
|
|
2021-08-18 16:25:16 +00:00
|
|
|
func QuitTray() {
|
|
|
|
systray.Quit()
|
|
|
|
}
|
|
|
|
|
2021-05-16 15:23:28 +00:00
|
|
|
func onExit() {
|
|
|
|
if RUNNING {
|
|
|
|
QUIT = true
|
|
|
|
RESTART <- true
|
2023-06-11 18:48:03 +00:00
|
|
|
// Safety Sleep (Ensure shutdown tasks get done)
|
|
|
|
time.Sleep(time.Second)
|
2021-05-16 15:23:28 +00:00
|
|
|
}
|
|
|
|
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")
|
2021-05-25 19:16:42 +00:00
|
|
|
mOpenLogs := systray.AddMenuItem("Open logs", "Open jfa-go log file.")
|
2021-05-16 20:01:31 +00:00
|
|
|
as := NewAutostart("jfa-go", "A user management system for Jellyfin", "Run on login", "Run jfa-go on user login.")
|
2021-05-16 15:23:28 +00:00
|
|
|
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)
|
|
|
|
}()
|
2021-08-16 19:41:07 +00:00
|
|
|
|
2021-05-16 15:23:28 +00:00
|
|
|
defer func() {
|
|
|
|
systray.Quit()
|
|
|
|
}()
|
|
|
|
|
|
|
|
RESTART = make(chan bool, 1)
|
2021-06-01 18:54:13 +00:00
|
|
|
TRAYRESTART = make(chan bool, 1)
|
2021-05-16 15:23:28 +00:00
|
|
|
go start(false, true)
|
|
|
|
mStart.Disable()
|
|
|
|
mStop.Enable()
|
|
|
|
mRestart.Enable()
|
2021-05-16 20:01:31 +00:00
|
|
|
go as.HandleCheck()
|
2021-06-01 18:54:13 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2021-05-16 15:23:28 +00:00
|
|
|
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()
|
|
|
|
}
|
2021-06-01 18:54:13 +00:00
|
|
|
case <-TRAYRESTART:
|
|
|
|
trayRestart()
|
2021-05-16 15:23:28 +00:00
|
|
|
case <-mRestart.ClickedCh:
|
2021-06-01 18:54:13 +00:00
|
|
|
trayRestart()
|
2021-05-25 19:16:42 +00:00
|
|
|
case <-mOpenLogs.ClickedCh:
|
2023-02-02 13:42:15 +00:00
|
|
|
log.Printf("Opening %s\n", logPath)
|
|
|
|
OpenFile(logPath)
|
2021-05-16 15:23:28 +00:00
|
|
|
case <-mQuit.ClickedCh:
|
|
|
|
systray.Quit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|