From 189e4bdc244f93c1597ff874d1f14aa9e2f97df8 Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 18 May 2016 23:59:30 -0700 Subject: [PATCH 1/2] Always handle when the user opens a torrent Fixes #523 --- renderer/index.js | 59 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/renderer/index.js b/renderer/index.js index db603af6..dd35f0ef 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -87,7 +87,7 @@ function init () { // OS integrations: // ...drag and drop a torrent or video file to play or seed - dragDrop('body', (files) => dispatch('onOpen', files)) + dragDrop('body', onDrag) // ...same thing if you paste a torrent document.addEventListener('paste', onPaste) @@ -252,16 +252,7 @@ function dispatch (action, ...args) { setDimensions(args[0] /* dimensions */) } if (action === 'backToList') { - // Exit any modals and screens with a back button - state.modal = null - while (state.location.hasBack()) state.location.back() - - // Work around virtual-dom issue: it doesn't expose its redraw function, - // and only redraws on requestAnimationFrame(). That means when the user - // closes the window (hide window / minimize to tray) and we want to pause - // the video, we update the vdom but it keeps playing until you reopen! - var mediaTag = document.querySelector('video,audio') - if (mediaTag) mediaTag.pause() + backToList() } if (action === 'escapeBack') { if (state.modal) { @@ -436,6 +427,19 @@ function openSubtitles () { }) } +function backToList () { + // Exit any modals and screens with a back button + state.modal = null + while (state.location.hasBack()) state.location.back() + + // Work around virtual-dom issue: it doesn't expose its redraw function, + // and only redraws on requestAnimationFrame(). That means when the user + // closes the window (hide window / minimize to tray) and we want to pause + // the video, we update the vdom but it keeps playing until you reopen! + var mediaTag = document.querySelector('video,audio') + if (mediaTag) mediaTag.pause() +} + // Checks whether we are connected and already casting // Returns false if we not casting (state.playing.location === 'local') // or if we're trying to connect but haven't yet ('chromecast-pending', etc) @@ -546,18 +550,35 @@ function saveState () { update() } +// Called when the user clicks a magnet link or torrent, or uses the Open dialog function onOpen (files) { if (!Array.isArray(files)) files = [ files ] - // In the player, the only drag-drop function is adding subtitles - var isInPlayer = state.location.current().url === 'player' - if (isInPlayer) { - return addSubtitles(files.filter(isSubtitle), true) - } + // Return to the home screen + state.modal = null + backToList() - // Otherwise, you can only drag-drop onto the home screen + if (files.every(isTorrent)) { + // All .torrent files? Start downloading + files.forEach(addTorrent) + } else { + // Show the Create Torrent screen. Let's seed those files. + showCreateTorrent(files) + } +} + +// Called when the user drag-drops files onto the app +function onDrag (files) { + if (!Array.isArray(files)) files = [ files ] + + var isInPlayer = state.location.current().url === 'player' var isHome = state.location.current().url === 'home' && !state.modal - if (isHome) { + + if (isInPlayer) { + // In the player, the only drag-drop function is adding subtitles + addSubtitles(files.filter(isSubtitle), true) + } else if (isHome) { + // Otherwise, you can only drag-drop onto the home screen if (files.every(isTorrent)) { // All .torrent files? Start downloading files.forEach(addTorrent) @@ -566,6 +587,8 @@ function onOpen (files) { showCreateTorrent(files) } } + + update() } function isTorrent (file) { From 81d5a367da6f1335be2f4347c2aec1431da1e8fd Mon Sep 17 00:00:00 2001 From: DC Date: Thu, 19 May 2016 00:44:59 -0700 Subject: [PATCH 2/2] Add new torrents to top and scroll to top This means people who add a lot of torrents will always have their latest torrents at the top when they open the app, instead of having to scroll all the way down --- renderer/index.js | 13 ++++++++++--- renderer/views/app.js | 3 ++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/renderer/index.js b/renderer/index.js index dd35f0ef..601bc0a7 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -213,6 +213,7 @@ function dispatch (action, ...args) { onOpen(args[0] /* files */) } if (action === 'addTorrent') { + backToList() addTorrent(args[0] /* torrent */) } if (action === 'showOpenTorrentFile') { @@ -427,11 +428,16 @@ function openSubtitles () { }) } +// Quits any modal popovers and returns to the torrent list screen function backToList () { // Exit any modals and screens with a back button state.modal = null while (state.location.hasBack()) state.location.back() + // If we were already on the torrent list, scroll to the top + var contentTag = document.querySelector('.content') + if (contentTag) contentTag.scrollTop = 0 + // Work around virtual-dom issue: it doesn't expose its redraw function, // and only redraws on requestAnimationFrame(). That means when the user // closes the window (hide window / minimize to tray) and we want to pause @@ -555,7 +561,6 @@ function onOpen (files) { if (!Array.isArray(files)) files = [ files ] // Return to the home screen - state.modal = null backToList() if (files.every(isTorrent)) { @@ -814,7 +819,7 @@ function torrentInfoHash (torrentKey, infoHash) { torrentKey: torrentKey, status: 'new' } - state.saved.torrents.push(torrentSummary) + state.saved.torrents.unshift(torrentSummary) sound.play('ADD') } @@ -1266,8 +1271,10 @@ function onPaste (e) { torrentIds.forEach(function (torrentId) { torrentId = torrentId.trim() if (torrentId.length === 0) return - dispatch('addTorrent', torrentId) + addTorrent(torrentId) }) + + update() } function onFocus (e) { diff --git a/renderer/views/app.js b/renderer/views/app.js index 06ac70da..a834c15b 100644 --- a/renderer/views/app.js +++ b/renderer/views/app.js @@ -54,12 +54,13 @@ function App (state) { function getErrorPopover (state) { var now = new Date().getTime() var recentErrors = state.errors.filter((x) => now - x.time < 5000) + var hasErrors = recentErrors.length > 0 var errorElems = recentErrors.map(function (error) { return hx`
${error.message}
` }) return hx` -
+
Error
${errorElems}