From 8a485575f5b6628dd8cfa52a119397d377b99de4 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 13 Apr 2021 08:41:59 +0200 Subject: [PATCH] ui: Added recent files to File menu --- include/window.hpp | 2 - plugins/builtin/source/lang/de_DE.cpp | 1 + plugins/builtin/source/lang/en_US.cpp | 1 + plugins/builtin/source/lang/it_IT.cpp | 5 ++- .../include/hex/helpers/shared_data.hpp | 2 + .../libimhex/source/helpers/shared_data.cpp | 2 + source/views/view_hexeditor.cpp | 43 +++++++++++-------- source/views/view_information.cpp | 1 - source/window.cpp | 14 +++--- 9 files changed, 42 insertions(+), 29 deletions(-) diff --git a/include/window.hpp b/include/window.hpp index b5d77af01..441408eb0 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -53,8 +53,6 @@ namespace hex { double m_lastFrameTime; static inline std::tuple s_currShortcut = { -1, -1 }; - - std::list m_recentFiles; }; } \ No newline at end of file diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index 7f9762124..952bb6c08 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -153,6 +153,7 @@ namespace hex::plugin::builtin { { "hex.view.hexeditor.script.file.title", "Loader Script: Datei öffnen" }, { "hex.view.hexeditor.menu.file.open_file", "Datei öffnen..." }, + { "hex.view.hexeditor.menu.file.open_recent", "Kürzlich geöffnete Dateien"}, { "hex.view.hexeditor.menu.file.save", "Speichern" }, { "hex.view.hexeditor.menu.file.save_as", "Speichern unter..." }, { "hex.view.hexeditor.menu.file.close", "Schliessen" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index 61a1e3575..106f4d16f 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -153,6 +153,7 @@ namespace hex::plugin::builtin { { "hex.view.hexeditor.script.file.title", "Loader Script: Open File" }, { "hex.view.hexeditor.menu.file.open_file", "Open File..." }, + { "hex.view.hexeditor.menu.file.open_recent", "Open Recent"}, { "hex.view.hexeditor.menu.file.save", "Save" }, { "hex.view.hexeditor.menu.file.save_as", "Save As..." }, { "hex.view.hexeditor.menu.file.close", "Close" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index 0e895df15..384e0ce8e 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -136,6 +136,7 @@ namespace hex::plugin::builtin { { "hex.view.hexeditor.name", "Hex editor" }, { "hex.view.hexeditor.save_changes", "Salva le modifiche" }, { "hex.view.hexeditor.open_file", "Apri File" }, + { "hex.view.hexeditor.menu.file.open_recent", "File recenti" }, { "hex.view.hexeditor.open_project", "Apri i Progetti" }, { "hex.view.hexeditor.save_project", "Salva i Progetti" }, { "hex.view.hexeditor.save_data", "Salva i Dati" }, @@ -155,7 +156,7 @@ namespace hex::plugin::builtin { { "hex.view.hexeditor.menu.file.open_file", "Apri File..." }, { "hex.view.hexeditor.menu.file.save", "Salva" }, { "hex.view.hexeditor.menu.file.save_as", "Salva come..." }, - // { "hex.view.hexeditor.menu.file.close", "" }, + { "hex.view.hexeditor.menu.file.close", "Chiudi" }, { "hex.view.hexeditor.menu.file.open_project", "Apri un Progetto..." }, { "hex.view.hexeditor.menu.file.save_project", "Salva Progetto..." }, { "hex.view.hexeditor.menu.file.load_encoding_file", "Carica una codifica personalizzata..." }, @@ -512,7 +513,7 @@ namespace hex::plugin::builtin { { "hex.builtin.setting.interface.color.classic", "Classico" }, { "hex.builtin.setting.interface.language", "Lingua" }, { "hex.builtin.setting.interface.fps", "Limite FPS" }, - // { "hex.builtin.setting.interface.highlight_alpha", "" }, + { "hex.builtin.setting.interface.highlight_alpha", "Evidenziazione dell'opacità" }, { "hex.builtin.provider.file.path", "Percorso del File" }, { "hex.builtin.provider.file.size", "Dimensione" }, diff --git a/plugins/libimhex/include/hex/helpers/shared_data.hpp b/plugins/libimhex/include/hex/helpers/shared_data.hpp index 4f4d4edd3..5101766a9 100644 --- a/plugins/libimhex/include/hex/helpers/shared_data.hpp +++ b/plugins/libimhex/include/hex/helpers/shared_data.hpp @@ -69,6 +69,8 @@ namespace hex { static std::vector dataProcessorNodes; static u32 dataProcessorNodeIdCounter; + static std::list recentFilePaths; + static int mainArgc; static char **mainArgv; diff --git a/plugins/libimhex/source/helpers/shared_data.cpp b/plugins/libimhex/source/helpers/shared_data.cpp index 328f5a991..99a00b678 100644 --- a/plugins/libimhex/source/helpers/shared_data.cpp +++ b/plugins/libimhex/source/helpers/shared_data.cpp @@ -25,6 +25,8 @@ namespace hex { std::vector SharedData::dataProcessorNodes; u32 SharedData::dataProcessorNodeIdCounter = 1; + std::list SharedData::recentFilePaths; + int SharedData::mainArgc; char **SharedData::mainArgv; diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index ab9903052..a3e35c5c7 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -130,6 +130,7 @@ namespace hex { EventManager::subscribe(this, [this](const std::string &filePath) { this->openFile(filePath); + this->getWindowOpenState() = true; }); EventManager::subscribe(this, [this](Region region) { @@ -144,7 +145,7 @@ namespace hex { }); EventManager::subscribe(this, [this]() { - this->openFile(ProjectFile::getFilePath()); + EventManager::post(ProjectFile::getFilePath()); }); EventManager::subscribe(this, [](GLFWwindow *window) { @@ -319,18 +320,18 @@ namespace hex { ImGui::NewLine(); confirmButtons("hex.common.load"_lang, "hex.common.cancel"_lang, - [this, &provider] { - if (!this->m_loaderScriptScriptPath.empty() && !this->m_loaderScriptFilePath.empty()) { - this->openFile(this->m_loaderScriptFilePath); - LoaderScript::setFilePath(this->m_loaderScriptFilePath); - LoaderScript::setDataProvider(provider); - LoaderScript::processFile(this->m_loaderScriptScriptPath); - ImGui::CloseCurrentPopup(); - } - }, - [] { - ImGui::CloseCurrentPopup(); - } + [this, &provider] { + if (!this->m_loaderScriptScriptPath.empty() && !this->m_loaderScriptFilePath.empty()) { + EventManager::post(this->m_loaderScriptFilePath); + LoaderScript::setFilePath(this->m_loaderScriptFilePath); + LoaderScript::setDataProvider(provider); + LoaderScript::processFile(this->m_loaderScriptScriptPath); + ImGui::CloseCurrentPopup(); + } + }, + [] { + ImGui::CloseCurrentPopup(); + } ); ImGui::EndPopup(); @@ -363,11 +364,20 @@ namespace hex { if (ImGui::MenuItem("hex.view.hexeditor.menu.file.open_file"_lang, "CTRL + O")) { View::openFileBrowser("hex.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](auto path) { - this->openFile(path); - this->getWindowOpenState() = true; + EventManager::post(path); }); } + if (ImGui::BeginMenu("hex.view.hexeditor.menu.file.open_recent"_lang, !SharedData::recentFilePaths.empty())) { + for (auto &path : SharedData::recentFilePaths) { + if (ImGui::MenuItem(std::filesystem::path(path).filename().string().c_str())) { + EventManager::post(path); + } + } + + ImGui::EndMenu(); + } + if (ImGui::MenuItem("hex.view.hexeditor.menu.file.save"_lang, "CTRL + S", false, provider != nullptr && provider->isWritable())) { save(); } @@ -376,7 +386,7 @@ namespace hex { saveAs(); } - if (ImGui::MenuItem("hex.view.hexeditor.menu.file.close", "", false, provider != nullptr && provider->isAvailable())) { + if (ImGui::MenuItem("hex.view.hexeditor.menu.file.close"_lang, "", false, provider != nullptr && provider->isAvailable())) { EventManager::post(); delete SharedData::currentProvider; SharedData::currentProvider = nullptr; @@ -388,7 +398,6 @@ namespace hex { View::openFileBrowser("hex.view.hexeditor.menu.file.open_project"_lang, DialogMode::Open, { { "Project File", "hexproj" } }, [this](auto path) { ProjectFile::load(path); EventManager::post(); - this->getWindowOpenState() = true; }); } diff --git a/source/views/view_information.cpp b/source/views/view_information.cpp index 7c12da656..d8cc6b437 100644 --- a/source/views/view_information.cpp +++ b/source/views/view_information.cpp @@ -14,7 +14,6 @@ #include #include -#include namespace hex { diff --git a/source/window.cpp b/source/window.cpp index c93da1f23..ac4ab56ac 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -112,11 +112,11 @@ namespace hex { }); EventManager::subscribe(this, [this](const std::string &path){ - this->m_recentFiles.push_front(path); + SharedData::recentFilePaths.push_front(path); { std::list uniques; - for (auto &file : this->m_recentFiles) { + for (auto &file : SharedData::recentFilePaths) { bool exists = false; for (auto &unique : uniques) { @@ -130,12 +130,12 @@ namespace hex { if (uniques.size() > 5) break; } - this->m_recentFiles = uniques; + SharedData::recentFilePaths = uniques; } { std::vector recentFilesVector; - std::copy(this->m_recentFiles.begin(), this->m_recentFiles.end(), std::back_inserter(recentFilesVector)); + std::copy(SharedData::recentFilePaths.begin(), SharedData::recentFilePaths.end(), std::back_inserter(recentFilesVector)); ContentRegistry::Settings::write("hex.builtin.setting.imhex", "hex.builtin.setting.imhex.recent_files", recentFilesVector); } @@ -158,7 +158,7 @@ namespace hex { EventManager::post(); for (const auto &path : ContentRegistry::Settings::read("hex.builtin.setting.imhex", "hex.builtin.setting.imhex.recent_files")) - this->m_recentFiles.push_back(path); + SharedData::recentFilePaths.push_back(path); } Window::~Window() { @@ -430,8 +430,8 @@ namespace hex { ImGui::TableNextColumn(); ImGui::TextUnformatted("hex.welcome.start.recent"_lang); { - if (!this->m_recentFiles.empty()) { - for (auto &path : this->m_recentFiles) { + if (!SharedData::recentFilePaths.empty()) { + for (auto &path : SharedData::recentFilePaths) { if (ImGui::BulletHyperlink(std::filesystem::path(path).filename().string().c_str())) { EventManager::post(path); break;