2020-03-20 20:28:31 +00:00
|
|
|
import { nativeImage } from 'electron';
|
|
|
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal';
|
2019-06-15 10:44:18 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const { hasSwitch } = process.electronBinding('command_line');
|
2019-10-04 17:49:09 +00:00
|
|
|
|
2019-06-15 10:44:18 +00:00
|
|
|
// |options.types| can't be empty and must be an array
|
|
|
|
function isValid (options: Electron.SourcesOptions) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const types = options ? options.types : undefined;
|
|
|
|
return Array.isArray(types);
|
2019-06-15 10:44:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const enableStacks = hasSwitch('enable-api-filtering-logging');
|
2019-10-04 17:49:09 +00:00
|
|
|
|
|
|
|
function getCurrentStack () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const target = {};
|
2019-10-04 17:49:09 +00:00
|
|
|
if (enableStacks) {
|
2020-03-20 20:28:31 +00:00
|
|
|
Error.captureStackTrace(target, getCurrentStack);
|
2019-10-04 17:49:09 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return (target as any).stack;
|
2019-10-04 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 10:44:18 +00:00
|
|
|
export async function getSources (options: Electron.SourcesOptions) {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (!isValid(options)) throw new Error('Invalid options');
|
2019-06-15 10:44:18 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const captureWindow = options.types.includes('window');
|
|
|
|
const captureScreen = options.types.includes('screen');
|
2019-06-15 10:44:18 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const { thumbnailSize = { width: 150, height: 150 } } = options;
|
|
|
|
const { fetchWindowIcons = false } = options;
|
2019-06-15 10:44:18 +00:00
|
|
|
|
2019-08-23 22:45:50 +00:00
|
|
|
const sources = await ipcRendererInternal.invoke<ElectronInternal.GetSourcesResult[]>('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', {
|
2019-06-15 10:44:18 +00:00
|
|
|
captureWindow,
|
|
|
|
captureScreen,
|
|
|
|
thumbnailSize,
|
|
|
|
fetchWindowIcons
|
2020-03-20 20:28:31 +00:00
|
|
|
} as ElectronInternal.GetSourcesOptions, getCurrentStack());
|
2019-06-15 10:44:18 +00:00
|
|
|
|
|
|
|
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
|
2020-03-20 20:28:31 +00:00
|
|
|
}));
|
2019-06-15 10:44:18 +00:00
|
|
|
}
|