From a91dc4e1eaba04b8e2ec5028beb545b2af95185e Mon Sep 17 00:00:00 2001 From: Dan Flettre Date: Sat, 26 Mar 2016 22:23:50 -0500 Subject: [PATCH] add context menu with share/save actions --- renderer/index.js | 42 ++++++++++++++++++++++++++++++++++ renderer/views/torrent-list.js | 4 +++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/renderer/index.js b/renderer/index.js index 1dd3a755..c492c209 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -30,6 +30,7 @@ var torrentPoster = require('./lib/torrent-poster') // and this IPC channel receives from and sends messages to the main process var ipcRenderer = electron.ipcRenderer var clipboard = electron.clipboard +var dialog = remote.require('dialog') // For easy debugging in Developer Tools var state = global.state = require('./state') @@ -209,6 +210,9 @@ function dispatch (action, ...args) { if (action === 'toggleSelectTorrent') { toggleSelectTorrent(args[0] /* infoHash */) } + if (action === 'openTorrentContextMenu') { + openTorrentContextMenu(args[0] /* torrentSummary */) + } if (action === 'openChromecast') { Cast.openChromecast() } @@ -795,6 +799,44 @@ function toggleSelectTorrent (infoHash) { update() } +function openTorrentContextMenu (torrentSummary) { + var menu = new remote.Menu() + menu.append(new remote.MenuItem({ + label: 'Save Torrent File As...', + click: () => saveTorrentFileAs(torrentSummary) + })) + + menu.append(new remote.MenuItem({ + label: 'Copy Instant.io Link to Clipboard', + click: () => clipboard.writeText(`https://instant.io/#${torrentSummary.infoHash}`) + })) + + menu.append(new remote.MenuItem({ + label: 'Copy Magnet Link to Clipboard', + click: () => clipboard.writeText(torrentSummary.magnetURI) + })) + + menu.popup(remote.getCurrentWindow()) +} + +function saveTorrentFileAs (torrentSummary) { + var newFileName = `${path.parse(torrentSummary.name).name}.torrent` + var opts = { + title: 'Save Torrent File', + defaultPath: path.join(state.saved.downloadPath, newFileName), + filters: [{ name: 'Torrents', extensions: ['torrent'] }] + } + dialog.showSaveDialog(remote.getCurrentWindow(), opts, (savePath) => { + var torrentFile = fs.createReadStream(torrentSummary.torrentPath) + var savedTorrentFile = fs.createWriteStream(savePath) + torrentFile.on('error', (err) => console.error('Error reading torrent file', err)) + savedTorrentFile.on('error', (err) => console.error('Error saving torrent file', err)) + savedTorrentFile.on('close', () => console.log('Torrent saved', savePath)) + + torrentFile.pipe(savedTorrentFile) + }) +} + // Set window dimensions to match video dimensions or fill the screen function setDimensions (dimensions) { // Don't modify the window size if it's already maximized diff --git a/renderer/views/torrent-list.js b/renderer/views/torrent-list.js index 4d613149..48c05c1f 100644 --- a/renderer/views/torrent-list.js +++ b/renderer/views/torrent-list.js @@ -47,7 +47,9 @@ function TorrentList (state, dispatch) { if (isSelected) classes.push('selected') classes = classes.join(' ') return hx` -
dispatch('toggleSelectTorrent', infoHash)}> +
dispatch('openTorrentContextMenu', torrentSummary)} + onclick=${() => dispatch('toggleSelectTorrent', infoHash)}> ${renderTorrentMetadata(torrent, torrentSummary)} ${renderTorrentButtons(torrentSummary)} ${isSelected ? renderTorrentDetails(torrent, torrentSummary) : ''}