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

Drop event fires even if AcceptedOperation is set to None #18770

Open
kucint opened this issue Nov 12, 2024 · 2 comments · May be fixed by #18808
Open

Drop event fires even if AcceptedOperation is set to None #18770

kucint opened this issue Nov 12, 2024 · 2 comments · May be fixed by #18808
Assignees
Labels
difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification

Comments

@kucint
Copy link

kucint commented Nov 12, 2024

Current behavior

This issue is related to WinAppSDK Framework only.
On Desktop Framework it works fine!

Create a control with AllowDrop set to true.
Drag another control on it.
Drop event is not fired.

Have look on example:

  • Red rectangle can be dragged.
  • Blue rectangle can be a drop target
  • Yellow Grid can be a a drop target:

image

<ScrollViewer>
  <Grid Width="800"
        Height="600"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        ColumnSpacing="20"
        Background="LightYellow"
        AllowDrop="True"
        Drop="Grid_Drop">
        <Grid.ColumnDefinitions >
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Rectangle Grid.Column="0"
                   Width="100" Height="80"
                   Stroke="Black"
                   StrokeThickness="1"
                   Fill="LightCoral"
                   CanDrag="True" />
  
        <Rectangle Grid.Column="1"
                   Width="100" Height="80"
                   Stroke="Black"
                   StrokeThickness="1"
                   Fill="LightBlue"
                   AllowDrop="True"
                   DragEnter="BlueRectangle_DragEnter"
                   DragOver="BlueRectangle_DragOver"
                   DragLeave="BlueRectangle_DragLeave"
                   Drop="BlueRectangle_Drop" />
  </Grid>
</ScrollViewer>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void BlueRectangle_Drop(object sender, DragEventArgs e)
    {
        Debug.WriteLine("BlueRectangle_Drop");
    }

    private void BlueRectangle_DragEnter(object sender, DragEventArgs e)
    {
        Debug.WriteLine("BlueRectangle_DragEnter");
    }

    private void BlueRectangle_DragOver(object sender, DragEventArgs e)
    {
        Debug.WriteLine("BlueRectangle_DragOver");
    }

    private void BlueRectangle_DragLeave(object sender, DragEventArgs e)
    {
        Debug.WriteLine("BlueRectangle_DragLeave");
    }

    private void Grid_Drop(object sender, DragEventArgs e)
    {
        Debug.WriteLine("Grid_Drop");
    }
}

Please note that only Drop event is affected.
All other events related to AllowDrop like DragEnter , DragOver, DragLeave work fine.

Expected behavior

On WinAppSDK Framework, events shall fire correctly:

  • Dragging and then dropping the Red rectangle on Yellow background shall fire Grid_Drop.
  • Dragging and then dropping the Red rectangle on the Blue rectangle shall fire BlueRectangle_Drop.

How to reproduce it (as minimally and precisely as possible)

Minimal repro project: UnoDragDropApp.zip

Workaround

No response

Works on UWP/WinUI

None

Environment

No response

NuGet package version(s)

"Uno.Sdk": "5.5.32"

Affected platforms

No response

IDE

No response

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

@kucint kucint added difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification labels Nov 12, 2024
@kucint kucint changed the title [WinAppSDK] AllowDrop has no effect [WinAppSDK] Drop event does not fire even if AllowDrop is set to true Nov 12, 2024
@ramezgerges ramezgerges linked a pull request Nov 15, 2024 that will close this issue
6 tasks
@ramezgerges
Copy link
Contributor

This seems to be an Uno bug actually. The provided code should NOT work. It seems like we fire a Drop event unconditionally, which is incorrect.

@kucint You need to set DragEventArgs.AcceptedOperation in your DragOver handlers. Dragging and dropping in WinUI (and most platforms actually) work by negotiating between the source and drop targets. The dragging source advertises a set of possible formats (e.g. png for a dragged image, a file path for a file, etc.) and possible operations (Copy, Move, and Link). The drop target must announce that it accepts one of these advertised formats and operations and that it can in fact be a drop target for this dragging operation. It does this in DragOver by setting AcceptedOperation to anything other than None (for your usecase, any value other than None will work, since you're not actually Copying or Moving anything). When you release the pointer, an internal check checks if an operation was accepted or not, and if one was, it fires a Drop event. We seem to be missing this check in Uno, and we're firing Drop regardless of AcceptedOperation.

For a more detailed explanation of the DnD state machine, see https://learn.microsoft.com/en-us/windows/apps/design/input/drag-and-drop.

@ramezgerges ramezgerges changed the title [WinAppSDK] Drop event does not fire even if AllowDrop is set to true Drop event fires even if AcceptedOperation is set to None Nov 15, 2024
@kucint
Copy link
Author

kucint commented Nov 15, 2024

@ramezgerges
I see!
I will try to implement drag & drop as you suggest and let you know.
Thanks for info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants