-
-
Save Galkon/f467fdaf8f03cc0aac9f811e2a54c04d to your computer and use it in GitHub Desktop.
Display order mismatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP --> | |
<meta http-equiv="Content-Security-Policy" content="script-src 'self'"> | |
<title>Display ordering bug</title> | |
</head> | |
<body> | |
<button id="screenshot-by-index" style="margin-bottom: 16px">Capture Screenshot by Display Index</button> | |
<button id="screenshot-by-id" style="margin-bottom: 16px">Capture Screenshot by Display ID</button> | |
<img id="preview" src="" style="display: none; max-width: 100vw; max-height: 100vh;"></img> | |
<!-- You can also require other files to run in this process --> | |
<script src="./renderer.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modules to control application life and create native browser window | |
const { app, BrowserWindow, screen, desktopCapturer, ipcMain } = require('electron') | |
const path = require('node:path') | |
const getActiveDisplayBounds = () => { | |
const cursorPoint = screen.getCursorScreenPoint() | |
const display = screen.getDisplayNearestPoint(cursorPoint) | |
return { | |
display, | |
bounds: { | |
x: display.workArea.x + 1, | |
y: display.workArea.y + 1, | |
width: display.workArea.width - 2, | |
height: display.workArea.height - 2 | |
} | |
} | |
} | |
const getScreenshotByDisplayIndex = async () => { | |
const {display, bounds} = getActiveDisplayBounds() | |
const displays = screen.getAllDisplays() | |
const displayIndex = displays.findIndex((disp) => disp.id === display.id) | |
const thumbnailSize = { | |
width: bounds.width, | |
height: bounds.height | |
} | |
console.info('Display index:', displayIndex) | |
console.info('Screen display ID by index:', displays[displayIndex].id) | |
// then we get the screen sources we can capture | |
const sources = await desktopCapturer.getSources({ | |
types: ['screen'], | |
thumbnailSize: thumbnailSize | |
}) | |
// then we get the source for the display the overlay is on | |
const source = sources[displayIndex] | |
console.log('Source display ID by index:', source.display_id) | |
return source.thumbnail.toDataURL() | |
} | |
const getScreenshotByDisplayId = async () => { | |
const {display, bounds} = getActiveDisplayBounds() | |
const thumbnailSize = { | |
width: bounds.width, | |
height: bounds.height | |
} | |
// then we get the screen sources we can capture | |
const sources = await desktopCapturer.getSources({ | |
types: ['screen'], | |
thumbnailSize: thumbnailSize | |
}) | |
// then we get the source for the display the overlay is on | |
const source = sources.find(s => s.display_id == display.id) | |
return source.thumbnail.toDataURL() | |
} | |
function createWindow () { | |
// Create the browser window. | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
preload: path.join(__dirname, 'preload.js') | |
} | |
}) | |
// and load the index.html of the app. | |
mainWindow.loadFile('index.html') | |
// Open the DevTools. | |
// mainWindow.webContents.openDevTools() | |
} | |
// This method will be called when Electron has finished | |
// initialization and is ready to create browser windows. | |
// Some APIs can only be used after this event occurs. | |
app.whenReady().then(() => { | |
createWindow() | |
app.on('activate', function () { | |
// On macOS it's common to re-create a window in the app when the | |
// dock icon is clicked and there are no other windows open. | |
if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
}) | |
}) | |
// Quit when all windows are closed, except on macOS. There, it's common | |
// for applications and their menu bar to stay active until the user quits | |
// explicitly with Cmd + Q. | |
app.on('window-all-closed', function () { | |
if (process.platform !== 'darwin') app.quit() | |
}) | |
// In this file you can include the rest of your app's specific main process | |
// code. You can also put them in separate files and require them here. | |
ipcMain.handle('screenshotByIndex', async () => { | |
return await getScreenshotByDisplayIndex() | |
}) | |
ipcMain.handle('screenshotById', async () => { | |
return await getScreenshotByDisplayId() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "jumbled-swing-trouble-bw6qw", | |
"productName": "jumbled-swing-trouble-bw6qw", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "Galkon", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "30.0.2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { contextBridge, ipcRenderer} = require('electron') | |
/** | |
* The preload script runs before `index.html` is loaded | |
* in the renderer. It has access to web APIs as well as | |
* Electron's renderer process modules and some polyfilled | |
* Node.js functions. | |
* | |
* https://www.electronjs.org/docs/latest/tutorial/sandbox | |
*/ | |
window.addEventListener('DOMContentLoaded', () => { | |
const replaceText = (selector, text) => { | |
const element = document.getElementById(selector) | |
if (element) element.innerText = text | |
} | |
for (const type of ['chrome', 'node', 'electron']) { | |
replaceText(`${type}-version`, process.versions[type]) | |
} | |
}) | |
contextBridge.exposeInMainWorld('screenshotByIndex', async () => { | |
return ipcRenderer.invoke('screenshotByIndex') | |
}) | |
contextBridge.exposeInMainWorld('screenshotById', async () => { | |
return ipcRenderer.invoke('screenshotById') | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file is loaded via the <script> tag in the index.html file and will | |
* be executed in the renderer process for that window. No Node.js APIs are | |
* available in this process because `nodeIntegration` is turned off and | |
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js` | |
* to expose Node.js functionality from the main process. | |
*/ | |
const captureScreenshot = async (byId = false) => { | |
const dataUrl = byId ? (await window.screenshotById()) : (await window.screenshotByIndex()) | |
const img = document.getElementById("preview") | |
img.src = dataUrl | |
img.style.display = "flex" | |
return dataUrl | |
} | |
window.addEventListener('DOMContentLoaded', () => { | |
const btn1 = document.getElementById('screenshot-by-index') | |
btn1.onclick = () => captureScreenshot(false) | |
const btn2 = document.getElementById('screenshot-by-id') | |
btn2.onclick = () => captureScreenshot(true) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment