diff --git a/main/menu.js b/main/menu.js index 1e52e2ff..ba71f922 100644 --- a/main/menu.js +++ b/main/menu.js @@ -117,12 +117,14 @@ function getMenuItem (label) { // Prompts the user for a file or folder, then makes a torrent out of the data function showCreateTorrent () { + // Allow only a single selection + // To create a multi-file torrent, the user must select a folder electron.dialog.showOpenDialog({ title: 'Select a file or folder for the torrent file.', - properties: [ 'openFile', 'openDirectory', 'multiSelections' ] + properties: [ 'openFile', 'openDirectory' ] }, function (filenames) { if (!Array.isArray(filenames)) return - windows.main.send('dispatch', 'seed', filenames) + windows.main.send('dispatch', 'seed', filenames[0]) }) } diff --git a/renderer/index.css b/renderer/index.css index 7057ff64..af498d56 100644 --- a/renderer/index.css +++ b/renderer/index.css @@ -271,10 +271,32 @@ i:not(.disabled):hover { padding: 20px; } +.create-torrent-modal input, .open-torrent-address-modal input { width: calc(100% - 100px) } +.create-torrent-modal .torrent-attribute { + white-space: nowrap; +} + +.create-torrent-modal .torrent-attribute>* { + display: inline-block; +} + +.create-torrent-modal .torrent-attribute label { + width: 60px; + margin-right: 10px; + vertical-align: top; +} + +.create-torrent-modal .torrent-attribute div { + font-family: Consolas, monospace; + white-space: nowrap; +} + + + /* * BUTTONS */ diff --git a/renderer/index.js b/renderer/index.js index 144d447e..5abf4e12 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -1,7 +1,7 @@ console.time('init') var cfg = require('application-config')('WebTorrent') -var createTorrent = require('create-torrent') +var defaultAnnounceList = require('create-torrent').announceList var dragDrop = require('drag-drop') var electron = require('electron') var EventEmitter = require('events') @@ -42,7 +42,7 @@ var dialog = remote.require('dialog') var state = global.state = require('./state') // Force use of webtorrent trackers on all torrents -global.WEBTORRENT_ANNOUNCE = createTorrent.announceList +global.WEBTORRENT_ANNOUNCE = defaultAnnounceList .map((arr) => arr[0]) .filter((url) => url.indexOf('wss://') === 0 || url.indexOf('ws://') === 0) @@ -207,13 +207,21 @@ function dispatch (action, ...args) { addTorrent(args[0] /* torrent */) } if (action === 'showCreateTorrent') { - ipcRenderer.send('showCreateTorrent') + ipcRenderer.send('showCreateTorrent') /* open file or folder to seed */ } if (action === 'showOpenTorrentFile') { - ipcRenderer.send('showOpenTorrentFile') + ipcRenderer.send('showOpenTorrentFile') /* open torrent file */ } if (action === 'seed') { - seed(args[0] /* files */) + // TODO: right now, creating a torrent thru File > Create New Torrent + // creates and starts seeding a new torrent directly, while dragging files + // or folders onto the app opens the create-torrent-modal + // + // That's because the former gets a single string and the latter gets a list + // of W3C File objects. We should fix this inconsitency, ideally without + // duping this code in the drag-drop module: + // https://github.com/feross/drag-drop/blob/master/index.js + createTorrent({files: args[0]} /* file or folder path */) } if (action === 'openFile') { openFile(args[0] /* infoHash */, args[1] /* index */) @@ -316,6 +324,9 @@ function dispatch (action, ...args) { if (action === 'saveState') { saveState() } + if (action === 'createTorrent') { + createTorrent(args[0] /* options */) + } // Update the virtual-dom, unless it's just a mouse move event if (action !== 'mediaMouseMoved') { @@ -445,7 +456,7 @@ function onOpen (files) { }) // everything else = seed these files - seed(files.filter(isNotTorrent)) + showCreateTorrentModal(files.filter(isNotTorrent)) } function onPaste (e) { @@ -553,10 +564,18 @@ function stopTorrenting (infoHash) { if (torrent) torrent.destroy() } -// Creates a torrent for a local file and starts seeding it -function seed (files) { +// Prompts the user to create a torrent for a local file or folder +function showCreateTorrentModal (files) { if (files.length === 0) return - var torrent = lazyLoadClient().seed(files) + state.modal = { + id: 'create-torrent-modal', + files: files + } +} + +// Creates a new torrent and start seeeding +function createTorrent (options) { + var torrent = state.client.seed(options.files, options) addTorrentToList(torrent) addTorrentEvents(torrent) } diff --git a/renderer/views/app.js b/renderer/views/app.js index 83d46853..a300fa7b 100644 --- a/renderer/views/app.js +++ b/renderer/views/app.js @@ -9,7 +9,8 @@ var Player = require('./player') var TorrentList = require('./torrent-list') var Modals = { 'open-torrent-address-modal': require('./open-torrent-address-modal'), - 'update-available-modal': require('./update-available-modal') + 'update-available-modal': require('./update-available-modal'), + 'create-torrent-modal': require('./create-torrent-modal') } function App (state, dispatch) { @@ -67,7 +68,7 @@ function App (state, dispatch) { return hx`
Create New Torrent
++ +
+ +
+ +
+ + +
+