using System; using System.Threading.Tasks; using Avalonia.Input.Platform; using Avalonia.Interactivity; namespace Avalonia.Input { public static class DragDrop { /// /// Event which is raised, when a drag-and-drop operation enters the element. /// public static readonly RoutedEvent DragEnterEvent = RoutedEvent.Register("DragEnter", RoutingStrategies.Bubble, typeof(DragDrop)); /// /// Event which is raised, when a drag-and-drop operation leaves the element. /// public static readonly RoutedEvent DragLeaveEvent = RoutedEvent.Register("DragLeave", RoutingStrategies.Bubble, typeof(DragDrop)); /// /// Event which is raised, when a drag-and-drop operation is updated while over the element. /// public static readonly RoutedEvent DragOverEvent = RoutedEvent.Register("DragOver", RoutingStrategies.Bubble, typeof(DragDrop)); /// /// Event which is raised, when a drag-and-drop operation should complete over the element. /// public static readonly RoutedEvent DropEvent = RoutedEvent.Register("Drop", RoutingStrategies.Bubble, typeof(DragDrop)); public static readonly AttachedProperty AllowDropProperty = AvaloniaProperty.RegisterAttached("AllowDrop", typeof(DragDrop), inherits: true); /// /// Gets a value indicating whether the given element can be used as the target of a drag-and-drop operation. /// public static bool GetAllowDrop(Interactive interactive) { return interactive.GetValue(AllowDropProperty); } /// /// Sets a value indicating whether the given interactive can be used as the target of a drag-and-drop operation. /// public static void SetAllowDrop(Interactive interactive, bool value) { interactive.SetValue(AllowDropProperty, value); } /// /// Adds a handler for the DragEnter attached event. /// /// The element to attach the handler to. /// The handler for the event. public static void AddDragEnterHandler(Interactive element, EventHandler handler) { element.AddHandler(DragEnterEvent, handler); } /// /// Removes a handler for the DragEnter attached event. /// /// The element to remove the handler from. /// The handler to remove. public static void RemoveDragEnterHandler(Interactive element, EventHandler handler) { element.RemoveHandler(DragEnterEvent, handler); } /// /// Adds a handler for the DragLeave attached event. /// /// The element to attach the handler to. /// The handler for the event. public static void AddDragLeaveHandler(Interactive element, EventHandler handler) { element.AddHandler(DragLeaveEvent, handler); } /// /// Removes a handler for the DragLeave attached event. /// /// The element to remove the handler from. /// The handler to remove. public static void RemoveDragLeaveHandler(Interactive element, EventHandler handler) { element.RemoveHandler(DragLeaveEvent, handler); } /// /// Adds a handler for the DragOver attached event. /// /// The element to attach the handler to. /// The handler for the event. public static void AddDragOverHandler(Interactive element, EventHandler handler) { element.AddHandler(DragOverEvent, handler); } /// /// Removes a handler for the DragOver attached event. /// /// The element to remove the handler from. /// The handler to remove. public static void RemoveDragOverHandler(Interactive element, EventHandler handler) { element.RemoveHandler(DragOverEvent, handler); } /// /// Adds a handler for the Drop attached event. /// /// The element to attach the handler to. /// The handler for the event. public static void AddDropHandler(Interactive element, EventHandler handler) { element.AddHandler(DropEvent, handler); } /// /// Removes a handler for the Drop attached event. /// /// The element to remove the handler from. /// The handler to remove. public static void RemoveDropHandler(Interactive element, EventHandler handler) { element.RemoveHandler(DropEvent, handler); } /// /// Starts a dragging operation with the given and returns the applied drop effect from the target. /// /// public static Task DoDragDropAsync( PointerEventArgs triggerEvent, IDataTransfer dataTransfer, DragDropEffects allowedEffects) { if (AvaloniaLocator.Current.GetService() is not { } dragSource) { dataTransfer.Dispose(); return Task.FromResult(DragDropEffects.None); } return dragSource.DoDragDropAsync(triggerEvent, dataTransfer, allowedEffects); } } }