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
First Navigation in Winform WebView2 Instance Steals Focus #862
Comments
i can give you my code maybe helping use winforms 4.6.2 framework |
public frmBrowser()
} mainform |
Did you also have issues with WebView2 stealing focus? And did your code solve your issues? |
Yes above framwork 4.6.2 had it but after going to 4.6.2 and changing the navigatie code i didn't have it any more |
I believe this behavior is by design, but I'll open a bug on our backlog to verify. Are you seeing this behavior in other instances, or only on first navigate when quickly making the app lose focus? Does @jarno9981's workaround work for you? |
This behavior is seen on first navigation of any WebView2 instance when quickly making the app lose focus, and the workaround of downgrading to .NET 4.6.2 did not work. |
Yes work around works for me |
@AidanVizzion Can you try running the app without the debugger attached? Do you still see the same behavior? We think it may be related to running under the VS debugger. In general, background apps shouldn't be able to force themselves to be foreground. |
@champnic Yes, we still see the same behavior when we build for Release and run the .exe. |
Yes, we were starting the .exe directly from Explorer and seeing the same behavior. We also see the same behavior when starting without debugging like you mentioned, |
Thanks for trying it out and confirming the issue is still there. We'll take a look on our end. |
Hi, I'm the dev looking into this issue. @AidanVizzion I'm unable to repro for .NET Framework 4.7.2 WinForms. I tried out your sample code but when I click on the button to navigate, then switch to another window, the navigation completes in the background and does not bring the WinForms window to the forefront. Could you provide the OS, SDK and runtime version you are seeing this on? |
I'm running on Windows 7. SDK version is 1.0.705.50. Runtime version version is 89.0.774.54. |
I'm seeing the same thing in a WPF application where when the WebView is navigated focus does not work properly in other control on the same form. It's really odd, but if I don't navigate the WebView I get normal behavior, but with the WV navigated I see this funky behavior:
It seems that the WV is not returning focus properly back to the Window after rendering. As said, visually it appears that focus is in the right place, but it's not behaving correctly when using keys. Pressing keys multiple times, or clicking on the Window provides proper control focus to the actually focused control. I've not found a good way to work around this - even if I delay focusing or focus multiple times in code using the Dispatcher, this same behavior persists. Only some active UI operation seems to get the focus to work correctly (like a delayed key sequence like ctrl, then Enter for example). Here's what this looks like: Notice that I press alt-c to open the form. Form opens and focus goes to the combo box (you can see it by way of the highlight. If I press Enter, focus goes to the control box, if I press Control Enter it goes to the control box. If I esc out of the control box, then the Combo gets focus and I can scroll through the options. IOW, the combo had focus all along but somehow due to the WV it's not actually acting like its focused. I've tried all sorts of stuff including delaying and dispatcher activation. Forcing focus to the form first, activating. None of it seems to help other than pressing keys that basically get eaten. If I type a letter like If I don't navigate the WebView, then focus works as expected when the form is activated. |
Starting yesterday this bug also affects For our multi-window application this effectively mean that focus is transfered between windows without any user interaction as soon as something updates content in WebView2. Only workaround sofar is to avoid calling reload when root window is not active... Currently this is a major issue for our customers that recieve automatic updates:
|
@pontusn Thanks for the update. What OS versions are you seeing this on? And presumable "starting yesterday" means with the new v100 runtime? If you use the older v99 runtime does the issue go away? (You can use Fixed Version runtime download for v99 to check) |
@champnic The problem appeared widely from "nowhere", hence it's some automatic upgrade. |
What version of the runtime are you currently seeing this on? |
WebView2 Runtime 100.0.1185.36 |
Are you able to see the issue if you use the Fixed Version 99? |
@champnic Repro steps:
99.*: 100.*: Currently we have mitigated this problem by disabling the Refresh-method in our API for inactive windows. |
facing the same problem. |
In our application the problem mostly occure due to legacy customizations that refresh background windows. Now we detect this and use an internal flag to delay refresh until activated. Snipplet in previous post is used to detect if top level parent is active. Unfortunately this does not handle refresh triggered by JS window.location.reload etc, but in our case that is not a problem. |
No details on why this is happening yet. We'll take a look soon though to try and get a fix. |
I have easier way of reproducing the issue.
@champnic can you share any timeline when this bug can be expected to be looked at ? |
Hi I'll take a look at this and try in the next couple days. Thanks for the repro information! |
So I was able to repro some things but not others. @dhohlin does your bug repro w just setting source? I found that this focus 'stealing' behavior can repro in WPF w non-webview controls. If you have 2 windows one on top of the other, setting the visibility of one of them from Hidden to Visible causes them to take the foreground. @pontusn are you working in a Win32 application? When I have 2 windows open w WebViews in them, I do not see one taking focus from the other when reloading. WebView will successfully reload in the background. It would be helpful if you could help come up with a min repro app. |
We are hosting WebView2 via Win32 as an drop-in replacement for IE/WebBrowser via a ATL-based ActiveX. Our main application is MFC-based. The problem is most evident when there are multiple top-level windows, each hosting several instances of WebView2. Any reload (from JS or via external API) will result in focus theft. Problem can also be seen for input controls hosted in same top-level as WebView2, however this is more subtle. Maybe I can PM details on how to use our desktop client to reproduce? Not sure if I can find time to build standalone app to repro... |
Sure that sounds reasonable, please PM me |
My tests were done in WinForms application. I click on button (handler as listed above) and after content is rendered by WebView button is not focused anymore. I.e. pressing arrow keys is handled by WebView itself. |
@dhohlin I think the focus behavior you are describing is an artifact of the WPF framework, not from WebView. Could you confirm if your bug behavior repros if you use a control other than WebView? |
@johna-ms it's definitely not any artifact of WPF. I am using windows forms and I can clearly see that wrong focus behaviour occurs with latest available evergreen WebView runtime (100.0.1185.50) and is not occurring if I use fixed runtime version 99.0.1150.55.
Once again issue is clearly happening in 100.0.1185.50 and not in 99.0.1150.55. |
Thanks for the update dhohlin I'll take a look at your repro app |
any luck there? do you need any more info may be ? @johna-ms |
@johna-ms Any updates on this? Looks like I'm facing the same problem. |
Hi guys, sorry for the late update. I've been on some high priority work the past couple days. I'll give an update this week |
@dhohlin I'm able to repro w your app and have a stack. It's looking like this is on us and I have a fix in mind. There are still some inconsistencies in my investigation I need to iron out. I will update more next week |
Update: Workaround:
These calls were added to workaround some previous bug for request filters in |
any updates @johna-ms ? |
Sorry I wasn't able to take a look at this this week. As @pontusn has discovered, this looks to be an issue with put_IsVisible from our side. I'll try and update early next week. |
@johna-ms any luck with the fix may be ? |
Confirming this is a bug related to put_IsVisible. I've prototyped a fix locally and can likely have it in for 103 stable. I'll update the thread when the fix is available to try in canary |
Great news, thanks @johna-ms ! |
I was frantically trying to understand what went wrong for weeks now :-( . |
Hello all, this actually ended up being applied to 102 as well. Please try out 102 or 103 and see if the issue is fixed for you |
Can confirm. 102.0.1245.33 fixes the issue for me. Many thanks. |
Thanks! Resolving this. Please reawaken if there is still an issue. |
Confirmed fixed in 102.0.1245.33. Thanks @johna-ms ! |
Hi,
I'm testing out WebView2 in Winform C# applications, and it seems that whenever a WebView2 instance is created, and the first navigation is completed, the application automatically steals focus from any other application that was focused at the time.
To illustrate, let's say I have an application called SampleApp with the following form:
And SampleApp has the following code:
When I click the
Navigate to Microsoft.com
button, and quickly click outside of SampleApp shifting the focus the away, the focus is immediately brought back to SampleApp after the navigation tomicrosoft.com
is complete. Is this expected behavior, or a bug? And is there anyway I can prevent the focus from moving back to SampleApp after the navigation is complete?Thank you!
AB#31515353
The text was updated successfully, but these errors were encountered: