@@ -53,7 +53,7 @@ function buildCheckbox (key, label) {
53
53
// they natively work as soon as the menu opens. They don't work like that on Windows
54
54
// or other OSes and must be registered globally. They still collide with global
55
55
// accelerator. Please see ../utils/setup-global-shortcut.js for more info.
56
- function buildMenu ( ctx , peerCount ) {
56
+ function buildMenu ( ctx , peerCount = 0 ) {
57
57
return Menu . buildFromTemplate ( [
58
58
...[
59
59
[ 'ipfsIsStarting' , 'yellow' ] ,
@@ -72,7 +72,7 @@ function buildMenu (ctx, peerCount) {
72
72
} ) ) ,
73
73
{
74
74
id : 'peerCount' ,
75
- label : peerCount . toString ( ) + ' ' + i18n . t ( 'peerCount' ) ,
75
+ label : i18n . t ( 'peerCount' , { peerCount } ) ,
76
76
enabled : false
77
77
} ,
78
78
{
@@ -296,9 +296,7 @@ module.exports = function (ctx) {
296
296
if ( state . status === STATUS . STARTING_FINISHED && ctx . getIpfsd ) {
297
297
ctx . getIpfsd ( ) . then ( ( daemon ) => {
298
298
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 )
302
300
} )
303
301
} )
304
302
} else {
@@ -310,7 +308,7 @@ module.exports = function (ctx) {
310
308
menu = buildMenu ( ctx , state . peerCount )
311
309
312
310
tray . setContextMenu ( menu )
313
- tray . setToolTip ( state . peerCount . toString ( ) + ' ' + i18n . t ( ' peerCount' ) )
311
+ tray . setToolTip ( i18n . t ( 'peerCount' , { peerCount : state . peerCount } ) )
314
312
315
313
menu . on ( 'menu-will-show' , ( ) => { ipcMain . emit ( 'menubar-will-open' ) } )
316
314
menu . on ( 'menu-will-close' , ( ) => { ipcMain . emit ( 'menubar-will-close' ) } )
@@ -374,13 +372,13 @@ module.exports = function (ctx) {
374
372
// On Linux, in order for changes made to individual MenuItems to take effect,
375
373
// you have to call setContextMenu again - https://electronjs.org/docs/api/tray
376
374
tray . setContextMenu ( menu )
375
+ tray . setToolTip ( i18n . t ( 'peerCount' , { peerCount : state . peerCount } ) )
377
376
}
378
377
}
379
378
380
379
ipcMain . on ( 'menubar-will-open' , ( ) => {
381
380
fetchPeers ( )
382
381
setupMenu ( )
383
- updateMenu ( )
384
382
} )
385
383
386
384
ipcMain . on ( 'ipfsd' , status => {
@@ -411,9 +409,9 @@ module.exports = function (ctx) {
411
409
ipcMain . on ( 'peerCountFetched' , peerCount => {
412
410
// When a new peer count is retrieved, rebuild the menu and update
413
411
// the tray tooltip with the new number if necessary.
414
- if ( peerCount !== state . peerCount ) {
412
+ if ( typeof peerCount === 'number' && peerCount !== state . peerCount ) {
415
413
state . peerCount = peerCount
416
- tray . setToolTip ( state . peerCount . toString ( ) + ' ' + i18n . t ( ' peerCount' ) )
414
+ tray . setToolTip ( i18n . t ( 'peerCount' , { peerCount : state . peerCount } ) )
417
415
}
418
416
} )
419
417
@@ -422,7 +420,17 @@ module.exports = function (ctx) {
422
420
423
421
setupMenu ( )
424
422
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
+
426
434
ctx . tray = tray
427
435
logger . info ( '[tray] started' )
428
436
}
0 commit comments