Skip to content

Commit

Permalink
fix: Potential fix for drag and drop not working for WinRAR (Jorixon#288
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Jorixon authored Nov 10, 2024
1 parent 5c617df commit 45026dc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public bool CanDragDropMod(IReadOnlyList<IStorageItem>? items)

public async Task DragDropModAsync(IReadOnlyList<IStorageItem> items)
{
if (!CanDragDropMod(items))
{
_notificationService.ShowNotification("Drag And Drop operation failed",
"The operation failed because the selected item is not a valid mod file or folder.",
TimeSpan.FromSeconds(5));
return;
}

await CommandWrapperAsync(true, async () =>
{
try
Expand Down Expand Up @@ -77,6 +85,14 @@ public bool CanDragDropModUrl(Uri? uri)

public async Task DragDropModUrlAsync(Uri uri)
{
if (!CanDragDropModUrl(uri))
{
_notificationService.ShowNotification("Drag And Drop operation failed",
"The operation failed because the selected item is not a valid https GameBanana mod URL.",
TimeSpan.FromSeconds(5));
return;
}

await CommandWrapperAsync(true, async () =>
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@
AllowDrop="False"
Background="Transparent"
DragEnter="ModListArea_OnDragEnter"
DragOver="ModListArea_OnDragEnter"
Drop="ModListArea_OnDrop">

<!--
Expand All @@ -300,7 +299,6 @@
Background="Transparent"
CornerRadius="10"
DragEnter="ModListArea_OnDragEnter"
DragOver="ModListArea_OnDragEnter"
Drop="ModListArea_OnDrop">
<characterDetailsPages:ModGrid x:Name="ModGrid" IsEnabled="{x:Bind ViewModel.IsNotHardBusy, Mode=OneWay}" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Windows.ApplicationModel.DataTransfer;
using System.Runtime.InteropServices;
using Windows.ApplicationModel.DataTransfer;
using CommunityToolkit.WinUI.UI.Animations;
using GIMI_ModManager.WinUI.Contracts.Services;
using GIMI_ModManager.WinUI.Helpers.Xaml;
Expand All @@ -11,6 +12,7 @@
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Serilog;

namespace GIMI_ModManager.WinUI.Views.CharacterDetailsPages;

Expand Down Expand Up @@ -220,35 +222,61 @@ private void SearchModsTextBox_OnTextChanged(object sender, TextChangedEventArgs
private async void ModListArea_OnDragEnter(object sender, DragEventArgs e)
{
var deferral = e.GetDeferral();
if (e.DataView.Contains(StandardDataFormats.WebLink))
try
{
var uri = await e.DataView.GetWebLinkAsync();
if (ViewModel.CanDragDropModUrl(uri))
e.AcceptedOperation = DataPackageOperation.Copy;
if (e.DataView.Contains(StandardDataFormats.WebLink))
{
var uri = await e.DataView.GetWebLinkAsync();
if (ViewModel.CanDragDropModUrl(uri))
e.AcceptedOperation = DataPackageOperation.Copy;
}
else if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
try
{
var storageItems = await e.DataView.GetStorageItemsAsync();
if (ViewModel.CanDragDropMod(storageItems))
e.AcceptedOperation = DataPackageOperation.Copy;
}
catch (COMException exception)
{
// When drag and dropping a folder from within an archive in WinRAR, GetStorageItemsAsync throws a COMException
// For this case, assume this is a valid drag and drop operation as the command itself will also check if the items are valid
// when (exception.HResult == -2147221404) HResult that is thrown specifically for WinRAR

e.AcceptedOperation = DataPackageOperation.Copy;

if (exception.HResult != -2147221404)
{
Log.Error(exception, "Error while checking if the dragged items are valid.");
}
}
}
}
else if (e.DataView.Contains(StandardDataFormats.StorageItems))
finally
{
var storageItems = await e.DataView.GetStorageItemsAsync();
if (ViewModel.CanDragDropMod(storageItems))
e.AcceptedOperation = DataPackageOperation.Copy;
deferral.Complete();
}

deferral.Complete();
}

private async void ModListArea_OnDrop(object sender, DragEventArgs e)
{
var deferral = e.GetDeferral();
if (e.DataView.Contains(StandardDataFormats.WebLink))
try
{
await ViewModel.DragDropModUrlAsync(await e.DataView.GetWebLinkAsync());
if (e.DataView.Contains(StandardDataFormats.WebLink))
{
await ViewModel.DragDropModUrlAsync(await e.DataView.GetWebLinkAsync());
}
else if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
await ViewModel.DragDropModAsync(await e.DataView.GetStorageItemsAsync());
}
}
else if (e.DataView.Contains(StandardDataFormats.StorageItems))
finally
{
await ViewModel.DragDropModAsync(await e.DataView.GetStorageItemsAsync());
deferral.Complete();
}

deferral.Complete();
}

private void ViewToggleSwitch_OnToggled(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit 45026dc

Please sign in to comment.