From 2b18821416319a67e3d0f5252b6b69b81a651b78 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 3 Mar 2016 15:52:42 -0800 Subject: [PATCH] dock: inc badge when torrent completes --- index.js | 20 ++++++++++++++++++++ main/index.js | 8 +++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 180c95df..67371a47 100644 --- a/index.js +++ b/index.js @@ -35,6 +35,10 @@ app.on('activate', function () { } }) +app.on('browser-window-focus', function () { + setBadge('') +}) + app.on('window-all-closed', function () { if (process.platform !== 'darwin') { app.quit() @@ -58,6 +62,9 @@ electron.ipcMain.on('setAspectRatio', function (e, aspectRatio, extraSize) { setAspectRatio(aspectRatio, extraSize) }) +electron.ipcMain.on('setBadge', function (e, text) { + setBadge(text) +}) electron.ipcMain.on('setProgress', function (e, progress) { setProgress(progress) @@ -123,8 +130,21 @@ function setAspectRatio (aspectRatio, extraSize) { } } +// Display string in dock badging area (OS X) +function setBadge (text) { + debug('setBadge %s', text) + if (mainWindow && !mainWindow.isFocused()) { + if (text === '+') { + // special value to increment the badge number + text = (Number(app.dock.getBadge()) || 0) + 1 + } + app.dock.setBadge(String(text)) + } +} + // Show progress bar. Valid range is [0, 1]. Remove when < 0; indeterminate when > 1. function setProgress (progress) { + debug('setProgress %s', progress) if (mainWindow) { mainWindow.setProgressBar(progress) } diff --git a/main/index.js b/main/index.js index 1c65833b..c1432c96 100644 --- a/main/index.js +++ b/main/index.js @@ -54,10 +54,9 @@ function init () { updateThrottled = throttle(update, 1000) - var client = new WebTorrent() + client = new WebTorrent() client.on('warning', onWarning) client.on('error', onError) - state.view.client = client dragDrop('body', onFiles) @@ -175,7 +174,10 @@ function seed (files) { function addTorrentEvents (torrent) { torrent.on('infoHash', update) - torrent.on('done', update) + torrent.on('done', function () { + electron.ipcRenderer.send('setBadge', '+') + update() + }) torrent.on('download', updateThrottled) torrent.on('upload', updateThrottled) torrent.on('ready', function () {