@@ -5,7 +5,8 @@ const path = require('path')
5
5
const {
6
6
app, // Module to control application life.
7
7
protocol, // Module to control protocol handling
8
- BrowserWindow, // Module to create native browser window.
8
+ BaseWindow, // Module to create native browser window.
9
+ BrowserWindow,
9
10
webContents,
10
11
session,
11
12
ipcMain : ipc ,
@@ -14,7 +15,8 @@ const {
14
15
dialog,
15
16
nativeTheme,
16
17
shell,
17
- net
18
+ net,
19
+ WebContentsView
18
20
} = electron
19
21
20
22
crashReporter . start ( {
@@ -98,24 +100,24 @@ function sendIPCToWindow (window, action, data) {
98
100
return
99
101
}
100
102
101
- if ( window && window . webContents && window . webContents . isLoadingMainFrame ( ) ) {
103
+ if ( window && getWindowWebContents ( window ) . isLoadingMainFrame ( ) ) {
102
104
// immediately after a did-finish-load event, isLoading can still be true,
103
105
// so wait a bit to confirm that the page is really loading
104
106
setTimeout ( function ( ) {
105
- if ( window . webContents . isLoadingMainFrame ( ) ) {
106
- window . webContents . once ( 'did-finish-load' , function ( ) {
107
- window . webContents . send ( action , data || { } )
107
+ if ( getWindowWebContents ( window ) . isLoadingMainFrame ( ) ) {
108
+ getWindowWebContents ( window ) . once ( 'did-finish-load' , function ( ) {
109
+ getWindowWebContents ( window ) . send ( action , data || { } )
108
110
} )
109
111
} else {
110
- window . webContents . send ( action , data || { } )
112
+ getWindowWebContents ( window ) . send ( action , data || { } )
111
113
}
112
114
} , 0 )
113
115
} else if ( window ) {
114
- window . webContents . send ( action , data || { } )
116
+ getWindowWebContents ( window ) . send ( action , data || { } )
115
117
} else {
116
118
var window = createWindow ( )
117
- window . webContents . once ( 'did-finish-load' , function ( ) {
118
- window . webContents . send ( action , data || { } )
119
+ getWindowWebContents ( window ) . once ( 'did-finish-load' , function ( ) {
120
+ getWindowWebContents ( window ) . send ( action , data || { } )
119
121
} )
120
122
}
121
123
}
@@ -188,7 +190,7 @@ function createWindow (customArgs = {}) {
188
190
}
189
191
190
192
function createWindowWithBounds ( bounds , customArgs ) {
191
- const newWin = new BrowserWindow ( {
193
+ const newWin = new BaseWindow ( {
192
194
width : bounds . width ,
193
195
height : bounds . height ,
194
196
x : bounds . x ,
@@ -201,6 +203,15 @@ function createWindowWithBounds (bounds, customArgs) {
201
203
frame : settings . get ( 'useSeparateTitlebar' ) ,
202
204
alwaysOnTop : settings . get ( 'windowAlwaysOnTop' ) ,
203
205
backgroundColor : '#fff' , // the value of this is ignored, but setting it seems to work around https://github.com/electron/electron/issues/10559
206
+ } )
207
+
208
+ // windows and linux always use a menu button in the upper-left corner instead
209
+ // if frame: false is set, this won't have any effect, but it does apply on Linux if "use separate titlebar" is enabled
210
+ if ( process . platform !== 'darwin' ) {
211
+ newWin . setMenuBarVisibility ( false )
212
+ }
213
+
214
+ const mainView = new WebContentsView ( {
204
215
webPreferences : {
205
216
nodeIntegration : true ,
206
217
contextIsolation : false ,
@@ -217,20 +228,19 @@ function createWindowWithBounds (bounds, customArgs) {
217
228
]
218
229
}
219
230
} )
231
+ mainView . webContents . loadURL ( browserPage )
232
+ mainView . setBounds ( { x : 0 , y : 0 , width : bounds . width , height : bounds . height } )
233
+ newWin . contentView . addChildView ( mainView )
220
234
221
- // windows and linux always use a menu button in the upper-left corner instead
222
- // if frame: false is set, this won't have any effect, but it does apply on Linux if "use separate titlebar" is enabled
223
- if ( process . platform !== 'darwin' ) {
224
- newWin . setMenuBarVisibility ( false )
225
- }
226
-
227
- // and load the index.html of the app.
228
- newWin . loadURL ( browserPage )
235
+ newWin . on ( 'resize' , function ( ) {
236
+ const winBounds = newWin . getBounds ( )
237
+ mainView . setBounds ( { x : 0 , y : 0 , width : winBounds . width , height : winBounds . height } )
238
+ } )
229
239
230
240
if ( bounds . maximized ) {
231
241
newWin . maximize ( )
232
242
233
- newWin . webContents . once ( 'did-finish-load' , function ( ) {
243
+ mainView . webContents . once ( 'did-finish-load' , function ( ) {
234
244
sendIPCToWindow ( newWin , 'maximize' )
235
245
} )
236
246
}
@@ -269,7 +279,7 @@ function createWindowWithBounds (bounds, customArgs) {
269
279
270
280
newWin . on ( 'blur' , function ( ) {
271
281
// if the devtools for this window are focused, this check will be false, and we keep the focused class on the window
272
- if ( BrowserWindow . getFocusedWindow ( ) !== newWin ) {
282
+ if ( BaseWindow . getFocusedWindow ( ) !== newWin ) {
273
283
sendIPCToWindow ( newWin , 'blur' )
274
284
}
275
285
} )
@@ -311,7 +321,7 @@ function createWindowWithBounds (bounds, customArgs) {
311
321
}
312
322
313
323
// prevent remote pages from being loaded using drag-and-drop, since they would have node access
314
- newWin . webContents . on ( 'will-navigate' , function ( e , url ) {
324
+ mainView . webContents . on ( 'will-navigate' , function ( e , url ) {
315
325
if ( url !== browserPage ) {
316
326
e . preventDefault ( )
317
327
}
@@ -340,7 +350,7 @@ app.on('ready', function () {
340
350
341
351
const newWin = createWindow ( )
342
352
343
- newWin . webContents . on ( 'did-finish-load' , function ( ) {
353
+ getWindowWebContents ( newWin ) . on ( 'did-finish-load' , function ( ) {
344
354
// if a URL was passed as a command line argument (probably because Min is set as the default browser on Linux), open it.
345
355
handleCommandLineArguments ( process . argv )
346
356
@@ -403,8 +413,7 @@ app.on('activate', function (/* e, hasVisibleWindows */) {
403
413
} )
404
414
405
415
ipc . on ( 'focusMainWebContents' , function ( ) {
406
- //TODO fix
407
- windows . getCurrent ( ) . webContents . focus ( )
416
+ getWindowWebContents ( windows . getCurrent ( ) ) . focus ( )
408
417
} )
409
418
410
419
ipc . on ( 'showSecondaryMenu' , function ( event , data ) {
@@ -430,25 +439,30 @@ ipc.on('quit', function () {
430
439
} )
431
440
432
441
ipc . on ( 'tab-state-change' , function ( e , events ) {
442
+ const sourceWindowId = windows . windowFromContents ( e . sender ) ?. id
443
+ if ( ! sourceWindowId ) {
444
+ console . warn ( 'warning: received tab state update from window after destruction, ignoring' )
445
+ return
446
+ }
433
447
windows . getAll ( ) . forEach ( function ( window ) {
434
- if ( window . webContents . id !== e . sender . id ) {
435
- window . webContents . send ( 'tab-state-change-receive' , {
436
- sourceWindowId : windows . windowFromContents ( e . sender ) . id ,
448
+ if ( getWindowWebContents ( window ) . id !== e . sender . id ) {
449
+ getWindowWebContents ( window ) . send ( 'tab-state-change-receive' , {
450
+ sourceWindowId,
437
451
events
438
452
} )
439
453
}
440
454
} )
441
455
} )
442
456
443
457
ipc . on ( 'request-tab-state' , function ( e ) {
444
- const otherWindow = windows . getAll ( ) . find ( w => w . webContents . id !== e . sender . id )
458
+ const otherWindow = windows . getAll ( ) . find ( w => getWindowWebContents ( w ) . id !== e . sender . id )
445
459
if ( ! otherWindow ) {
446
460
throw new Error ( 'secondary window doesn\'t exist as source for tab state' )
447
461
}
448
462
ipc . once ( 'return-tab-state' , function ( e2 , data ) {
449
463
e . returnValue = data
450
464
} )
451
- otherWindow . webContents . send ( 'read-tab-state' )
465
+ getWindowWebContents ( otherWindow ) . send ( 'read-tab-state' )
452
466
} )
453
467
454
468
/* places service */
@@ -472,4 +486,8 @@ app.once('ready', function() {
472
486
473
487
ipc . on ( 'places-connect' , function ( e ) {
474
488
placesWindow . webContents . postMessage ( 'places-connect' , null , e . ports )
475
- } )
489
+ } )
490
+
491
+ function getWindowWebContents ( win ) {
492
+ return win . getContentView ( ) . children [ 0 ] . webContents
493
+ }
0 commit comments