From d72999df573f0217ea20187b2561d5e5dac685c5 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 3 Mar 2016 17:19:31 -0800 Subject: [PATCH] move more state into state.view --- main/index.js | 48 +++++++++++++++----------------------- main/views/app.js | 6 ++--- main/views/header.js | 2 +- main/views/torrent-list.js | 2 +- 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/main/index.js b/main/index.js index 45a0f579..48cdcf20 100644 --- a/main/index.js +++ b/main/index.js @@ -30,23 +30,22 @@ global.WEBTORRENT_ANNOUNCE = createTorrent.announceList var state = global.state = { server: null, - player: null, - currentPage: { - type: 'list' - }, view: { - title: 'WebTorrent', + url: '/', dock: { badge: 0, progress: 0 }, + devices: { + airplay: null, + chromecast: null + }, + client: null, // TODO: remove this + // history: [], + // historyIndex: 0, isFocused: true, - client: null, // TODO: remove this from the view - savedWindowBounds: null, - history: [], - historyIndex: 0, - chromecast: null, - airplay: null + mainWindowBounds: null, + title: 'WebTorrent' } } @@ -72,7 +71,7 @@ function init () { }) airplay.createBrowser().on('deviceOn', function (player) { - state.view.airplay = player + state.view.devices.airplay = player }).start() document.addEventListener('paste', function () { @@ -130,9 +129,6 @@ function dispatch (action, ...args) { if (action === 'openPlayer') { openPlayer(args[0] /* torrent */) } - // if (action === 'closePlayer') { - // closePlayer() - // } if (action === 'openChromecast') { openChromecast(args[0] /* torrent */) } @@ -143,10 +139,12 @@ function dispatch (action, ...args) { setDimensions(args[0] /* dimensions */) } if (action === 'back') { - if (state.player === 'local') { + if (state.view.url === '/player') { restoreBounds() - closePlayer() + closeServer() } + state.view.url = '/' + update() } } @@ -240,17 +238,11 @@ function closeServer () { function openPlayer (torrent) { startServer(torrent, function () { - state.player = 'local' + state.view.url = '/player' update() }) } -function closePlayer () { - closeServer() - state.player = null - update() -} - function openChromecast (torrent) { startServer(torrent, function () { state.view.chromecast.play(state.server.networkURL, { title: 'WebTorrent — ' + torrent.name }) @@ -258,22 +250,20 @@ function openChromecast (torrent) { err.message = 'Chromecast: ' + err.message onError(err) }) - state.player = 'chromecast' update() }) } function openAirplay (torrent) { startServer(torrent, function () { - state.view.airplay.play(state.server.networkURL, 0, function () {}) + state.view.devices.airplay.play(state.server.networkURL, 0, function () {}) // TODO: handle airplay errors - state.player = 'airplay' update() }) } function setDimensions (dimensions) { - state.view.savedWindowBounds = electron.remote.getCurrentWindow().getBounds() + state.view.mainWindowBounds = electron.remote.getCurrentWindow().getBounds() // Limit window size to screen size var workAreaSize = electron.remote.screen.getPrimaryDisplay().workAreaSize @@ -299,7 +289,7 @@ function setDimensions (dimensions) { function restoreBounds () { electron.ipcRenderer.send('setAspectRatio', 0) - electron.ipcRenderer.send('setBounds', state.view.savedWindowBounds, true) + electron.ipcRenderer.send('setBounds', state.view.mainWindowBounds, true) } function onError (err) { diff --git a/main/views/app.js b/main/views/app.js index faec6610..3659c5b5 100644 --- a/main/views/app.js +++ b/main/views/app.js @@ -11,10 +11,10 @@ function App (state, dispatch) { Header(state, dispatch), h('.content', [ (function () { - if (state.player === 'local') { - return Player(state, dispatch) - } else { + if (state.view.url === '/') { return TorrentList(state, dispatch) + } else if (state.view.url === '/player') { + return Player(state, dispatch) } })() ]) diff --git a/main/views/header.js b/main/views/header.js index 5f35ec6d..f3b58288 100644 --- a/main/views/header.js +++ b/main/views/header.js @@ -14,7 +14,7 @@ function Header (state, dispatch) { }, 'chevron_right') ]), (function () { - if (state.player !== 'local') { + if (state.url !== '/player') { return h('.nav.right', [ h('i.icon.add', { onclick: onAddTorrent diff --git a/main/views/torrent-list.js b/main/views/torrent-list.js index 4b59db6e..ee215281 100644 --- a/main/views/torrent-list.js +++ b/main/views/torrent-list.js @@ -42,7 +42,7 @@ function TorrentList (state, dispatch) { } })(), (function () { - if (state.view.airplay) { + if (state.view.devices.airplay) { return h('i.btn.icon.airplay', { className: !torrent.ready ? 'disabled' : '', onclick: openAirplay