From 12703c5a939b85ea1b99e93749214cc9467def78 Mon Sep 17 00:00:00 2001 From: Stuart Nelson Date: Sun, 14 Feb 2021 15:06:25 -0700 Subject: [PATCH] feat: Show discovered peer count --- assets/locales/en.json | 1 + src/tray.js | 43 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/assets/locales/en.json b/assets/locales/en.json index 823baa229..3c4c25deb 100644 --- a/assets/locales/en.json +++ b/assets/locales/en.json @@ -4,6 +4,7 @@ "ipfsIsStarting": "IPFS is Starting", "ipfsIsNotRunning": "IPFS is Not Running", "ipfsHasErrored": "IPFS has Errored", + "peerCount": "Peers", "runningWithGC": "Running (GC in progress)", "runningWhileCheckingForUpdate": "Running (Checking for Updates)", "start": "Start", diff --git a/src/tray.js b/src/tray.js index 749140fe6..06656f8e4 100644 --- a/src/tray.js +++ b/src/tray.js @@ -46,7 +46,7 @@ function buildCheckbox (key, label) { // they natively work as soon as the menu opens. They don't work like that on Windows // or other OSes and must be registered globally. They still collide with global // accelerator. Please see ../utils/setup-global-shortcut.js for more info. -function buildMenu (ctx) { +function buildMenu (ctx, peerCount) { return Menu.buildFromTemplate([ ...[ ['ipfsIsStarting', 'yellow'], @@ -63,6 +63,11 @@ function buildMenu (ctx) { enabled: false, icon: path.resolve(path.join(__dirname, `../assets/icons/status/${color}.png`)) })), + { + id: 'peerCount', + label: peerCount.toString() + ' ' + i18n.t('peerCount'), + enabled: false + }, { id: 'restartIpfs', label: i18n.t('restart'), @@ -253,7 +258,8 @@ module.exports = function (ctx) { const state = { status: null, gcRunning: false, - isUpdating: false + isUpdating: false, + peerCount: 0 } // macOS tray drop files @@ -271,15 +277,30 @@ module.exports = function (ctx) { }) } + const pollPeers = () => { + // If the daemon is running, send a request to retrieve the number + // of connected peers. Emit 'peersPolled' event upon retrieval. + if (state.status === STATUS.STARTING_FINISHED && ctx.getIpfsd) { + ctx.getIpfsd().then((daemon) => { + daemon.api.swarm.peers().then((value) => { + if (value.length) { + ipcMain.emit('peersPolled', value.length) + } + }) + }) + } else { + ipcMain.emit('peersPolled', 0) + } + } + const setupMenu = () => { - menu = buildMenu(ctx) + menu = buildMenu(ctx, state.peerCount) tray.setContextMenu(menu) - tray.setToolTip('IPFS Desktop') + tray.setToolTip(state.peerCount.toString() + ' ' + i18n.t('peerCount')) menu.on('menu-will-show', () => { ipcMain.emit('menubar-will-open') }) menu.on('menu-will-close', () => { ipcMain.emit('menubar-will-close') }) - updateMenu() } @@ -292,6 +313,7 @@ module.exports = function (ctx) { menu.getMenuItemById('ipfsIsStopping').visible = status === STATUS.STOPPING_STARTED && !gcRunning && !isUpdating menu.getMenuItemById('ipfsIsNotRunning').visible = status === STATUS.STOPPING_FINISHED && !gcRunning && !isUpdating menu.getMenuItemById('ipfsHasErrored').visible = errored && !gcRunning && !isUpdating + menu.getMenuItemById('peerCount').visible = status === STATUS.STARTING_FINISHED menu.getMenuItemById('runningWithGC').visible = gcRunning menu.getMenuItemById('runningWhileCheckingForUpdate').visible = isUpdating @@ -365,10 +387,21 @@ module.exports = function (ctx) { updateMenu() }) + ipcMain.on('peersPolled', peerCount => { + state.peerCount = peerCount + menu = buildMenu(ctx, state.peerCount) + menu.on('menu-will-show', () => { ipcMain.emit('menubar-will-open') }) + menu.on('menu-will-close', () => { ipcMain.emit('menubar-will-close') }) + tray.setContextMenu(menu) + tray.setToolTip(state.peerCount.toString() + ' ' + i18n.t('peerCount')) + updateMenu() + }) + ipcMain.on('configUpdated', () => { updateMenu() }) ipcMain.on('languageUpdated', () => { setupMenu() }) setupMenu() + setInterval(pollPeers, 60000) ctx.tray = tray logger.info('[tray] started')