fix: add newly created projects to "Recent" (#2492)

<!--
Please provide as much information as possible about what your PR aims
to do.
PRs with no description will most likely be closed until more
information is provided.
If you're planing on changing fundamental behaviour or add big new
features, please open a GitHub Issue first before starting to work on
it.
If it's not something big and you still want to contact us about it,
feel free to do so !
-->

### Problem description
<!-- Describe the bug that you fixed/feature request that you
implemented, or link to an existing issue describing it -->
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
<!-- Explain what you did to correct the problem -->
I also save projects as recent when saving them (I don't make a
difference between saving existing and new projects)

### Screenshots
<!-- If your change is visual, take a screenshot showing it. Ideally,
make before/after sceenshots -->

### Additional things
<!-- Anything else you would like to say -->
This commit is contained in:
iTrooz
2025-10-31 20:34:29 +01:00
committed by GitHub
parent f762cc2906
commit 5d77402211
3 changed files with 38 additions and 27 deletions

View File

@@ -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

View File

@@ -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;
}

View File

@@ -87,6 +87,33 @@ namespace hex::plugin::builtin::recent {
}
void saveCurrentProjectAsRecent() {
if (!ContentRegistry::Settings::read<bool>("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<bool>("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() {