electron-demo.js/drag-drop-main.js

56 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-04-21 19:24:57 +00:00
var electron = require('electron');
var app = electron.app;
var ipc = electron.ipcMain;
var argv = sliceArgv(process.argv);
var win;
function init(window) {
if (win) {
console.error("can't initiliaze drag-and-drop multiple times");
return;
}
win = window;
ipc.once('ipcReady', function () {
app.ipcReady = true;
processArgv(argv);
});
}
app.on('open-file', onOpen);
app.on('open-url', onOpen);
function onOpen(e, filename) {
e.preventDefault();
if (app.ipcReady) {
processArgv([ filename ]);
} else {
argv.push(filename);
}
}
function sliceArgv(argv) {
var count = 1;
// We need to determine if we were run using electron or as a system installed app.
if (argv[0].search('electron') >= 0) {
count += 1;
}
return argv.slice(count);
}
function processArgv(argv) {
var files = [];
argv.forEach(function(arg) {
if (arg.substr(0 ,2) == '--') {
console.log('received argument', arg);
} else {
files.push(arg);
}
});
if (files.length > 0) {
win.webContents.send('files', files);
}
}
module.exports.init = init;