Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add WebContentsView since BrowserView is deprecated from Electron 29.0.0. #201

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 './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
17 changes: 17 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as Electron from 'electron';
import { isVersionGreaterOrEqual } from './src/common/utils';

const electronVersion = process.versions.electron;

// Check if WebContentsView exists and if the version is >= 29.0.0
let WebContentsView_: any; // Use 'any' for dynamic checking

if (isVersionGreaterOrEqual('29.0.0', electronVersion) && 'WebContentsView' in Electron) {
WebContentsView_ = Electron.WebContentsView; // Assign if it exists
} else {
console.warn("WebContentsView is not available in this version of Electron.");
WebContentsView_ = undefined; // Explicitly set to undefined if not available
}

// Export the WebContentsView for use in other parts of your application
export const WebContentsView = WebContentsView_;
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;
}