@@ -86,8 +86,14 @@ const store = new Store({ name: "electron-config" });
8686
8787let mainWindow = null ;
8888global . appQuitting = false ;
89- global . minimizeToTray = store . get ( 'minimizeToTray' , true ) ;
9089
90+ // It's important to call `path.join` so we don't end up with the packaged asar in the final path.
91+ const iconFile = `riot.${ process . platform === 'win32' ? 'ico' : 'png' } ` ;
92+ const iconPath = path . join ( __dirname , ".." , ".." , "img" , iconFile ) ;
93+ const trayConfig = {
94+ icon_path : iconPath ,
95+ brand : vectorConfig . brand || 'Riot' ,
96+ } ;
9197
9298// handle uncaught errors otherwise it displays
9399// stack traces in popup dialogs, which is terrible (which
@@ -168,10 +174,16 @@ ipcMain.on('ipcCall', async function(ev, payload) {
168174 }
169175 break ;
170176 case 'getMinimizeToTrayEnabled' :
171- ret = global . minimizeToTray ;
177+ ret = tray . hasTray ( ) ;
172178 break ;
173179 case 'setMinimizeToTrayEnabled' :
174- store . set ( 'minimizeToTray' , global . minimizeToTray = args [ 0 ] ) ;
180+ if ( args [ 0 ] ) {
181+ // Create trayIcon icon
182+ tray . create ( trayConfig ) ;
183+ } else {
184+ tray . destroy ( ) ;
185+ }
186+ store . set ( 'minimizeToTray' , args [ 0 ] ) ;
175187 break ;
176188 case 'getAutoHideMenuBarEnabled' :
177189 ret = global . mainWindow . isMenuBarAutoHide ( ) ;
@@ -330,11 +342,6 @@ app.on('ready', () => {
330342 console . log ( 'No update_base_url is defined: auto update is disabled' ) ;
331343 }
332344
333- // It's important to call `path.join` so we don't end up with the packaged
334- // asar in the final path.
335- const iconFile = `riot.${ process . platform === 'win32' ? 'ico' : 'png' } ` ;
336- const iconPath = path . join ( __dirname , ".." , ".." , "img" , iconFile ) ;
337-
338345 // Load the previous window state with fallback to defaults
339346 const mainWindowState = windowStateKeeper ( {
340347 defaultWidth : 1024 ,
@@ -367,15 +374,8 @@ app.on('ready', () => {
367374 mainWindow . loadURL ( 'vector://vector/webapp/' ) ;
368375 Menu . setApplicationMenu ( vectorMenu ) ;
369376
370- // explicitly hide because setApplicationMenu on Linux otherwise shows...
371- // https://github.com/electron/electron/issues/9621
372- mainWindow . hide ( ) ;
373-
374377 // Create trayIcon icon
375- tray . create ( {
376- icon_path : iconPath ,
377- brand : vectorConfig . brand || 'Riot' ,
378- } ) ;
378+ if ( store . get ( 'minimizeToTray' , true ) ) tray . create ( trayConfig ) ;
379379
380380 mainWindow . once ( 'ready-to-show' , ( ) => {
381381 mainWindowState . manage ( mainWindow ) ;
@@ -392,7 +392,8 @@ app.on('ready', () => {
392392 mainWindow = global . mainWindow = null ;
393393 } ) ;
394394 mainWindow . on ( 'close' , ( e ) => {
395- if ( global . minimizeToTray && ! global . appQuitting && ( tray . hasTray ( ) || process . platform === 'darwin' ) ) {
395+ // If we are not quitting and have a tray icon then minimize to tray
396+ if ( ! global . appQuitting && tray . hasTray ( ) ) {
396397 // On Mac, closing the window just hides it
397398 // (this is generally how single-window Mac apps
398399 // behave, eg. Mail.app)
0 commit comments