Skip to content

Commit

Permalink
Update code for newer Electorn versions.
Browse files Browse the repository at this point in the history
• Switch to the new `WebContentsView` API in places `BrowserView` API
  was used.
• Add support for `speaker-selection` permission (default: deny).
  • Loading branch information
SpacingBat3 committed Jul 6, 2024
1 parent c39e12a commit 986cc7a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions sources/code/main/modules/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const defaultAppConfig = Object.freeze({
"notifications": null as boolean|null,
"display-capture": true,
"background-sync": false,
"speaker-selection": false
},
},
advanced: {
Expand Down
16 changes: 9 additions & 7 deletions sources/code/main/windows/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
net,
ipcMain,
desktopCapturer,
BrowserView,
WebContentsView,
systemPreferences
} from "electron/main";
import * as getMenu from "../modules/menu";
Expand Down Expand Up @@ -160,8 +160,9 @@ export default function createMainWindow(...flags:MainWindowFlags): BrowserWindo
})
.reduce((previousValue,currentValue) => (previousValue??false) && (currentValue??false))??true;
};
type handlerParamType<H extends "request"|"check", T extends number> = Parameters<Exclude<Parameters<Electron.Session[`setPermission${Capitalize<H>}Handler`]>[0],null>>[T];
/** Common handler for */
const permissionHandler = (type:"request"|"check",webContentsUrl:string, permission:string, details:Electron.PermissionRequest|Electron.MediaAccessPermissionRequest|Electron.PermissionCheckHandlerHandlerDetails):boolean|null => {
const permissionHandler = <T extends "request"|"check">(type:T,webContentsUrl:string, permission:string, details:handlerParamType<T,3>) => {
// Verify URL address of the permissions.
try {
const webContents = new URL(webContentsUrl);
Expand Down Expand Up @@ -199,6 +200,7 @@ export default function createMainWindow(...flags:MainWindowFlags): BrowserWindo
case "notifications":
case "fullscreen":
case "background-sync":
case "speaker-selection":
return appConfig.value.settings.privacy.permissions[permission]??false;
default:
return null;
Expand Down Expand Up @@ -266,7 +268,7 @@ export default function createMainWindow(...flags:MainWindowFlags): BrowserWindo
if(appConfig.value.settings.privacy.permissions[permission] === null)
promises.push(permissionDialog(permission));
else
promises.push(Promise.resolve(appConfig.value.settings.privacy.permissions[permission] === true));
promises.push(Promise.resolve(appConfig.value.settings.privacy.permissions[permission]??false));
});
if(promises.length === 0)
Promise.all(promises)
Expand Down Expand Up @@ -457,7 +459,7 @@ export default function createMainWindow(...flags:MainWindowFlags): BrowserWindo
win.webContents.session.setDisplayMediaRequestHandler((req, callback) => {
const checkStatus =
// Handle lock and check for a presence of another BrowserView.
lock || win.getBrowserViews().length !== 0 ||
lock || win.contentView.children.length !== 0 ||
// Fail when client has denied the permission to the capturer.
(
appConfig.value.settings.privacy.permissions["display-capture"] &&
Expand Down Expand Up @@ -495,7 +497,7 @@ export default function createMainWindow(...flags:MainWindowFlags): BrowserWindo
thumbnail: nativeImage.createEmpty()
} satisfies Electron.DesktopCapturerSource]);
if(lock) {
const view = new BrowserView({
const view = new WebContentsView({
webPreferences: {
preload: resolve(app.getAppPath(), "app/code/renderer/preload/capturer.js"),
nodeIntegration: false,
Expand All @@ -521,14 +523,14 @@ export default function createMainWindow(...flags:MainWindowFlags): BrowserWindo
return appConfig.value.screenShareStore;
});
ipcMain.once("closeCapturerView", (_event,data:Electron.Streams) => {
win.removeBrowserView(view);
win.contentView.addChildView(view);
view.webContents.delete();
win.removeListener("resize", autoResize);
ipcMain.removeHandler("capturer-get-settings");
callback(data);
lock = false;
});
win.setBrowserView(view);
win.contentView.addChildView(view);
void view.webContents.loadFile(resolve(app.getAppPath(), "sources/assets/web/html/capturer.html"));
view.webContents.once("did-finish-load", () => {
autoResize();
Expand Down
3 changes: 2 additions & 1 deletion sources/translations/en/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"fullscreen": "Fullscreen",
"notifications": "Notifications",
"display-capture": "Desktop capture",
"background-sync": "Background synchronisation"
"background-sync": "Background synchronisation",
"speaker-selection": "Audio devices list"
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion sources/translations/pl/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"fullscreen": "Pełny ekran",
"notifications": "Powiadomienia",
"display-capture": "Nagrywanie ekranu",
"background-sync": "Synchronizacja w tle"
"background-sync": "Synchronizacja w tle",
"speaker-selection": "Lista urządzeń audio"
}
}
},
Expand Down

0 comments on commit 986cc7a

Please sign in to comment.