diff --git a/src/modules/peek/Peek.UI/MainWindowViewModel.cs b/src/modules/peek/Peek.UI/MainWindowViewModel.cs index ac46365f8b..3bd809630d 100644 --- a/src/modules/peek/Peek.UI/MainWindowViewModel.cs +++ b/src/modules/peek/Peek.UI/MainWindowViewModel.cs @@ -226,24 +226,36 @@ namespace Peek.UI OnPropertyChanged(nameof(DisplayItemCount)); // Attempt the deletion then navigate to the next file. - DispatcherQueue.GetForCurrentThread().TryEnqueue(() => + DispatcherQueue.GetForCurrentThread().TryEnqueue(async () => { - Task.Delay(DeleteDelayMs); + await Task.Delay(DeleteDelayMs); int result = DeleteFile(item, hwnd); - if (result != 0) + if (result == 0) { - // On failure, log the error, show a message in the UI, and reinstate the - // deleted file if it still exists. - DeleteErrorMessageHelper.LogError(result); - ShowDeleteError(item.Name, result); + // Success. + return; + } - if (File.Exists(item.Path)) + if (result == ERROR_CANCELLED) + { + if (Path.GetPathRoot(item.Path) is string root) { - _deletedItemIndexes.Remove(index); - OnPropertyChanged(nameof(DisplayItemCount)); + var driveInfo = new DriveInfo(root); + Logger.LogInfo($"User cancelled deletion of \"{item.Name}\" on " + + $"{driveInfo.DriveType} drive."); } } + else + { + // For failures other than user cancellation, log the error and show a message + // in the UI. + DeleteErrorMessageHelper.LogError(result); + ShowDeleteError(item.Name, result); + } + + // For all errors, reinstate the deleted file if it still exists. + ReinstateDeletedFile(item, index); }); Navigate(_navigationDirection, isAfterDelete: true); @@ -278,6 +290,15 @@ namespace Peek.UI return result; } + private void ReinstateDeletedFile(IFileSystemItem item, int index) + { + if (File.Exists(item.Path)) + { + _deletedItemIndexes.Remove(index); + OnPropertyChanged(nameof(DisplayItemCount)); + } + } + /// /// Informs shell listeners like Explorer windows that a delete operation has occurred. /// diff --git a/src/modules/peek/Peek.UI/Native/NativeMethods.cs b/src/modules/peek/Peek.UI/Native/NativeMethods.cs index 1fe0035f3e..35040e1648 100644 --- a/src/modules/peek/Peek.UI/Native/NativeMethods.cs +++ b/src/modules/peek/Peek.UI/Native/NativeMethods.cs @@ -94,6 +94,11 @@ namespace Peek.UI.Native /// Declared in shellapi.h./remarks> internal const ushort FOF_WANTNUKEWARNING = 0x4000; + /// + /// The user cancelled the delete operation. Not classified as an error for our purposes. + /// + internal const int ERROR_CANCELLED = 1223; + /// /// Common error codes when calling SHFileOperation to delete a file. /// diff --git a/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml b/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml index 78879bc74c..539baf9471 100644 --- a/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml +++ b/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml @@ -69,7 +69,7 @@ DefaultButton="Close"> - + diff --git a/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml.cs b/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml.cs index 461ca227c1..5af4a7bce2 100644 --- a/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml.cs +++ b/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml.cs @@ -86,8 +86,8 @@ namespace Peek.UI { if (await ShowDeleteConfirmationDialogAsync() == ContentDialogResult.Primary) { - // Delete after asking for confirmation. Persist the "Don't ask again" choice if set. - ViewModel.DeleteItem(DeleteDontAskCheckbox.IsChecked, this.GetWindowHandle()); + // Delete after asking for confirmation. Persist the "Don't warn again" choice if set. + ViewModel.DeleteItem(DeleteDontWarnCheckbox.IsChecked, this.GetWindowHandle()); } } else @@ -104,8 +104,7 @@ namespace Peek.UI private async Task ShowDeleteConfirmationDialogAsync() { - DeleteDontAskCheckbox.IsChecked = false; - + DeleteDontWarnCheckbox.IsChecked = false; DeleteConfirmationDialog.XamlRoot = Content.XamlRoot; return await DeleteConfirmationDialog.ShowAsync(); diff --git a/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw b/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw index b24e866c87..4ffec5f685 100644 --- a/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw +++ b/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw @@ -369,7 +369,7 @@ Are you sure you want to delete this file? - + Don't show this warning again \ No newline at end of file