Skip to content

Commit 6657fae

Browse files
authored
Feature: Added drag and drop for sidebar items (#18236)
1 parent ae18e7e commit 6657fae

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

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

Lines changed: 27 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,30 @@ 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.Move | DataPackageOperation.Copy | DataPackageOperation.Link;
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+
}
159+
catch
160+
{
161+
}
162+
finally
163+
{
164+
deferral.Complete();
165+
}
166+
});
141167
}
142168

143169
private void SetFlyoutOpen(bool isOpen = true)

0 commit comments

Comments
 (0)