Skip to content

Commit 7859cfc

Browse files
committed
Detect right drag from explorer
1 parent 699e466 commit 7859cfc

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

src/Files.App/ViewModels/Layouts/BaseLayoutViewModel.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
using System.IO;
88
using System.Runtime.InteropServices;
99
using System.Windows.Input;
10+
using Vanara.PInvoke;
1011
using Windows.ApplicationModel.DataTransfer;
1112
using Windows.ApplicationModel.DataTransfer.DragDrop;
1213
using Windows.Storage;
1314
using Windows.System;
15+
using WinRT;
1416

1517
namespace Files.App.ViewModels.Layouts
1618
{
@@ -100,6 +102,10 @@ public async Task DragOverAsync(DragEventArgs e)
100102
return;
101103
}
102104

105+
// Check if this is a right-button drag operation and store into the dataobject
106+
if (e.Modifiers.HasFlag(DragDropModifiers.RightButton))
107+
e.DataView.As<Shell32.IDataObjectProvider>().GetDataObject().SetData<bool>("dragRightButton", true);
108+
103109
if (FilesystemHelpers.HasDraggedStorageItems(e.DataView))
104110
{
105111
e.Handled = true;
@@ -187,7 +193,9 @@ public async Task DropAsync(DragEventArgs e)
187193

188194
try
189195
{
190-
if ((bool)e.DataView.Properties.GetValueOrDefault("dragRightButton", false))
196+
var isRightButtonDrag = e.DataView.As<Shell32.IDataObjectProvider>().GetDataObject().GetData<bool>("dragRightButton");
197+
198+
if (isRightButtonDrag)
191199
{
192200
SafetyExtensions.IgnoreExceptions(() => ShellContextFlyoutFactory.InvokeRightButtonDropMenu(_associatedInstance.ShellViewModel.WorkingDirectory, e.DataView, e.AcceptedOperation));
193201
}

src/Files.App/ViewModels/UserControls/AddressToolbarViewModel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
using Microsoft.UI.Xaml.Input;
1111
using System.IO;
1212
using System.Windows.Input;
13+
using Vanara.PInvoke;
1314
using Windows.ApplicationModel.DataTransfer;
15+
using Windows.ApplicationModel.DataTransfer.DragDrop;
1416
using Windows.UI.Text;
17+
using WinRT;
1518
using FocusManager = Microsoft.UI.Xaml.Input.FocusManager;
1619

1720
namespace Files.App.ViewModels.UserControls
@@ -354,6 +357,10 @@ public async Task PathBoxItem_DragOver(object sender, DragEventArgs e)
354357
}
355358
}
356359

360+
// Check if this is a right-button drag operation and store into the dataobject
361+
if (e.Modifiers.HasFlag(DragDropModifiers.RightButton))
362+
e.DataView.As<Shell32.IDataObjectProvider>().GetDataObject().SetData<bool>("dragRightButton", true);
363+
357364
// In search page
358365
if (!FilesystemHelpers.HasDraggedStorageItems(e.DataView) || string.IsNullOrEmpty(pathBoxItem.Path))
359366
{

src/Files.App/ViewModels/UserControls/SidebarViewModel.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
using System.Collections.Specialized;
1212
using System.IO;
1313
using System.Windows.Input;
14+
using Vanara.PInvoke;
1415
using Windows.ApplicationModel.DataTransfer;
1516
using Windows.ApplicationModel.DataTransfer.DragDrop;
1617
using Windows.Storage;
1718
using Windows.System;
1819
using Windows.UI.Core;
20+
using WinRT;
1921

2022
namespace Files.App.ViewModels.UserControls
2123
{
@@ -1073,6 +1075,10 @@ private async Task HandleLocationItemDragOverAsync(LocationItem locationItem, It
10731075
{
10741076
var rawEvent = args.RawEvent;
10751077

1078+
// Check if this is a right-button drag operation and store into the dataobject
1079+
if (args.RawEvent.Modifiers.HasFlag(DragDropModifiers.RightButton))
1080+
args.DroppedItem.As<Shell32.IDataObjectProvider>().GetDataObject().SetData<bool>("dragRightButton", true);
1081+
10761082
if (Utils.Storage.FilesystemHelpers.HasDraggedStorageItems(args.DroppedItem))
10771083
{
10781084
args.RawEvent.Handled = true;
@@ -1158,6 +1164,10 @@ private async Task HandleDriveItemDragOverAsync(DriveItem driveItem, ItemDragOve
11581164
if (!Utils.Storage.FilesystemHelpers.HasDraggedStorageItems(args.DroppedItem))
11591165
return;
11601166

1167+
// Check if this is a right-button drag operation and store into the dataobject
1168+
if (args.RawEvent.Modifiers.HasFlag(DragDropModifiers.RightButton))
1169+
args.DroppedItem.As<Shell32.IDataObjectProvider>().GetDataObject().SetData<bool>("dragRightButton", true);
1170+
11611171
args.RawEvent.Handled = true;
11621172

11631173
var storageItems = await Utils.Storage.FilesystemHelpers.GetDraggedStorageItems(args.DroppedItem);
@@ -1251,20 +1261,27 @@ private async Task HandleLocationItemDroppedAsync(LocationItem locationItem, Ite
12511261
await QuickAccessService.PinToSidebarAsync(item.Path);
12521262
}
12531263
}
1254-
else if ((bool)args.DroppedItem.Properties.GetValueOrDefault("dragRightButton", false))
1255-
{
1256-
SafetyExtensions.IgnoreExceptions(() => ShellContextFlyoutFactory.InvokeRightButtonDropMenu(locationItem.Path, args.DroppedItem, args.RawEvent.AcceptedOperation));
1257-
}
12581264
else
12591265
{
1260-
await FilesystemHelpers.PerformOperationTypeAsync(args.RawEvent.AcceptedOperation, args.DroppedItem, locationItem.Path, false, true);
1266+
var isRightButtonDrag = args.DroppedItem.As<Shell32.IDataObjectProvider>().GetDataObject().GetData<bool>("dragRightButton");
1267+
1268+
if (isRightButtonDrag)
1269+
{
1270+
SafetyExtensions.IgnoreExceptions(() => ShellContextFlyoutFactory.InvokeRightButtonDropMenu(locationItem.Path, args.DroppedItem, args.RawEvent.AcceptedOperation));
1271+
}
1272+
else
1273+
{
1274+
await FilesystemHelpers.PerformOperationTypeAsync(args.RawEvent.AcceptedOperation, args.DroppedItem, locationItem.Path, false, true);
1275+
}
12611276
}
12621277
}
12631278
}
12641279

12651280
private Task<ReturnResult> HandleDriveItemDroppedAsync(DriveItem driveItem, ItemDroppedEventArgs args)
12661281
{
1267-
if ((bool)args.DroppedItem.Properties.GetValueOrDefault("dragRightButton", false))
1282+
var isRightButtonDrag = args.DroppedItem.As<Shell32.IDataObjectProvider>().GetDataObject().GetData<bool>("dragRightButton");
1283+
1284+
if (isRightButtonDrag)
12681285
{
12691286
SafetyExtensions.IgnoreExceptions(() => ShellContextFlyoutFactory.InvokeRightButtonDropMenu(driveItem.Path, args.DroppedItem, args.RawEvent.AcceptedOperation));
12701287
return Task.FromResult(ReturnResult.Success);

src/Files.App/Views/Layouts/BaseLayoutPage.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,6 @@ private void Item_DragStarting(UIElement sender, DragStartingEventArgs args)
998998

999999
var selectedItems = SelectedItems?.ToList<object>() ?? new();
10001000
selectedItems.AddIfNotPresent(item);
1001-
args.Data.Properties["dragRightButton"] = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.RightButton).HasFlag(CoreVirtualKeyStates.Down);
10021001
args.Cancel = DragItemsStarting(ItemsControl, args.Data, selectedItems);
10031002
}
10041003

@@ -1061,6 +1060,10 @@ private async void Item_DragOver(object sender, DragEventArgs e)
10611060

10621061
DragOperationDeferral? deferral = null;
10631062

1063+
// Check if this is a right-button drag operation and store into the dataobject
1064+
if (e.Modifiers.HasFlag(DragDropModifiers.RightButton))
1065+
e.DataView.As<Shell32.IDataObjectProvider>().GetDataObject().SetData<bool>("dragRightButton", true);
1066+
10641067
try
10651068
{
10661069
deferral = e.GetDeferral();
@@ -1164,7 +1167,9 @@ protected virtual async void Item_Drop(object sender, DragEventArgs e)
11641167
var item = GetItemFromElement(sender);
11651168
if (item is not null)
11661169
{
1167-
if ((bool)e.DataView.Properties.GetValueOrDefault("dragRightButton", false))
1170+
var isRightButtonDrag = e.DataView.As<Shell32.IDataObjectProvider>().GetDataObject().GetData<bool>("dragRightButton");
1171+
1172+
if (isRightButtonDrag)
11681173
{
11691174
SafetyExtensions.IgnoreExceptions(() => ShellContextFlyoutFactory.InvokeRightButtonDropMenu(item.ItemPath, e.DataView, e.AcceptedOperation));
11701175
}

src/Files.App/Views/Shells/BaseShellPage.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
using Microsoft.UI.Xaml.Navigation;
1010
using System.Runtime.CompilerServices;
1111
using System.Runtime.InteropServices;
12+
using Vanara.PInvoke;
1213
using Windows.Foundation.Metadata;
1314
using Windows.System;
1415
using Windows.UI.Core;
16+
using WinRT;
1517
using DispatcherQueueTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
1618

1719
namespace Files.App.Views.Shells
@@ -417,7 +419,9 @@ protected void CoreWindow_PointerPressed(object sender, PointerRoutedEventArgs a
417419

418420
protected async void ShellPage_PathBoxItemDropped(object sender, PathBoxItemDroppedEventArgs e)
419421
{
420-
if ((bool)e.Package.Properties.GetValueOrDefault("dragRightButton", false))
422+
var isRightButtonDrag = e.Package.As<Shell32.IDataObjectProvider>().GetDataObject().GetData<bool>("dragRightButton");
423+
424+
if (isRightButtonDrag)
421425
{
422426
SafetyExtensions.IgnoreExceptions(() => ShellContextFlyoutFactory.InvokeRightButtonDropMenu(e.Path, e.Package, e.AcceptedOperation));
423427
}

0 commit comments

Comments
 (0)