Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ public async Task<IStorageHistory> CopyItemsAsync(IList<IStorageItemWithPath> so
var copyResult = new ShellOperationResult();
if (sourceRename.Any())
{
var resultItem = await FileOperationsHelpers.CopyItemAsync(sourceRename.Select(s => s.Path).ToArray(), destinationRename.ToArray(), false, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID);
var resultItem = await FileOperationsHelpers.CopyItemAsync(sourceRename.Select(s => s.Path).ToArray(), destinationRename.ToArray(), false, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID, progress);

result &= (FilesystemResult)resultItem.Item1;

copyResult.Items.AddRange(resultItem.Item2?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
}
if (sourceReplace.Any())
{
var resultItem = await FileOperationsHelpers.CopyItemAsync(sourceReplace.Select(s => s.Path).ToArray(), destinationReplace.ToArray(), true, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID);
var resultItem = await FileOperationsHelpers.CopyItemAsync(sourceReplace.Select(s => s.Path).ToArray(), destinationReplace.ToArray(), true, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID, progress);

result &= (FilesystemResult)resultItem.Item1;

Expand Down Expand Up @@ -330,7 +330,7 @@ public async Task<IStorageHistory> DeleteItemsAsync(IList<IStorageItemWithPath>
var operationID = Guid.NewGuid().ToString();
using var r = cancellationToken.Register(CancelOperation, operationID, false);

var (success, deleteResult) = await FileOperationsHelpers.DeleteItemAsync(deleleFilePaths.ToArray(), permanently, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID);
var (success, deleteResult) = await FileOperationsHelpers.DeleteItemAsync(deleleFilePaths.ToArray(), permanently, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID, progress);

var result = (FilesystemResult)success;
var shellOpResult = deleteResult;
Expand Down Expand Up @@ -442,14 +442,14 @@ public async Task<IStorageHistory> MoveItemsAsync(IList<IStorageItemWithPath> so
var moveResult = new ShellOperationResult();
if (sourceRename.Any())
{
var (status, response) = await FileOperationsHelpers.MoveItemAsync(sourceRename.Select(s => s.Path).ToArray(), destinationRename.ToArray(), false, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID);
var (status, response) = await FileOperationsHelpers.MoveItemAsync(sourceRename.Select(s => s.Path).ToArray(), destinationRename.ToArray(), false, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID, progress);

result &= (FilesystemResult)status;
moveResult.Items.AddRange(response?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
}
if (sourceReplace.Any())
{
var (status, response) = await FileOperationsHelpers.MoveItemAsync(sourceReplace.Select(s => s.Path).ToArray(), destinationReplace.ToArray(), true, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID);
var (status, response) = await FileOperationsHelpers.MoveItemAsync(sourceReplace.Select(s => s.Path).ToArray(), destinationReplace.ToArray(), true, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID, progress);

result &= (FilesystemResult)status;
moveResult.Items.AddRange(response?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
Expand Down Expand Up @@ -643,7 +643,7 @@ public async Task<IStorageHistory> RestoreItemsFromTrashAsync(IList<IStorageItem
using var r = cancellationToken.Register(CancelOperation, operationID, false);

var moveResult = new ShellOperationResult();
var (status, response) = await FileOperationsHelpers.MoveItemAsync(source.Select(s => s.Path).ToArray(), destination.ToArray(), false, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID);
var (status, response) = await FileOperationsHelpers.MoveItemAsync(source.Select(s => s.Path).ToArray(), destination.ToArray(), false, NativeWinApiHelper.CoreWindowHandle.ToInt64(), operationID, progress);
Copy link
Member

@yaira2 yaira2 Oct 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little beyond the scope of this PR but it be good to consolidate this code.


var result = (FilesystemResult)status;
moveResult.Items.AddRange(response?.Final ?? Enumerable.Empty<ShellOperationItemResult>());
Expand Down Expand Up @@ -718,19 +718,6 @@ await sourceMatch.Select(x => x.src).ToListAsync(),
}
}

private void OnProgressUpdated(object sender, Dictionary<string, JsonElement> message, string currentOperation, IProgress<float> progress)
{
if (message.ContainsKey("OperationID"))
{
var operationID = message["OperationID"].GetString();
if (operationID == currentOperation)
{
var value = message["Progress"].GetInt64();
progress?.Report(value);
}
}
}

private async void CancelOperation(object operationID)
=> FileOperationsHelpers.TryCancelOperation((string)operationID);

Expand Down
9 changes: 6 additions & 3 deletions src/Files.App/Helpers/FileOperationsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public static Task<bool> DragDropAsync(string dropPath)
});
}

public static Task<(bool, ShellOperationResult)> DeleteItemAsync(string[] fileToDeletePath, bool permanently, long ownerHwnd, string operationID = "")
public static Task<(bool, ShellOperationResult)> DeleteItemAsync(string[] fileToDeletePath, bool permanently, long ownerHwnd, string operationID = "", IProgress<float>? progress = default)
{
operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID;

Expand Down Expand Up @@ -285,6 +285,7 @@ public static Task<bool> DragDropAsync(string dropPath)
{
throw new Win32Exception(unchecked((int)0x80004005)); // E_FAIL, stops operation
}
progress?.Report(e.ProgressPercentage);
progressHandler.UpdateOperation(operationID, e.ProgressPercentage);
};

Expand Down Expand Up @@ -362,7 +363,7 @@ public static Task<bool> DragDropAsync(string dropPath)
});
}

public static Task<(bool, ShellOperationResult)> MoveItemAsync(string[] fileToMovePath, string[] moveDestination, bool overwriteOnMove, long ownerHwnd, string operationID = "")
public static Task<(bool, ShellOperationResult)> MoveItemAsync(string[] fileToMovePath, string[] moveDestination, bool overwriteOnMove, long ownerHwnd, string operationID = "", IProgress<float>? progress = default)
{
operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID;

Expand Down Expand Up @@ -419,6 +420,7 @@ public static Task<bool> DragDropAsync(string dropPath)
{
throw new Win32Exception(unchecked((int)0x80004005)); // E_FAIL, stops operation
}
progress?.Report(e.ProgressPercentage);
progressHandler.UpdateOperation(operationID, e.ProgressPercentage);
};

Expand All @@ -437,7 +439,7 @@ public static Task<bool> DragDropAsync(string dropPath)
});
}

public static Task<(bool, ShellOperationResult)> CopyItemAsync(string[] fileToCopyPath, string[] copyDestination, bool overwriteOnCopy, long ownerHwnd, string operationID = "")
public static Task<(bool, ShellOperationResult)> CopyItemAsync(string[] fileToCopyPath, string[] copyDestination, bool overwriteOnCopy, long ownerHwnd, string operationID = "", IProgress<float>? progress = default)
{
operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID;

Expand Down Expand Up @@ -495,6 +497,7 @@ public static Task<bool> DragDropAsync(string dropPath)
{
throw new Win32Exception(unchecked((int)0x80004005)); // E_FAIL, stops operation
}
progress?.Report(e.ProgressPercentage);
progressHandler.UpdateOperation(operationID, e.ProgressPercentage);
};

Expand Down