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

48 lines
1.3 KiB
JavaScript
Raw Normal View History

'use strict'
2018-09-13 16:10:51 +00:00
const { ipcRenderer, nativeImage } = require('electron')
2016-01-12 02:40:23 +00:00
const includes = [].includes
let currentId = 0
2016-01-12 02:40:23 +00:00
2017-10-24 16:28:15 +00:00
const incrementId = () => {
currentId += 1
return currentId
}
2016-01-12 02:40:23 +00:00
2017-10-24 23:36:06 +00:00
// |options.types| can't be empty and must be an array
function isValid (options) {
2017-10-24 23:36:06 +00:00
const types = options ? options.types : undefined
return Array.isArray(types)
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
exports.getSources = function (options, callback) {
if (!isValid(options)) return callback(new Error('Invalid options'))
const captureWindow = includes.call(options.types, 'window')
const 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
}
const id = incrementId()
ipcRenderer.send('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, options.thumbnailSize, id)
return ipcRenderer.once(`ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`, (event, sources) => {
callback(null, (() => {
2017-10-24 16:28:15 +00:00
const results = []
sources.forEach(source => {
2016-01-12 02:40:23 +00:00
results.push({
id: source.id,
name: source.name,
thumbnail: nativeImage.createFromDataURL(source.thumbnail),
display_id: source.display_id
2016-03-25 19:57:17 +00:00
})
})
2016-03-25 19:57:17 +00:00
return results
})())
})
}