Skip to content
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

Add Delete functionality for Peek #35418

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Fix delete delay. Do not regard user cancellations of permanent delet…
…es as an error, but log them as info anyway. More descriptive name for delete confirmation dialog checkbox.
daverayment committed Jan 28, 2025
commit e03008412860a869e3a9dbb143825a2cb7e79fca
41 changes: 31 additions & 10 deletions src/modules/peek/Peek.UI/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -226,24 +226,36 @@ public void DeleteItem(bool? skipConfirmationChecked, nint hwnd)
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 @@ private int DeleteFile(IFileSystemItem item, nint hwnd)
return result;
}

private void ReinstateDeletedFile(IFileSystemItem item, int index)
{
if (File.Exists(item.Path))
{
_deletedItemIndexes.Remove(index);
OnPropertyChanged(nameof(DisplayItemCount));
}
}

/// <summary>
/// Informs shell listeners like Explorer windows that a delete operation has occurred.
/// </summary>
5 changes: 5 additions & 0 deletions src/modules/peek/Peek.UI/Native/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -94,6 +94,11 @@ internal struct SHFILEOPSTRUCT
/// <remarks>Declared in shellapi.h./remarks>
internal const ushort FOF_WANTNUKEWARNING = 0x4000;

/// <summary>
/// The user cancelled the delete operation. Not classified as an error for our purposes.
/// </summary>
internal const int ERROR_CANCELLED = 1223;

/// <summary>
/// Common error codes when calling SHFileOperation to delete a file.
/// </summary>
2 changes: 1 addition & 1 deletion src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@
DefaultButton="Close">
<StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Spacing="12">
<TextBlock x:Uid="DeleteConfirmationDialog_Message" TextWrapping="Wrap" />
<CheckBox x:Name="DeleteDontAskCheckbox" x:Uid="DeleteConfirmationDialog_DontAskCheckbox" />
<CheckBox x:Name="DeleteDontWarnCheckbox" x:Uid="DeleteConfirmationDialog_DontWarnCheckbox" />
</StackPanel>
</ContentDialog>
</Grid>
7 changes: 3 additions & 4 deletions src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -86,8 +86,8 @@ private async Task DeleteItem()
{
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 @@ private async Task DeleteItem()

private async Task<ContentDialogResult> ShowDeleteConfirmationDialogAsync()
{
DeleteDontAskCheckbox.IsChecked = false;

DeleteDontWarnCheckbox.IsChecked = false;
DeleteConfirmationDialog.XamlRoot = Content.XamlRoot;

return await DeleteConfirmationDialog.ShowAsync();
2 changes: 1 addition & 1 deletion src/modules/peek/Peek.UI/Strings/en-us/Resources.resw
Original file line number Diff line number Diff line change
@@ -369,7 +369,7 @@
<data name="DeleteConfirmationDialog_Message.Text" xml:space="preserve">
<value>Are you sure you want to delete this file?</value>
</data>
<data name="DeleteConfirmationDialog_DontAskCheckbox.Content" xml:space="preserve">
<data name="DeleteConfirmationDialog_DontWarnCheckbox.Content" xml:space="preserve">
<value>Don't show this warning again</value>
</data>
</root>