diff --git a/src/Files.App/BaseLayout.cs b/src/Files.App/BaseLayout.cs index 9190ab99c6c3..5e9c0e3fd0c9 100644 --- a/src/Files.App/BaseLayout.cs +++ b/src/Files.App/BaseLayout.cs @@ -1006,15 +1006,13 @@ protected void FileListItem_RightTapped(object sender, RightTappedRoutedEventArg ItemManipulationModel.SetSelectedItem(rightClickedItem); } - private readonly RecycleBinHelpers recycleBinHelpers = new(); - protected void InitializeDrag(UIElement containter, ListedItem item) { if (item is null) return; UninitializeDrag(containter); - if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !recycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable) + if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !RecycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable) { containter.AllowDrop = true; containter.DragOver += Item_DragOver; diff --git a/src/Files.App/Filesystem/FilesystemOperations/FilesystemOperations.cs b/src/Files.App/Filesystem/FilesystemOperations/FilesystemOperations.cs index 3419ce6ca30e..68aae1bac3d8 100644 --- a/src/Files.App/Filesystem/FilesystemOperations/FilesystemOperations.cs +++ b/src/Files.App/Filesystem/FilesystemOperations/FilesystemOperations.cs @@ -33,14 +33,11 @@ public class FilesystemOperations : IFilesystemOperations private IShellPage associatedInstance; - private RecycleBinHelpers recycleBinHelpers; - #region Constructor public FilesystemOperations(IShellPage associatedInstance) { this.associatedInstance = associatedInstance; - recycleBinHelpers = new RecycleBinHelpers(); } #endregion Constructor @@ -485,7 +482,7 @@ public async Task DeleteAsync(IStorageItemWithPath source, FileSystemProgress fsProgress = new(progress, true, FileSystemStatusCode.InProgress); fsProgress.Report(); - bool deleteFromRecycleBin = recycleBinHelpers.IsPathUnderRecycleBin(source.Path); + bool deleteFromRecycleBin = RecycleBinHelpers.IsPathUnderRecycleBin(source.Path); FilesystemResult fsResult = FileSystemStatusCode.InProgress; @@ -828,7 +825,6 @@ private async static Task CloneDirectoryAsync(BaseStorageFold public void Dispose() { - recycleBinHelpers = null; associatedInstance = null; } @@ -939,7 +935,7 @@ public async Task DeleteItemsAsync(IList if (token.IsCancellationRequested) break; - permanently = recycleBinHelpers.IsPathUnderRecycleBin(source[i].Path) || originalPermanently; + permanently = RecycleBinHelpers.IsPathUnderRecycleBin(source[i].Path) || originalPermanently; rawStorageHistory.Add(await DeleteAsync(source[i], null, permanently, token)); fsProgress.ProcessedItemsCount++; diff --git a/src/Files.App/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs b/src/Files.App/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs index ff6914853484..029ef5dcbd9d 100644 --- a/src/Files.App/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs +++ b/src/Files.App/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs @@ -36,8 +36,6 @@ public class FilesystemHelpers : IFilesystemHelpers private ItemManipulationModel itemManipulationModel => associatedInstance.SlimContentPage?.ItemManipulationModel; - private RecycleBinHelpers recycleBinHelpers; - private readonly CancellationToken cancellationToken; #region Helpers Members @@ -81,7 +79,6 @@ public FilesystemHelpers(IShellPage associatedInstance, CancellationToken cancel this.associatedInstance = associatedInstance; this.cancellationToken = cancellationToken; filesystemOperations = new ShellFilesystemOperations(this.associatedInstance); - recycleBinHelpers = new RecycleBinHelpers(); } #endregion Constructor @@ -125,8 +122,8 @@ public async Task DeleteItemsAsync(IEnumerable item.Path).Any(path => recycleBinHelpers.IsPathUnderRecycleBin(path)); - var canBeSentToBin = !deleteFromRecycleBin && await recycleBinHelpers.HasRecycleBin(source.FirstOrDefault()?.Path); + var deleteFromRecycleBin = source.Select(item => item.Path).Any(path => RecycleBinHelpers.IsPathUnderRecycleBin(path)); + var canBeSentToBin = !deleteFromRecycleBin && await RecycleBinHelpers.HasRecycleBin(source.FirstOrDefault()?.Path); if (showDialog && UserSettingsService.FoldersSettingsService.ShowConfirmDeleteDialog) // Check if the setting to show a confirmation dialog is on { @@ -134,7 +131,7 @@ public async Task DeleteItemsAsync(IEnumerable? binItems = null; foreach (var src in source) { - if (recycleBinHelpers.IsPathUnderRecycleBin(src.Path)) + if (RecycleBinHelpers.IsPathUnderRecycleBin(src.Path)) { binItems ??= await RecycleBinHelpers.EnumerateRecycleBin(); if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json @@ -374,7 +371,7 @@ public async Task CopyItemsFromClipboard(DataPackageView packageVi List binItems = null; foreach (var item in source) { - if (recycleBinHelpers.IsPathUnderRecycleBin(item.Path)) + if (RecycleBinHelpers.IsPathUnderRecycleBin(item.Path)) { binItems ??= await RecycleBinHelpers.EnumerateRecycleBin(); if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json @@ -513,7 +510,7 @@ public async Task MoveItemsFromClipboard(DataPackageView packageVi List binItems = null; foreach (var item in source) { - if (recycleBinHelpers.IsPathUnderRecycleBin(item.Path)) + if (RecycleBinHelpers.IsPathUnderRecycleBin(item.Path)) { binItems ??= await RecycleBinHelpers.EnumerateRecycleBin(); if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json @@ -640,7 +637,7 @@ public async Task RecycleItemsFromClipboard(DataPackageView packag var source = await GetDraggedStorageItems(packageView); ReturnResult returnStatus = ReturnResult.InProgress; - source = source.Where(x => !recycleBinHelpers.IsPathUnderRecycleBin(x.Path)); // Can't recycle items already in recyclebin + source = source.Where(x => !RecycleBinHelpers.IsPathUnderRecycleBin(x.Path)); // Can't recycle items already in recyclebin returnStatus = await DeleteItemsAsync(source, showDialog, false, registerHistory); return returnStatus; @@ -845,7 +842,6 @@ public void Dispose() associatedInstance = null; filesystemOperations = null; - recycleBinHelpers = null; } #endregion IDisposable diff --git a/src/Files.App/Filesystem/FilesystemOperations/ShellFilesystemOperations.cs b/src/Files.App/Filesystem/FilesystemOperations/ShellFilesystemOperations.cs index 10ceabbefaec..bfe552a94fa6 100644 --- a/src/Files.App/Filesystem/FilesystemOperations/ShellFilesystemOperations.cs +++ b/src/Files.App/Filesystem/FilesystemOperations/ShellFilesystemOperations.cs @@ -27,8 +27,6 @@ public class ShellFilesystemOperations : IFilesystemOperations private FilesystemOperations filesystemOperations; - private RecycleBinHelpers recycleBinHelpers; - private IDialogService DialogService { get; } = Ioc.Default.GetRequiredService(); #endregion Private Members @@ -39,7 +37,6 @@ public ShellFilesystemOperations(IShellPage associatedInstance) { this.associatedInstance = associatedInstance; filesystemOperations = new FilesystemOperations(associatedInstance); - recycleBinHelpers = new RecycleBinHelpers(); } #endregion Constructor @@ -321,7 +318,7 @@ public async Task DeleteItemsAsync(IList FileSystemProgress fsProgress = new(progress, true, FileSystemStatusCode.InProgress); fsProgress.Report(); var deleleFilePaths = source.Select(s => s.Path).Distinct(); - var deleteFromRecycleBin = source.Any() && recycleBinHelpers.IsPathUnderRecycleBin(source.ElementAt(0).Path); + var deleteFromRecycleBin = source.Any() && RecycleBinHelpers.IsPathUnderRecycleBin(source.ElementAt(0).Path); permanently |= deleteFromRecycleBin; if (deleteFromRecycleBin) @@ -748,7 +745,7 @@ private async Task GetFileListDialog(IEnumerable source, s List binItems = null; foreach (var src in source) { - if (recycleBinHelpers.IsPathUnderRecycleBin(src)) + if (RecycleBinHelpers.IsPathUnderRecycleBin(src)) { binItems ??= await RecycleBinHelpers.EnumerateRecycleBin(); if (!binItems.IsEmpty()) // Might still be null because we're deserializing the list from Json @@ -781,7 +778,6 @@ public void Dispose() filesystemOperations?.Dispose(); filesystemOperations = null; - recycleBinHelpers = null; associatedInstance = null; } diff --git a/src/Files.App/Helpers/RecycleBinHelpers.cs b/src/Files.App/Helpers/RecycleBinHelpers.cs index c262b6cc407b..3ec77ee553a3 100644 --- a/src/Files.App/Helpers/RecycleBinHelpers.cs +++ b/src/Files.App/Helpers/RecycleBinHelpers.cs @@ -14,11 +14,11 @@ namespace Files.App.Helpers { - public class RecycleBinHelpers + public static class RecycleBinHelpers { #region Private Members - private static readonly Regex recycleBinPathRegex = new Regex(@"^[A-Z]:\\\$Recycle\.Bin\\", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); + private static readonly Regex recycleBinPathRegex = new(@"^[A-Z]:\\\$Recycle\.Bin\\", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); #endregion Private Members @@ -31,30 +31,25 @@ public static ulong GetSize() { return (ulong)Win32Shell.QueryRecycleBin().BinSize; } - - public async Task IsRecycleBinItem(IStorageItem item) + + public static async Task IsRecycleBinItem(IStorageItem item) { List recycleBinItems = await EnumerateRecycleBin(); return recycleBinItems.Any((shellItem) => shellItem.RecyclePath == item.Path); } - public async Task IsRecycleBinItem(string path) + public static async Task IsRecycleBinItem(string path) { List recycleBinItems = await EnumerateRecycleBin(); return recycleBinItems.Any((shellItem) => shellItem.RecyclePath == path); } - public bool IsPathUnderRecycleBin(string path) + public static bool IsPathUnderRecycleBin(string path) { return !string.IsNullOrWhiteSpace(path) && recycleBinPathRegex.IsMatch(path); } - public static async Task S_EmptyRecycleBin() - { - await new RecycleBinHelpers().EmptyRecycleBin(); - } - - public async Task EmptyRecycleBin() + public static async Task EmptyRecycleBin() { var ConfirmEmptyBinDialog = new ContentDialog() { @@ -95,12 +90,7 @@ public async Task EmptyRecycleBin() } } - public static async Task S_RestoreRecycleBin(IShellPage associatedInstance) - { - await new RecycleBinHelpers().RestoreRecycleBin(associatedInstance); - } - - public async Task RestoreRecycleBin(IShellPage associatedInstance) + public static async Task RestoreRecycleBin(IShellPage associatedInstance) { var ConfirmEmptyBinDialog = new ContentDialog() { @@ -120,12 +110,7 @@ public async Task RestoreRecycleBin(IShellPage associatedInstance) } } - public static async Task S_RestoreSelectionRecycleBin(IShellPage associatedInstance) - { - await new RecycleBinHelpers().RestoreSelectionRecycleBin(associatedInstance); - } - - public async Task RestoreSelectionRecycleBin(IShellPage associatedInstance) + public static async Task RestoreSelectionRecycleBin(IShellPage associatedInstance) { var ConfirmEmptyBinDialog = new ContentDialog() { @@ -143,7 +128,7 @@ public async Task RestoreSelectionRecycleBin(IShellPage associatedInstance) } //WINUI3 - private ContentDialog SetContentDialogRoot(ContentDialog contentDialog) + private static ContentDialog SetContentDialogRoot(ContentDialog contentDialog) { if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8)) { @@ -152,7 +137,7 @@ private ContentDialog SetContentDialogRoot(ContentDialog contentDialog) return contentDialog; } - public async Task HasRecycleBin(string? path) + public static async Task HasRecycleBin(string? path) { if (string.IsNullOrEmpty(path) || path.StartsWith(@"\\?\", StringComparison.Ordinal)) return false; @@ -162,17 +147,12 @@ public async Task HasRecycleBin(string? path) return result.Item1 &= result.Item2 is not null && result.Item2.Items.All(x => x.Succeeded); } - public bool RecycleBinHasItems() + public static bool RecycleBinHasItems() { return Win32Shell.QueryRecycleBin().NumItems > 0; } - public static async Task S_RestoreItem(IShellPage associatedInstance) - { - await new RecycleBinHelpers().RestoreItem(associatedInstance); - } - - private async Task RestoreItem(IShellPage associatedInstance) + public static async Task RestoreItem(IShellPage associatedInstance) { var items = associatedInstance.SlimContentPage.SelectedItems.ToList().Where(x => x is RecycleBinItem).Select((item) => new { @@ -184,12 +164,7 @@ private async Task RestoreItem(IShellPage associatedInstance) await associatedInstance.FilesystemHelpers.RestoreItemsFromTrashAsync(items.Select(x => x.Source), items.Select(x => x.Dest), true); } - public static async Task S_DeleteItem(IShellPage associatedInstance) - { - await new RecycleBinHelpers().DeleteItem(associatedInstance); - } - - public virtual async Task DeleteItem(IShellPage associatedInstance) + public static async Task DeleteItem(IShellPage associatedInstance) { var items = associatedInstance.SlimContentPage.SelectedItems.ToList().Select((item) => StorageHelpers.FromPathAndType( item.ItemPath, diff --git a/src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs b/src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs index 138c989e8bdb..9c9cef50375b 100644 --- a/src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs +++ b/src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs @@ -153,17 +153,17 @@ public virtual void UnpinDirectoryFromFavorites(RoutedEventArgs e) public virtual async void EmptyRecycleBin(RoutedEventArgs e) { - await RecycleBinHelpers.S_EmptyRecycleBin(); + await RecycleBinHelpers.EmptyRecycleBin(); } public virtual async void RestoreRecycleBin(RoutedEventArgs e) { - await RecycleBinHelpers.S_RestoreRecycleBin(associatedInstance); + await RecycleBinHelpers.RestoreRecycleBin(associatedInstance); } public virtual async void RestoreSelectionRecycleBin(RoutedEventArgs e) { - await RecycleBinHelpers.S_RestoreSelectionRecycleBin(associatedInstance); + await RecycleBinHelpers.RestoreSelectionRecycleBin(associatedInstance); } public virtual async void QuickLook(RoutedEventArgs e) @@ -183,12 +183,12 @@ public virtual void CutItem(RoutedEventArgs e) public virtual async void RestoreItem(RoutedEventArgs e) { - await RecycleBinHelpers.S_RestoreItem(associatedInstance); + await RecycleBinHelpers.RestoreItem(associatedInstance); } public virtual async void DeleteItem(RoutedEventArgs e) { - await RecycleBinHelpers.S_DeleteItem(associatedInstance); + await RecycleBinHelpers.DeleteItem(associatedInstance); } public virtual void ShowFolderProperties(RoutedEventArgs e) => ShowProperties(e); diff --git a/src/Files.App/UserControls/SidebarControl.xaml.cs b/src/Files.App/UserControls/SidebarControl.xaml.cs index 96a91452c0b4..cd6382032e5d 100644 --- a/src/Files.App/UserControls/SidebarControl.xaml.cs +++ b/src/Files.App/UserControls/SidebarControl.xaml.cs @@ -1045,7 +1045,7 @@ private async void LoadShellMenuItems(CommandBarFlyout itemContextMenuFlyout, Co var emptyRecycleBinItem = itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "EmptyRecycleBin") as AppBarButton; if (emptyRecycleBinItem is not null) { - var binHasItems = new RecycleBinHelpers().RecycleBinHasItems(); + var binHasItems = RecycleBinHelpers.RecycleBinHasItems(); emptyRecycleBinItem.IsEnabled = binHasItems; } } diff --git a/src/Files.App/ViewModels/SidebarViewModel.cs b/src/Files.App/ViewModels/SidebarViewModel.cs index 82c86935e96b..5ad253e5b500 100644 --- a/src/Files.App/ViewModels/SidebarViewModel.cs +++ b/src/Files.App/ViewModels/SidebarViewModel.cs @@ -525,7 +525,7 @@ public async void UpdateSectionVisibility(SectionType sectionType, bool show) public async void EmptyRecycleBin(RoutedEventArgs e) { - await RecycleBinHelpers.S_EmptyRecycleBin(); + await RecycleBinHelpers.EmptyRecycleBin(); } private void UserSettingsService_OnSettingChangedEvent(object sender, SettingChangedEventArgs e)