Skip to content

Commit

Permalink
chore: screenshare cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
smartfrigde committed Nov 5, 2024
1 parent afcd79b commit f3b7020
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"cSpell.ignorePaths": ["assets/lang"],
"editor.defaultFormatter": "biomejs.biome",
"[typescript]": {
"editor.defaultFormatter": "vscode.typescript-language-features"
"editor.defaultFormatter": "biomejs.biome"
}
}
125 changes: 57 additions & 68 deletions src/screenshare/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,75 +23,64 @@ const audioDialogOptions: MessageBoxOptions = {

function registerCustomHandler(): void {
session.defaultSession.setDisplayMediaRequestHandler(
(request, callback) => {
async (request, callback) => {
console.log(request);
try {
desktopCapturer
.getSources({
types: ["screen", "window"],
})
.then((sources) => {
if (!sources) return callback({});
isDone = false;
console.log(sources);
if (process.platform === "linux" && process.env.XDG_SESSION_TYPE?.toLowerCase() === "wayland") {
console.log("WebRTC Capturer detected, skipping window creation."); //assume webrtc capturer is used
const options: Streams = { video: sources[0] };
if (sources[0] === undefined) return callback({});
void dialog.showMessageBox(capturerWindow, audioDialogOptions).then(({ response }) => {
if (response === 0) {
callback({ video: sources[0], audio: getConfig("audio") });
} else {
callback(options);
}
});
} else {
capturerWindow = new BrowserWindow({
width: 800,
height: 600,
title: "Legcord Screenshare",
darkTheme: true,
icon:
getConfig("customIcon") ??
path.join(import.meta.dirname, "../", "/assets/desktop.png"),
frame: true,
autoHideMenuBar: true,
webPreferences: {
sandbox: false,
spellcheck: false,
preload: path.join(import.meta.dirname, "screenshare", "preload.mjs"),
},
});
ipcMain.once(
"selectScreenshareSource",
(_event, id: string, name: string, audio: boolean) => {
isDone = true;
console.log(`Audio status: ${audio}`);
capturerWindow.close();
const result = { id, name };
let options: Streams = { video: sources[0] };
switch (process.platform) {
case "win32":
case "linux":
options = { video: result };
if (audio) options = { video: result, audio: getConfig("audio") };
callback(options);
break;
default:
callback({ video: result });
}
},
);
capturerWindow.on("closed", () => {
if (!isDone) callback({});
});
void capturerWindow.loadFile(path.join(import.meta.dirname, "html", "picker.html"));
capturerWindow.webContents.send("getSources", sources);
}
});
} catch (error) {
console.error("Error in screenshare handler: ", error);
callback({});
isDone = false;
const sources = await desktopCapturer
.getSources({
types: ["window", "screen"],
})
.catch((err) => console.error(err));

if (!sources) return callback({});
if (process.platform === "linux" && process.env.XDG_SESSION_TYPE?.toLowerCase() === "wayland") {
console.log("WebRTC Capturer detected, skipping window creation.");
const options: Streams = { video: sources[0] };
if (sources[0] === undefined) return callback({});
void dialog.showMessageBox(capturerWindow, audioDialogOptions).then(({ response }) => {
if (response === 0) {
callback({ video: sources[0], audio: getConfig("audio") });
} else {
callback(options);
}
});
} else {
capturerWindow = new BrowserWindow({
width: 800,
height: 600,
title: "Legcord Screenshare",
darkTheme: true,
icon: getConfig("customIcon") ?? path.join(import.meta.dirname, "../", "/assets/desktop.png"),
frame: true,
autoHideMenuBar: true,
webPreferences: {
sandbox: false,
spellcheck: false,
preload: path.join(import.meta.dirname, "screenshare", "preload.mjs"),
},
});
ipcMain.once("selectScreenshareSource", (_event, id: string, name: string, audio: boolean) => {
isDone = true;
console.log(`Audio status: ${audio}`);
capturerWindow.close();
const result = { id, name };
let options: Streams = { video: sources[0] };
switch (process.platform) {
case "win32":
case "linux":
options = { video: result };
if (audio) options = { video: result, audio: getConfig("audio") };
callback(options);
break;
default:
callback({ video: result });
}
});
capturerWindow.on("closed", () => {
if (!isDone) callback({});
});
void capturerWindow.loadFile(path.join(import.meta.dirname, "html", "picker.html"));
capturerWindow.webContents.send("getSources", sources);
}
},
{ useSystemPicker: true },
Expand Down

0 comments on commit f3b7020

Please sign in to comment.