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 10 commits into
base: main
Choose a base branch
from

Conversation

daverayment
Copy link
Contributor

@daverayment daverayment commented Oct 13, 2024

Summary of the Pull Request

This PR adds the ability for users to delete the file currently being previewed in Peek.

PR Checklist

Detailed Description of the Pull Request / Additional comments

There are several new and changed elements in this PR:

  • Navigation has been updated to track forward and backward movement through files. This is necessary so deleting a file always shows the next item in the natural sequence, i.e. the previous item if the user was moving backwards. This replicates Windows Photo and other media viewers.
  • The indexes of deleted items are recorded rather than altering the Items collection (which could be very large). There are now DisplayIndex and DisplayItemCount properties, which reflect the calculated position and total. The old CurrentIndex still points to the actual index of the current item in the collection, but this is now internal only.
  • The MainWindow ViewModel includes quite a bit of navigation-related code. We may wish to split this out into a separate model or provider in the future.
  • When previewing a selection of items (where the number and total are shown in the title bar), these counts will always be shown. Previously, this info would not show if the count was 1. (This was likely because opening Peek while a single item is selected expands the Items collection to cover the entire folder and turns off the "(position/total)" display.)
  • A text message has been added for when there are no more files remaining, i.e. the user has deleted the last one from their selection (or from the current folder if Peek wasn't opened via multi-selection). It may be worthwhile to add a simple icon by the side here - perhaps an image placeholder graphic? - but I've gone with the simple approach for now.
  • Changes were made to the title bar code to ensure that the last filename and "open in application" details were not shown when there are no more files to preview. This was because there was an (entirely reasonable!) assumption in the code that there would always be one or more files being previewed.
  • The delete operation sends the item to the Recycle Bin only. It was considered whether pressing Shift + Delete could do a permanent delete, and this is certainly possible in the future, but this release errs on the side of caution, especially as it's a new feature and involves deleting things.
  • The Delete function itself relies upon a shell file operation, reflecting the fact that we're previewing shell items. This required adding some code to NativeMethods.cs for the API call, struct, flags, and error messages.
  • Only file deletion is allowed. I considered allowing for empty folder deletes, too, but, again, this first release is deliberately conservative.
  • Updated Peek documentation.

Screenshots

No More Files message

Screenshot 2024-10-13 230651 (Small)

File Count For Single Remaining File

image

Validation Steps Performed

(Manual tests only.)

Tested:

  • Single and multi-selection methods of opening Peek.
  • Deleting from beginning, end and middle of a sequence (after opening Peek via multi-selection).
  • Deleting until no files remained.
  • Previewing a folder containing a mix of files and folders.
  • Previewing a folder containing a mix of files which can be previewed and unsupported file types.
  • Attempting to delete folders via Peek.
  • Deleting a file in the selected list via Explorer before it could be previewed.

Removed package updates, as these are not relevant to the new functionality.
…rce text. Also altered the text style to be consistent with the FailedFallbackPreviewControl error page.
src/modules/peek/Peek.FilePreviewer/FilePreview.xaml Outdated Show resolved Hide resolved
src/modules/peek/Peek.UI/MainWindowViewModel.cs Outdated Show resolved Hide resolved
{
wFunc = FO_DELETE,
pFrom = path + "\0\0",
fFlags = (ushort)(FOF_NOCONFIRMATION | (permanent ? 0 : FOF_ALLOWUNDO)),
Copy link
Collaborator

@htcfreek htcfreek Oct 14, 2024

Choose a reason for hiding this comment

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

Does this override the recycle bin property? (It is possible to configure the delete confirmation in the recycle bin properties.)

How does Fotos app handle it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This replicates how the Photos application handles deletes. Pressing Delete immediately sends the file to the Recycle Bin without popping up a message, and then it moves on to the next file.

The permanent argument is there for possible future use, but is always false for now.

src/modules/peek/Peek.UI/MainWindowViewModel.cs Outdated Show resolved Hide resolved
@htcfreek
Copy link
Collaborator

@daverayment
Please fix the merge conflicts. (This is the reason for the failing test.)

This comment has been minimized.

@daverayment
Copy link
Contributor Author

@htcfreek

Thanks for your comments and suggestions.

I've updated the code with an error InfoBar, which is shown when a delete fails:

image

I've added user-friendly messages for the most common failures. Others result in a generic message being shown on the InfoBar.

The failure is also logged, with the code and a more technical error message (taken from winerror.h).

If the file still exists after the deletion attempt, it is kept in the items collection for previewing; otherwise it is removed.

An aside: Windows Photo handles files being renamed or deleted differently to Peek, in that it seems to constantly monitor the folder. (Try changing the filename of a photo you're previewing and you can see it update in the UI after a second or two.) We may wish to replicate this functionality in the future, as it would be increase robustness, but that it outside of the scope of this issue.

@crutkas crutkas added the Needs-Review This Pull Request awaits the review of a maintainer. label Nov 20, 2024
Copy link
Collaborator

@jaimecbernardo jaimecbernardo left a comment

Choose a reason for hiding this comment

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

Thank you for opening the pull request.
Generally looks good to me, but there's the issue that this might be unintentionally pressed by the user, getting a file deleted he might not have intended. Even though it ends up in the Recycling Bin, I think having a Message Box Prompt first would be safer. I'm OK with a setting to disable this warning, but it should show a confirmation prompt by default.
Hope this makes sense.

@Jay-o-Way
Copy link
Collaborator

Even though it ends up in the Recycling Bin, I think having a Message Box Prompt first would be safer.

Agree. Could also try to get/use the setting for the Recycle Bin itself.

@htcfreek
Copy link
Collaborator

htcfreek commented Dec 2, 2024

Even though it ends up in the Recycling Bin, I think having a Message Box Prompt first would be safer.

Agree. Could also try to get/use the setting for the Recycle Bin itself.

Doesn't the code does this yet? Or better asked: Isn't this handled by Recycle Bin itself?

@Jay-o-Way
Copy link
Collaborator

Doesn't the code does this yet? Or better asked: Isn't this handled by Recycle Bin itself?

Nope, the code uses DeleteFile(item) -> FO_DELETE and that's an explicit action. I'd say the setting is an Explorer setting.

@daverayment
Copy link
Contributor Author

@jaimecbernardo @htcfreek @Jay-o-Way

Thank you for the review and guidance. However, I disagree with displaying a prompt.

The default Windows functionality for file deletions is not to show a confirmation prompt. The whole purpose of the Recycle Bin is to to protect against accidental deletions. The behaviour in Peek was deliberately chosen to be consistent with both Windows Explorer and other file viewer tools such as the Windows Photo App and third party apps like IrfanView. I believe users would find a confirmation dialog unnecessary or redundant every time they want to delete a file, which could be a common action when sorting through a large folder of files. Also, PowerToys is supposed to be for power users, so I don't think we should go out of our way to provide guardrails for easily-reversible common mistakes such as accidentally pressing the delete key (which is also not near the default Peek navigation keys).

To summarise, I don't think we should provide non-default behaviour which would slow down a user's workflow.

@jaimecbernardo
Copy link
Collaborator

I've tried the Windows "Photos" app, it does show a confirmation prompt:
image

@jaimecbernardo
Copy link
Collaborator

I imagine you might've already clicked that "Don't show this warning again" @daverayment .
What I'm proposing is having a prompt such as this one from the Windows "Photos" app. But also a setting to allow the prompt to not be shown, but I think the prompt should be shown by default.

@jaimecbernardo
Copy link
Collaborator

I've also just tried the feature on an external drive. As expected, files were permanently deleted without going to the recycle bin. I don't feel easy causing potential data loss with a feature that wasn't there before without some initial guardrails so the user understands that feature is there 😅 Hope this makes sense to you.

@daverayment
Copy link
Contributor Author

Thanks, @jaimecbernardo

Sorry, yes, you're right! I must have clicked the "Don't Show" checkbox when I initially started using Windows Photo. Please forgive the lapse in memory - it was a long time ago... 😬

My main concern was that presenting dialogs every time a delete was requested would get in the way, but I'm fine with your suggestion of replicating the Photo app's behaviour with the "Don't Show" checkbox. This will give me a chance to have a play with the Settings code, too, which I've been avoiding.

I'm very concerned that the delete operation skipped the Recycle Bin for a file on your external device. Would you mind seeing what your Recycle Bin properties are for that drive, please? It is possible to turn off the Recycle Bin on a per-drive basis:

image

The Peek code just calls the underlying Shell API, so it should be consistent with doing a delete from Explorer (without holding Shift down); it is a bug if it behaves differently for external drives.

@Jay-o-Way
Copy link
Collaborator

Jay-o-Way commented Dec 3, 2024

@daverayment using a delete function in most code languages (like DeleteFile(item) -> FO_DELETE) is very explicit - it doesn't care about a recycling bin. Code languages often have a "FileRecycle" function too, but...

Sadly, C# doesn't have a native API to delete files and folders to Recycle Bin. But Visual Basic has.

https://www.c-sharpcorner.com/blogs/extension-methods-for-delete-files-and-folders-to-recycle-bin

Also check out

@jaimecbernardo
Copy link
Collaborator

I'm very concerned that the delete operation skipped the Recycle Bin for a file on your external device. Would you mind seeing what your Recycle Bin properties are for that drive, please? It is possible to turn off the Recycle Bin on a per-drive basis:

image

@daverayment There's nothing on Recycle Bin for this drive and another I've tested. I'm guessing because their file system is FAT32.

@jaimecbernardo
Copy link
Collaborator

Here's the last two Settings added to Peek as a reference. Thank you 😉
#26308
#26364

@htcfreek
Copy link
Collaborator

htcfreek commented Dec 3, 2024

@daverayment , @jaimecbernardo
For best user behavior the setting should be controllable from the dialogue in Peek (disable message) and the settings page (disable and enable) in PT Settings. Is this possible?

@daverayment
Copy link
Contributor Author

daverayment commented Dec 6, 2024

@daverayment using a delete function in most code languages (like DeleteFile(item) -> FO_DELETE) is very explicit - it doesn't care about a recycling bin. Code languages often have a "FileRecycle" function too, but...

Sadly, C# doesn't have a native API to delete files and folders to Recycle Bin. But Visual Basic has.

Hi, @Jay-o-Way !

Thanks for the message. It is certainly a shame that System.IO doesn't include recycling options when deleting files.

As you noticed, this update to Peek uses the Shell File Operation FO_DELETE. This is also used internally by Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile() (see the VB Core library source here). FO_DELETE does support a flag to control whether it attempts to move the file to the Recycle Bin (FOF_ALLOWUNDO) and various others to control which progress and error messages are shown.

This isn't enough for our needs, though, so I will add the dialog box to prompt the user when they try to delete a file, as previously discussed. Giving users the option to permanently dismiss this warning replicates what the Windows Photo App does, and is a good compromise for power users.

The reason @jaimecbernardo experienced the issue with the FAT32 volume is because I needed to include a flag called FOF_WANTNUKEWARNING, which is very well named! I will correct that in the PR. Thank you for the catch, Jaime!

The VB Core library containing DeleteFile() is 1.2 MB, so I'm keen to just call the Shell API directly to reduce the potential bloat.

@daverayment
Copy link
Contributor Author

@daverayment , @jaimecbernardo For best user behavior the setting should be controllable from the dialogue in Peek (disable message) and the settings page (disable and enable) in PT Settings. Is this possible?

@htcfreek

Yes, that's what I assumed we'd be doing. I haven't used the Settings system before, but I'll give it a go and I'll ask if I get stuck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In for .88 Needs-Review This Pull Request awaits the review of a maintainer.
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

5 participants