electron/lib/browser/api/browser-window.js

166 lines
4.6 KiB
JavaScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
'use strict';
2020-03-20 20:28:31 +00:00
const electron = require('electron');
const { TopLevelWindow, deprecate } = electron;
2020-03-20 20:28:31 +00:00
const { BrowserWindow } = process.electronBinding('window');
2016-01-12 02:40:23 +00:00
2020-03-20 20:28:31 +00:00
Object.setPrototypeOf(BrowserWindow.prototype, TopLevelWindow.prototype);
2016-01-12 02:40:23 +00:00
BrowserWindow.prototype._init = function () {
// Call parent class's _init.
2020-03-20 20:28:31 +00:00
TopLevelWindow.prototype._init.call(this);
2016-01-12 02:40:23 +00:00
// Avoid recursive require.
2020-03-20 20:28:31 +00:00
const { app } = electron;
2016-01-12 02:40:23 +00:00
2020-03-20 20:28:31 +00:00
const nativeSetBounds = this.setBounds;
this.setBounds = (bounds, ...opts) => {
bounds = {
...this.getBounds(),
...bounds
2020-03-20 20:28:31 +00:00
};
nativeSetBounds.call(this, bounds, ...opts);
};
// Sometimes the webContents doesn't get focus when window is shown, so we
// have to force focusing on webContents in this case. The safest way is to
// focus it when we first start to load URL, if we do it earlier it won't
// have effect, if we do it later we might move focus in the page.
//
2016-06-18 13:26:26 +00:00
// Though this hack is only needed on macOS when the app is launched from
// Finder, we still do it on all platforms in case of other bugs we don't
// know.
this.webContents.once('load-url', function () {
2020-03-20 20:28:31 +00:00
this.focus();
});
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Redirect focus/blur event to app instance too.
this.on('blur', (event) => {
2020-03-20 20:28:31 +00:00
app.emit('browser-window-blur', event, this);
});
this.on('focus', (event) => {
2020-03-20 20:28:31 +00:00
app.emit('browser-window-focus', event, this);
});
2016-01-12 02:40:23 +00:00
// Subscribe to visibilityState changes and pass to renderer process.
2020-03-20 20:28:31 +00:00
let isVisible = this.isVisible() && !this.isMinimized();
const visibilityChanged = () => {
2020-03-20 20:28:31 +00:00
const newState = this.isVisible() && !this.isMinimized();
2016-04-13 14:10:31 +00:00
if (isVisible !== newState) {
2020-03-20 20:28:31 +00:00
isVisible = newState;
const visibilityState = isVisible ? 'visible' : 'hidden';
this.webContents.emit('-window-visibility-change', visibilityState);
}
2020-03-20 20:28:31 +00:00
};
2020-03-20 20:28:31 +00:00
const visibilityEvents = ['show', 'hide', 'minimize', 'maximize', 'restore'];
for (const event of visibilityEvents) {
2020-03-20 20:28:31 +00:00
this.on(event, visibilityChanged);
}
2016-01-14 18:35:29 +00:00
// Notify the creation of the window.
2020-03-20 20:28:31 +00:00
const event = process.electronBinding('event').createEmpty();
app.emit('browser-window-created', event, this);
2016-01-12 02:40:23 +00:00
2016-03-11 19:05:48 +00:00
Object.defineProperty(this, 'devToolsWebContents', {
2016-01-12 02:40:23 +00:00
enumerable: true,
configurable: false,
get () {
2020-03-20 20:28:31 +00:00
return this.webContents.devToolsWebContents;
2016-01-12 02:40:23 +00:00
}
2020-03-20 20:28:31 +00:00
});
};
2016-01-12 02:40:23 +00:00
const isBrowserWindow = (win) => {
2020-03-20 20:28:31 +00:00
return win && win.constructor.name === 'BrowserWindow';
};
BrowserWindow.fromId = (id) => {
2020-03-20 20:28:31 +00:00
const win = TopLevelWindow.fromId(id);
return isBrowserWindow(win) ? win : null;
};
BrowserWindow.getAllWindows = () => {
2020-03-20 20:28:31 +00:00
return TopLevelWindow.getAllWindows().filter(isBrowserWindow);
};
BrowserWindow.getFocusedWindow = () => {
for (const window of BrowserWindow.getAllWindows()) {
2020-03-20 20:28:31 +00:00
if (window.isFocused() || window.isDevToolsFocused()) return window;
2016-01-12 02:40:23 +00:00
}
2020-03-20 20:28:31 +00:00
return null;
};
2016-01-12 02:40:23 +00:00
BrowserWindow.fromWebContents = (webContents) => {
2016-12-06 00:18:53 +00:00
for (const window of BrowserWindow.getAllWindows()) {
2020-03-20 20:28:31 +00:00
if (window.webContents && window.webContents.equal(webContents)) return window;
2016-01-12 02:40:23 +00:00
}
2020-03-20 20:28:31 +00:00
return null;
};
2016-01-12 02:40:23 +00:00
BrowserWindow.fromBrowserView = (browserView) => {
for (const window of BrowserWindow.getAllWindows()) {
2020-03-20 20:28:31 +00:00
if (window.getBrowserView() === browserView) return window;
}
2017-11-22 23:48:11 +00:00
2020-03-20 20:28:31 +00:00
return null;
};
2016-01-14 18:35:29 +00:00
// Helpers.
Object.assign(BrowserWindow.prototype, {
loadURL (...args) {
2020-03-20 20:28:31 +00:00
return this.webContents.loadURL(...args);
},
getURL (...args) {
2020-03-20 20:28:31 +00:00
return this.webContents.getURL();
},
loadFile (...args) {
2020-03-20 20:28:31 +00:00
return this.webContents.loadFile(...args);
},
reload (...args) {
2020-03-20 20:28:31 +00:00
return this.webContents.reload(...args);
},
send (...args) {
2020-03-20 20:28:31 +00:00
return this.webContents.send(...args);
},
openDevTools (...args) {
2020-03-20 20:28:31 +00:00
return this.webContents.openDevTools(...args);
},
closeDevTools () {
2020-03-20 20:28:31 +00:00
return this.webContents.closeDevTools();
},
isDevToolsOpened () {
2020-03-20 20:28:31 +00:00
return this.webContents.isDevToolsOpened();
},
isDevToolsFocused () {
2020-03-20 20:28:31 +00:00
return this.webContents.isDevToolsFocused();
},
toggleDevTools () {
2020-03-20 20:28:31 +00:00
return this.webContents.toggleDevTools();
},
inspectElement (...args) {
2020-03-20 20:28:31 +00:00
return this.webContents.inspectElement(...args);
},
inspectSharedWorker () {
2020-03-20 20:28:31 +00:00
return this.webContents.inspectSharedWorker();
},
inspectServiceWorker () {
2020-03-20 20:28:31 +00:00
return this.webContents.inspectServiceWorker();
},
showDefinitionForSelection () {
2020-03-20 20:28:31 +00:00
return this.webContents.showDefinitionForSelection();
2016-07-05 22:43:57 +00:00
},
capturePage (...args) {
2020-03-20 20:28:31 +00:00
return this.webContents.capturePage(...args);
2017-03-01 18:55:28 +00:00
},
setTouchBar (touchBar) {
2020-03-20 20:28:31 +00:00
electron.TouchBar._setOnWindow(touchBar, this);
},
setBackgroundThrottling (allowed) {
2020-03-20 20:28:31 +00:00
this.webContents.setBackgroundThrottling(allowed);
2017-03-01 18:55:28 +00:00
}
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
module.exports = BrowserWindow;