Skip to content

Commit b8fb587

Browse files
committed
Add support for dragging recent files
1 parent 34d908c commit b8fb587

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/Files.App/UserControls/Widgets/RecentFilesWidget.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
x:Name="RecentFilesListView"
3838
HorizontalAlignment="Stretch"
3939
VerticalAlignment="Stretch"
40+
AllowDrop="True"
41+
CanDragItems="True"
42+
DragItemsStarting="RecentFilesListView_DragItemsStarting"
4043
IsItemClickEnabled="True"
4144
IsRightTapEnabled="True"
4245
ItemClick="RecentFilesListView_ItemClick"

src/Files.App/UserControls/Widgets/RecentFilesWidget.xaml.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using Microsoft.UI.Xaml.Controls;
55
using Microsoft.UI.Xaml.Input;
6+
using Windows.ApplicationModel.DataTransfer;
7+
using Windows.Storage;
68

79
namespace Files.App.UserControls.Widgets
810
{
@@ -30,5 +32,37 @@ private void RecentFilesListView_RightTapped(object sender, RightTappedRoutedEve
3032
{
3133
ViewModel.BuildItemContextMenu(e.OriginalSource, e);
3234
}
35+
36+
private async void RecentFilesListView_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
37+
{
38+
var items = e.Items.OfType<RecentItem>().ToList();
39+
if (items.Count > 0)
40+
{
41+
var storageItems = new List<IStorageItem>();
42+
foreach (var item in items)
43+
{
44+
try
45+
{
46+
var file = await StorageFile.GetFileFromPathAsync(item.Path);
47+
if (file != null)
48+
storageItems.Add(file);
49+
}
50+
catch
51+
{
52+
e.Cancel = true;
53+
}
54+
}
55+
56+
if (storageItems.Count > 0)
57+
{
58+
// Create a new data package and set the storage items
59+
DataPackage dataPackage = new DataPackage();
60+
dataPackage.SetStorageItems(storageItems);
61+
e.Data.SetDataProvider(StandardDataFormats.StorageItems, request => request.SetData(storageItems));
62+
63+
e.Data.RequestedOperation = DataPackageOperation.Move | DataPackageOperation.Copy | DataPackageOperation.Link;
64+
}
65+
}
66+
}
3367
}
3468
}

0 commit comments

Comments
 (0)