@@ -16,15 +16,17 @@ package tray
1616
1717import (
1818 "log/slog"
19+ "os/exec"
20+ "runtime"
1921
2022 "fyne.io/systray"
2123)
2224
2325// App holds references to all major components of the tray
2426// application.
2527type App struct {
26- config TrayConfig
27- process * ProcessManager
28+ config TrayConfig
29+ conn * ConnectionManager
2830}
2931
3032// NewApp creates and initialises the tray application.
@@ -40,9 +42,9 @@ func NewApp() (*App, error) {
4042
4143 a := & App {
4244 config : cfg ,
43- process : NewProcessManager (
44- WithBinary (cfg .AdderBinary ),
45- WithConfigFile (cfg .AdderConfig ),
45+ conn : NewConnectionManager (
46+ WithConnectionAddress (cfg .APIAddress ),
47+ WithConnectionPort (cfg .APIPort ),
4648 ),
4749 }
4850
@@ -55,43 +57,65 @@ func (a *App) Run() {
5557}
5658
5759// onReady is called when the system tray is initialised. It
58- // configures the tray icon, menu, and starts adder if configured.
60+ // configures the tray icon, menu, and connects to adder if
61+ // configured.
5962func (a * App ) onReady () {
6063 systray .SetTitle ("Adder" )
6164 systray .SetTooltip ("Adder - Cardano Event Streamer" )
6265
63- mStart := systray .AddMenuItem ("Start" , "Start adder" )
64- mStop := systray .AddMenuItem ("Stop" , "Stop adder" )
65- mRestart := systray .AddMenuItem (
66- "Restart" , "Restart adder" ,
66+ mStatus := systray .AddMenuItem (
67+ "Status: " + a .conn .status .Status ().String (), "" ,
6768 )
69+ mStatus .Disable ()
6870 systray .AddSeparator ()
71+
72+ mConnect := systray .AddMenuItem ("Connect" , "Connect to adder" )
73+ mDisconnect := systray .AddMenuItem (
74+ "Disconnect" , "Disconnect from adder" ,
75+ )
76+ mReconnect := systray .AddMenuItem (
77+ "Reconnect" , "Reconnect to adder" ,
78+ )
79+ systray .AddSeparator ()
80+
81+ mShowConfig := systray .AddMenuItem (
82+ "Show Config Folder" , "Open the config directory" ,
83+ )
84+ mShowLogs := systray .AddMenuItem (
85+ "Show Logs" , "Open the log directory" ,
86+ )
87+ systray .AddSeparator ()
88+
6989 mQuit := systray .AddMenuItem ("Quit" , "Quit adder-tray" )
7090
91+ // Update status menu item when connection status changes
92+ a .conn .status .OnChange (func (s Status ) {
93+ mStatus .SetTitle ("Status: " + s .String ())
94+ })
95+
7196 go func () {
7297 for {
7398 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 {
99+ case <- mConnect .ClickedCh :
100+ if err := a .conn .Connect (); err != nil {
83101 slog .Error (
84- "failed to stop adder " ,
102+ "failed to connect " ,
85103 "error" , err ,
86104 )
87105 }
88- case <- mRestart .ClickedCh :
89- if err := a .process .Restart (); err != nil {
106+ case <- mDisconnect .ClickedCh :
107+ a .conn .Disconnect ()
108+ case <- mReconnect .ClickedCh :
109+ if err := a .conn .Reconnect (); err != nil {
90110 slog .Error (
91- "failed to restart adder " ,
111+ "failed to reconnect " ,
92112 "error" , err ,
93113 )
94114 }
115+ case <- mShowConfig .ClickedCh :
116+ openFolder (ConfigDir ())
117+ case <- mShowLogs .ClickedCh :
118+ openFolder (LogDir ())
95119 case <- mQuit .ClickedCh :
96120 systray .Quit ()
97121 return
@@ -102,31 +126,42 @@ func (a *App) onReady() {
102126 slog .Info ("starting adder-tray" )
103127
104128 if a .config .AutoStart {
105- if err := a .process . Start (); err != nil {
129+ if err := a .conn . Connect (); err != nil {
106130 slog .Error (
107- "failed to auto-start adder" ,
131+ "failed to auto-connect to adder" ,
108132 "error" , err ,
109133 )
110134 }
111135 }
112136}
113137
114- // onExit is called when the system tray is shutting down.
138+ // onExit is called when the system tray is shutting down. It
139+ // disconnects the WS client but does NOT stop the adder service.
115140func (a * App ) onExit () {
116141 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- }
142+ a .conn .Disconnect ()
126143}
127144
128- // Shutdown requests a graceful shutdown of the tray application
129- // and its managed adder process.
145+ // Shutdown requests a graceful shutdown of the tray application.
130146func (a * App ) Shutdown () {
131147 systray .Quit ()
132148}
149+
150+ // openFolder opens the given directory in the platform file manager.
151+ func openFolder (dir string ) {
152+ var cmd string
153+ switch runtime .GOOS {
154+ case "darwin" :
155+ cmd = "open"
156+ case "windows" :
157+ cmd = "explorer"
158+ default :
159+ cmd = "xdg-open"
160+ }
161+ p := exec .Command (cmd , dir ) //nolint:gosec // directory path from internal config
162+ if err := p .Start (); err != nil {
163+ slog .Error ("failed to open folder" , "dir" , dir , "error" , err )
164+ return
165+ }
166+ _ = p .Process .Release ()
167+ }
0 commit comments