From 8ab2a1415bb6bf83c0efb7f703d97cbdd716a8f5 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 7 Mar 2016 19:36:01 -0800 Subject: [PATCH] =?UTF-8?q?update=20player=20window=20title=C2=A0to=20torr?= =?UTF-8?q?ent=20name=20(Windows,=20Linux)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also moves all the state “diffing” for purposes of updating the app’s window via Electron APIs into one function updateElectron(). --- main/ipc.js | 4 ++++ renderer/index.js | 32 +++++++++++++++++++++++--------- renderer/state.js | 1 + 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/main/ipc.js b/main/ipc.js index 9fdaf938..ea476b06 100644 --- a/main/ipc.js +++ b/main/ipc.js @@ -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) }) diff --git a/renderer/index.js b/renderer/index.js index d2f45d43..6b240487 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -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') } diff --git a/renderer/state.js b/renderer/state.js index 4fb84a45..8fe8b7a2 100644 --- a/renderer/state.js +++ b/renderer/state.js @@ -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.