Skip to content

Commit

Permalink
fix(agent): tray icon not shown
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Sep 27, 2023
1 parent 8f116ef commit d2bcc00
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
63 changes: 30 additions & 33 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,69 +70,66 @@ func newAgent(appID string, headless bool) *Agent {
done: make(chan struct{}),
headless: headless,
}
a.ui = ui.NewFyneUI(a, headless)
a.ui = ui.NewFyneUI(a)
a.config = config.NewFyneConfig()
a.setupLogging()
return a
}

// Run is the "main loop" of the agent. It sets up the agent, loads the config
// then spawns a sensor tracker and the workers to gather sensor data and
// publish it to Home Assistant
func Run(options AgentOptions) {
var wg sync.WaitGroup
var ctx context.Context
var cancelFunc context.CancelFunc
var err error

agent := newAgent(options.ID, options.Headless)
defer close(agent.done)

agent.setupLogging()
registrationDone := make(chan struct{})
configDone := make(chan struct{})

go agent.registrationProcess(context.Background(), "", "", options.Register, options.Headless, registrationDone)

var workerWg sync.WaitGroup
var ctx context.Context
var cancelFunc context.CancelFunc
wg.Add(1)
go func() {
defer wg.Done()
<-registrationDone
var err error
log.Trace().Msg("Registration check/action done.")
if err = UpgradeConfig(agent.config); err != nil {
log.Warn().Err(err).Msg("Could not upgrade config.")
}
if err = ValidateConfig(agent.config); err != nil {
log.Fatal().Err(err).Msg("Could not validate config.")
}
ctx, cancelFunc = agent.setupContext()
close(configDone)
if agent.sensors, err = tracker.NewSensorTracker(agent); err != nil {
log.Fatal().Err(err).Msg("Could not start.")
}
}()
wg.Wait()
log.Trace().Msg("Config load/validation done.")

if agent.sensors, err = tracker.NewSensorTracker(agent); err != nil {
log.Fatal().Err(err).Msg("Could not start.")
}

wg.Add(1)
go func() {
defer wg.Done()
sensorWorkers := deviceConfig.SensorWorkers()
sensorWorkers = append(sensorWorkers, device.ExternalIPUpdater)

workerWg.Add(1)
go func() {
defer workerWg.Done()
device.StartWorkers(ctx, agent.sensors, sensorWorkers...)
}()
workerWg.Add(1)
go func() {
defer workerWg.Done()
agent.runNotificationsWorker(ctx, options)
}()
device.StartWorkers(ctx, agent.sensors, sensorWorkers...)
}()

wg.Add(1)
go func() {
<-configDone
agent.handleSignals(cancelFunc)
agent.handleShutdown(ctx)
agent.ui.DisplayTrayIcon(ctx, agent)
defer wg.Done()
agent.runNotificationsWorker(ctx, options)
}()

if !options.Headless {
agent.ui.Run()
}
workerWg.Wait()
<-ctx.Done()
agent.handleSignals(cancelFunc)
agent.handleShutdown(ctx)
agent.ui.DisplayTrayIcon(ctx, agent)
agent.ui.Run()

wg.Wait()
}

// Register runs a registration flow. It either prompts the user for needed
Expand Down
5 changes: 3 additions & 2 deletions internal/agent/ui/fyneUI.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type fyneUI struct {
}

func (ui *fyneUI) Run() {
log.Trace().Msg("Starting Fyne UI loop.")
ui.app.Run()
}

Expand All @@ -62,13 +63,13 @@ func (ui *fyneUI) openURL(u string) {
}
}

func NewFyneUI(agent Agent, headless bool) *fyneUI {
func NewFyneUI(agent Agent) *fyneUI {
ui := &fyneUI{
app: app.NewWithID(agent.AppID()),
text: translations.NewTranslator(),
}
ui.app.SetIcon(&TrayIcon{})
if !headless {
if !agent.IsHeadless() {
ui.mainWindow = ui.app.NewWindow(agent.AppName())
ui.mainWindow.SetCloseIntercept(func() {
ui.mainWindow.Hide()
Expand Down

0 comments on commit d2bcc00

Please sign in to comment.