2023-08-28 11:23:10 +00:00
|
|
|
const { shell, ipcRenderer } = require('electron/renderer')
|
2019-10-16 15:17:50 +00:00
|
|
|
|
2023-08-10 08:53:23 +00:00
|
|
|
const fs = require('node:fs').promises
|
2023-07-17 08:30:53 +00:00
|
|
|
const os = require('node:os')
|
|
|
|
const path = require('node:path')
|
2019-10-16 15:17:50 +00:00
|
|
|
|
|
|
|
const screenshot = document.getElementById('screen-shot')
|
|
|
|
const screenshotMsg = document.getElementById('screenshot-path')
|
|
|
|
|
2020-09-14 17:36:54 +00:00
|
|
|
screenshot.addEventListener('click', async (event) => {
|
2019-10-16 15:17:50 +00:00
|
|
|
screenshotMsg.textContent = 'Gathering screens...'
|
2020-09-14 17:36:54 +00:00
|
|
|
const thumbSize = await determineScreenShotSize()
|
2019-10-16 15:17:50 +00:00
|
|
|
const options = { types: ['screen'], thumbnailSize: thumbSize }
|
|
|
|
|
2023-08-10 08:53:23 +00:00
|
|
|
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')
|
2019-10-16 15:17:50 +00:00
|
|
|
|
2023-08-10 08:53:23 +00:00
|
|
|
await fs.writeFile(screenshotPath, source.thumbnail.toPNG())
|
|
|
|
shell.openExternal(`file://${screenshotPath}`)
|
2019-10-16 15:17:50 +00:00
|
|
|
|
2023-08-10 08:53:23 +00:00
|
|
|
const message = `Saved screenshot to: ${screenshotPath}`
|
|
|
|
screenshotMsg.textContent = message
|
|
|
|
}
|
|
|
|
}
|
2019-10-16 15:17:50 +00:00
|
|
|
})
|
|
|
|
|
2020-09-14 17:36:54 +00:00
|
|
|
async function determineScreenShotSize () {
|
|
|
|
const screenSize = await ipcRenderer.invoke('get-screen-size')
|
2019-10-16 15:17:50 +00:00
|
|
|
const maxDimension = Math.max(screenSize.width, screenSize.height)
|
|
|
|
return {
|
|
|
|
width: maxDimension * window.devicePixelRatio,
|
|
|
|
height: maxDimension * window.devicePixelRatio
|
|
|
|
}
|
|
|
|
}
|