Skip to content

Commit

Permalink
feat: Show discovered peer count
Browse files Browse the repository at this point in the history
  • Loading branch information
sutartmelson committed Feb 14, 2021
1 parent 36ff4e4 commit 12703c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 38 additions & 5 deletions src/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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'),
Expand Down Expand Up @@ -253,7 +258,8 @@ module.exports = function (ctx) {
const state = {
status: null,
gcRunning: false,
isUpdating: false
isUpdating: false,
peerCount: 0
}

// macOS tray drop files
Expand All @@ -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()
}

Expand All @@ -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

Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 12703c5

Please sign in to comment.