@@ -6,9 +6,11 @@ import log from './logger'
6
6
import { getAppVersion } from './util'
7
7
import { hideWindow , showWindow , showInDock } from './dock-utils'
8
8
import { showMainWindow , sendToMainWindowRenderer } from './main-window'
9
+ import { getTray , setTray } from './app-state'
9
10
10
11
// Safe tray destruction with error handling
11
- export function safeTrayDestroy ( tray : Tray | null ) {
12
+ export function safeTrayDestroy ( ) {
13
+ const tray = getTray ( )
12
14
try {
13
15
if ( tray && ! tray . isDestroyed ( ) ) {
14
16
tray . destroy ( )
@@ -62,13 +64,10 @@ function getIcon(): Electron.NativeImage {
62
64
///////////////////////////////////////////////////
63
65
64
66
const createTrayWithSetup =
65
- ( setupFn : ( tray : Tray , toolHiveIsRunning : boolean ) => void ) =>
67
+ ( setupFn : ( toolHiveIsRunning : boolean ) => void ) =>
66
68
( toolHiveIsRunning : boolean ) => {
67
69
try {
68
- const tray = new Tray ( getIcon ( ) )
69
-
70
- setupFn ( tray , toolHiveIsRunning )
71
- return tray
70
+ return setupFn ( toolHiveIsRunning )
72
71
} catch ( error ) {
73
72
log . error ( 'Failed to create tray: ' , error )
74
73
throw error
@@ -111,10 +110,7 @@ const showWindowWithFocus = (window: BrowserWindow) => {
111
110
bringToFrontOnWindows ( window )
112
111
}
113
112
114
- const handleStartOnLogin = async (
115
- currentTray : Tray ,
116
- toolHiveIsRunning : boolean
117
- ) => {
113
+ const handleStartOnLogin = async ( toolHiveIsRunning : boolean ) => {
118
114
const currentStatus = getAutoLaunchStatus ( )
119
115
120
116
try {
@@ -127,10 +123,10 @@ const handleStartOnLogin = async (
127
123
}
128
124
129
125
// Update the tray menu to reflect the new state
130
- setupTrayMenu ( currentTray , toolHiveIsRunning )
126
+ setupTrayMenu ( toolHiveIsRunning )
131
127
132
128
// Update the application menu to reflect the new state
133
- createApplicationMenu ( currentTray )
129
+ createApplicationMenu ( )
134
130
} catch ( error ) {
135
131
log . error ( 'Failed to toggle auto-launch: ' , error )
136
132
}
@@ -153,14 +149,14 @@ const getCurrentAppVersion = () => {
153
149
}
154
150
}
155
151
156
- const startOnLoginMenu = ( currentTray : Tray , toolHiveIsRunning : boolean ) => {
152
+ const startOnLoginMenu = ( toolHiveIsRunning : boolean ) => {
157
153
const isStartOnLogin = getAutoLaunchStatus ( )
158
154
return {
159
155
label : 'Start on login' ,
160
156
checked : isStartOnLogin ,
161
157
accelerator : 'CmdOrCtrl+L' ,
162
158
type : 'checkbox' as const ,
163
- click : ( ) => handleStartOnLogin ( currentTray , toolHiveIsRunning ) ,
159
+ click : ( ) => handleStartOnLogin ( toolHiveIsRunning ) ,
164
160
}
165
161
}
166
162
@@ -195,11 +191,11 @@ const createQuitMenuItem = () => ({
195
191
196
192
const createSeparator = ( ) => ( { type : 'separator' as const } )
197
193
198
- const createMenuTemplate = ( currentTray : Tray , toolHiveIsRunning : boolean ) => [
194
+ const createMenuTemplate = ( toolHiveIsRunning : boolean ) => [
199
195
createStatusMenuItem ( toolHiveIsRunning ) ,
200
196
getCurrentAppVersion ( ) ,
201
197
createSeparator ( ) ,
202
- startOnLoginMenu ( currentTray , toolHiveIsRunning ) ,
198
+ startOnLoginMenu ( toolHiveIsRunning ) ,
203
199
createSeparator ( ) ,
204
200
createShowMenuItem ( ) ,
205
201
createHideMenuItem ( ) ,
@@ -233,8 +229,15 @@ const createClickHandler = () => {
233
229
}
234
230
}
235
231
236
- function setupTrayMenu ( tray : Tray , toolHiveIsRunning : boolean ) {
237
- const menuTemplate = createMenuTemplate ( tray , toolHiveIsRunning )
232
+ // if tray is not there let's create a new one
233
+ function setupTrayMenu ( toolHiveIsRunning : boolean ) {
234
+ let tray = getTray ( )
235
+ if ( ! tray || tray . isDestroyed ( ) ) {
236
+ tray = new Tray ( getIcon ( ) )
237
+ setTray ( tray )
238
+ }
239
+
240
+ const menuTemplate = createMenuTemplate ( toolHiveIsRunning )
238
241
const contextMenu = Menu . buildFromTemplate ( menuTemplate )
239
242
240
243
tray . setToolTip ( 'ToolHive' )
@@ -247,11 +250,12 @@ function setupTrayMenu(tray: Tray, toolHiveIsRunning: boolean) {
247
250
}
248
251
249
252
// Function to update tray status without recreating the entire tray
250
- export const updateTrayStatus = ( tray : Tray , toolHiveIsRunning : boolean ) => {
253
+ export const updateTrayStatus = ( toolHiveIsRunning : boolean ) => {
254
+ const tray = getTray ( )
251
255
if ( ! tray || tray . isDestroyed ( ) ) return
252
256
253
257
try {
254
- setupTrayMenu ( tray , toolHiveIsRunning )
258
+ setupTrayMenu ( toolHiveIsRunning )
255
259
} catch ( error ) {
256
260
log . error ( 'Failed to update tray status: ' , error )
257
261
}
0 commit comments