electron/atom/browser/lib/desktop-capturer.coffee
Haojian Wu 36c0ad7fda Refine more about desktop capturer API.
* Simplify the coffeescript code.
* Add more options in desktopCapturer.startUpdating.
2015-10-05 12:47:01 +08:00

38 lines
1.4 KiB
CoffeeScript

ipc = require 'ipc'
BrowserWindow = require 'browser-window'
# The browser module manages all desktop-capturer moduels in renderer process.
desktopCapturer = process.atomBinding('desktop_capturer').desktopCapturer
getWebContentsFromId = (id) ->
windows = BrowserWindow.getAllWindows()
return window.webContents for window in windows when window.webContents?.getId() == id
# The set for tracking id of webContents.
webContentsIds = new Set
stopDesktopCapture = (id) ->
webContentsIds.delete id
# Stop updating if no renderer process listens the desktop capturer.
if webContentsIds.size is 0
desktopCapturer.stopUpdating()
# Handle `desktopCapturer.startUpdating` API.
ipc.on 'ATOM_BROWSER_DESKTOP_CAPTURER_START_UPDATING', (event, args) ->
id = event.sender.getId()
if not webContentsIds.has id
# Stop sending desktop capturer events to the destroyed webContents.
event.sender.on 'destroyed', ()->
stopDesktopCapture id
# Start updating the desktopCapturer if it doesn't.
if webContentsIds.size is 0
desktopCapturer.startUpdating args
webContentsIds.add id
# Handle `desktopCapturer.stopUpdating` API.
ipc.on 'ATOM_BROWSER_DESKTOP_CAPTURER_STOP_UPDATING', (event) ->
stopDesktopCapture event.sender.getId()
desktopCapturer.emit = (event_name, event, desktopId, name, thumbnail) ->
webContentsIds.forEach (id) ->
getWebContentsFromId(id).send 'ATOM_RENDERER_DESKTOP_CAPTURER', event_name, desktopId, name, thumbnail?.toDataUrl()