From 5d77402211dde92b716fd18496ba4c66cfa95b9b Mon Sep 17 00:00:00 2001 From: iTrooz Date: Fri, 31 Oct 2025 20:34:29 +0100 Subject: [PATCH] fix: add newly created projects to "Recent" (#2492) ### Problem description Projects weren't being saved as recent when a new project was saved. They were only added as recent when re-opening the project ### Implementation description I also save projects as recent when saving them (I don't make a difference between saving existing and new projects) ### Screenshots ### Additional things --- .../hex/api/events/events_lifecycle.hpp | 5 ++ plugins/builtin/source/content/project.cpp | 2 + plugins/builtin/source/content/recent.cpp | 58 ++++++++++--------- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/libimhex/include/hex/api/events/events_lifecycle.hpp b/lib/libimhex/include/hex/api/events/events_lifecycle.hpp index 684c4b6fd..678cf2b81 100644 --- a/lib/libimhex/include/hex/api/events/events_lifecycle.hpp +++ b/lib/libimhex/include/hex/api/events/events_lifecycle.hpp @@ -79,6 +79,11 @@ namespace hex { */ EVENT_DEF(EventProjectOpened); + /** + * @brief Called when a project is saved/saved as + */ + EVENT_DEF(EventProjectSaved); + /** * @brief Called when a native message was received from another ImHex instance * @param rawData Raw bytes received from other instance diff --git a/plugins/builtin/source/content/project.cpp b/plugins/builtin/source/content/project.cpp index 472836514..a82c61e00 100644 --- a/plugins/builtin/source/content/project.cpp +++ b/plugins/builtin/source/content/project.cpp @@ -174,6 +174,8 @@ namespace hex::plugin::builtin { AchievementManager::unlockAchievement("hex.builtin.achievement.starting_out", "hex.builtin.achievement.starting_out.save_project.name"); + EventProjectSaved::post(); + return result; } diff --git a/plugins/builtin/source/content/recent.cpp b/plugins/builtin/source/content/recent.cpp index ee6d53cdc..a3590e714 100644 --- a/plugins/builtin/source/content/recent.cpp +++ b/plugins/builtin/source/content/recent.cpp @@ -87,6 +87,33 @@ namespace hex::plugin::builtin::recent { } + void saveCurrentProjectAsRecent() { + if (!ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.save_recent_providers", true)) { + return; + } + auto fileName = fmt::format("{:%y%m%d_%H%M%S}.json", fmt::gmtime(std::chrono::system_clock::now())); + + auto projectFileName = ProjectFile::getPath().filename(); + if (projectFileName == BackupFileName) + return; + + // The recent provider is saved to every "recent" directory + for (const auto &recentPath : paths::Recent.write()) { + wolv::io::File recentFile(recentPath / fileName, wolv::io::File::Mode::Create); + if (!recentFile.isValid()) + continue; + + nlohmann::json recentEntry { + { "type", "project" }, + { "displayName", wolv::util::toUTF8String(projectFileName) }, + { "path", wolv::util::toUTF8String(ProjectFile::getPath()) } + }; + + recentFile.writeString(recentEntry.dump(4)); + } + + updateRecentEntries(); + } void registerEventHandlers() { // Save every opened provider as a "recent" shortcut @@ -123,33 +150,10 @@ namespace hex::plugin::builtin::recent { updateRecentEntries(); }); - // Save opened projects as a "recent" shortcut - (void)EventProjectOpened::subscribe([] { - if (ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.save_recent_providers", true)) { - auto fileName = fmt::format("{:%y%m%d_%H%M%S}.json", fmt::gmtime(std::chrono::system_clock::now())); - - auto projectFileName = ProjectFile::getPath().filename(); - if (projectFileName == BackupFileName) - return; - - // The recent provider is saved to every "recent" directory - for (const auto &recentPath : paths::Recent.write()) { - wolv::io::File recentFile(recentPath / fileName, wolv::io::File::Mode::Create); - if (!recentFile.isValid()) - continue; - - nlohmann::json recentEntry { - { "type", "project" }, - { "displayName", wolv::util::toUTF8String(projectFileName) }, - { "path", wolv::util::toUTF8String(ProjectFile::getPath()) } - }; - - recentFile.writeString(recentEntry.dump(4)); - } - } - - updateRecentEntries(); - }); + // Add opened projects to "recents" shortcuts + (void)EventProjectOpened::subscribe(saveCurrentProjectAsRecent); + // When saving a project, update its "recents" entry. This is mostly useful when using saving a new project + (void)EventProjectSaved::subscribe(saveCurrentProjectAsRecent); } void updateRecentEntries() {