From d69eee55dd0d7bd936ced588a83f3ec4ecba16ad Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 1 Feb 2021 19:03:45 +0100 Subject: [PATCH] Added recent files selection to Welcome screen --- include/window.hpp | 3 ++ plugins/libimhex/include/hex/api/event.hpp | 1 + source/views/view_hexeditor.cpp | 4 +- source/window.cpp | 52 +++++++++++++++++++++- 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/include/window.hpp b/include/window.hpp index c8a3bfed7..6e8613be2 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -46,6 +47,8 @@ namespace hex { bool m_demoWindowOpen = false; static inline std::tuple s_currShortcut = { -1, -1 }; + + std::list m_recentFiles; }; } \ No newline at end of file diff --git a/plugins/libimhex/include/hex/api/event.hpp b/plugins/libimhex/include/hex/api/event.hpp index 98853b9be..ae78cdaac 100644 --- a/plugins/libimhex/include/hex/api/event.hpp +++ b/plugins/libimhex/include/hex/api/event.hpp @@ -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 diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index 5d1a1de21..f11f0a8a1 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -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); } diff --git a/source/window.cpp b/source/window.cpp index 52890b05d..5ee984e47 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -82,8 +82,51 @@ namespace hex { return { }; }); + EventManager::subscribe(Events::FileLoaded, this, [this](auto userData) -> std::any { + auto path = std::any_cast(userData); + + this->m_recentFiles.push_front(path); + + { + std::list 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 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();