Skip to content

Commit 5d77402

Browse files
authored
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 -->
1 parent f762cc2 commit 5d77402

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

lib/libimhex/include/hex/api/events/events_lifecycle.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ namespace hex {
7979
*/
8080
EVENT_DEF(EventProjectOpened);
8181

82+
/**
83+
* @brief Called when a project is saved/saved as
84+
*/
85+
EVENT_DEF(EventProjectSaved);
86+
8287
/**
8388
* @brief Called when a native message was received from another ImHex instance
8489
* @param rawData Raw bytes received from other instance

plugins/builtin/source/content/project.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ namespace hex::plugin::builtin {
174174

175175
AchievementManager::unlockAchievement("hex.builtin.achievement.starting_out", "hex.builtin.achievement.starting_out.save_project.name");
176176

177+
EventProjectSaved::post();
178+
177179
return result;
178180
}
179181

plugins/builtin/source/content/recent.cpp

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ namespace hex::plugin::builtin::recent {
8787

8888
}
8989

90+
void saveCurrentProjectAsRecent() {
91+
if (!ContentRegistry::Settings::read<bool>("hex.builtin.setting.general", "hex.builtin.setting.general.save_recent_providers", true)) {
92+
return;
93+
}
94+
auto fileName = fmt::format("{:%y%m%d_%H%M%S}.json", fmt::gmtime(std::chrono::system_clock::now()));
95+
96+
auto projectFileName = ProjectFile::getPath().filename();
97+
if (projectFileName == BackupFileName)
98+
return;
99+
100+
// The recent provider is saved to every "recent" directory
101+
for (const auto &recentPath : paths::Recent.write()) {
102+
wolv::io::File recentFile(recentPath / fileName, wolv::io::File::Mode::Create);
103+
if (!recentFile.isValid())
104+
continue;
105+
106+
nlohmann::json recentEntry {
107+
{ "type", "project" },
108+
{ "displayName", wolv::util::toUTF8String(projectFileName) },
109+
{ "path", wolv::util::toUTF8String(ProjectFile::getPath()) }
110+
};
111+
112+
recentFile.writeString(recentEntry.dump(4));
113+
}
114+
115+
updateRecentEntries();
116+
}
90117

91118
void registerEventHandlers() {
92119
// Save every opened provider as a "recent" shortcut
@@ -123,33 +150,10 @@ namespace hex::plugin::builtin::recent {
123150
updateRecentEntries();
124151
});
125152

126-
// Save opened projects as a "recent" shortcut
127-
(void)EventProjectOpened::subscribe([] {
128-
if (ContentRegistry::Settings::read<bool>("hex.builtin.setting.general", "hex.builtin.setting.general.save_recent_providers", true)) {
129-
auto fileName = fmt::format("{:%y%m%d_%H%M%S}.json", fmt::gmtime(std::chrono::system_clock::now()));
130-
131-
auto projectFileName = ProjectFile::getPath().filename();
132-
if (projectFileName == BackupFileName)
133-
return;
134-
135-
// The recent provider is saved to every "recent" directory
136-
for (const auto &recentPath : paths::Recent.write()) {
137-
wolv::io::File recentFile(recentPath / fileName, wolv::io::File::Mode::Create);
138-
if (!recentFile.isValid())
139-
continue;
140-
141-
nlohmann::json recentEntry {
142-
{ "type", "project" },
143-
{ "displayName", wolv::util::toUTF8String(projectFileName) },
144-
{ "path", wolv::util::toUTF8String(ProjectFile::getPath()) }
145-
};
146-
147-
recentFile.writeString(recentEntry.dump(4));
148-
}
149-
}
150-
151-
updateRecentEntries();
152-
});
153+
// Add opened projects to "recents" shortcuts
154+
(void)EventProjectOpened::subscribe(saveCurrentProjectAsRecent);
155+
// When saving a project, update its "recents" entry. This is mostly useful when using saving a new project
156+
(void)EventProjectSaved::subscribe(saveCurrentProjectAsRecent);
153157
}
154158

155159
void updateRecentEntries() {

0 commit comments

Comments
 (0)