add context menu with share/save actions

This commit is contained in:
Dan Flettre
2016-03-26 22:23:50 -05:00
parent ba74aac7bf
commit a91dc4e1ea
2 changed files with 45 additions and 1 deletions

View File

@@ -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

View File

@@ -47,7 +47,9 @@ function TorrentList (state, dispatch) {
if (isSelected) classes.push('selected')
classes = classes.join(' ')
return hx`
<div style=${style} class=${classes} onclick=${() => dispatch('toggleSelectTorrent', infoHash)}>
<div style=${style} class=${classes}
oncontextmenu=${() => dispatch('openTorrentContextMenu', torrentSummary)}
onclick=${() => dispatch('toggleSelectTorrent', infoHash)}>
${renderTorrentMetadata(torrent, torrentSummary)}
${renderTorrentButtons(torrentSummary)}
${isSelected ? renderTorrentDetails(torrent, torrentSummary) : ''}