electron/lib/renderer/api/desktop-capturer.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-01-14 22:11:50 +00:00
const ipcRenderer = require('electron').ipcRenderer;
2016-01-15 01:03:55 +00:00
const nativeImage = require('electron').nativeImage;
2016-01-12 02:40:23 +00:00
2016-01-14 22:11:50 +00:00
var nextId = 0;
2016-01-15 17:30:29 +00:00
var includes = [].includes;
2016-01-12 02:40:23 +00:00
2016-01-14 22:11:50 +00:00
var getNextId = function() {
2016-01-12 02:40:23 +00:00
return ++nextId;
};
2016-01-14 18:35:29 +00:00
// |options.type| can not be empty and has to include 'window' or 'screen'.
2016-01-14 22:11:50 +00:00
var isValid = function(options) {
2016-01-12 02:40:23 +00:00
return ((options != null ? options.types : void 0) != null) && Array.isArray(options.types);
};
exports.getSources = function(options, callback) {
var captureScreen, captureWindow, id;
if (!isValid(options)) {
return callback(new Error('Invalid options'));
}
2016-01-15 17:30:29 +00:00
captureWindow = includes.call(options.types, 'window');
captureScreen = includes.call(options.types, 'screen');
2016-01-12 02:40:23 +00:00
if (options.thumbnailSize == null) {
options.thumbnailSize = {
width: 150,
height: 150
};
}
id = getNextId();
ipcRenderer.send('ATOM_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, options.thumbnailSize, id);
return ipcRenderer.once("ATOM_RENDERER_DESKTOP_CAPTURER_RESULT_" + id, function(event, sources) {
var source;
return callback(null, (function() {
var i, len, results;
results = [];
for (i = 0, len = sources.length; i < len; i++) {
source = sources[i];
results.push({
id: source.id,
name: source.name,
thumbnail: nativeImage.createFromDataURL(source.thumbnail)
});
}
return results;
})());
});
};