@@ -46,7 +46,7 @@ function buildCheckbox (key, label) {
46
46
// they natively work as soon as the menu opens. They don't work like that on Windows
47
47
// or other OSes and must be registered globally. They still collide with global
48
48
// accelerator. Please see ../utils/setup-global-shortcut.js for more info.
49
- function buildMenu ( ctx ) {
49
+ function buildMenu ( ctx , peerCount ) {
50
50
return Menu . buildFromTemplate ( [
51
51
...[
52
52
[ 'ipfsIsStarting' , 'yellow' ] ,
@@ -63,6 +63,11 @@ function buildMenu (ctx) {
63
63
enabled : false ,
64
64
icon : path . resolve ( path . join ( __dirname , `../assets/icons/status/${ color } .png` ) )
65
65
} ) ) ,
66
+ {
67
+ id : 'peerCount' ,
68
+ label : peerCount . toString ( ) + ' ' + i18n . t ( 'peerCount' ) ,
69
+ enabled : false
70
+ } ,
66
71
{
67
72
id : 'restartIpfs' ,
68
73
label : i18n . t ( 'restart' ) ,
@@ -253,7 +258,8 @@ module.exports = function (ctx) {
253
258
const state = {
254
259
status : null ,
255
260
gcRunning : false ,
256
- isUpdating : false
261
+ isUpdating : false ,
262
+ peerCount : 0
257
263
}
258
264
259
265
// macOS tray drop files
@@ -271,15 +277,30 @@ module.exports = function (ctx) {
271
277
} )
272
278
}
273
279
280
+ const pollPeers = ( ) => {
281
+ // If the daemon is running, send a request to retrieve the number
282
+ // of connected peers. Emit 'peersPolled' event upon retrieval.
283
+ if ( state . status === STATUS . STARTING_FINISHED && ctx . getIpfsd ) {
284
+ ctx . getIpfsd ( ) . then ( ( daemon ) => {
285
+ daemon . api . swarm . peers ( ) . then ( ( value ) => {
286
+ if ( value . length ) {
287
+ ipcMain . emit ( 'peersPolled' , value . length )
288
+ }
289
+ } )
290
+ } )
291
+ } else {
292
+ ipcMain . emit ( 'peersPolled' , 0 )
293
+ }
294
+ }
295
+
274
296
const setupMenu = ( ) => {
275
- menu = buildMenu ( ctx )
297
+ menu = buildMenu ( ctx , state . peerCount )
276
298
277
299
tray . setContextMenu ( menu )
278
- tray . setToolTip ( 'IPFS Desktop' )
300
+ tray . setToolTip ( state . peerCount . toString ( ) + ' ' + i18n . t ( 'peerCount' ) )
279
301
280
302
menu . on ( 'menu-will-show' , ( ) => { ipcMain . emit ( 'menubar-will-open' ) } )
281
303
menu . on ( 'menu-will-close' , ( ) => { ipcMain . emit ( 'menubar-will-close' ) } )
282
-
283
304
updateMenu ( )
284
305
}
285
306
@@ -292,6 +313,7 @@ module.exports = function (ctx) {
292
313
menu . getMenuItemById ( 'ipfsIsStopping' ) . visible = status === STATUS . STOPPING_STARTED && ! gcRunning && ! isUpdating
293
314
menu . getMenuItemById ( 'ipfsIsNotRunning' ) . visible = status === STATUS . STOPPING_FINISHED && ! gcRunning && ! isUpdating
294
315
menu . getMenuItemById ( 'ipfsHasErrored' ) . visible = errored && ! gcRunning && ! isUpdating
316
+ menu . getMenuItemById ( 'peerCount' ) . visible = status === STATUS . STARTING_FINISHED
295
317
menu . getMenuItemById ( 'runningWithGC' ) . visible = gcRunning
296
318
menu . getMenuItemById ( 'runningWhileCheckingForUpdate' ) . visible = isUpdating
297
319
@@ -365,10 +387,25 @@ module.exports = function (ctx) {
365
387
updateMenu ( )
366
388
} )
367
389
390
+ ipcMain . on ( 'peersPolled' , peerCount => {
391
+ // When a new peer count is retrieved, rebuild the menu and update
392
+ // the tray tooltip with the new number if necessary.
393
+ if ( peerCount !== state . peerCount ) {
394
+ state . peerCount = peerCount
395
+ menu = buildMenu ( ctx , state . peerCount )
396
+ menu . on ( 'menu-will-show' , ( ) => { ipcMain . emit ( 'menubar-will-open' ) } )
397
+ menu . on ( 'menu-will-close' , ( ) => { ipcMain . emit ( 'menubar-will-close' ) } )
398
+ tray . setContextMenu ( menu )
399
+ tray . setToolTip ( state . peerCount . toString ( ) + ' ' + i18n . t ( 'peerCount' ) )
400
+ updateMenu ( )
401
+ }
402
+ } )
403
+
368
404
ipcMain . on ( 'configUpdated' , ( ) => { updateMenu ( ) } )
369
405
ipcMain . on ( 'languageUpdated' , ( ) => { setupMenu ( ) } )
370
406
371
407
setupMenu ( )
408
+ setInterval ( pollPeers , 60000 )
372
409
373
410
ctx . tray = tray
374
411
logger . info ( '[tray] started' )
0 commit comments