From 96c3bb1e38f76bfcc52c1a3402a5e4ecfa52a3df Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 14 Mar 2023 14:07:18 +0100 Subject: [PATCH] feat: Limit recent files to 5 files, add option to disable saving them Closes #950 --- plugins/builtin/romfs/lang/en_US.json | 1 + .../source/content/settings_entries.cpp | 11 ++++++++++ .../builtin/source/content/welcome_screen.cpp | 22 ++++++++++++++++--- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index 0830b6d0c..cd6e24981 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -395,6 +395,7 @@ "hex.builtin.setting.general.auto_load_patterns": "Auto-load supported pattern", "hex.builtin.setting.general.check_for_updates": "Check for updates on startup", "hex.builtin.setting.general.enable_unicode": "Load all unicode characters", + "hex.builtin.setting.general.save_recent_providers": "Save recently used providers", "hex.builtin.setting.general.show_tips": "Show tips on startup", "hex.builtin.setting.general.sync_pattern_source": "Sync pattern source code between providers", "hex.builtin.setting.hex_editor": "Hex Editor", diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index ead7f48f5..e853b44ee 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -100,6 +100,17 @@ namespace hex::plugin::builtin { return false; }); + ContentRegistry::Settings::add("hex.builtin.setting.general", "hex.builtin.setting.general.save_recent_providers", 1, [](auto name, nlohmann::json &setting) { + static bool enabled = static_cast(setting); + + if (ImGui::Checkbox(name.data(), &enabled)) { + setting = static_cast(enabled); + return true; + } + + return false; + }); + /* Interface */ ContentRegistry::Settings::add("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", "Dark", [](auto name, nlohmann::json &setting) { diff --git a/plugins/builtin/source/content/welcome_screen.cpp b/plugins/builtin/source/content/welcome_screen.cpp index 6001d704e..ca23c3fad 100644 --- a/plugins/builtin/source/content/welcome_screen.cpp +++ b/plugins/builtin/source/content/welcome_screen.cpp @@ -30,6 +30,8 @@ namespace hex::plugin::builtin { + constexpr static auto MaxRecentProviders = 5; + static ImGui::Texture s_bannerTexture, s_backdropTexture; static std::fs::path s_safetyBackupPath; @@ -85,7 +87,7 @@ namespace hex::plugin::builtin { }); std::unordered_set uniqueProviders; - for (u32 i = 0; i < recentFilePaths.size() && uniqueProviders.size() < 5; i++) { + for (u32 i = 0; i < recentFilePaths.size() && uniqueProviders.size() < MaxRecentProviders; i++) { auto &path = recentFilePaths[i]; try { auto jsonData = nlohmann::json::parse(wolv::io::File(path, wolv::io::File::Mode::Read).readString()); @@ -98,6 +100,20 @@ namespace hex::plugin::builtin { } catch (...) { } } + // Delete all recent provider files that are not in the list + for (const auto &path : recentFilePaths) { + bool found = false; + for (const auto &provider : uniqueProviders) { + if (path == provider.filePath) { + found = true; + break; + } + } + + if (!found) + wolv::io::fs::remove(path); + } + std::copy(uniqueProviders.begin(), uniqueProviders.end(), std::front_inserter(s_recentProviders)); }); } @@ -248,7 +264,7 @@ namespace hex::plugin::builtin { ImGui::TableNextRow(ImGuiTableRowFlags_None, ImGui::GetTextLineHeightWithSpacing() * 9); ImGui::TableNextColumn(); - ImGui::UnderlinedText("hex.builtin.welcome.start.recent"_lang); + ImGui::UnderlinedText(s_recentProviders.empty() ? "" : "hex.builtin.welcome.start.recent"_lang); ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 5_scaled); { if (!s_recentProvidersUpdating) { @@ -476,7 +492,7 @@ namespace hex::plugin::builtin { }); (void)EventManager::subscribe([](prv::Provider *provider) { - { + if (ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.save_recent_providers", 1) == 1) { for (const auto &recentPath : fs::getDefaultPaths(fs::ImHexPath::Recent)) { auto fileName = hex::format("{:%y%m%d_%H%M%S}.json", fmt::gmtime(std::chrono::system_clock::now())); wolv::io::File recentFile(recentPath / fileName, wolv::io::File::Mode::Create);