MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
444 stars 53 forks source link

Dragging from WebView2 to any WPF control doesn't work if a WPF Popup is opened #2690

Open q71114 opened 2 years ago

q71114 commented 2 years ago

Description Dragging from WebView2 to any WPF control doesn't work if a WPF Popup is opened

Version SDK: 1.0.1222-prerelease Runtime: Evergreen, 104.0.1293.54 Framework: WPF OS: Win11 21H2 (OS Build 22000, 856)

Repro Steps

  1. Create a sample app with a menu and add the code from additional context to open the popup on DragOver.
  2. Start dragging any element from WebView2 (text, image link, etc.)
  3. Drag that element over the menu to make the submenu open
  4. Drop the element

Expected:

Actual:

Screenshots drag-drop-bug

Additional context The same steps work fine when dragging things from Edge / Chrome which makes me think this is a WebView2 bug, not a WPF bug.

The following code was used with WebView2WpfBrowser in WebView2Samples project:

        private void Menu_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = e.AllowedEffects;
            e.Handled = true;

            if (sender is UIElement uiElement)
            {
                var position = e.GetPosition(uiElement);
                var result = VisualTreeHelper.HitTest(uiElement, position);
                var menuItem = FindVisualParent<MenuItem>(result?.VisualHit);
                if (menuItem?.HasItems == true)
                {
                    menuItem.IsSubmenuOpen = true;
                }
            }
        }

        private void Menu_Drop(object sender, DragEventArgs e)
        {
            e.Handled = true;

            MessageBox.Show(this, $"'{e.Data.GetData(DataFormats.UnicodeText)}' dropped");
        }

        public static T FindVisualParent<T>(DependencyObject element)
            where T : DependencyObject =>
            element == null ? null : element as T ?? FindVisualParent<T>(VisualTreeHelper.GetParent(element));

AB#46318657

plantree commented 1 year ago

Hi @q71114, it seems the Drop event works in your screenshots. If you are available, please share your project which consist of .sln.

q71114 commented 1 year ago

Hi @q71114, it seems the Drop event works in your screenshots.

Hey, the first Drop worked when the menu Popup was not open. Subsequent drops were unsuccessful while the Popup was open but it's hard to see in the screen capture as nothing is happening. You can notice that the drag cursor didn't change back to normal cursor because it got stuck in the dragging state even though the item was dropped.

If you are available, please share your project which consist of .sln.

You can find the project here.

plantree commented 1 year ago

Hi @q71114,

Sorry for the late reply.

I tried the demo you provide and came across a issue arising from DragOver. Meanwhile, I found a useful doc here: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/drag-and-drop-overview?view=netframeworkdesktop-4.8, which helps to understand drag-drop more deeply.

Could you provide your usage scenario to help better understand your issue?

q71114 commented 1 year ago

Hey @plantree,

This is a basic bookmark implementation when you dragging an url over a bookmark folder which opens the folder content in a popup in order for the user to drop the url into the specific location inside that folder.

plantree commented 1 year ago

Hi @q71114,

It seems that you want to drag something from WebView2 to Menu, and the issue is coming from a drop target which receives the dropped object? Have you tried drag from another source? With the same event handler?

q71114 commented 1 year ago

Have you tried drag from another source? With the same event handler?

Yes, dragging from other source work fine. I updated the sample project so you can test yourself as well.

In this example I drag & drop from notepad to the Menu and it works fine, but when doing that from WebView2 it executes the menu item instead and doesn't leave the dragging state. When trying to drag & drop it again the application ultimately crashes. dnd-bug

plantree commented 1 year ago

Hi @q71114,

Thanks for your demo. I tried in Notepad, and it works like the GIF you provide.

Do you mean the WebView2 should behaves the same as Notepad? Not execute the menu item and leave the dragging state.

q71114 commented 1 year ago

Hi @q71114,

Thanks for your demo. I tried in Notepad, and it works like the GIF you provide.

Do you mean the WebView2 should behaves the same as Notepad? Not execute the menu item and leave the dragging state.

Hi @plantree,

Yes, that is correct.

ElyssaJyu commented 1 year ago

Hi @q71114 Sorry for the late reply. I have cloned and repro your project. The unexpected behavior does exist. Also, I tried to drag the text content from navigation bar or text box in WPF window, instead of from WebvVew2 control. The behavior will be "not execute the menu item when leave the drag, but also it doesn't execute the drop event." There might be a design related to Drop event for WPF control, but I still need further investigation. I will sync to you as soon as possible.