refactor: re-implement desktop-capturer in TypeScript (#18580)
This commit is contained in:
parent
4ef8de69ef
commit
370e9522b4
9 changed files with 127 additions and 144 deletions
|
@ -1,42 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const { nativeImage, deprecate } = require('electron')
|
||||
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
|
||||
|
||||
// |options.types| can't be empty and must be an array
|
||||
function isValid (options) {
|
||||
const types = options ? options.types : undefined
|
||||
return Array.isArray(types)
|
||||
}
|
||||
|
||||
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
|
||||
}))
|
||||
}
|
||||
|
||||
exports.getSources = (options) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!isValid(options)) throw new Error('Invalid options')
|
||||
|
||||
const captureWindow = options.types.includes('window')
|
||||
const captureScreen = options.types.includes('screen')
|
||||
|
||||
if (options.thumbnailSize == null) {
|
||||
options.thumbnailSize = {
|
||||
width: 150,
|
||||
height: 150
|
||||
}
|
||||
}
|
||||
if (options.fetchWindowIcons == null) {
|
||||
options.fetchWindowIcons = false
|
||||
}
|
||||
|
||||
ipcRendererUtils.invoke('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, options.thumbnailSize, options.fetchWindowIcons)
|
||||
.then(sources => resolve(mapSources(sources)), reject)
|
||||
})
|
||||
}
|
33
lib/renderer/api/desktop-capturer.ts
Normal file
33
lib/renderer/api/desktop-capturer.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import { nativeImage } from 'electron'
|
||||
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||
|
||||
// |options.types| can't be empty and must be an array
|
||||
function isValid (options: Electron.SourcesOptions) {
|
||||
const types = options ? options.types : undefined
|
||||
return Array.isArray(types)
|
||||
}
|
||||
|
||||
export async function getSources (options: Electron.SourcesOptions) {
|
||||
if (!isValid(options)) throw new Error('Invalid options')
|
||||
|
||||
const captureWindow = options.types.includes('window')
|
||||
const captureScreen = options.types.includes('screen')
|
||||
|
||||
const { thumbnailSize = { width: 150, height: 150 } } = options
|
||||
const { fetchWindowIcons = false } = options
|
||||
|
||||
const sources = await ipcRendererUtils.invoke<ElectronInternal.GetSourcesResult[]>('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', {
|
||||
captureWindow,
|
||||
captureScreen,
|
||||
thumbnailSize,
|
||||
fetchWindowIcons
|
||||
} as ElectronInternal.GetSourcesOptions)
|
||||
|
||||
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
|
||||
}))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue