Skip to content
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

[Bug]: put_AreBrowserAcceleratorKeysEnabled(FALSE) doesn't work in some Win32 apps on runtime 120 #4278

Closed
ptc-shunt opened this issue Jan 5, 2024 · 14 comments
Assignees
Labels
bug Something isn't working regression Something used to work but doesn't anymore tracked We are tracking this work internally.

Comments

@ptc-shunt
Copy link

ptc-shunt commented Jan 5, 2024

What happened?

put_AreBrowserAcceleratorKeysEnabled(FALSE) is not working in the evergreen 120.0.2210.91 runtime, at least for some Win32 desktop apps. The standard accelerator keys (F5, Print etc) continue to work. The problem is also present in the 122.0.2323.0 canary.

It behaves as expected if I use an old 116.0.1935.0 canary runtime. I don't have any intermediate versions to try and pin down when it stopped.

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

120.0.2210.91

SDK Version

1.0.2210.55

Framework

Win32

Operating System

Windows 10

OS Version

10.0.19045

Repro steps

In the Win32_GettingStarted solution, add these lines after the existing call to put_IsWebMessageEnabled in HelloWebView.cpp:

    wil::com_ptr<ICoreWebView2Settings3> settings3 = settings.try_query<ICoreWebView2Settings3>();				
    settings3->put_AreBrowserAcceleratorKeysEnabled(FALSE);

Run the application and notice that F5, Ctrl+P have not been disabled.

(For some reason it works in the larger WebView2APISample. I haven't been able to spot a salient difference).

Repros in Edge Browser

No

Regression

Regression in newer Runtime

Last working version (if regression)

Works in 116.0.1935.0, unknown which version it stopped working. SDK version doesn't seem to matter.

AB#48454809

@ptc-shunt ptc-shunt added the bug Something isn't working label Jan 5, 2024
@github-actions github-actions bot added the regression Something used to work but doesn't anymore label Jan 5, 2024
@vbryh-msft vbryh-msft assigned tofuandeve and unassigned vbryh-msft and lflores-ms Jan 5, 2024
@vbryh-msft
Copy link
Contributor

@champnic FYI

@tofuandeve
Copy link
Contributor

Hi @ptc-shunt , I'm looking into this issue. I am also able to repro the issue in the GettingStarted sample app but not the other one. I'll look more into this to see why this is happening. Thanks for reporting the issue.

@tofuandeve
Copy link
Contributor

I can repro this in WinUI2/WinUI3 and Win32 sample apps in Win32_GettingStarted but not in the bigger apps. Opened a tracking bug on our side to continue looking

@tofuandeve
Copy link
Contributor

Hi @ptc-shunt , we were able to root cause the issue and made a fix for it. The fix is now available in Canary build Version 122.0.2359.0.
Please let me know if you running into any other issue with it. :)
Thanks!

@ptc-shunt
Copy link
Author

Confirmed fixed in 122.0.2362.0. Thanks.

@Urmeli0815
Copy link

I also ran into this problem and can confirm that it's fixed in canary.

@tofuandeve is there a way to workaround the problem as it doesn't happen always in the Test-Apps? Or is there a chance the fix gets released earlier? We currently get quite some support calls from our customers as our app gets unusable when the user presses F5.

@richardhauer
Copy link

I have Edge 122.0.2365.50 from the beta channel
Trying to build a MAUI Blazor Hybrid desktop app
F5 still triggers an app refresh even though 'AreBrowserAcceleratorKeysEnabled' is set to false

Is this still a bug or something in the WinUI plumbing?

@tofuandeve
Copy link
Contributor

@richardhauer I tested on a WinUI3 app with Canary channel v123 and can't seem to repro the issue, I'll try with Beta to see if I could repro this.

Could you try with Canary in the meantime and let us know is you are still seeing the issue? Also could you share how you set AreBrowserAcceleratorKeysEnabled setting for WebView2 in your app?

@tofuandeve
Copy link
Contributor

@Urmeli0815 Yes! It is possible to work around it by creating a handler for ICoreWebView2Controller::add_AcceleratorKeyPressed in the meantime until the fix makes it to stable release :)

@richardhauer
Copy link

richardhauer commented Feb 25, 2024

thanks @tofuandeve

I have a MAUI Blazor Hybrid app on NET8
App.xaml is the Application which loads MainPage.xaml into Application.MainPage. MainPage.xaml has only the one component, a BlazorView` with the name BlazorHost.

There's an event handler in the XAML for the component's BlazorWebViewInitialized event:

private void DisableWebViewFeatures( WebView2 webView )
{
	var settings = webView.CoreWebView2.Settings;
	settings.AreBrowserAcceleratorKeysEnabled = false; // TODO this is a known bug https://github.com/MicrosoftEdge/WebView2Feedback/issues/4241
	settings.AreDefaultContextMenusEnabled = false;
	settings.AreDefaultScriptDialogsEnabled = false;
	settings.AreDevToolsEnabled = false;
	settings.IsBuiltInErrorPageEnabled = false;
	settings.IsGeneralAutofillEnabled = false;
	settings.IsPasswordAutosaveEnabled = false;
	settings.IsPinchZoomEnabled = false;
	settings.IsSwipeNavigationEnabled = false;
	settings.IsZoomControlEnabled = false;

#if DEBUG
	settings.AreDevToolsEnabled = true;
#endif
}

private void BlazorHost_BlazorWebViewInitialized( object sender, Microsoft.AspNetCore.Components.WebView.BlazorWebViewInitializedEventArgs e )
{
#if WINDOWS
	DisableWebViewFeatures( e.WebView );
#endif
}

@richardhauer
Copy link

@tofuandeve I had a hell of a time trying to map into ICoreWebView2Controller::add_AcceleratorKeyPressed
It's very difficult to locate this interface and get a mapping to it.

For the record and future travellers here, this is what I came up with, that did work. I've included the namespaces because a lot of people don't do that and it can be a real headache locating services, particularly when there's more than one with the same name.

This from MainPage.xaml

public partial class MainPage : ContentPage {
    private CoreWebView2Controller? Cwv2c;

    private void BlazorHost_BlazorWebViewInitialized( object sender, Microsoft.AspNetCore.Components.WebView.BlazorWebViewInitializedEventArgs e )
    {
        _ = Application.Current!.Dispatcher.DispatchAsync(
            async () => {
                Cwv2c = await e.WebView.CoreWebView2.Environment.CreateCoreWebView2ControllerAsync(
                    CoreWebView2ControllerWindowReference.CreateFromWindowHandle(
                        (ulong)WinRT.Interop.WindowNative.GetWindowHandle( Application.Current.Windows[0].Handler.PlatformView )
                    )
                );
                Cwv2c.AcceleratorKeyPressed += (sender,arg) => { arg.Handled = true; };
            }
        );
    }
}

Having said all that, as if by magic, the AreBrowserAcceleratorKeysEnabled property started working for me today, so there you go! Asy and ye shall receive... or something like that. 😎

@tofuandeve
Copy link
Contributor

@richardhauer Hi, just a quick check since I can't repro this issue in Beta either. These changes in the settings for WebView2 will only take effect after a navigation. Did you navigate the WebView2 after DisableWebViewFeatures was called?

@richardhauer
Copy link

richardhauer commented Feb 27, 2024

@tofuandeve interesting... tl;dr I don't know what the event order is; we can test it empirically I guess, if it still matters.

I think it depends what you mean by "navigation". The settings are set in the "Initialized" event handler, as directed by the guidance that I saw in various locations on the internet. There is a URL set declaratively in the XAML (this is a Blazor Web View component).

I would assume the navigation would happen after the Initialize event, but I don't know conclusively. Further, since the document is being loaded locally it may be a "document.write()" and not a navigation event at all. Once Blazor is loaded there's no navigation in the normal sense, instead content is dynamically rewritten into the page.

If this were a website we'd be talking about WASM/WebSockets, but since this is all local and there's no "server" per se I'm not actually sure how the content gets into the browser. Could be a custom URL mapping or just direct manipulation of the DOM I guess.

@tofuandeve
Copy link
Contributor

@richardhauer This is an example of how devs can navigate the Webview2 control to apply the changes made to settings component in WinUI3:

  public MainWindow()
    {
        this.InitializeComponent();
        AddressBar.Text = "https://developer.microsoft.com/en-us/microsoft-edge/webview2/";

        WebView2.NavigationCompleted += WebView2_NavigationCompleted;
        WebView2.CoreWebView2Initialized += WebView2_CoreWebView2Initialized;

        WebView2.Source = new Uri(AddressBar.Text);
        StatusUpdate("Ready");

    }
    
    private void WebView2_CoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs args)
    {
         sender.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
    }

The Settings value can be set in WebView2_CoreWebView2Initialized, and setting the WebView2.Source is one of the ways that devs can use to navigate the WebView2 control to make sure that all the changes to Settings are applied. CoreWebView2Settings Class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working regression Something used to work but doesn't anymore tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

6 participants