@@ -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 ) {
56
+ function buildMenu ( ctx , peerCount ) {
57
57
return Menu . buildFromTemplate ( [
58
58
...[
59
59
[ 'ipfsIsStarting' , 'yellow' ] ,
@@ -70,6 +70,11 @@ function buildMenu (ctx) {
70
70
enabled : false ,
71
71
icon : path . resolve ( path . join ( __dirname , `../assets/icons/status/${ color } .png` ) )
72
72
} ) ) ,
73
+ {
74
+ id : 'peerCount' ,
75
+ label : peerCount . toString ( ) + ' ' + i18n . t ( 'peerCount' ) ,
76
+ enabled : false
77
+ } ,
73
78
{
74
79
id : 'restartIpfs' ,
75
80
label : i18n . t ( 'restart' ) ,
@@ -260,7 +265,8 @@ module.exports = function (ctx) {
260
265
const state = {
261
266
status : null ,
262
267
gcRunning : false ,
263
- isUpdating : false
268
+ isUpdating : false ,
269
+ peerCount : 0
264
270
}
265
271
266
272
// macOS tray drop files
@@ -284,15 +290,30 @@ module.exports = function (ctx) {
284
290
tray . on ( 'right-click' , popupMenu )
285
291
tray . on ( 'double-click' , ( ) => ctx . launchWebUI ( '/' ) )
286
292
293
+ const pollPeers = ( ) => {
294
+ // If the daemon is running, send a request to retrieve the number
295
+ // of connected peers. Emit 'peersPolled' event upon retrieval.
296
+ if ( state . status === STATUS . STARTING_FINISHED && ctx . getIpfsd ) {
297
+ ctx . getIpfsd ( ) . then ( ( daemon ) => {
298
+ daemon . api . swarm . peers ( ) . then ( ( value ) => {
299
+ if ( value . length ) {
300
+ ipcMain . emit ( 'peersPolled' , value . length )
301
+ }
302
+ } )
303
+ } )
304
+ } else {
305
+ ipcMain . emit ( 'peersPolled' , 0 )
306
+ }
307
+ }
308
+
287
309
const setupMenu = ( ) => {
288
- menu = buildMenu ( ctx )
310
+ menu = buildMenu ( ctx , state . peerCount )
289
311
290
312
tray . setContextMenu ( menu )
291
- tray . setToolTip ( 'IPFS Desktop' )
313
+ tray . setToolTip ( state . peerCount . toString ( ) + ' ' + i18n . t ( 'peerCount' ) )
292
314
293
315
menu . on ( 'menu-will-show' , ( ) => { ipcMain . emit ( 'menubar-will-open' ) } )
294
316
menu . on ( 'menu-will-close' , ( ) => { ipcMain . emit ( 'menubar-will-close' ) } )
295
-
296
317
updateMenu ( )
297
318
}
298
319
@@ -305,6 +326,7 @@ module.exports = function (ctx) {
305
326
menu . getMenuItemById ( 'ipfsIsStopping' ) . visible = status === STATUS . STOPPING_STARTED && ! gcRunning && ! isUpdating
306
327
menu . getMenuItemById ( 'ipfsIsNotRunning' ) . visible = status === STATUS . STOPPING_FINISHED && ! gcRunning && ! isUpdating
307
328
menu . getMenuItemById ( 'ipfsHasErrored' ) . visible = errored && ! gcRunning && ! isUpdating
329
+ menu . getMenuItemById ( 'peerCount' ) . visible = status === STATUS . STARTING_FINISHED
308
330
menu . getMenuItemById ( 'runningWithGC' ) . visible = gcRunning
309
331
menu . getMenuItemById ( 'runningWhileCheckingForUpdate' ) . visible = isUpdating
310
332
@@ -380,10 +402,25 @@ module.exports = function (ctx) {
380
402
updateMenu ( )
381
403
} )
382
404
405
+ ipcMain . on ( 'peersPolled' , peerCount => {
406
+ // When a new peer count is retrieved, rebuild the menu and update
407
+ // the tray tooltip with the new number if necessary.
408
+ if ( peerCount !== state . peerCount ) {
409
+ state . peerCount = peerCount
410
+ menu = buildMenu ( ctx , state . peerCount )
411
+ menu . on ( 'menu-will-show' , ( ) => { ipcMain . emit ( 'menubar-will-open' ) } )
412
+ menu . on ( 'menu-will-close' , ( ) => { ipcMain . emit ( 'menubar-will-close' ) } )
413
+ tray . setContextMenu ( menu )
414
+ tray . setToolTip ( state . peerCount . toString ( ) + ' ' + i18n . t ( 'peerCount' ) )
415
+ updateMenu ( )
416
+ }
417
+ } )
418
+
383
419
ipcMain . on ( 'configUpdated' , ( ) => { updateMenu ( ) } )
384
420
ipcMain . on ( 'languageUpdated' , ( ) => { setupMenu ( ) } )
385
421
386
422
setupMenu ( )
423
+ setInterval ( pollPeers , 60000 )
387
424
388
425
ctx . tray = tray
389
426
logger . info ( '[tray] started' )
0 commit comments