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

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-03-25 19:57:17 +00:00
const ipcRenderer = require('electron').ipcRenderer
const nativeImage = require('electron').nativeImage
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
var nextId = 0
var includes = [].includes
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
var getNextId = function () {
return ++nextId
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// |options.type| can not be empty and has to include 'window' or 'screen'.
2016-03-25 19:57:17 +00:00
var isValid = function (options) {
return ((options != null ? options.types : void 0) != null) && Array.isArray(options.types)
}
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
exports.getSources = function (options, callback) {
var captureScreen, captureWindow, id
2016-01-12 02:40:23 +00:00
if (!isValid(options)) {
2016-03-25 19:57:17 +00:00
return callback(new Error('Invalid options'))
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +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
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
id = getNextId()
ipcRenderer.send('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, options.thumbnailSize, id)
return ipcRenderer.once('ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_' + id, function (event, sources) {
2016-03-25 19:57:17 +00:00
var source
return callback(null, (function () {
var i, len, results
results = []
2016-01-12 02:40:23 +00:00
for (i = 0, len = sources.length; i < len; i++) {
2016-03-25 19:57:17 +00:00
source = sources[i]
2016-01-12 02:40:23 +00:00
results.push({
id: source.id,
name: source.name,
thumbnail: nativeImage.createFromDataURL(source.thumbnail)
2016-03-25 19:57:17 +00:00
})
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
return results
})())
})
}