Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions samples/ControlCatalog/Pages/DragAndDropPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
MaxWidth="260"
Background="{DynamicResource SystemAccentColorDark1}"
DragDrop.AllowDrop="True">
<TextBlock TextWrapping="Wrap">Drop some text or files here (Copy)</TextBlock>
<TextBlock TextWrapping="Wrap">Drop some text, files, bitmap or custom format here (Copy)</TextBlock>
</Border>
<Border Name="MoveTarget"
Padding="16"
MaxWidth="260"
Background="{DynamicResource SystemAccentColorDark1}"
DragDrop.AllowDrop="True">
<TextBlock TextWrapping="Wrap">Drop some text or files here (Move)</TextBlock>
<TextBlock TextWrapping="Wrap">Drop some text or custom format here (Move)</TextBlock>
</Border>
</StackPanel>
</WrapPanel>
Expand Down
3 changes: 3 additions & 0 deletions samples/ControlCatalog/Pages/DragAndDropPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ async void Drop(object? sender, DragEventArgs e)
e.DragEffects = e.DragEffects & (DragDropEffects.Copy);
}

if (e.DragEffects == DragDropEffects.None)
return;

if (e.DataTransfer.Contains(DataFormat.Text))
{
DropState.Content = e.DataTransfer.TryGetText();
Expand Down
3 changes: 3 additions & 0 deletions src/Avalonia.Base/Input/AsyncDataTransferExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ internal static IDataTransfer ToSynchronous(this IAsyncDataTransfer asyncDataTra
return new AsyncToSyncDataTransfer(asyncDataTransfer);
}

internal static IAsyncDataTransfer ToAsynchronous(this IDataTransfer dataTransfer)
=> dataTransfer as IAsyncDataTransfer ?? new SyncToAsyncDataTransfer(dataTransfer);

/// <summary>
/// Gets whether a <see cref="IAsyncDataTransfer"/> supports a specific format.
/// </summary>
Expand Down
43 changes: 43 additions & 0 deletions src/Avalonia.Base/Input/SyncToAsyncDataTransfer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;

namespace Avalonia.Input;

/// <summary>
/// Wraps a <see cref="IDataTransfer"/> into a <see cref="IAsyncDataTransfer"/>.
/// </summary>
/// <param name="dataTransfer">The sync object to wrap.</param>
internal sealed class SyncToAsyncDataTransfer(IDataTransfer dataTransfer)
: IDataTransfer, IAsyncDataTransfer
{
private SyncToAsyncDataTransferItem[]? _items;

public IReadOnlyList<DataFormat> Formats
=> dataTransfer.Formats;

public IReadOnlyList<SyncToAsyncDataTransferItem> Items
=> _items ??= ProvideItems();

IReadOnlyList<IDataTransferItem> IDataTransfer.Items
=> dataTransfer.Items;

IReadOnlyList<IAsyncDataTransferItem> IAsyncDataTransfer.Items
=> Items;

private SyncToAsyncDataTransferItem[] ProvideItems()
{
var asyncItems = dataTransfer.Items;
var count = asyncItems.Count;
var syncItems = new SyncToAsyncDataTransferItem[count];

for (var i = 0; i < count; ++i)
{
var asyncItem = asyncItems[i];
syncItems[i] = new SyncToAsyncDataTransferItem(asyncItem);
}

return syncItems;
}

public void Dispose()
=> dataTransfer.Dispose();
}
21 changes: 21 additions & 0 deletions src/Avalonia.Base/Input/SyncToAsyncDataTransferItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Avalonia.Input;

/// <summary>
/// Wraps a <see cref="IDataTransferItem"/> into a <see cref="IAsyncDataTransferItem"/>.
/// </summary>
/// <param name="dataTransferItem">The sync item to wrap.</param>
internal sealed class SyncToAsyncDataTransferItem(IDataTransferItem dataTransferItem)
: IDataTransferItem, IAsyncDataTransferItem
{
public IReadOnlyList<DataFormat> Formats
=> dataTransferItem.Formats;

public object? TryGetRaw(DataFormat format)
=> dataTransferItem.TryGetRaw(format);

public Task<object?> TryGetRawAsync(DataFormat format)
=> Task.FromResult(dataTransferItem.TryGetRaw(format));
}
1 change: 1 addition & 0 deletions src/Avalonia.Controls/Platform/InProcessDragSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public async Task<DragDropEffects> DoDragDropAsync(
using (_result.Subscribe(new AnonymousObserver<DragDropEffects>(tcs)))
{
var effect = await tcs.Task;
dataTransfer.Dispose();
return effect;
}
}
Expand Down
82 changes: 0 additions & 82 deletions src/Avalonia.X11/Clipboard/ClipboardDataReader.cs

This file was deleted.

Loading
Loading