|
| 1 | +// Copyright 2026 Blink Labs Software |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package tray |
| 16 | + |
| 17 | +import ( |
| 18 | + "log/slog" |
| 19 | + |
| 20 | + "fyne.io/systray" |
| 21 | +) |
| 22 | + |
| 23 | +// App holds references to all major components of the tray |
| 24 | +// application. |
| 25 | +type App struct { |
| 26 | + config TrayConfig |
| 27 | + process *ProcessManager |
| 28 | +} |
| 29 | + |
| 30 | +// NewApp creates and initialises the tray application. |
| 31 | +func NewApp() (*App, error) { |
| 32 | + cfg, err := LoadConfig() |
| 33 | + if err != nil { |
| 34 | + slog.Warn( |
| 35 | + "failed to load config, using defaults", |
| 36 | + "error", err, |
| 37 | + ) |
| 38 | + cfg = DefaultConfig() |
| 39 | + } |
| 40 | + |
| 41 | + a := &App{ |
| 42 | + config: cfg, |
| 43 | + process: NewProcessManager( |
| 44 | + WithBinary(cfg.AdderBinary), |
| 45 | + WithConfigFile(cfg.AdderConfig), |
| 46 | + ), |
| 47 | + } |
| 48 | + |
| 49 | + return a, nil |
| 50 | +} |
| 51 | + |
| 52 | +// Run starts the system tray and blocks until Quit is called. |
| 53 | +func (a *App) Run() { |
| 54 | + systray.Run(a.onReady, a.onExit) |
| 55 | +} |
| 56 | + |
| 57 | +// onReady is called when the system tray is initialised. It |
| 58 | +// configures the tray icon, menu, and starts adder if configured. |
| 59 | +func (a *App) onReady() { |
| 60 | + systray.SetTitle("Adder") |
| 61 | + systray.SetTooltip("Adder - Cardano Event Streamer") |
| 62 | + |
| 63 | + mStart := systray.AddMenuItem("Start", "Start adder") |
| 64 | + mStop := systray.AddMenuItem("Stop", "Stop adder") |
| 65 | + mRestart := systray.AddMenuItem( |
| 66 | + "Restart", "Restart adder", |
| 67 | + ) |
| 68 | + systray.AddSeparator() |
| 69 | + mQuit := systray.AddMenuItem("Quit", "Quit adder-tray") |
| 70 | + |
| 71 | + go func() { |
| 72 | + for { |
| 73 | + select { |
| 74 | + case <-mStart.ClickedCh: |
| 75 | + if err := a.process.Start(); err != nil { |
| 76 | + slog.Error( |
| 77 | + "failed to start adder", |
| 78 | + "error", err, |
| 79 | + ) |
| 80 | + } |
| 81 | + case <-mStop.ClickedCh: |
| 82 | + if err := a.process.Stop(); err != nil { |
| 83 | + slog.Error( |
| 84 | + "failed to stop adder", |
| 85 | + "error", err, |
| 86 | + ) |
| 87 | + } |
| 88 | + case <-mRestart.ClickedCh: |
| 89 | + if err := a.process.Restart(); err != nil { |
| 90 | + slog.Error( |
| 91 | + "failed to restart adder", |
| 92 | + "error", err, |
| 93 | + ) |
| 94 | + } |
| 95 | + case <-mQuit.ClickedCh: |
| 96 | + systray.Quit() |
| 97 | + return |
| 98 | + } |
| 99 | + } |
| 100 | + }() |
| 101 | + |
| 102 | + slog.Info("starting adder-tray") |
| 103 | + |
| 104 | + if a.config.AutoStart { |
| 105 | + if err := a.process.Start(); err != nil { |
| 106 | + slog.Error( |
| 107 | + "failed to auto-start adder", |
| 108 | + "error", err, |
| 109 | + ) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// onExit is called when the system tray is shutting down. |
| 115 | +func (a *App) onExit() { |
| 116 | + slog.Info("shutting down adder-tray") |
| 117 | + |
| 118 | + if a.process.IsRunning() { |
| 119 | + if err := a.process.Stop(); err != nil { |
| 120 | + slog.Error( |
| 121 | + "error stopping adder during shutdown", |
| 122 | + "error", err, |
| 123 | + ) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// Shutdown requests a graceful shutdown of the tray application |
| 129 | +// and its managed adder process. |
| 130 | +func (a *App) Shutdown() { |
| 131 | + systray.Quit() |
| 132 | +} |
0 commit comments