2018-09-22 12:28:50 +00:00
|
|
|
'use strict'
|
|
|
|
|
2019-01-18 23:29:32 +00:00
|
|
|
const { nativeImage, deprecate } = require('electron')
|
2018-10-06 11:48:00 +00:00
|
|
|
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-24 16:01:51 +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
|
2017-10-24 16:01:51 +00:00
|
|
|
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
|
|
|
|
2018-12-20 02:44:30 +00:00
|
|
|
function mapSources (sources) {
|
|
|
|
return sources.map(source => ({
|
|
|
|
id: source.id,
|
|
|
|
name: source.name,
|
|
|
|
thumbnail: nativeImage.createFromDataURL(source.thumbnail),
|
|
|
|
display_id: source.display_id,
|
|
|
|
appIcon: source.appIcon ? nativeImage.createFromDataURL(source.appIcon) : null
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-01-18 23:29:32 +00:00
|
|
|
const getSources = (options) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!isValid(options)) throw new Error('Invalid options')
|
2017-10-24 16:01:51 +00:00
|
|
|
|
2019-01-18 23:29:32 +00:00
|
|
|
const captureWindow = includes.call(options.types, 'window')
|
|
|
|
const captureScreen = includes.call(options.types, 'screen')
|
|
|
|
|
|
|
|
if (options.thumbnailSize == null) {
|
|
|
|
options.thumbnailSize = {
|
|
|
|
width: 150,
|
|
|
|
height: 150
|
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2019-01-18 23:29:32 +00:00
|
|
|
if (options.fetchWindowIcons == null) {
|
|
|
|
options.fetchWindowIcons = false
|
2018-12-20 02:44:30 +00:00
|
|
|
}
|
2019-01-18 23:29:32 +00:00
|
|
|
|
|
|
|
const id = incrementId()
|
|
|
|
ipcRenderer.send('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, options.thumbnailSize, options.fetchWindowIcons, id)
|
|
|
|
return ipcRenderer.once(`ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`, (event, sources) => {
|
|
|
|
try {
|
|
|
|
resolve(mapSources(sources))
|
|
|
|
} catch (error) {
|
|
|
|
reject(error)
|
|
|
|
}
|
|
|
|
})
|
2016-03-25 19:57:17 +00:00
|
|
|
})
|
|
|
|
}
|
2019-01-18 23:29:32 +00:00
|
|
|
|
|
|
|
exports.getSources = deprecate.promisify(getSources, 1)
|