update player window title to torrent name (Windows, Linux)

This also moves all the state “diffing” for purposes of updating the
app’s window via Electron APIs into one function updateElectron().
This commit is contained in:
Feross Aboukhadijeh
2016-03-07 19:36:01 -08:00
parent 5171e1a7da
commit 8ab2a1415b
3 changed files with 28 additions and 9 deletions

View File

@@ -32,6 +32,10 @@ function init () {
windows.main.setFullScreen(!windows.main.isFullScreen())
})
ipcMain.on('setTitle', function (e, title) {
windows.main.setTitle(title)
})
ipcMain.on('log', function (e, message) {
console.log(message)
})

View File

@@ -68,7 +68,10 @@ function init () {
// Calling update() updates the UI given the current state
// Do this at least once a second to show latest state for each torrent
// (eg % downloaded) and to keep the cursor in sync when playing a video
setInterval(update, 1000)
setInterval(function () {
update()
updateClientProgress()
}, 1000)
// All state lives in state.js. `state.saved` is read from and written to a
// file. All other state is ephemeral. Here we'll load state.saved:
@@ -105,7 +108,6 @@ function init () {
// ...focus and blur. Needed to show correct dock icon text ("badge") in OSX
window.addEventListener('focus', function () {
state.isFocused = true
if (state.dock.badge > 0) ipcRenderer.send('setBadge', '')
state.dock.badge = 0
})
@@ -127,7 +129,21 @@ function render (state) {
// Calls render() to go from state -> UI, then applies to vdom to the real DOM.
function update () {
vdomLoop.update(state)
updateDockIcon()
updateElectron()
}
function updateElectron () {
if (state.title !== state.prev.title) {
state.prev.title = state.title
ipcRenderer.send('setTitle', state.title)
}
if (state.dock.progress !== state.prev.progress) {
state.prev.progress = state.dock.progress
ipcRenderer.send('setProgress', state.dock.progress)
}
if (state.dock.badge !== state.prev.badge) {
ipcRenderer.send('setBadge', state.dock.badge || '')
}
}
// Events from the UI never modify state directly. Instead they call dispatch()
@@ -247,7 +263,7 @@ function saveState () {
})
}
function updateDockIcon () {
function updateClientProgress () {
var progress = state.client.progress
var activeTorrentsExist = state.client.torrents.some(function (torrent) {
return torrent.progress !== 1
@@ -256,10 +272,7 @@ function updateDockIcon () {
if (!activeTorrentsExist || progress === 1) {
progress = -1
}
if (progress !== state.dock.progress) {
state.dock.progress = progress
ipcRenderer.send('setProgress', progress)
}
state.dock.progress = progress
}
function onFiles (files) {
@@ -367,7 +380,6 @@ function addTorrentEvents (torrent) {
if (!state.isFocused) {
state.dock.badge += 1
ipcRenderer.send('setBadge', state.dock.badge)
}
update()
@@ -433,6 +445,8 @@ function openPlayer (infoHash) {
function closePlayer () {
state.url = '/'
state.title = config.APP_NAME
update()
if (state.isFullScreen) {
ipcRenderer.send('toggleFullScreen')
}

View File

@@ -31,6 +31,7 @@ module.exports = {
duration: 1, /* seconds */
mouseStationarySince: 0 /* Unix time in ms */
},
prev: {}, /* used for state diffing in updateElectron() */
/* Saved state is read from and written to a file every time the app runs.
* It should be simple and minimal and must be JSON.