Skip to content

Commit 6915417

Browse files
sutartmelsonlidel
authored andcommitted
feat: Show discovered peer count
1 parent 1d68c58 commit 6915417

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

assets/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"ipfsIsStarting": "IPFS is Starting",
55
"ipfsIsNotRunning": "IPFS is Not Running",
66
"ipfsHasErrored": "IPFS has Errored",
7+
"peerCount": "Peers",
78
"runningWithGC": "Running (GC in progress)",
89
"runningWhileCheckingForUpdate": "Running (Checking for Updates)",
910
"start": "Start",

src/tray.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
56+
function buildMenu (ctx, peerCount) {
5757
return Menu.buildFromTemplate([
5858
...[
5959
['ipfsIsStarting', 'yellow'],
@@ -70,6 +70,11 @@ function buildMenu (ctx) {
7070
enabled: false,
7171
icon: path.resolve(path.join(__dirname, `../assets/icons/status/${color}.png`))
7272
})),
73+
{
74+
id: 'peerCount',
75+
label: peerCount.toString() + ' ' + i18n.t('peerCount'),
76+
enabled: false
77+
},
7378
{
7479
id: 'restartIpfs',
7580
label: i18n.t('restart'),
@@ -260,7 +265,8 @@ module.exports = function (ctx) {
260265
const state = {
261266
status: null,
262267
gcRunning: false,
263-
isUpdating: false
268+
isUpdating: false,
269+
peerCount: 0
264270
}
265271

266272
// macOS tray drop files
@@ -284,15 +290,30 @@ module.exports = function (ctx) {
284290
tray.on('right-click', popupMenu)
285291
tray.on('double-click', () => ctx.launchWebUI('/'))
286292

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+
287309
const setupMenu = () => {
288-
menu = buildMenu(ctx)
310+
menu = buildMenu(ctx, state.peerCount)
289311

290312
tray.setContextMenu(menu)
291-
tray.setToolTip('IPFS Desktop')
313+
tray.setToolTip(state.peerCount.toString() + ' ' + i18n.t('peerCount'))
292314

293315
menu.on('menu-will-show', () => { ipcMain.emit('menubar-will-open') })
294316
menu.on('menu-will-close', () => { ipcMain.emit('menubar-will-close') })
295-
296317
updateMenu()
297318
}
298319

@@ -305,6 +326,7 @@ module.exports = function (ctx) {
305326
menu.getMenuItemById('ipfsIsStopping').visible = status === STATUS.STOPPING_STARTED && !gcRunning && !isUpdating
306327
menu.getMenuItemById('ipfsIsNotRunning').visible = status === STATUS.STOPPING_FINISHED && !gcRunning && !isUpdating
307328
menu.getMenuItemById('ipfsHasErrored').visible = errored && !gcRunning && !isUpdating
329+
menu.getMenuItemById('peerCount').visible = status === STATUS.STARTING_FINISHED
308330
menu.getMenuItemById('runningWithGC').visible = gcRunning
309331
menu.getMenuItemById('runningWhileCheckingForUpdate').visible = isUpdating
310332

@@ -380,10 +402,25 @@ module.exports = function (ctx) {
380402
updateMenu()
381403
})
382404

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+
383419
ipcMain.on('configUpdated', () => { updateMenu() })
384420
ipcMain.on('languageUpdated', () => { setupMenu() })
385421

386422
setupMenu()
423+
setInterval(pollPeers, 60000)
387424

388425
ctx.tray = tray
389426
logger.info('[tray] started')

0 commit comments

Comments
 (0)