Skip to content

Commit

Permalink
chore: add warnings for deprecated BrowserWindow methods
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Sep 13, 2022
1 parent 12a7d7e commit 89f9516
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 83 deletions.
18 changes: 18 additions & 0 deletions docs/breaking-changes.md
Expand Up @@ -14,6 +14,24 @@ This document uses the following convention to categorize breaking changes:

## Planned Breaking API Changes (22.0)

### Deprecated: `BrowserWindow` APIs

The following APIs have been deprecated:

* `browserWindow.loadURL`
* `browserWindow.loadFile`
* `browserWindow.reload`
* `browserWindow.showDefinitionForSelection`
* `browserWindow.capturePage`

Use the webContents APIs instead:

* `browserWindow.webContents.loadURL`
* `browserWindow.webContents.loadFile`
* `browserWindow.webContents.reload`
* `browserWindow.webContents.showDefinitionForSelection`
* `browserWindow.webContents.capturePage`

### Removed: WebContents `new-window` event

The `new-window` event of WebContents has been removed. It is replaced by [`webContents.setWindowOpenHandler()`](api/web-contents.md#contentssetwindowopenhandlerhandler).
Expand Down
94 changes: 27 additions & 67 deletions lib/browser/api/browser-window.ts
@@ -1,4 +1,5 @@
import { BaseWindow, WebContents, Event, BrowserView, TouchBar } from 'electron/main';
import * as deprecate from '@electron/internal/common/deprecate';
import type { BrowserWindow as BWT } from 'electron/main';
const { BrowserWindow } = process._linkedBinding('electron_browser_window') as { BrowserWindow: typeof BWT };

Expand Down Expand Up @@ -93,72 +94,31 @@ BrowserWindow.prototype.setTouchBar = function (touchBar) {

// Forwarded to webContents:

BrowserWindow.prototype.loadURL = function (...args) {
return this.webContents.loadURL(...args);
};

BrowserWindow.prototype.getURL = function () {
return this.webContents.getURL();
};

BrowserWindow.prototype.loadFile = function (...args) {
return this.webContents.loadFile(...args);
};

BrowserWindow.prototype.reload = function (...args) {
return this.webContents.reload(...args);
};

BrowserWindow.prototype.send = function (...args) {
return this.webContents.send(...args);
};

BrowserWindow.prototype.openDevTools = function (...args) {
return this.webContents.openDevTools(...args);
};

BrowserWindow.prototype.closeDevTools = function () {
return this.webContents.closeDevTools();
};

BrowserWindow.prototype.isDevToolsOpened = function () {
return this.webContents.isDevToolsOpened();
};

BrowserWindow.prototype.isDevToolsFocused = function () {
return this.webContents.isDevToolsFocused();
};

BrowserWindow.prototype.toggleDevTools = function () {
return this.webContents.toggleDevTools();
};

BrowserWindow.prototype.inspectElement = function (...args) {
return this.webContents.inspectElement(...args);
};

BrowserWindow.prototype.inspectSharedWorker = function () {
return this.webContents.inspectSharedWorker();
};

BrowserWindow.prototype.inspectServiceWorker = function () {
return this.webContents.inspectServiceWorker();
};

BrowserWindow.prototype.showDefinitionForSelection = function () {
return this.webContents.showDefinitionForSelection();
};

BrowserWindow.prototype.capturePage = function (...args) {
return this.webContents.capturePage(...args);
};

BrowserWindow.prototype.getBackgroundThrottling = function () {
return this.webContents.getBackgroundThrottling();
};

BrowserWindow.prototype.setBackgroundThrottling = function (allowed: boolean) {
return this.webContents.setBackgroundThrottling(allowed);
};
const deprecatedMethods = [
'loadURL',
'getURL',
'loadFile',
'reload',
'send',
'openDevTools',
'closeDevTools',
'isDevToolsOpened',
'isDevToolsFocused',
'toggleDevTools',
'inspectElement',
'inspectSharedWorker',
'inspectServiceWorker',
'showDefinitionForSelection',
'capturePage',
'getBackgroundThrottling',
'setBackgroundThrottling'
];

for (const method of deprecatedMethods as (keyof Electron.BrowserWindow)[]) {
(BrowserWindow.prototype[method] as any) = function (this: Electron.BrowserWindow, ...args: any[]) {
deprecate.warn(`browserWindow.${method}`, `browserWindow.webContents.${method}`);
return (this.webContents[method as keyof Electron.WebContents] as any)(...args);
};
}

module.exports = BrowserWindow;
16 changes: 0 additions & 16 deletions typings/internal-electron.d.ts
Expand Up @@ -163,22 +163,6 @@ declare namespace Electron {
constructor(options: BrowserWindowConstructorOptions)
}

// Deprecated / undocumented BrowserWindow methods
interface BrowserWindow {
getURL(): string;
send(channel: string, ...args: any[]): void;
openDevTools(options?: Electron.OpenDevToolsOptions): void;
closeDevTools(): void;
isDevToolsOpened(): void;
isDevToolsFocused(): void;
toggleDevTools(): void;
inspectElement(x: number, y: number): void;
inspectSharedWorker(): void;
inspectServiceWorker(): void;
getBackgroundThrottling(): void;
setBackgroundThrottling(allowed: boolean): void;
}

namespace Main {
class BaseWindow extends Electron.BaseWindow {}
class View extends Electron.View {}
Expand Down

0 comments on commit 89f9516

Please sign in to comment.