diff --git a/index.js b/index.js index 34dae9ad..e530f2ad 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ app.on('open-url', onOpen) function onOpen (e, torrentId) { e.preventDefault() - mainWindow.send('action', 'addTorrent', torrentId) + mainWindow.send('addTorrent', torrentId) } // report crashes @@ -51,6 +51,18 @@ app.on('before-quit', function () { 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 () { var win = new electron.BrowserWindow({ backgroundColor: '#282828', @@ -87,10 +99,22 @@ function addTorrentFromPaste () { torrentIds.forEach(function (torrentId) { torrentId = torrentId.trim() 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) { 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 = [ { label: 'File', @@ -128,7 +145,7 @@ var template = [ properties: [ 'openFile', 'openDirectory', 'multiSelections' ] }, function (filenames) { if (!Array.isArray(filenames)) return - mainWindow.send('action', 'seed', filenames) + mainWindow.send('seed', filenames) }) } }, @@ -142,7 +159,7 @@ var template = [ }, function (filenames) { if (!Array.isArray(filenames)) return filenames.forEach(function (filename) { - mainWindow.send('action', 'addTorrent', filename) + mainWindow.send('addTorrent', filename) }) }) } diff --git a/main/index.js b/main/index.js index e22224d6..022e35b5 100644 --- a/main/index.js +++ b/main/index.js @@ -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( // 'Peers: ' + torrent.swarm.wires.length + ' ' + diff --git a/main/views/header.js b/main/views/header.js index 5cd7015e..5f35ec6d 100644 --- a/main/views/header.js +++ b/main/views/header.js @@ -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) diff --git a/main/views/player.js b/main/views/player.js index c5485c77..98b68d24 100644 --- a/main/views/player.js +++ b/main/views/player.js @@ -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) } } diff --git a/main/views/torrent-list.js b/main/views/torrent-list.js index 21d053be..84ab2145 100644 --- a/main/views/torrent-list.js +++ b/main/views/torrent-list.js @@ -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