Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gave92 committed Aug 13, 2021
1 parent 69e827b commit 4ce550f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 73 deletions.
21 changes: 6 additions & 15 deletions Files/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
using Files.Views;
using Microsoft.Toolkit.Uwp;
using Microsoft.Toolkit.Uwp.UI;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
Expand All @@ -27,7 +25,6 @@
using Windows.ApplicationModel.DataTransfer.DragDrop;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
Expand Down Expand Up @@ -843,28 +840,22 @@ protected async void Item_DragOver(object sender, DragEventArgs e)
}, TimeSpan.FromMilliseconds(1000), false);
}

var (hResult, draggedItems) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);
if (hResult == 1)
if (Filesystem.FilesystemHelpers.HasDraggedStorageItems(e.DataView))
{
e.Handled = true;
if (InstanceViewModel.IsPageTypeSearchResults)

var (handledByFtp, draggedItems) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);

if (InstanceViewModel.IsPageTypeSearchResults || draggedItems.Any(draggedItem => draggedItem.Path == item.ItemPath))
{
e.AcceptedOperation = DataPackageOperation.None;
}
else
else if (handledByFtp)
{
e.DragUIOverride.IsCaptionVisible = true;
e.DragUIOverride.Caption = string.Format("CopyToFolderCaptionText".GetLocalized(), item.ItemName);
e.AcceptedOperation = DataPackageOperation.Copy;
}
}
else if (draggedItems.Any())
{
e.Handled = true;
if (InstanceViewModel.IsPageTypeSearchResults || draggedItems.Any(draggedItem => draggedItem.Path == item.ItemPath))
{
e.AcceptedOperation = DataPackageOperation.None;
}
else
{
e.DragUIOverride.IsCaptionVisible = true;
Expand Down
45 changes: 25 additions & 20 deletions Files/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -657,18 +657,20 @@ public async Task<ReturnResult> CopyItemAsync(IStorageItemWithPath source, strin

public async Task<ReturnResult> RecycleItemsFromClipboard(DataPackageView packageView, string destination, bool showDialog, bool registerHistory)
{
var (hResult, source) = await GetDraggedStorageItems(packageView);
if (hResult == 1)
{
// Not supported
return ReturnResult.Failed;
}
if (!source.Any())
if (!HasDraggedStorageItems(packageView))
{
// Happens if you copy some text and then you Ctrl+V in Files
return ReturnResult.BadArgumentException;
}

var (handledByFtp, source) = await GetDraggedStorageItems(packageView);

if (handledByFtp)
{
// Not supported
return ReturnResult.Failed;
}

ReturnResult returnStatus = ReturnResult.InProgress;

source = source.Where(x => !recycleBinHelpers.IsPathUnderRecycleBin(x.Path)).ToList(); // Can't recycle items already in recyclebin
Expand All @@ -679,8 +681,9 @@ public async Task<ReturnResult> RecycleItemsFromClipboard(DataPackageView packag

public async Task<ReturnResult> CopyItemsFromClipboard(DataPackageView packageView, string destination, bool showDialog, bool registerHistory)
{
var (hResult, source) = await GetDraggedStorageItems(packageView);
if (hResult == 1)
var (handledByFtp, source) = await GetDraggedStorageItems(packageView);

if (handledByFtp)
{
var connection = await ServiceConnection;
if (connection != null)
Expand Down Expand Up @@ -899,18 +902,20 @@ public async Task<ReturnResult> MoveItemAsync(IStorageItemWithPath source, strin

public async Task<ReturnResult> MoveItemsFromClipboard(DataPackageView packageView, string destination, bool showDialog, bool registerHistory)
{
var (hResult, source) = await GetDraggedStorageItems(packageView);
if (hResult == 1)
{
// Not supported
return ReturnResult.Failed;
}
if (!source.Any())
if (!HasDraggedStorageItems(packageView))
{
// Happens if you copy some text and then you Ctrl+V in Files
return ReturnResult.BadArgumentException;
}

var (handledByFtp, source) = await GetDraggedStorageItems(packageView);

if (handledByFtp)
{
// Not supported
return ReturnResult.Failed;
}

ReturnResult returnStatus = ReturnResult.InProgress;

var destinations = new List<string>();
Expand Down Expand Up @@ -1087,7 +1092,7 @@ public static bool HasDraggedStorageItems(DataPackageView packageView)
return packageView.Contains(StandardDataFormats.StorageItems) || (packageView.Properties.TryGetValue("FileDrop", out var data));
}

public static async Task<(int hResult, IEnumerable<IStorageItemWithPath> items)> GetDraggedStorageItems(DataPackageView packageView)
public static async Task<(bool handledByFtp, IEnumerable<IStorageItemWithPath> items)> GetDraggedStorageItems(DataPackageView packageView)
{
var itemsList = new List<IStorageItemWithPath>();
if (packageView.Contains(StandardDataFormats.StorageItems))
Expand All @@ -1099,12 +1104,12 @@ public static bool HasDraggedStorageItems(DataPackageView packageView)
}
catch (Exception ex) when ((uint)ex.HResult == 0x80040064 || (uint)ex.HResult == 0x8004006A)
{
return (1, itemsList);
return (true, itemsList);
}
catch (Exception ex)
{
App.Logger.Warn(ex, ex.Message);
return (0, itemsList);
return (false, itemsList);
}
}
if (packageView.Properties.TryGetValue("FileDrop", out var data))
Expand All @@ -1114,7 +1119,7 @@ public static bool HasDraggedStorageItems(DataPackageView packageView)
itemsList.AddRange(source);
}
}
return (0, itemsList);
return (false, itemsList);
}

public static bool ContainsRestrictedCharacters(string input)
Expand Down
28 changes: 10 additions & 18 deletions Files/Interacts/BaseLayoutCommandImplementationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,22 +516,24 @@ public virtual void GridViewSizeIncrease(KeyboardAcceleratorInvokedEventArgs e)
public virtual async void DragOver(DragEventArgs e)
{
var deferral = e.GetDeferral();

itemManipulationModel.ClearSelection();

var pwd = associatedInstance.FilesystemViewModel.WorkingDirectory.TrimPath();
var folderName = (Path.IsPathRooted(pwd) && Path.GetPathRoot(pwd) == pwd) ? Path.GetPathRoot(pwd) : Path.GetFileName(pwd);

var (hResult, draggedItems) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);
if (hResult == 1)
if (Filesystem.FilesystemHelpers.HasDraggedStorageItems(e.DataView))
{
e.Handled = true;

var (handledByFtp, draggedItems) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);

var pwd = associatedInstance.FilesystemViewModel.WorkingDirectory.TrimPath();
var folderName = (Path.IsPathRooted(pwd) && Path.GetPathRoot(pwd) == pwd) ? Path.GetPathRoot(pwd) : Path.GetFileName(pwd);

// As long as one file doesn't already belong to this folder
if (associatedInstance.InstanceViewModel.IsPageTypeSearchResults)
if (associatedInstance.InstanceViewModel.IsPageTypeSearchResults || (draggedItems.Any() && draggedItems.AreItemsAlreadyInFolder(associatedInstance.FilesystemViewModel.WorkingDirectory)))
{
e.AcceptedOperation = DataPackageOperation.None;
}
else
else if (handledByFtp)
{
if (pwd.StartsWith(App.AppSettings.RecycleBinPath))
{
Expand All @@ -544,16 +546,6 @@ public virtual async void DragOver(DragEventArgs e)
e.AcceptedOperation = DataPackageOperation.Copy;
}
}
}
else if (draggedItems.Any())
{
e.Handled = true;

// As long as one file doesn't already belong to this folder
if (associatedInstance.InstanceViewModel.IsPageTypeSearchResults || (draggedItems.Any() && draggedItems.AreItemsAlreadyInFolder(associatedInstance.FilesystemViewModel.WorkingDirectory)))
{
e.AcceptedOperation = DataPackageOperation.None;
}
else
{
e.DragUIOverride.IsCaptionVisible = true;
Expand Down
25 changes: 8 additions & 17 deletions Files/UserControls/SidebarControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,17 +554,19 @@ private async void NavigationViewLocationItem_DragOver(object sender, DragEventA

var deferral = e.GetDeferral();

var (hResult, storageItems) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);
if (hResult == 1)
if (Filesystem.FilesystemHelpers.HasDraggedStorageItems(e.DataView))
{
e.Handled = true;

var (handledByFtp, storageItems) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);

if (string.IsNullOrEmpty(locationItem.Path) ||
locationItem.Path.StartsWith("Home".GetLocalized(), StringComparison.OrdinalIgnoreCase))
(storageItems.Any() && storageItems.AreItemsAlreadyInFolder(locationItem.Path))
|| locationItem.Path.StartsWith("Home".GetLocalized(), StringComparison.OrdinalIgnoreCase))
{
e.AcceptedOperation = DataPackageOperation.None;
}
else
else if (handledByFtp)
{
if (locationItem.Path.StartsWith(App.AppSettings.RecycleBinPath))
{
Expand All @@ -577,17 +579,6 @@ private async void NavigationViewLocationItem_DragOver(object sender, DragEventA
e.AcceptedOperation = DataPackageOperation.Copy;
}
}
}
else if (storageItems.Any())
{
e.Handled = true;

if (string.IsNullOrEmpty(locationItem.Path) ||
(storageItems.Any() && storageItems.AreItemsAlreadyInFolder(locationItem.Path))
|| locationItem.Path.StartsWith("Home".GetLocalized(), StringComparison.OrdinalIgnoreCase))
{
e.AcceptedOperation = DataPackageOperation.None;
}
else
{
e.DragUIOverride.IsCaptionVisible = true;
Expand Down Expand Up @@ -693,14 +684,14 @@ private async void NavigationViewDriveItem_DragOver(object sender, DragEventArgs
var deferral = e.GetDeferral();
e.Handled = true;

var (hResult, storageItems) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);
var (handledByFtp, storageItems) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);

if ("DriveCapacityUnknown".GetLocalized().Equals(driveItem.SpaceText, StringComparison.OrdinalIgnoreCase) ||
(storageItems.Any() && storageItems.AreItemsAlreadyInFolder(driveItem.Path)))
{
e.AcceptedOperation = DataPackageOperation.None;
}
else if (hResult == 1)
else if (handledByFtp)
{
e.DragUIOverride.IsCaptionVisible = true;
e.DragUIOverride.Caption = string.Format("CopyToFolderCaptionText".GetLocalized(), driveItem.Text);
Expand Down
4 changes: 2 additions & 2 deletions Files/ViewModels/NavToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ public async void PathBoxItem_DragOver(object sender, DragEventArgs e)
e.Handled = true;
var deferral = e.GetDeferral();

var (hResult, storageItems) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);
if (hResult == 1)
var (handledByFtp, storageItems) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);
if (handledByFtp)
{
e.AcceptedOperation = DataPackageOperation.None;
deferral.Complete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private async Task Drop(DragEventArgs e)

if (Filesystem.FilesystemHelpers.HasDraggedStorageItems(e.DataView))
{
var (hResult, items) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);
var (_, items) = await Filesystem.FilesystemHelpers.GetDraggedStorageItems(e.DataView);

if (await AddItemsFromPath(items.ToDictionary((item) => item.Path, (item) => item.ItemType)))
{
Expand Down

0 comments on commit 4ce550f

Please sign in to comment.