microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.35k stars 678 forks source link

After WebView2 changes the window, clicking WebView2 will not activate the new window but the main window #7335

Open sudongg opened 2 years ago

sudongg commented 2 years ago

Describe the bug

WinAppSdk Self-contained project, when WebView2 changes the window, clicking on the new window activates the main window.

Steps to reproduce the bug

  1. New project 2.MainWindow.xaml add
    <Grid>
        <ContentPresenter x:Name="ContentPresenter" />
        <StackPanel Orientation="Vertical"  VerticalAlignment="Center" HorizontalAlignment="Left">
            <Button Click="Button_Click" Background="Red" Foreground="White" Content="New Window" Width="150" Height="50"/>
        </StackPanel>
    </Grid>

3.MainWindow.xaml.cs Add

        public MainWindow()
        {
            this.InitializeComponent();

            WebView = new WebView2() { Source = new Uri("https://github.com/") };
            ContentPresenter.Content = WebView;
        }

        public MainWindow(WebView2 webView2)
        {
            this.InitializeComponent();

            ContentPresenter.Content = webView2;
        }

        WebView2 WebView;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ContentPresenter.Content = null;
            MainWindow appWindow = new MainWindow(WebView);
            appWindow.Activate();
        }
  1. Click the "New Window" button after running
  2. The main window will be activated every time you click on the new window

Expected behavior

Only the window where WebView2 is located will be activated

Screenshots

No response

NuGet package version

1.1.1

Packaging type

Unpackaged

Windows version

Windows 10 version 2004 (19041, May 2020 Update)

IDE

Visual Studio 2022

Additional context

No response

applefanbois commented 1 year ago

waiting...

ranjeshj commented 1 year ago

This looks to be fixed in WASDK 1.3, it does not seem to repro in WinUI3 Gallery. @applefanbois @sudongg please reactivate if you see this with latest bits. Thanks!

applefanbois commented 1 year ago

I have 1.3.230724000 and the problem is still there.

1-I create a webview2 attached to the main window. 2-Then create a new window 3-Re attach the first webview2 to the new window

The webview display either white page or may display correctly. Clicking on the webview2 in the new window bring back to front the main window. Richt click on the webview2 in the new window brings the context menu in the main window.

If I create the webview2 in the new window and move it to the new window, the situation is reversed.

That problem is NOT fixed.

Simple when you detach a gui object from another one, you need to tell everyone in the webview2 that they have a new parent. Somebody at Microsoft took some shortcuts to connect the the original windows not looking at the parent GUI.

codendone commented 1 year ago

Thanks for clarifying! I think this aspect is what was missed when previously trying to repro this:

Re attach the first webview2 to the new window

Moving a WebView2 to a different window might be hitting a hosting issue where some aspect remains connected to the original HWND it was hosted in.

HO-COOH commented 10 months ago

This exact issue still presents in wasdk 1.5 experimental. And it is critical to our business code.

applefanbois commented 10 months ago

We simply tell our customers that need to be able to transfer a webpage to another window (mostly to compare forms) to get the macOS version of our App instead.