Tweak screensharing

This commit is contained in:
smartfridge 2022-12-25 19:27:08 +01:00
parent 8e16371521
commit 334d28542f
2 changed files with 32 additions and 35 deletions

View file

@ -2,7 +2,8 @@ import {BrowserWindow, desktopCapturer, ipcMain, session, shell} from "electron"
import path from "path";
import {iconPath} from "../main";
var capturerWindow: BrowserWindow;
session.defaultSession.setDisplayMediaRequestHandler(async (request, callback) => {
function registerCustomHandler() {
session.defaultSession.setDisplayMediaRequestHandler(async (request, callback) => {
const sources = await desktopCapturer.getSources({
types: ["screen", "window"]
});
@ -21,17 +22,15 @@ session.defaultSession.setDisplayMediaRequestHandler(async (request, callback) =
preload: path.join(__dirname, "preload.js")
}
});
ipcMain.on("selectScreenshareSource", (event, id, name) => {
ipcMain.once("selectScreenshareSource", (event, id, name) => {
console.log(sources[id]);
console.log(id);
callback({video: {id, name}});
capturerWindow.close();
});
capturerWindow.webContents.setWindowOpenHandler(({url}) => {
shell.openExternal(url);
return {action: "deny"};
var result = {id, name};
callback({video: result});
});
capturerWindow.loadURL(`file://${__dirname}/picker.html`);
capturerWindow.webContents.send("getSources", sources);
});
});
}
registerCustomHandler();

View file

@ -5,7 +5,7 @@ interface IPCSources {
thumbnail: HTMLCanvasElement;
}
async function addDisplays() {
ipcRenderer.on("getSources", (event, arg) => {
ipcRenderer.once("getSources", (event, arg) => {
var sources: IPCSources[] = arg;
console.log(sources);
const selectionElem = document.createElement("div");
@ -45,8 +45,6 @@ async function addDisplays() {
}
} catch (err) {
console.error(err);
} finally {
ipcRenderer.sendSync("closeScreenshareWindow");
}
});
});