New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New-window event handler can return its own BrowserWindow instance #7407

Merged
merged 7 commits into from Oct 6, 2016

Conversation

Projects
None yet
5 participants
@MichaelVasseur

MichaelVasseur commented Sep 29, 2016

This pull request allows 'new-window' event handlers (running in the master process) to give back the BrowserWindow instance they created when the default behavior is prevented.

The suggested way to return this window is to set the new property newGuest with the newly created BrowserWindow. This may not be the best way to achieve this so others suggestions are welcome.

@deepak1556

This comment has been minimized.

Member

deepak1556 commented Sep 29, 2016

What scenario does this turn helpful ?

@MichaelVasseur

This comment has been minimized.

MichaelVasseur commented Sep 29, 2016

@deepak1556 In our application, we have a class that encapsulates BrowserWindow. For each new window, we must create one instance of our object and then return the encapsulated BrowserWindow.

@zcbenz

This comment has been minimized.

Contributor

zcbenz commented Oct 5, 2016

👍

@zcbenz

This comment has been minimized.

Contributor

zcbenz commented Oct 5, 2016

@MichaelVasseur I'm testing your change but the tests are failing, can you fix them?

Also notice that I just did a rebase of this branch from master branch, so you need to do a reset locally.

@MichaelVasseur

This comment has been minimized.

MichaelVasseur commented Oct 5, 2016

@zcbenz Ah ok, I did the rebase locally but didn't pushed yet because of the failing tests.

@zcbenz

This comment has been minimized.

Contributor

zcbenz commented Oct 6, 2016

Thanks!

@zcbenz zcbenz merged commit 5285b72 into electron:master Oct 6, 2016

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@dogancelik

This comment has been minimized.

dogancelik commented Jan 15, 2017

Can you show an example how to use this?
I want to get new window's WebContents
I think created window should be available in e.target, that would be awesome

@MichaelVasseur

This comment has been minimized.

MichaelVasseur commented Jan 28, 2017

@dogancelik , please see sample code below :

const myMainWindow = new Electron.BrowserWindow(...);
myMainWindow.webContents.on('new-window', (event) => {
   event['newGuest'] = new MyBrowserWindowClass();
   event.preventDefault();
});

Regarding using the target field, from my point of view it was better to use a specific field (newGuest) for this particular workflow.

Hope this helps !

Mike

@enlight

This comment has been minimized.

Contributor

enlight commented Feb 10, 2017

@MichaelVasseur The docs for this need to be improved. I'm still unclear on the purpose of this newGuest property, and how you actually use it. The example your provided shows how to set the property but what happens after? How do you access the object you've assigned to that property?

@MichaelVasseur

This comment has been minimized.

MichaelVasseur commented Feb 10, 2017

@enlight Actually the BrowserWindow you assign to newGuest will be used by Electron itself.
If you look at the code, you'll see that when Electron creates the BrowserWindow for you, it does somes specific tasks related to this object. If you create the BrowserWindow by yourself without setting the newGuest property, all these specific tasks won't be done and issues may occur.

@enlight

This comment has been minimized.

Contributor

enlight commented Feb 10, 2017

@MichaelVasseur OK, let me know if the following is correct and I'll update the docs:

If you call event.preventDefault() and manually create a new BrowserWindow instance then you must set event.newGuest to the new instance, failing to do so may result in unexpected behavior.

@MichaelVasseur

This comment has been minimized.

MichaelVasseur commented Feb 10, 2017

It's perfect !
Thank you @enlight

enlight added a commit that referenced this pull request Feb 10, 2017

[ci skip] Improve docs for `new-window` event
This should help clarify the purpose of `event.newGuest` as per the recent discussion in #7407.

@emmkimme emmkimme deleted the thomsonreuters:New_Window_Handler_Can_Create_And_Return_Window branch Nov 14, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment