From 6bcd434585d0681db8950599a4b0470362be54cc Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Tue, 19 Feb 2019 17:35:35 -0800 Subject: [PATCH] Prevent crash when tray icon no longer available (due to apt) --- app/tray_icon.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/tray_icon.js b/app/tray_icon.js index 407c2584ec..d913cf06bc 100644 --- a/app/tray_icon.js +++ b/app/tray_icon.js @@ -1,5 +1,6 @@ const path = require('path'); +const fs = require('fs'); const { app, Menu, Tray } = require('electron'); let trayContextMenu = null; @@ -78,14 +79,20 @@ function createTrayIcon(getMainWindow, messages) { }; tray.updateIcon = unreadCount => { + let image; + if (unreadCount > 0) { const filename = `${String(unreadCount >= 10 ? 10 : unreadCount)}.png`; - tray.setImage( - path.join(__dirname, '..', 'images', 'alert', iconSize, filename) - ); + image = path.join(__dirname, '..', 'images', 'alert', iconSize, filename); } else { - tray.setImage(iconNoNewMessages); + image = iconNoNewMessages; } + + if (!fs.existsSync(image)) { + console.log('tray.updateIcon: Image for tray update does not exist!'); + return; + } + tray.setImage(image); }; tray.on('click', tray.showWindow);