2023-08-29 19:52:16 +00:00
|
|
|
const { BrowserWindow, app, screen, ipcMain, desktopCapturer, shell } = require('electron/main')
|
|
|
|
const fs = require('node:fs').promises
|
|
|
|
const os = require('node:os')
|
|
|
|
const path = require('node:path')
|
2019-10-16 15:17:50 +00:00
|
|
|
|
|
|
|
let mainWindow = null
|
|
|
|
|
2023-08-29 19:52:16 +00:00
|
|
|
function determineScreenShotSize (devicePixelRatio) {
|
|
|
|
const screenSize = screen.getPrimaryDisplay().workAreaSize
|
|
|
|
const maxDimension = Math.max(screenSize.width, screenSize.height)
|
|
|
|
return {
|
|
|
|
width: maxDimension * devicePixelRatio,
|
|
|
|
height: maxDimension * devicePixelRatio
|
|
|
|
}
|
|
|
|
}
|
2020-09-14 17:36:54 +00:00
|
|
|
|
2023-08-29 19:52:16 +00:00
|
|
|
async function takeScreenshot (devicePixelRatio) {
|
|
|
|
const thumbSize = determineScreenShotSize(devicePixelRatio)
|
|
|
|
const options = { types: ['screen'], thumbnailSize: thumbSize }
|
|
|
|
|
|
|
|
const sources = await desktopCapturer.getSources(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')
|
|
|
|
|
|
|
|
await fs.writeFile(screenshotPath, source.thumbnail.toPNG())
|
|
|
|
shell.openExternal(`file://${screenshotPath}`)
|
|
|
|
|
|
|
|
return `Saved screenshot to: ${screenshotPath}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ipcMain.handle('take-screenshot', (event, devicePixelRatio) => takeScreenshot(devicePixelRatio))
|
2023-08-10 08:53:23 +00:00
|
|
|
|
2019-10-16 15:17:50 +00:00
|
|
|
function createWindow () {
|
|
|
|
const windowOptions = {
|
|
|
|
width: 600,
|
|
|
|
height: 300,
|
|
|
|
title: 'Take a Screenshot',
|
|
|
|
webPreferences: {
|
2023-08-29 19:52:16 +00:00
|
|
|
preload: path.join(__dirname, 'preload.js')
|
2019-10-16 15:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mainWindow = new BrowserWindow(windowOptions)
|
|
|
|
mainWindow.loadFile('index.html')
|
|
|
|
|
|
|
|
mainWindow.on('closed', () => {
|
|
|
|
mainWindow = null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:43:22 +00:00
|
|
|
app.whenReady().then(() => {
|
2019-10-16 15:17:50 +00:00
|
|
|
createWindow()
|
|
|
|
})
|