Skip to content

Commit

Permalink
feat: add WebContentsView since BrowserView is deprecated.
Browse files Browse the repository at this point in the history
Add new WebContentsView class as BrowserView is deprecated.
Ref: electron#198
  • Loading branch information
ductridev committed Oct 14, 2024
1 parent 9da4a9c commit 4927fe1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Electron from 'electron';
import { WebContentsView } from './src/main/index';

export {
ClientRequest,
Expand Down Expand Up @@ -57,6 +58,9 @@ export var Tray: typeof Electron.Tray;
export var webContents: typeof Electron.webContents;
export var webFrameMain: typeof Electron.webFrameMain;

// Taken from `RemoteMainInterface` but WebContentsView is only available in Electron >= 29.0.0
export { WebContentsView };

// Taken from `Remote`
export function getCurrentWebContents(): Electron.WebContents;
export function getCurrentWindow(): Electron.BrowserWindow;
Expand Down
11 changes: 11 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function isVersionGreaterOrEqual(requiredVersion: string, currentVersion: string): boolean {
const required = requiredVersion.split('.').map(Number);
const current = currentVersion.split('.').map(Number);

for (let i = 0; i < required.length; i++) {
if (current[i] > required[i]) return true;
if (current[i] < required[i]) return false;
}

return true;
}
12 changes: 12 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
export { initialize, isInitialized, enable } from "./server";

import { isVersionGreaterOrEqual } from '../common/utils';

const electronVersion = process.versions.electron;

let WebContentsView: typeof Electron.WebContentsView | undefined;

if (isVersionGreaterOrEqual('29.0.0', electronVersion)) {
WebContentsView = Electron.WebContentsView;
}

export { WebContentsView };

0 comments on commit 4927fe1

Please sign in to comment.