StarlioX/utils/tray.go

67 lines
1.1 KiB
Go
Raw Normal View History

package utils
2022-09-18 19:27:41 +03:00
import (
"fmt"
2022-09-25 11:04:32 +03:00
"os"
2022-09-18 19:27:41 +03:00
"github.com/getlantern/systray"
"github.com/shirou/gopsutil/v3/process"
"github.com/skratchdot/open-golang/open"
)
func Tray() {
2022-09-21 10:46:02 +03:00
systray.SetIcon(GetIcon("web/static/image/icons/icon.ico"))
2023-06-05 18:54:14 +03:00
systray.SetTitle("Starlio")
systray.SetTooltip("Starlio")
2022-09-19 02:07:09 +03:00
ui := systray.AddMenuItem("Open UI", "Open UI")
2022-09-18 19:27:41 +03:00
exit := systray.AddMenuItem("Quit", "Quit the whole app")
for {
select {
case <-ui.ClickedCh:
err := open.Run("http://localhost:3000")
2022-09-18 19:27:41 +03:00
if err != nil {
2022-09-25 11:04:32 +03:00
Logger(err.Error())
2022-09-18 19:27:41 +03:00
}
case <-exit.ClickedCh:
Quit()
}
}
}
func Quit() {
2023-06-05 18:54:14 +03:00
err := KillProcess("Starlio.exe")
2022-09-18 19:27:41 +03:00
if err != nil {
2022-09-25 11:04:32 +03:00
Logger(err.Error())
2022-09-18 19:27:41 +03:00
}
systray.Quit()
}
func KillProcess(name string) error {
processes, err := process.Processes()
if err != nil {
2022-09-25 11:04:32 +03:00
Logger(err.Error())
2022-09-18 19:27:41 +03:00
}
for _, p := range processes {
2022-09-25 11:04:32 +03:00
n, err := p.Name()
if err != nil {
Logger(err.Error())
2022-09-18 19:27:41 +03:00
}
if n == name {
return p.Kill()
}
}
return fmt.Errorf("process not found")
}
func GetIcon(s string) []byte {
b, err := os.ReadFile(s)
if err != nil {
2022-09-25 11:04:32 +03:00
Logger(err.Error())
2022-09-18 19:27:41 +03:00
}
return b
}