SymphonyElectron/js/desktopCapturer/getSources.js
Kiran Niranjan 08c25e4b58 Electron-256 (Changed the implementation from modal to electron window) (#308)
1. Completed functionality
2. Refactored and added code comments
3. Fixed some style issues
4. Added some safety checks
5. Updated Window size
6. Added some safety checks
7. Fixed some keyboard interaction
8. Fixed styles for Windows OS
9. Added a functionality to place screen picker based on the event sender
10. Updated the code to open the screen picker on top of the focused window instead of finding a ref using window name
11. Added a HTML content to display error message
12. Updated window title
13. Added missing return
14. Changed the method name from `openScreenPickerWindowWindow` to `openScreenPickerWindow`
15. Fixed a typo and added code comment
16. Changes as per PR review
17. Created Enum for key code
18. Updated for loop to for..of loop
19. Updated colors from hex to rgba
20. Setting cursor property as pointer for cancel button and item-container
21. Made window draggable
22. Changed font-family to system fonts
23. Added box shadow for buttons
24. Added a new API to support backward compatibility and deprecated the existing one
25. Fixed the condition prevent a new window from being opened if there is an existing window
2018-03-09 15:31:44 +05:30

94 lines
2.9 KiB
JavaScript

'use strict';
// This code provides equivalent of desktopCapturer.getSources that works in
// a sandbox renderer. see: https://electron.atom.io/docs/api/desktop-capturer/
//
// The code here is not entirely kosher/stable as it is using private ipc
// events. The code was take directly from electron.asar file provided in
// prebuilt node module. Note: the slight difference here is the thumbnail
// returns a base64 encoded image rather than a electron nativeImage.
//
// Until electron provides access to desktopCapturer in a sandboxed
// renderer process, this will have to do. See github issue posted here to
// electron: https://github.com/electron/electron/issues/9312
const { ipcRenderer, remote } = require('electron');
const { isWindowsOS } = require('../utils/misc');
let nextId = 0;
let includes = [].includes;
function getNextId() {
return ++nextId;
}
/**
* Checks if the options and their types are valid
* @param options |options.type| can not be empty and has to include 'window' or 'screen'.
* @returns {boolean}
*/
function isValid(options) {
return ((options !== null ? options.types : undefined) !== null) && Array.isArray(options.types);
}
/**
* Gets the sources for capturing screens / windows
* @param options
* @param callback
* @returns {*}
*/
function getSources(options, callback) {
let captureScreen, captureWindow, id;
if (!isValid(options)) {
return callback(new Error('Invalid options'));
}
captureWindow = includes.call(options.types, 'window');
captureScreen = includes.call(options.types, 'screen');
let updatedOptions = options;
if (!updatedOptions.thumbnailSize) {
updatedOptions.thumbnailSize = {
width: 150,
height: 150
};
}
if (isWindowsOS) {
/**
* Sets the captureWindow to false if Desktop composition
* is disabled otherwise true
*
* Setting captureWindow to false returns only screen sources
* @type {boolean}
*/
captureWindow = remote.systemPreferences.isAeroGlassEnabled();
}
id = getNextId();
ipcRenderer.send('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, updatedOptions.thumbnailSize, id);
return ipcRenderer.once('ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_' + id, function(event, sources) {
let source;
callback(null, (function() {
let i, len, results;
results = [];
for (i = 0, len = sources.length; i < len; i++) {
source = sources[i];
results.push({
id: source.id,
name: source.name,
thumbnail: source.thumbnail
});
}
return results;
}()));
});
}
/**
* @deprecated instead use getSource
* @type {getSources}
*/
module.exports = getSources;