automatically size player on start

This commit is contained in:
Feross Aboukhadijeh
2016-03-03 01:05:00 -08:00
parent 128cb679aa
commit 4d094c8209
5 changed files with 117 additions and 39 deletions

View File

@@ -18,6 +18,8 @@ var createElement = require('virtual-dom/create-element')
var diff = require('virtual-dom/diff')
var patch = require('virtual-dom/patch')
var HEADER_HEIGHT = 38
var App = require('./views/app')
global.WEBTORRENT_ANNOUNCE = createTorrent.announceList
@@ -29,12 +31,20 @@ global.WEBTORRENT_ANNOUNCE = createTorrent.announceList
})
var state = global.state = {
title: 'WebTorrent',
torrents: [],
server: null,
player: null,
chromcast: null,
airplay: null
currentPage: {
type: 'list'
},
view: {
title: 'WebTorrent',
savedWindowBounds: null,
history: [],
historyIndex: 0,
chromecast: null,
airplay: null
}
}
var currentVDom, rootElement, getClient, updateThrottled
@@ -63,16 +73,16 @@ function init () {
dragDrop('body', onFiles)
chromecasts.on('update', function (player) {
state.chromecast = player
state.view.chromecast = player
update()
})
airplay.createBrowser().on('deviceOn', function (player) {
state.airplay = player
state.view.airplay = player
}).start()
document.addEventListener('paste', function () {
electron.ipcRenderer.send('action', 'addTorrentFromPaste')
electron.ipcRenderer.send('addTorrentFromPaste')
})
}
init()
@@ -95,19 +105,32 @@ function dispatch (action, ...args) {
if (action === 'openPlayer') {
openPlayer(args[0] /* torrent */)
}
if (action === 'closePlayer') {
closePlayer()
}
// if (action === 'closePlayer') {
// closePlayer()
// }
if (action === 'openChromecast') {
openChromecast(args[0] /* torrent */)
}
if (action === 'openAirplay') {
openAirplay(args[0] /* torrent */)
}
if (action === 'setDimensions') {
setDimensions(args[0] /* dimensions */)
}
if (action === 'back') {
if (state.player === 'local') {
restoreBounds()
closePlayer()
}
}
}
electron.ipcRenderer.on('action', function (e, action, ...args) {
dispatch(action, ...args)
electron.ipcRenderer.on('addTorrent', function (e, torrentId) {
addTorrent(torrentId)
})
electron.ipcRenderer.on('seed', function (e, files) {
seed(files)
})
function onFiles (files) {
@@ -224,9 +247,8 @@ function closePlayer () {
function openChromecast (torrent) {
startServer(torrent, function () {
console.log(state.server.networkURL)
state.chromecast.play(state.server.networkURL, { title: 'WebTorrent — ' + torrent.name })
state.chromecast.on('error', function (err) {
state.view.chromecast.play(state.server.networkURL, { title: 'WebTorrent — ' + torrent.name })
state.view.chromecast.on('error', function (err) {
err.message = 'Chromecast: ' + err.message
onError(err)
})
@@ -237,16 +259,40 @@ function openChromecast (torrent) {
function openAirplay (torrent) {
startServer(torrent, function () {
state.airplay.play(state.server.networkURL, 0, function () {})
state.view.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()
// Limit window size to screen size
var workAreaSize = electron.remote.screen.getPrimaryDisplay().workAreaSize
var width = Math.min(dimensions.width, workAreaSize.width)
var height = Math.min(dimensions.height, workAreaSize.height)
var aspectRatio = width / height
// add header height
height += HEADER_HEIGHT
// Center window on screen
var x = Math.floor((workAreaSize.width - width) / 2)
var y = Math.floor((workAreaSize.height - height) / 2)
electron.ipcRenderer.send('setAspectRatio', aspectRatio, { width: 0, height: HEADER_HEIGHT })
electron.ipcRenderer.send('setBounds', { x, y, width, height })
}
function restoreBounds () {
electron.ipcRenderer.send('setAspectRatio', 0)
electron.ipcRenderer.send('setBounds', state.view.savedWindowBounds, true)
}
// function onTorrent (torrent) {
// function updateSpeed () {
// ipc.send('')
// var progress = (100 * torrent.progress).toFixed(1)
// util.updateSpeed(
// '<b>Peers:</b> ' + torrent.swarm.wires.length + ' ' +

View File

@@ -4,10 +4,14 @@ var h = require('virtual-dom/h')
function Header (state, dispatch) {
return h('.header', [
h('.title', state.title),
h('.title', state.view.title),
h('.nav.left', [
h('i.icon.back.disabled', 'chevron_left'),
h('i.icon.forward', 'chevron_right')
h('i.icon.back', {
onclick: onBack
}, 'chevron_left'),
h('i.icon.forward', {
onclick: onForward
}, 'chevron_right')
]),
(function () {
if (state.player !== 'local') {
@@ -20,6 +24,14 @@ function Header (state, dispatch) {
})()
])
function onBack (e) {
dispatch('back')
}
function onForward (e) {
dispatch('forward')
}
function onAddTorrent (e) {
var torrentId = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d&dn=sintel.mp4'
dispatch('addTorrent', torrentId)

View File

@@ -7,15 +7,18 @@ function Player (state, dispatch) {
h('video', {
src: state.server.localURL,
autoplay: true,
controls: true
}),
h('a.close', {
onclick: closePlayer
}, 'Close')
controls: true,
onplaying: onPlaying
})
])
function closePlayer () {
dispatch('closePlayer')
function onPlaying (e) {
var video = e.target
var dimensions = {
width: video.videoWidth,
height: video.videoHeight
}
dispatch('setDimensions', dimensions)
}
}

View File

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