@@ -53,7 +53,7 @@ function buildCheckbox (key, label) {
5353// they natively work as soon as the menu opens. They don't work like that on Windows
5454// or other OSes and must be registered globally. They still collide with global
5555// accelerator. Please see ../utils/setup-global-shortcut.js for more info.
56- function buildMenu ( ctx , peerCount ) {
56+ function buildMenu ( ctx , peerCount = 0 ) {
5757 return Menu . buildFromTemplate ( [
5858 ...[
5959 [ 'ipfsIsStarting' , 'yellow' ] ,
@@ -72,7 +72,7 @@ function buildMenu (ctx, peerCount) {
7272 } ) ) ,
7373 {
7474 id : 'peerCount' ,
75- label : peerCount . toString ( ) + ' ' + i18n . t ( 'peerCount' ) ,
75+ label : i18n . t ( 'peerCount' , { peerCount } ) ,
7676 enabled : false
7777 } ,
7878 {
@@ -296,9 +296,7 @@ module.exports = function (ctx) {
296296 if ( state . status === STATUS . STARTING_FINISHED && ctx . getIpfsd ) {
297297 ctx . getIpfsd ( ) . then ( ( daemon ) => {
298298 daemon . api . swarm . peers ( ) . then ( ( value ) => {
299- if ( value . length ) {
300- ipcMain . emit ( 'peerCountFetched' , value . length )
301- }
299+ ipcMain . emit ( 'peerCountFetched' , Array . isArray ( value ) ? value . length : 0 )
302300 } )
303301 } )
304302 } else {
@@ -310,7 +308,7 @@ module.exports = function (ctx) {
310308 menu = buildMenu ( ctx , state . peerCount )
311309
312310 tray . setContextMenu ( menu )
313- tray . setToolTip ( state . peerCount . toString ( ) + ' ' + i18n . t ( ' peerCount' ) )
311+ tray . setToolTip ( i18n . t ( 'peerCount' , { peerCount : state . peerCount } ) )
314312
315313 menu . on ( 'menu-will-show' , ( ) => { ipcMain . emit ( 'menubar-will-open' ) } )
316314 menu . on ( 'menu-will-close' , ( ) => { ipcMain . emit ( 'menubar-will-close' ) } )
@@ -374,13 +372,13 @@ module.exports = function (ctx) {
374372 // On Linux, in order for changes made to individual MenuItems to take effect,
375373 // you have to call setContextMenu again - https://electronjs.org/docs/api/tray
376374 tray . setContextMenu ( menu )
375+ tray . setToolTip ( i18n . t ( 'peerCount' , { peerCount : state . peerCount } ) )
377376 }
378377 }
379378
380379 ipcMain . on ( 'menubar-will-open' , ( ) => {
381380 fetchPeers ( )
382381 setupMenu ( )
383- updateMenu ( )
384382 } )
385383
386384 ipcMain . on ( 'ipfsd' , status => {
@@ -411,9 +409,9 @@ module.exports = function (ctx) {
411409 ipcMain . on ( 'peerCountFetched' , peerCount => {
412410 // When a new peer count is retrieved, rebuild the menu and update
413411 // the tray tooltip with the new number if necessary.
414- if ( peerCount !== state . peerCount ) {
412+ if ( typeof peerCount === 'number' && peerCount !== state . peerCount ) {
415413 state . peerCount = peerCount
416- tray . setToolTip ( state . peerCount . toString ( ) + ' ' + i18n . t ( ' peerCount' ) )
414+ tray . setToolTip ( i18n . t ( 'peerCount' , { peerCount : state . peerCount } ) )
417415 }
418416 } )
419417
@@ -422,7 +420,17 @@ module.exports = function (ctx) {
422420
423421 setupMenu ( )
424422
425- tray . on ( 'mouse-move' , ( ) => { fetchPeers ( ) } )
423+ // Trigger peer count refresh using event available on specific platform
424+ const platformSupportsMouseMoveEvent = IS_MAC || IS_WIN
425+ if ( platformSupportsMouseMoveEvent ) {
426+ tray . on ( 'mouse-move' , fetchPeers )
427+ } else {
428+ /* TODO: what to do on Linux?
429+ - 'mouse-move' event is mac and windows only
430+ - When app indicator is used on Linux, the 'click' events are ignored.
431+ */
432+ }
433+
426434 ctx . tray = tray
427435 logger . info ( '[tray] started' )
428436}
0 commit comments