From 1ce2fdd63d2cff52adbc18fdef6830a0f31e765f Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Thu, 10 Aug 2023 10:53:23 +0200 Subject: [PATCH] docs: fix take-screenshot fiddle to use desktopCapturer in main.js (#39420) --- .../media/screenshot/take-screenshot/main.js | 6 +++- .../screenshot/take-screenshot/renderer.js | 31 ++++++++----------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/docs/fiddles/media/screenshot/take-screenshot/main.js b/docs/fiddles/media/screenshot/take-screenshot/main.js index 5754ff62a89e..cb09498a0d44 100644 --- a/docs/fiddles/media/screenshot/take-screenshot/main.js +++ b/docs/fiddles/media/screenshot/take-screenshot/main.js @@ -1,4 +1,4 @@ -const { BrowserWindow, app, screen, ipcMain } = require('electron') +const { BrowserWindow, app, screen, ipcMain, desktopCapturer } = require('electron') let mainWindow = null @@ -6,6 +6,10 @@ ipcMain.handle('get-screen-size', () => { return screen.getPrimaryDisplay().workAreaSize }) +ipcMain.handle('get-sources', (event, options) => { + return desktopCapturer.getSources(options) +}) + function createWindow () { const windowOptions = { width: 600, diff --git a/docs/fiddles/media/screenshot/take-screenshot/renderer.js b/docs/fiddles/media/screenshot/take-screenshot/renderer.js index ab07d1ceaca0..fd615325b8f3 100644 --- a/docs/fiddles/media/screenshot/take-screenshot/renderer.js +++ b/docs/fiddles/media/screenshot/take-screenshot/renderer.js @@ -1,6 +1,6 @@ -const { desktopCapturer, shell, ipcRenderer } = require('electron') +const { shell, ipcRenderer } = require('electron') -const fs = require('node:fs') +const fs = require('node:fs').promises const os = require('node:os') const path = require('node:path') @@ -12,24 +12,19 @@ screenshot.addEventListener('click', async (event) => { const thumbSize = await determineScreenShotSize() const options = { types: ['screen'], thumbnailSize: thumbSize } - desktopCapturer.getSources(options, (error, sources) => { - if (error) return console.log(error) + const sources = await ipcRenderer.invoke('get-sources', options) + for (const source of sources) { + const sourceName = source.name.toLowerCase() + if (sourceName === 'entire screen' || sourceName === 'screen 1') { + const screenshotPath = path.join(os.tmpdir(), 'screenshot.png') - sources.forEach((source) => { - const sourceName = source.name.toLowerCase() - if (sourceName === 'entire screen' || sourceName === 'screen 1') { - const screenshotPath = path.join(os.tmpdir(), 'screenshot.png') + await fs.writeFile(screenshotPath, source.thumbnail.toPNG()) + shell.openExternal(`file://${screenshotPath}`) - fs.writeFile(screenshotPath, source.thumbnail.toPNG(), (error) => { - if (error) return console.log(error) - shell.openExternal(`file://${screenshotPath}`) - - const message = `Saved screenshot to: ${screenshotPath}` - screenshotMsg.textContent = message - }) - } - }) - }) + const message = `Saved screenshot to: ${screenshotPath}` + screenshotMsg.textContent = message + } + } }) async function determineScreenShotSize () {