Allow dropping files on dock icon

Fixes #584
This commit is contained in:
DC
2016-05-26 00:50:45 -07:00
parent 839bec0363
commit 955fe76c3c
2 changed files with 38 additions and 19 deletions

View File

@@ -130,6 +130,7 @@ function sliceArgv (argv) {
}
function processArgv (argv) {
var pathsToOpen = []
argv.forEach(function (arg) {
if (arg === '-n') {
menu.showOpenSeedFiles()
@@ -141,7 +142,15 @@ function processArgv (argv) {
// Ignore OS X launchd "process serial number" argument
// More: https://github.com/feross/webtorrent-desktop/issues/214
} else {
windows.main.send('dispatch', 'onOpen', arg)
pathsToOpen.push(arg)
}
})
if (pathsToOpen.length > 0) openFilePaths(pathsToOpen)
}
// Convert paths to {name, path, size} objects, then send to renderer process
// Opening files means either adding torrents, creating and seeding a torrent
// from files, or adding subtitles
function openFilePaths (paths) {
windows.main.send('dispatch', 'onOpen', paths)
}

View File

@@ -804,20 +804,38 @@ function startTorrentingSummary (torrentSummary) {
// Shows the Create Torrent page with options to seed a given file or folder
function showCreateTorrent (files) {
if (Array.isArray(files)) {
state.location.go({
url: 'create-torrent',
files: files
})
return
if (files.length === 0 || typeof files[0] !== 'string') {
state.location.go({
url: 'create-torrent',
files: files
})
return
}
} else {
files = [files]
}
var fileOrFolder = files
findFilesRecursive(fileOrFolder, showCreateTorrent)
findFilesRecursive(files, showCreateTorrent)
}
// Recursively finds {name, path, size} for all files in a folder
// Calls `cb` on success, calls `onError` on failure
function findFilesRecursive (fileOrFolder, cb) {
function findFilesRecursive (paths, cb) {
if (paths.length > 1) {
var numComplete = 0
var ret = []
paths.forEach(function (path) {
findFilesRecursive([path], function (fileObjs) {
ret = ret.concat(fileObjs)
if (++numComplete === paths.length) {
cb(ret)
}
})
})
return
}
var fileOrFolder = paths[0]
fs.stat(fileOrFolder, function (err, stat) {
if (err) return onError(err)
@@ -835,16 +853,8 @@ function findFilesRecursive (fileOrFolder, cb) {
var folderPath = fileOrFolder
fs.readdir(folderPath, function (err, fileNames) {
if (err) return onError(err)
var numComplete = 0
var ret = []
fileNames.forEach(function (fileName) {
findFilesRecursive(path.join(folderPath, fileName), function (fileObjs) {
ret = ret.concat(fileObjs)
if (++numComplete === fileNames.length) {
cb(ret)
}
})
})
var paths = fileNames.map((fileName) => path.join(folderPath, fileName))
findFilesRecursive(paths, cb)
})
})
}