desktop-capturer.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const {ipcRenderer, nativeImage} = require('electron')
  2. const includes = [].includes
  3. let currentId = 0
  4. const incrementId = () => {
  5. currentId += 1
  6. return currentId
  7. }
  8. // |options.types| can't be empty and must be an array
  9. function isValid (options) {
  10. const types = options ? options.types : undefined
  11. return Array.isArray(types)
  12. }
  13. exports.getSources = function (options, callback) {
  14. if (!isValid(options)) return callback(new Error('Invalid options'))
  15. const captureWindow = includes.call(options.types, 'window')
  16. const captureScreen = includes.call(options.types, 'screen')
  17. if (options.thumbnailSize == null) {
  18. options.thumbnailSize = {
  19. width: 150,
  20. height: 150
  21. }
  22. }
  23. const id = incrementId()
  24. ipcRenderer.send('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, options.thumbnailSize, id)
  25. return ipcRenderer.once(`ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`, (event, sources) => {
  26. callback(null, (() => {
  27. const results = []
  28. sources.forEach(source => {
  29. results.push({
  30. id: source.id,
  31. name: source.name,
  32. thumbnail: nativeImage.createFromDataURL(source.thumbnail),
  33. display_id: source.display_id
  34. })
  35. })
  36. return results
  37. })())
  38. })
  39. }