Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add IAsyncDropTarget to allow consuming async capable drops #13422

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

Open
JeremyKuhne opened this issue May 7, 2025 · 0 comments
Open

Add IAsyncDropTarget to allow consuming async capable drops #13422

JeremyKuhne opened this issue May 7, 2025 · 0 comments
Assignees
Labels
api-ready-for-review (2) API is ready for formal API review; applied by the issue owner blocking Used by the API Review Board needs-area-label
Milestone

Comments

@JeremyKuhne
Copy link
Member

Background and motivation

IDataObjectAsyncCapability allows drag drop sources to provide data asynchronously. We don't currently have a way to consume this in WinForms.

Chromium based apps require using this interface for receiving any file drops. The new Outlook falls in this category and you cannot drag messages from it without this support. It pops a dialog during the drop, which would make the target app freeze without async support.

To fully enable this scenario, we want to add IAsyncDropTarget that Controls can implement to opt into getting called back asynchronously when that is supported.

API Proposal

namespace System.Windows.Forms;

/// <summary>
///  Interface for a drop target that supports asynchronous processing.
/// </summary>
public interface IAsyncDropTarget : IDropTarget
{
    /// <summary>
    ///  When supporting this interface, this method will be callled if the drop source supports asynchronous processing.
    /// </summary>
    /// <remarks>
    ///  <para>
    ///   Similar to <see cref="IDropTarget.OnDragDrop"/>, but this method is called when a drop operation supports
    ///   asyncronous processing. It will not block the UI thread, any UI updates will need to be invoked to occur
    ///   on the UI thread.
    ///  </para>
    ///  <para>
    ///   Avoid dispatching the <see cref="DragEventArgs"/> back to the UI thread as invoking <see cref="DragEventArgs.Data"/>
    ///   on the UI thread will block it until the data is available. If existing code needs <see cref="DragEventArgs"/>
    ///   consider creating a new instance with a new <see cref="DataObject"/> that has extracted the data you're looking for.
    ///  </para>
    /// </remarks>
    void OnAsyncDragDrop(DragEventArgs e);
}

API Usage

public class MyTextBox : TextBox, IAsyncDropTarget
{
    public void OnAsyncDragDrop(DragEventArgs e)
    {
        if (e.Data is { } dataObject && dataObject.TryGetData(DataFormats.FileDrop, out string[]? files))
        {
            string data = string.Join(Environment.NewLine, files);
            InvokeAsync(() => Text = string.Join(Environment.NewLine, files)).Start();
        }
    }
}

Alternative Designs

There aren't a lot of great options that don't deviate significantly from the existing patterns you get from IDropTarget.

Risks

Not many async providers out there to vet the design. As such we might make this experimental for .NET 10.

Will this feature affect UI controls?

No localization or accessibility considerations.

@JeremyKuhne JeremyKuhne added api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation untriaged The team needs to look at this issue in the next triage api-ready-for-review (2) API is ready for formal API review; applied by the issue owner blocking Used by the API Review Board and removed api-suggestion (1) Early API idea and discussion, it is NOT ready for implementation untriaged The team needs to look at this issue in the next triage labels May 7, 2025
@JeremyKuhne JeremyKuhne added this to the .NET 10.0 milestone May 7, 2025
@merriemcgaw merriemcgaw modified the milestones: .NET 10.0, 10 Preview5 May 8, 2025
JeremyKuhne added a commit to JeremyKuhne/winforms that referenced this issue May 8, 2025
Chromium based apps don't support file drop without using IDataObjectAsyncCapability. This includes the new Outlook.

To support this we'll look for this interface in our current code paths and utilize it. This makes the async operation sync, which works, but isn't ideal. Chromium will pop a dialog that will leave WinForms modal as well until it is responded to.

If this behavior creates an issue it can be disabled with the appcontext switch: "Windows.DragDrop.DisableSyncOverAsync"

In order to truly support async we're also introducing a new interface to allow calling back off of the UI thread. This will be shipped as experimental for .NET 10 as there is a small risk we'll want to change the API based on real-world feedback. See dotnet#13422.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-ready-for-review (2) API is ready for formal API review; applied by the issue owner blocking Used by the API Review Board needs-area-label
Projects
None yet
Development

No branches or pull requests

3 participants