2023-12-13 21:01:03 +00:00
|
|
|
import { BaseWindow, WebContents, TouchBar, BrowserView } from 'electron/main';
|
2020-07-13 16:58:49 +00:00
|
|
|
import type { BrowserWindow as BWT } from 'electron/main';
|
2020-06-29 01:22:55 +00:00
|
|
|
const { BrowserWindow } = process._linkedBinding('electron_browser_window') as { BrowserWindow: typeof BWT };
|
|
|
|
|
2020-06-29 07:06:20 +00:00
|
|
|
Object.setPrototypeOf(BrowserWindow.prototype, BaseWindow.prototype);
|
2020-06-29 01:22:55 +00:00
|
|
|
|
2020-10-08 01:01:23 +00:00
|
|
|
BrowserWindow.prototype._init = function (this: BWT) {
|
2020-06-29 01:22:55 +00:00
|
|
|
// Call parent class's _init.
|
2023-12-13 21:01:03 +00:00
|
|
|
(BaseWindow.prototype as any)._init.call(this);
|
2020-06-29 01:22:55 +00:00
|
|
|
|
|
|
|
// Avoid recursive require.
|
|
|
|
const { app } = require('electron');
|
|
|
|
|
2023-11-13 03:51:56 +00:00
|
|
|
// Set ID at construction time so it's accessible after
|
2023-05-16 07:29:00 +00:00
|
|
|
// underlying window destruction.
|
|
|
|
const id = this.id;
|
|
|
|
Object.defineProperty(this, 'id', {
|
|
|
|
value: id,
|
|
|
|
writable: false
|
|
|
|
});
|
|
|
|
|
2020-06-29 01:22:55 +00:00
|
|
|
const nativeSetBounds = this.setBounds;
|
|
|
|
this.setBounds = (bounds, ...opts) => {
|
|
|
|
bounds = {
|
|
|
|
...this.getBounds(),
|
|
|
|
...bounds
|
|
|
|
};
|
|
|
|
nativeSetBounds.call(this, bounds, ...opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Redirect focus/blur event to app instance too.
|
2023-02-13 21:39:18 +00:00
|
|
|
this.on('blur', (event: Electron.Event) => {
|
2020-06-29 01:22:55 +00:00
|
|
|
app.emit('browser-window-blur', event, this);
|
|
|
|
});
|
2023-02-13 21:39:18 +00:00
|
|
|
this.on('focus', (event: Electron.Event) => {
|
2020-06-29 01:22:55 +00:00
|
|
|
app.emit('browser-window-focus', event, this);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Subscribe to visibilityState changes and pass to renderer process.
|
|
|
|
let isVisible = this.isVisible() && !this.isMinimized();
|
|
|
|
const visibilityChanged = () => {
|
|
|
|
const newState = this.isVisible() && !this.isMinimized();
|
|
|
|
if (isVisible !== newState) {
|
|
|
|
isVisible = newState;
|
|
|
|
const visibilityState = isVisible ? 'visible' : 'hidden';
|
|
|
|
this.webContents.emit('-window-visibility-change', visibilityState);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const visibilityEvents = ['show', 'hide', 'minimize', 'maximize', 'restore'];
|
|
|
|
for (const event of visibilityEvents) {
|
|
|
|
this.on(event as any, visibilityChanged);
|
|
|
|
}
|
|
|
|
|
2023-12-13 21:01:03 +00:00
|
|
|
this._browserViews = [];
|
|
|
|
|
2024-06-05 07:58:38 +00:00
|
|
|
this.on('closed', () => {
|
2023-12-13 21:01:03 +00:00
|
|
|
this._browserViews.forEach(b => b.webContents?.close({ waitForBeforeUnload: true }));
|
|
|
|
});
|
|
|
|
|
2020-06-29 01:22:55 +00:00
|
|
|
// Notify the creation of the window.
|
2023-02-13 21:39:18 +00:00
|
|
|
app.emit('browser-window-created', { preventDefault () {} }, this);
|
2020-06-29 01:22:55 +00:00
|
|
|
|
|
|
|
Object.defineProperty(this, 'devToolsWebContents', {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: false,
|
|
|
|
get () {
|
|
|
|
return this.webContents.devToolsWebContents;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const isBrowserWindow = (win: any) => {
|
|
|
|
return win && win.constructor.name === 'BrowserWindow';
|
|
|
|
};
|
|
|
|
|
|
|
|
BrowserWindow.fromId = (id: number) => {
|
2020-06-29 07:06:20 +00:00
|
|
|
const win = BaseWindow.fromId(id);
|
2020-06-29 01:22:55 +00:00
|
|
|
return isBrowserWindow(win) ? win as any as BWT : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
BrowserWindow.getAllWindows = () => {
|
2020-06-29 07:06:20 +00:00
|
|
|
return BaseWindow.getAllWindows().filter(isBrowserWindow) as any[] as BWT[];
|
2020-06-29 01:22:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BrowserWindow.getFocusedWindow = () => {
|
|
|
|
for (const window of BrowserWindow.getAllWindows()) {
|
2022-09-12 21:46:05 +00:00
|
|
|
if (!window.isDestroyed() && window.webContents && !window.webContents.isDestroyed()) {
|
|
|
|
if (window.isFocused() || window.webContents.isDevToolsFocused()) return window;
|
2022-03-29 16:22:58 +00:00
|
|
|
}
|
2020-06-29 01:22:55 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
BrowserWindow.fromWebContents = (webContents: WebContents) => {
|
2020-09-14 17:36:54 +00:00
|
|
|
return webContents.getOwnerBrowserWindow();
|
2020-06-29 01:22:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BrowserWindow.fromBrowserView = (browserView: BrowserView) => {
|
2020-11-17 19:12:02 +00:00
|
|
|
return BrowserWindow.fromWebContents(browserView.webContents);
|
2020-06-29 01:22:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
BrowserWindow.prototype.setTouchBar = function (touchBar) {
|
|
|
|
(TouchBar as any)._setOnWindow(touchBar, this);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
};
|
|
|
|
|
2023-12-13 21:01:03 +00:00
|
|
|
BrowserWindow.prototype.addBrowserView = function (browserView: BrowserView) {
|
|
|
|
if (browserView.ownerWindow) { browserView.ownerWindow.removeBrowserView(browserView); }
|
|
|
|
this.contentView.addChildView(browserView.webContentsView);
|
|
|
|
browserView.ownerWindow = this;
|
|
|
|
browserView.webContents._setOwnerWindow(this);
|
|
|
|
this._browserViews.push(browserView);
|
|
|
|
};
|
|
|
|
|
|
|
|
BrowserWindow.prototype.setBrowserView = function (browserView: BrowserView) {
|
|
|
|
this._browserViews.forEach(bv => {
|
|
|
|
this.removeBrowserView(bv);
|
|
|
|
});
|
|
|
|
if (browserView) { this.addBrowserView(browserView); }
|
|
|
|
};
|
|
|
|
|
|
|
|
BrowserWindow.prototype.removeBrowserView = function (browserView: BrowserView) {
|
|
|
|
const idx = this._browserViews.indexOf(browserView);
|
|
|
|
if (idx >= 0) {
|
|
|
|
this.contentView.removeChildView(browserView.webContentsView);
|
|
|
|
browserView.ownerWindow = null;
|
|
|
|
this._browserViews.splice(idx, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
BrowserWindow.prototype.getBrowserView = function () {
|
2024-07-08 10:13:53 +00:00
|
|
|
if (this._browserViews.length > 1) {
|
|
|
|
throw new Error('This BrowserWindow has multiple BrowserViews - use getBrowserViews() instead');
|
|
|
|
}
|
2023-12-13 21:01:03 +00:00
|
|
|
return this._browserViews[0] ?? null;
|
|
|
|
};
|
|
|
|
|
|
|
|
BrowserWindow.prototype.getBrowserViews = function () {
|
|
|
|
return [...this._browserViews];
|
|
|
|
};
|
|
|
|
|
|
|
|
BrowserWindow.prototype.setTopBrowserView = function (browserView: BrowserView) {
|
2024-07-08 10:13:53 +00:00
|
|
|
if (browserView.ownerWindow !== this) {
|
|
|
|
throw new Error('Given BrowserView is not attached to the window');
|
|
|
|
}
|
2024-07-01 09:36:04 +00:00
|
|
|
const idx = this._browserViews.indexOf(browserView);
|
|
|
|
if (idx >= 0) {
|
|
|
|
this.contentView.addChildView(browserView.webContentsView);
|
|
|
|
this._browserViews.splice(idx, 1);
|
|
|
|
this._browserViews.push(browserView);
|
|
|
|
}
|
2023-12-13 21:01:03 +00:00
|
|
|
};
|
|
|
|
|
2020-06-29 01:22:55 +00:00
|
|
|
module.exports = BrowserWindow;
|