Skip to content

Fix: Fixed COMException in HandleLocationItemDroppedAsync #14680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2024
Merged
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
6 changes: 4 additions & 2 deletions src/Files.App/UserControls/SideBar/ISideBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public interface ISidebarViewModel
/// Gets invoked when an item drags over any item of the sidebar.
/// </summary>
/// <param name="args">The <see cref="ItemDragOverEventArgs"/> for this event.</param>
void HandleItemDragOverAsync(ItemDragOverEventArgs args);
/// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns>
Task HandleItemDragOverAsync(ItemDragOverEventArgs args);

/// <summary>
/// Gets invoked when an item is dropped on any item of the sidebar.
/// </summary>
/// <param name="args">The <see cref="ItemDroppedEventArgs"/> for this event.</param>
void HandleItemDroppedAsync(ItemDroppedEventArgs args);
/// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns>
Task HandleItemDroppedAsync(ItemDroppedEventArgs args);

/// <summary>
/// Gets invoked when an item is invoked (double clicked) on any item of the sidebar.
Expand Down
18 changes: 14 additions & 4 deletions src/Files.App/UserControls/SideBar/SideBarItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private void Item_PointerReleased(object sender, Microsoft.UI.Xaml.Input.Pointer
}
}

private void ItemGrid_DragOver(object sender, DragEventArgs e)
private async void ItemGrid_DragOver(object sender, DragEventArgs e)
{
if (HasChildren)
{
Expand All @@ -407,7 +407,12 @@ private void ItemGrid_DragOver(object sender, DragEventArgs e)
VisualStateManager.GoToState(this, "DragInsertBelow", true);
}

Owner?.RaiseItemDragOver(this, insertsAbove, e);
if (Owner is not null)
{
var deferral = e.GetDeferral();
await Owner.RaiseItemDragOver(this, insertsAbove, e);
deferral.Complete();
}
}

private void ItemGrid_ContextRequested(UIElement sender, Microsoft.UI.Xaml.Input.ContextRequestedEventArgs args)
Expand All @@ -421,10 +426,15 @@ private void ItemGrid_DragLeave(object sender, DragEventArgs e)
UpdatePointerState();
}

private void ItemGrid_Drop(object sender, DragEventArgs e)
private async void ItemGrid_Drop(object sender, DragEventArgs e)
{
UpdatePointerState();
Owner?.RaiseItemDropped(this, DetermineDropTargetPosition(e), e);
if (Owner is not null)
{
var deferral = e.GetDeferral();
await Owner.RaiseItemDropped(this, DetermineDropTargetPosition(e), e);
deferral.Complete();
}
}

private SidebarItemDropPosition DetermineDropTargetPosition(DragEventArgs args)
Expand Down
12 changes: 4 additions & 8 deletions src/Files.App/UserControls/SideBar/SideBarView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public sealed partial class SidebarView : UserControl, INotifyPropertyChanged

private const double COMPACT_MAX_WIDTH = 200;

public event EventHandler<ItemDroppedEventArgs>? ItemDropped;
public event EventHandler<ItemDragOverEventArgs>? ItemDragOver;
public event EventHandler<object>? ItemInvoked;
public event EventHandler<ItemContextInvokedArgs>? ItemContextInvoked;
public event PropertyChangedEventHandler? PropertyChanged;
Expand Down Expand Up @@ -55,18 +53,16 @@ internal void RaiseContextRequested(SidebarItem item, Point e)
ViewModel.HandleItemContextInvokedAsync(item, new ItemContextInvokedArgs(item.Item, e));
}

internal void RaiseItemDropped(SidebarItem sideBarItem, SidebarItemDropPosition dropPosition, DragEventArgs rawEvent)
internal async Task RaiseItemDropped(SidebarItem sideBarItem, SidebarItemDropPosition dropPosition, DragEventArgs rawEvent)
{
if (sideBarItem.Item is null) return;
ItemDropped?.Invoke(sideBarItem, new ItemDroppedEventArgs(sideBarItem.Item, rawEvent.DataView, dropPosition, rawEvent));
ViewModel.HandleItemDroppedAsync(new ItemDroppedEventArgs(sideBarItem.Item, rawEvent.DataView, dropPosition, rawEvent));
await ViewModel.HandleItemDroppedAsync(new ItemDroppedEventArgs(sideBarItem.Item, rawEvent.DataView, dropPosition, rawEvent));
}

internal void RaiseItemDragOver(SidebarItem sideBarItem, SidebarItemDropPosition dropPosition, DragEventArgs rawEvent)
internal async Task RaiseItemDragOver(SidebarItem sideBarItem, SidebarItemDropPosition dropPosition, DragEventArgs rawEvent)
{
if (sideBarItem.Item is null) return;
ItemDragOver?.Invoke(sideBarItem, new ItemDragOverEventArgs(sideBarItem.Item, rawEvent.DataView, dropPosition, rawEvent));
ViewModel.HandleItemDragOverAsync(new ItemDragOverEventArgs(sideBarItem.Item, rawEvent.DataView, dropPosition, rawEvent));
await ViewModel.HandleItemDragOverAsync(new ItemDragOverEventArgs(sideBarItem.Item, rawEvent.DataView, dropPosition, rawEvent));
}

private void UpdateMinimalMode()
Expand Down
63 changes: 15 additions & 48 deletions src/Files.App/ViewModels/UserControls/SidebarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,26 +1077,19 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
}.Where(x => x.ShowItem).ToList();
}

public async void HandleItemDragOverAsync(ItemDragOverEventArgs args)
public async Task HandleItemDragOverAsync(ItemDragOverEventArgs args)
{
if (args.DropTarget is LocationItem locationItem)
{
HandleLocationItemDragOverAsync(locationItem, args);
}
await HandleLocationItemDragOverAsync(locationItem, args);
else if (args.DropTarget is DriveItem driveItem)
{
HandleDriveItemDragOverAsync(driveItem, args);
}
await HandleDriveItemDragOverAsync(driveItem, args);
else if (args.DropTarget is FileTagItem fileTagItem)
{
HandleTagItemDragOverAsync(fileTagItem, args);
}
await HandleTagItemDragOverAsync(fileTagItem, args);
}

private async void HandleLocationItemDragOverAsync(LocationItem locationItem, ItemDragOverEventArgs args)
private async Task HandleLocationItemDragOverAsync(LocationItem locationItem, ItemDragOverEventArgs args)
{
var rawEvent = args.RawEvent;
var deferral = rawEvent.GetDeferral();

if (Utils.Storage.FilesystemHelpers.HasDraggedStorageItems(args.DroppedItem))
{
Expand Down Expand Up @@ -1173,16 +1166,13 @@ private async void HandleLocationItemDragOverAsync(LocationItem locationItem, It
CompleteDragEventArgs(rawEvent, captionText, operationType);
}
}

deferral.Complete();
}

private async void HandleDriveItemDragOverAsync(DriveItem driveItem, ItemDragOverEventArgs args)
private async Task HandleDriveItemDragOverAsync(DriveItem driveItem, ItemDragOverEventArgs args)
{
if (!Utils.Storage.FilesystemHelpers.HasDraggedStorageItems(args.DroppedItem))
return;

var deferral = args.RawEvent.GetDeferral();
args.RawEvent.Handled = true;

var storageItems = await Utils.Storage.FilesystemHelpers.GetDraggedStorageItems(args.DroppedItem);
Expand Down Expand Up @@ -1228,16 +1218,13 @@ private async void HandleDriveItemDragOverAsync(DriveItem driveItem, ItemDragOve
}
CompleteDragEventArgs(args.RawEvent, captionText, operationType);
}

deferral.Complete();
}

private async void HandleTagItemDragOverAsync(FileTagItem tagItem, ItemDragOverEventArgs args)
private async Task HandleTagItemDragOverAsync(FileTagItem tagItem, ItemDragOverEventArgs args)
{
if (!Utils.Storage.FilesystemHelpers.HasDraggedStorageItems(args.DroppedItem))
return;

var deferral = args.RawEvent.GetDeferral();
args.RawEvent.Handled = true;

var storageItems = await Utils.Storage.FilesystemHelpers.GetDraggedStorageItems(args.DroppedItem);
Expand All @@ -1252,32 +1239,23 @@ private async void HandleTagItemDragOverAsync(FileTagItem tagItem, ItemDragOverE
args.RawEvent.DragUIOverride.Caption = string.Format("LinkToFolderCaptionText".GetLocalizedResource(), tagItem.Text);
args.RawEvent.AcceptedOperation = DataPackageOperation.Link;
}

deferral.Complete();
}


public async void HandleItemDroppedAsync(ItemDroppedEventArgs args)
public async Task HandleItemDroppedAsync(ItemDroppedEventArgs args)
{
if (args.DropTarget is LocationItem locationItem)
{
HandleLocationItemDroppedAsync(locationItem, args);
}
await HandleLocationItemDroppedAsync(locationItem, args);
else if (args.DropTarget is DriveItem driveItem)
{
HandleDriveItemDroppedAsync(driveItem, args);
}
await HandleDriveItemDroppedAsync(driveItem, args);
else if (args.DropTarget is FileTagItem fileTagItem)
{
HandleTagItemDroppedAsync(fileTagItem, args);
}
await HandleTagItemDroppedAsync(fileTagItem, args);
}

private async void HandleLocationItemDroppedAsync(LocationItem locationItem, ItemDroppedEventArgs args)
private async Task HandleLocationItemDroppedAsync(LocationItem locationItem, ItemDroppedEventArgs args)
{
if (Utils.Storage.FilesystemHelpers.HasDraggedStorageItems(args.DroppedItem))
{
var deferral = args.RawEvent.GetDeferral();
if (string.IsNullOrEmpty(locationItem.Path) && SectionType.Favorites.Equals(locationItem.Section)) // Pin to Favorites section
{
var storageItems = await Utils.Storage.FilesystemHelpers.GetDraggedStorageItems(args.DroppedItem);
Expand All @@ -1291,24 +1269,16 @@ private async void HandleLocationItemDroppedAsync(LocationItem locationItem, Ite
{
await FilesystemHelpers.PerformOperationTypeAsync(args.RawEvent.AcceptedOperation, args.DroppedItem, locationItem.Path, false, true);
}
deferral.Complete();
}
}

private async void HandleDriveItemDroppedAsync(DriveItem driveItem, ItemDroppedEventArgs args)
private Task HandleDriveItemDroppedAsync(DriveItem driveItem, ItemDroppedEventArgs args)
{
var deferral = args.RawEvent.GetDeferral();

await FilesystemHelpers.PerformOperationTypeAsync(args.RawEvent.AcceptedOperation, args.RawEvent.DataView, driveItem.Path, false, true);

deferral.Complete();
await Task.Yield();
return FilesystemHelpers.PerformOperationTypeAsync(args.RawEvent.AcceptedOperation, args.RawEvent.DataView, driveItem.Path, false, true);
}

private async void HandleTagItemDroppedAsync(FileTagItem fileTagItem, ItemDroppedEventArgs args)
private async Task HandleTagItemDroppedAsync(FileTagItem fileTagItem, ItemDroppedEventArgs args)
{
var deferral = args.RawEvent.GetDeferral();

var storageItems = await Utils.Storage.FilesystemHelpers.GetDraggedStorageItems(args.DroppedItem);
foreach (var item in storageItems.Where(x => !string.IsNullOrEmpty(x.Path)))
{
Expand All @@ -1319,9 +1289,6 @@ private async void HandleTagItemDroppedAsync(FileTagItem fileTagItem, ItemDroppe
FileTags = new[] { fileTagItem.FileTag.Uid }
};
}

deferral.Complete();
await Task.Yield();
}

private static DragEventArgs CompleteDragEventArgs(DragEventArgs e, string captionText, DataPackageOperation operationType)
Expand Down