From 482fdc939f9fd01dfdd4b75f485e30768c43e63d Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 1 Apr 2025 20:12:27 +0800 Subject: [PATCH] Add clean cache option --- Flow.Launcher/Languages/en.xaml | 2 + .../ViewModels/SettingsPaneAboutViewModel.cs | 49 ++++++++++++++++++- .../SettingPages/Views/SettingsPaneAbout.xaml | 4 ++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index 2df430c3b..6963d81c9 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -325,6 +325,8 @@ Log Folder Clear Logs Are you sure you want to delete all logs? + Clear Caches + Are you sure you want to delete all caches? Wizard User Data Location User settings and installed plugins are saved in the user data folder. This location may vary depending on whether it's in portable mode or not. diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs index 07e9fba1e..47cb1b6c3 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs @@ -27,6 +27,15 @@ public partial class SettingsPaneAboutViewModel : BaseModel } } + public string CacheFolderSize + { + get + { + var size = GetCacheFiles().Sum(file => file.Length); + return $"{App.API.GetTranslation("clearcachefolder")} ({BytesToReadableString(size)})"; + } + } + public string Website => Constant.Website; public string SponsorPage => Constant.SponsorPage; public string ReleaseNotes => _updater.GitHubRepository + "/releases/latest"; @@ -98,6 +107,21 @@ public partial class SettingsPaneAboutViewModel : BaseModel } } + [RelayCommand] + private void AskClearCacheFolderConfirmation() + { + var confirmResult = App.API.ShowMsgBox( + App.API.GetTranslation("clearcachefolderMessage"), + App.API.GetTranslation("clearcachefolder"), + MessageBoxButton.YesNo + ); + + if (confirmResult == MessageBoxResult.Yes) + { + ClearCacheFolder(); + } + } + [RelayCommand] private void OpenSettingsFolder() { @@ -112,7 +136,6 @@ public partial class SettingsPaneAboutViewModel : BaseModel App.API.OpenDirectory(parentFolderPath); } - [RelayCommand] private void OpenLogsFolder() { @@ -147,6 +170,30 @@ public partial class SettingsPaneAboutViewModel : BaseModel return GetLogDir(version).EnumerateFiles("*", SearchOption.AllDirectories).ToList(); } + private void ClearCacheFolder() + { + var cacheDirectory = GetCacheDir(); + var cacheFiles = GetCacheFiles(); + + cacheFiles.ForEach(f => f.Delete()); + + cacheDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly) + .ToList() + .ForEach(dir => dir.Delete(true)); + + OnPropertyChanged(nameof(CacheFolderSize)); + } + + private static DirectoryInfo GetCacheDir() + { + return new DirectoryInfo(DataLocation.CacheDirectory); + } + + private static List GetCacheFiles() + { + return GetCacheDir().EnumerateFiles("*", SearchOption.AllDirectories).ToList(); + } + private static string BytesToReadableString(long bytes) { const int scale = 1024; diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml index 75c513411..f7deee7cf 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml @@ -90,6 +90,10 @@ Margin="0 12 0 0" Icon=""> +