move dock state to renderer process
This commit is contained in:
10
index.js
10
index.js
@@ -35,10 +35,6 @@ app.on('activate', function () {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.on('browser-window-focus', function () {
|
|
||||||
setBadge('')
|
|
||||||
})
|
|
||||||
|
|
||||||
app.on('window-all-closed', function () {
|
app.on('window-all-closed', function () {
|
||||||
if (process.platform !== 'darwin') {
|
if (process.platform !== 'darwin') {
|
||||||
app.quit()
|
app.quit()
|
||||||
@@ -133,11 +129,7 @@ function setAspectRatio (aspectRatio, extraSize) {
|
|||||||
// Display string in dock badging area (OS X)
|
// Display string in dock badging area (OS X)
|
||||||
function setBadge (text) {
|
function setBadge (text) {
|
||||||
debug('setBadge %s', text)
|
debug('setBadge %s', text)
|
||||||
if (mainWindow && !mainWindow.isFocused()) {
|
if (mainWindow) {
|
||||||
if (text === '+') {
|
|
||||||
// special value to increment the badge number
|
|
||||||
text = (Number(app.dock.getBadge()) || 0) + 1
|
|
||||||
}
|
|
||||||
app.dock.setBadge(String(text))
|
app.dock.setBadge(String(text))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ var state = global.state = {
|
|||||||
},
|
},
|
||||||
view: {
|
view: {
|
||||||
title: 'WebTorrent',
|
title: 'WebTorrent',
|
||||||
|
dock: {
|
||||||
|
badge: 0,
|
||||||
|
progress: 0
|
||||||
|
},
|
||||||
|
isFocused: true,
|
||||||
client: null, // TODO: remove this from the view
|
client: null, // TODO: remove this from the view
|
||||||
savedWindowBounds: null,
|
savedWindowBounds: null,
|
||||||
history: [],
|
history: [],
|
||||||
@@ -48,17 +53,17 @@ var state = global.state = {
|
|||||||
var client, currentVDom, rootElement, updateThrottled
|
var client, currentVDom, rootElement, updateThrottled
|
||||||
|
|
||||||
function init () {
|
function init () {
|
||||||
|
client = global.client = new WebTorrent()
|
||||||
|
client.on('warning', onWarning)
|
||||||
|
client.on('error', onError)
|
||||||
|
state.view.client = client
|
||||||
|
|
||||||
currentVDom = App(state, dispatch)
|
currentVDom = App(state, dispatch)
|
||||||
rootElement = createElement(currentVDom)
|
rootElement = createElement(currentVDom)
|
||||||
document.body.appendChild(rootElement)
|
document.body.appendChild(rootElement)
|
||||||
|
|
||||||
updateThrottled = throttle(update, 1000)
|
updateThrottled = throttle(update, 1000)
|
||||||
|
|
||||||
client = new WebTorrent()
|
|
||||||
client.on('warning', onWarning)
|
|
||||||
client.on('error', onError)
|
|
||||||
state.view.client = client
|
|
||||||
|
|
||||||
dragDrop('body', onFiles)
|
dragDrop('body', onFiles)
|
||||||
|
|
||||||
chromecasts.on('update', function (player) {
|
chromecasts.on('update', function (player) {
|
||||||
@@ -73,6 +78,16 @@ function init () {
|
|||||||
document.addEventListener('paste', function () {
|
document.addEventListener('paste', function () {
|
||||||
electron.ipcRenderer.send('addTorrentFromPaste')
|
electron.ipcRenderer.send('addTorrentFromPaste')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
window.addEventListener('focus', function () {
|
||||||
|
state.view.isFocused = true
|
||||||
|
if (state.view.dock.badge > 0) electron.ipcRenderer.send('setBadge', '')
|
||||||
|
state.view.dock.badge = 0
|
||||||
|
})
|
||||||
|
|
||||||
|
window.addEventListener('blur', function () {
|
||||||
|
state.view.isFocused = false
|
||||||
|
})
|
||||||
}
|
}
|
||||||
init()
|
init()
|
||||||
|
|
||||||
@@ -90,15 +105,16 @@ setInterval(function () {
|
|||||||
}, 1000)
|
}, 1000)
|
||||||
|
|
||||||
function updateDockIcon () {
|
function updateDockIcon () {
|
||||||
if (state.view.client) {
|
var progress = state.view.client.progress
|
||||||
var progress = state.view.client.progress
|
var activeTorrentsExist = state.view.client.torrents.some(function (torrent) {
|
||||||
var activeTorrentsExist = state.view.client.torrents.some(function (torrent) {
|
return torrent.progress !== 1
|
||||||
return torrent.progress !== 1
|
})
|
||||||
})
|
// Hide progress bar when client has no torrents, or progress is 100%
|
||||||
// Hide progress bar when client has no torrents, or progress is 100%
|
if (!activeTorrentsExist || progress === 1) {
|
||||||
if (!activeTorrentsExist || progress === 1) {
|
progress = -1
|
||||||
progress = -1
|
}
|
||||||
}
|
if (progress !== state.view.dock.progress) {
|
||||||
|
state.view.dock.progress = progress
|
||||||
electron.ipcRenderer.send('setProgress', progress)
|
electron.ipcRenderer.send('setProgress', progress)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,7 +191,10 @@ function seed (files) {
|
|||||||
function addTorrentEvents (torrent) {
|
function addTorrentEvents (torrent) {
|
||||||
torrent.on('infoHash', update)
|
torrent.on('infoHash', update)
|
||||||
torrent.on('done', function () {
|
torrent.on('done', function () {
|
||||||
electron.ipcRenderer.send('setBadge', '+')
|
if (!state.view.isFocused) {
|
||||||
|
state.view.dock.badge += 1
|
||||||
|
electron.ipcRenderer.send('setBadge', state.view.dock.badge)
|
||||||
|
}
|
||||||
update()
|
update()
|
||||||
})
|
})
|
||||||
torrent.on('download', updateThrottled)
|
torrent.on('download', updateThrottled)
|
||||||
|
|||||||
Reference in New Issue
Block a user