Skip to content

Commit 77eee0d

Browse files
committed
Feature: Added drag and drop for sidebar items
1 parent ae18e7e commit 77eee0d

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/Files.App.Controls/Sidebar/SidebarItem.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using Microsoft.UI.Xaml.Automation;
77
using Microsoft.UI.Xaml.Automation.Peers;
88
using System.Collections.Specialized;
9+
using System.IO;
910
using Windows.ApplicationModel.DataTransfer;
11+
using Windows.Storage;
1012

1113
namespace Files.App.Controls
1214
{
@@ -90,6 +92,7 @@ public void HandleItemChange()
9092
HookupItemChangeListener(null, Item);
9193
UpdateExpansionState();
9294
ReevaluateSelection();
95+
CanDrag = Item?.GetType().GetProperty("Path")?.GetValue(Item) is string path && Path.IsPathRooted(path);
9396
}
9497

9598
private void HookupOwners()
@@ -137,7 +140,35 @@ private void HookupItemChangeListener(ISidebarItemModel? oldItem, ISidebarItemMo
137140

138141
private void SidebarItem_DragStarting(UIElement sender, DragStartingEventArgs args)
139142
{
140-
args.Data.SetData(StandardDataFormats.Text, Text?.ToString() ?? string.Empty);
143+
if (Item?.GetType().GetProperty("Path")?.GetValue(Item) is not string dragPath || !Path.IsPathRooted(dragPath))
144+
return;
145+
146+
args.Data.SetData(StandardDataFormats.Text, dragPath);
147+
args.Data.RequestedOperation = DataPackageOperation.Copy;
148+
args.Data.SetDataProvider(StandardDataFormats.StorageItems, async request =>
149+
{
150+
var deferral = request.GetDeferral();
151+
try
152+
{
153+
if (Directory.Exists(dragPath))
154+
{
155+
var folder = await StorageFolder.GetFolderFromPathAsync(dragPath);
156+
request.SetData(new IStorageItem[] { folder });
157+
}
158+
else if (File.Exists(dragPath))
159+
{
160+
var file = await StorageFile.GetFileFromPathAsync(dragPath);
161+
request.SetData(new IStorageItem[] { file });
162+
}
163+
}
164+
catch
165+
{
166+
}
167+
finally
168+
{
169+
deferral.Complete();
170+
}
171+
});
141172
}
142173

143174
private void SetFlyoutOpen(bool isOpen = true)

0 commit comments

Comments
 (0)