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

View file

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