/
browser-window.ts
162 lines (127 loc) · 4.68 KB
/
browser-window.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { BaseWindow, WebContents, Event, BrowserView, TouchBar } from 'electron/main';
import type { BrowserWindow as BWT } from 'electron/main';
const { BrowserWindow } = process._linkedBinding('electron_browser_window') as { BrowserWindow: typeof BWT };
Object.setPrototypeOf(BrowserWindow.prototype, BaseWindow.prototype);
BrowserWindow.prototype._init = function (this: BWT) {
// Call parent class's _init.
BaseWindow.prototype._init.call(this);
// Avoid recursive require.
const { app } = require('electron');
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.
this.on('blur', (event: Event) => {
app.emit('browser-window-blur', event, this);
});
this.on('focus', (event: Event) => {
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);
}
// Notify the creation of the window.
const event = process._linkedBinding('electron_browser_event').createEmpty();
app.emit('browser-window-created', event, this);
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) => {
const win = BaseWindow.fromId(id);
return isBrowserWindow(win) ? win as any as BWT : null;
};
BrowserWindow.getAllWindows = () => {
return BaseWindow.getAllWindows().filter(isBrowserWindow) as any[] as BWT[];
};
BrowserWindow.getFocusedWindow = () => {
for (const window of BrowserWindow.getAllWindows()) {
if (window.isFocused() || window.isDevToolsFocused()) return window;
}
return null;
};
BrowserWindow.fromWebContents = (webContents: WebContents) => {
return webContents.getOwnerBrowserWindow();
};
BrowserWindow.fromBrowserView = (browserView: BrowserView) => {
return BrowserWindow.fromWebContents(browserView.webContents);
};
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);
};
module.exports = BrowserWindow;