move more state into state.view

This commit is contained in:
Feross Aboukhadijeh
2016-03-03 17:19:31 -08:00
parent db29ba7535
commit d72999df57
4 changed files with 24 additions and 34 deletions

View File

@@ -30,23 +30,22 @@ global.WEBTORRENT_ANNOUNCE = createTorrent.announceList
var state = global.state = { var state = global.state = {
server: null, server: null,
player: null,
currentPage: {
type: 'list'
},
view: { view: {
title: 'WebTorrent', url: '/',
dock: { dock: {
badge: 0, badge: 0,
progress: 0 progress: 0
}, },
devices: {
airplay: null,
chromecast: null
},
client: null, // TODO: remove this
// history: [],
// historyIndex: 0,
isFocused: true, isFocused: true,
client: null, // TODO: remove this from the view mainWindowBounds: null,
savedWindowBounds: null, title: 'WebTorrent'
history: [],
historyIndex: 0,
chromecast: null,
airplay: null
} }
} }
@@ -72,7 +71,7 @@ function init () {
}) })
airplay.createBrowser().on('deviceOn', function (player) { airplay.createBrowser().on('deviceOn', function (player) {
state.view.airplay = player state.view.devices.airplay = player
}).start() }).start()
document.addEventListener('paste', function () { document.addEventListener('paste', function () {
@@ -130,9 +129,6 @@ function dispatch (action, ...args) {
if (action === 'openPlayer') { if (action === 'openPlayer') {
openPlayer(args[0] /* torrent */) openPlayer(args[0] /* torrent */)
} }
// if (action === 'closePlayer') {
// closePlayer()
// }
if (action === 'openChromecast') { if (action === 'openChromecast') {
openChromecast(args[0] /* torrent */) openChromecast(args[0] /* torrent */)
} }
@@ -143,10 +139,12 @@ function dispatch (action, ...args) {
setDimensions(args[0] /* dimensions */) setDimensions(args[0] /* dimensions */)
} }
if (action === 'back') { if (action === 'back') {
if (state.player === 'local') { if (state.view.url === '/player') {
restoreBounds() restoreBounds()
closePlayer() closeServer()
} }
state.view.url = '/'
update()
} }
} }
@@ -240,17 +238,11 @@ function closeServer () {
function openPlayer (torrent) { function openPlayer (torrent) {
startServer(torrent, function () { startServer(torrent, function () {
state.player = 'local' state.view.url = '/player'
update() update()
}) })
} }
function closePlayer () {
closeServer()
state.player = null
update()
}
function openChromecast (torrent) { function openChromecast (torrent) {
startServer(torrent, function () { startServer(torrent, function () {
state.view.chromecast.play(state.server.networkURL, { title: 'WebTorrent — ' + torrent.name }) state.view.chromecast.play(state.server.networkURL, { title: 'WebTorrent — ' + torrent.name })
@@ -258,22 +250,20 @@ function openChromecast (torrent) {
err.message = 'Chromecast: ' + err.message err.message = 'Chromecast: ' + err.message
onError(err) onError(err)
}) })
state.player = 'chromecast'
update() update()
}) })
} }
function openAirplay (torrent) { function openAirplay (torrent) {
startServer(torrent, function () { 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 // TODO: handle airplay errors
state.player = 'airplay'
update() update()
}) })
} }
function setDimensions (dimensions) { function setDimensions (dimensions) {
state.view.savedWindowBounds = electron.remote.getCurrentWindow().getBounds() state.view.mainWindowBounds = electron.remote.getCurrentWindow().getBounds()
// Limit window size to screen size // Limit window size to screen size
var workAreaSize = electron.remote.screen.getPrimaryDisplay().workAreaSize var workAreaSize = electron.remote.screen.getPrimaryDisplay().workAreaSize
@@ -299,7 +289,7 @@ function setDimensions (dimensions) {
function restoreBounds () { function restoreBounds () {
electron.ipcRenderer.send('setAspectRatio', 0) electron.ipcRenderer.send('setAspectRatio', 0)
electron.ipcRenderer.send('setBounds', state.view.savedWindowBounds, true) electron.ipcRenderer.send('setBounds', state.view.mainWindowBounds, true)
} }
function onError (err) { function onError (err) {

View File

@@ -11,10 +11,10 @@ function App (state, dispatch) {
Header(state, dispatch), Header(state, dispatch),
h('.content', [ h('.content', [
(function () { (function () {
if (state.player === 'local') { if (state.view.url === '/') {
return Player(state, dispatch)
} else {
return TorrentList(state, dispatch) return TorrentList(state, dispatch)
} else if (state.view.url === '/player') {
return Player(state, dispatch)
} }
})() })()
]) ])

View File

@@ -14,7 +14,7 @@ function Header (state, dispatch) {
}, 'chevron_right') }, 'chevron_right')
]), ]),
(function () { (function () {
if (state.player !== 'local') { if (state.url !== '/player') {
return h('.nav.right', [ return h('.nav.right', [
h('i.icon.add', { h('i.icon.add', {
onclick: onAddTorrent onclick: onAddTorrent

View File

@@ -42,7 +42,7 @@ function TorrentList (state, dispatch) {
} }
})(), })(),
(function () { (function () {
if (state.view.airplay) { if (state.view.devices.airplay) {
return h('i.btn.icon.airplay', { return h('i.btn.icon.airplay', {
className: !torrent.ready ? 'disabled' : '', className: !torrent.ready ? 'disabled' : '',
onclick: openAirplay onclick: openAirplay