1
0
mirror of synced 2024-11-12 02:00:52 +01:00

Added recent files selection to Welcome screen

This commit is contained in:
WerWolv 2021-02-01 19:03:45 +01:00
parent d9ec628333
commit d69eee55dd
4 changed files with 57 additions and 3 deletions

View File

@ -2,6 +2,7 @@
#include <filesystem>
#include <memory>
#include <string>
#include <vector>
#include <hex/helpers/utils.hpp>
@ -46,6 +47,8 @@ namespace hex {
bool m_demoWindowOpen = false;
static inline std::tuple<int, int> s_currShortcut = { -1, -1 };
std::list<std::string> m_recentFiles;
};
}

View File

@ -27,6 +27,7 @@ namespace hex {
SettingsChanged,
OpenWindow,
CloseImHex,
/* This is not a real event but a flag to show all events after this one are plugin ones */
Events_BuiltinEnd

View File

@ -214,7 +214,7 @@ namespace hex {
ImGui::TextUnformatted(Message);
ImGui::NewLine();
confirmButtons("Yes", "No", [] { std::exit(0); }, [] { ImGui::CloseCurrentPopup(); });
confirmButtons("Yes", "No", [] { View::postEvent(Events::CloseImHex); }, [] { ImGui::CloseCurrentPopup(); });
if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape)))
ImGui::CloseCurrentPopup();
@ -521,7 +521,7 @@ namespace hex {
this->getWindowOpenState() = true;
View::postEvent(Events::FileLoaded);
View::postEvent(Events::FileLoaded, path);
View::postEvent(Events::DataChanged);
View::postEvent(Events::PatternChanged);
}

View File

@ -82,8 +82,51 @@ namespace hex {
return { };
});
EventManager::subscribe(Events::FileLoaded, this, [this](auto userData) -> std::any {
auto path = std::any_cast<std::string>(userData);
this->m_recentFiles.push_front(path);
{
std::list<std::string> uniques;
for (auto &file : this->m_recentFiles) {
bool exists = false;
for (auto &unique : uniques) {
if (file == unique)
exists = true;
}
if (!exists)
uniques.push_back(file);
if (uniques.size() > 5)
break;
}
this->m_recentFiles = uniques;
}
{
std::vector<std::string> recentFilesVector;
std::copy(this->m_recentFiles.begin(), this->m_recentFiles.end(), std::back_inserter(recentFilesVector));
ContentRegistry::Settings::write("ImHex", "RecentFiles", recentFilesVector);
}
return { };
});
EventManager::subscribe(Events::CloseImHex, this, [this](auto) -> std::any {
glfwSetWindowShouldClose(this->m_window, true);
return { };
});
ContentRegistry::Settings::load();
View::postEvent(Events::SettingsChanged);
for (const auto &path : ContentRegistry::Settings::read("ImHex", "RecentFiles"))
this->m_recentFiles.push_back(path);
}
Window::~Window() {
@ -294,7 +337,14 @@ namespace hex {
ImGui::TableNextColumn();
ImGui::Text("Recent");
{
if (!this->m_recentFiles.empty()) {
for (auto &path : this->m_recentFiles) {
if (ImGui::BulletHyperlink(std::filesystem::path(path).filename().string().c_str())) {
EventManager::post(Events::FileDropped, path.c_str());
break;
}
}
}
}
ImGui::TableNextRow(ImGuiTableRowFlags_None, 100);
ImGui::TableNextColumn();