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

@@ -13,7 +13,7 @@ app.on('open-url', onOpen)
function onOpen (e, torrentId) { function onOpen (e, torrentId) {
e.preventDefault() e.preventDefault()
mainWindow.send('action', 'addTorrent', torrentId) mainWindow.send('addTorrent', torrentId)
} }
// report crashes // report crashes
@@ -51,6 +51,18 @@ app.on('before-quit', function () {
isQuitting = true isQuitting = true
}) })
electron.ipcMain.on('addTorrentFromPaste', function (e) {
addTorrentFromPaste()
})
electron.ipcMain.on('setBounds', function (e, bounds) {
setBounds(bounds)
})
electron.ipcMain.on('setAspectRatio', function (e, aspectRatio, extraSize) {
setAspectRatio(aspectRatio, extraSize)
})
function createMainWindow () { function createMainWindow () {
var win = new electron.BrowserWindow({ var win = new electron.BrowserWindow({
backgroundColor: '#282828', backgroundColor: '#282828',
@@ -87,10 +99,22 @@ function addTorrentFromPaste () {
torrentIds.forEach(function (torrentId) { torrentIds.forEach(function (torrentId) {
torrentId = torrentId.trim() torrentId = torrentId.trim()
if (torrentId.length === 0) return if (torrentId.length === 0) return
mainWindow.send('action', 'addTorrent', torrentId) mainWindow.send('addTorrent', torrentId)
}) })
} }
function setBounds (bounds) {
if (mainWindow) {
mainWindow.setBounds(bounds, true)
}
}
function setAspectRatio (aspectRatio, extraSize) {
if (mainWindow) {
mainWindow.setAspectRatio(aspectRatio, extraSize)
}
}
function toggleDevTools (win) { function toggleDevTools (win) {
win = win || electron.BrowserWindow.getFocusedWindow() win = win || electron.BrowserWindow.getFocusedWindow()
@@ -108,13 +132,6 @@ function reloadWindow (win) {
} }
} }
electron.ipcMain.on('action', function (event, action, ...args) {
debug('action %s', action)
if (action === 'addTorrentFromPaste') {
addTorrentFromPaste()
}
})
var template = [ var template = [
{ {
label: 'File', label: 'File',
@@ -128,7 +145,7 @@ var template = [
properties: [ 'openFile', 'openDirectory', 'multiSelections' ] properties: [ 'openFile', 'openDirectory', 'multiSelections' ]
}, function (filenames) { }, function (filenames) {
if (!Array.isArray(filenames)) return if (!Array.isArray(filenames)) return
mainWindow.send('action', 'seed', filenames) mainWindow.send('seed', filenames)
}) })
} }
}, },
@@ -142,7 +159,7 @@ var template = [
}, function (filenames) { }, function (filenames) {
if (!Array.isArray(filenames)) return if (!Array.isArray(filenames)) return
filenames.forEach(function (filename) { filenames.forEach(function (filename) {
mainWindow.send('action', 'addTorrent', filename) mainWindow.send('addTorrent', filename)
}) })
}) })
} }

View File

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

View File

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

View File

@@ -7,15 +7,18 @@ function Player (state, dispatch) {
h('video', { h('video', {
src: state.server.localURL, src: state.server.localURL,
autoplay: true, autoplay: true,
controls: true controls: true,
}), onplaying: onPlaying
h('a.close', { })
onclick: closePlayer
}, 'Close')
]) ])
function closePlayer () { function onPlaying (e) {
dispatch('closePlayer') 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 onclick: openPlayer
}, 'play_arrow'), }, 'play_arrow'),
(function () { (function () {
if (state.chromecast) { if (state.view.chromecast) {
return h('i.btn.icon.chromecast', { return h('i.btn.icon.chromecast', {
className: !torrent.ready ? 'disabled' : '', className: !torrent.ready ? 'disabled' : '',
onclick: openChromecast onclick: openChromecast
@@ -35,7 +35,7 @@ function TorrentList (state, dispatch) {
} }
})(), })(),
(function () { (function () {
if (state.airplay) { if (state.view.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