refactor: embed icons directly, improve cleanup flow

This commit is contained in:
2024-10-27 02:35:41 -05:00
parent 72f5de71f3
commit 82638baa65
5 changed files with 23 additions and 17 deletions

40
main.go
View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"embed"
"log/slog" "log/slog"
"os" "os"
"os/signal" "os/signal"
@@ -15,6 +16,12 @@ var (
service *ga.Service service *ga.Service
log *slog.Logger log *slog.Logger
stateChannel chan string stateChannel chan string
app *ga.App
)
var (
//go:embed "resources/*.ico"
icons embed.FS
) )
func HandleState(newState string) { func HandleState(newState string) {
@@ -30,8 +37,10 @@ func HandleState(newState string) {
} }
func setupHomeAssistant() { func setupHomeAssistant() {
var err error
// Connect to Home Assistant // Connect to Home Assistant
app, err := ga.NewApp(ga.NewAppRequest{ app, err = ga.NewApp(ga.NewAppRequest{
IpAddress: "home.imfucked.lol", // Replace with your Home Assistant IP Address IpAddress: "home.imfucked.lol", // Replace with your Home Assistant IP Address
HAAuthToken: os.Getenv("HA_AUTH_TOKEN"), HAAuthToken: os.Getenv("HA_AUTH_TOKEN"),
HomeZoneEntityId: "zone.home", HomeZoneEntityId: "zone.home",
@@ -42,10 +51,6 @@ func setupHomeAssistant() {
log.Error("Error connecting to Home Assistant", "error", err) log.Error("Error connecting to Home Assistant", "error", err)
os.Exit(1) os.Exit(1)
} }
defer func() {
app.Cleanup()
log.Debug("Deferred!")
}()
service = app.GetService() service = app.GetService()
@@ -83,7 +88,7 @@ func main() {
slog.Info("Starting hass-tray") slog.Info("Starting hass-tray")
go setupHomeAssistant() go setupHomeAssistant()
systray.Run(onReady, onExit) systray.Run(onReady, func() {})
} }
func onReady() { func onReady() {
@@ -94,17 +99,17 @@ func onReady() {
menuOpenLogs.Disable() menuOpenLogs.Disable()
// Load icons // Load icons
openIcon, err := os.ReadFile("open.ico") openIcon, err := icons.ReadFile("resources/open.ico")
if err != nil { if err != nil {
slog.Error("Unable to load icon", "error", err) slog.Error("Unable to load icon", "error", err)
os.Exit(1) os.Exit(1)
} }
closedIcon, err := os.ReadFile("closed.ico") closedIcon, err := icons.ReadFile("resources/closed.ico")
if err != nil { if err != nil {
slog.Error("Unable to load icon", "error", err) slog.Error("Unable to load icon", "error", err)
os.Exit(1) os.Exit(1)
} }
unknownIcon, err := os.ReadFile("unknown.ico") unknownIcon, err := icons.ReadFile("resources/unknown.ico")
if err != nil { if err != nil {
slog.Error("Unable to load icon", "error", err) slog.Error("Unable to load icon", "error", err)
os.Exit(1) os.Exit(1)
@@ -118,14 +123,15 @@ func onReady() {
signal.Notify(interruptChannel, os.Interrupt) signal.Notify(interruptChannel, os.Interrupt)
signal.Notify(interruptChannel, os.Kill) signal.Notify(interruptChannel, os.Kill)
loop:
for { for {
select { select {
case <-interruptChannel: case signal := <-interruptChannel:
slog.Info("Received interrupt signal, quitting") slog.Info("Received interrupt signal, quitting", "signal", signal)
systray.Quit() break loop
case <-menuQuit.ClickedCh: case <-menuQuit.ClickedCh:
slog.Info("Requesting exit") slog.Info("Quit clicked")
systray.Quit() break loop
case newState := <-stateChannel: case newState := <-stateChannel:
if newState == "open" { if newState == "open" {
systray.SetIcon(openIcon) systray.SetIcon(openIcon)
@@ -137,8 +143,8 @@ func onReady() {
} }
} }
} }
}
func onExit() {
slog.Info("Cleaning up")
systray.Quit()
app.Cleanup()
} }

View File

Before

Width:  |  Height:  |  Size: 7.6 KiB

After

Width:  |  Height:  |  Size: 7.6 KiB

View File

Before

Width:  |  Height:  |  Size: 7.5 KiB

After

Width:  |  Height:  |  Size: 7.5 KiB

BIN
resources/result.ico Normal file
View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 6.8 KiB

After

Width:  |  Height:  |  Size: 6.8 KiB