From 5db608c3fc3525687ef2674af196346e9390cc98 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 26 Sep 2021 21:18:25 +0200 Subject: [PATCH] ui: Fixed automatic pattern loading, added better pattern browse popup --- include/views/view_pattern_editor.hpp | 6 +- include/window.hpp | 3 + plugins/builtin/source/lang/de_DE.cpp | 2 + plugins/builtin/source/lang/en_US.cpp | 2 + plugins/builtin/source/lang/it_IT.cpp | 2 + plugins/builtin/source/lang/zh_CN.cpp | 2 + plugins/libimhex/include/hex/api/event.hpp | 3 +- .../libimhex/include/hex/helpers/utils.hpp | 2 +- plugins/libimhex/source/helpers/magic.cpp | 2 +- plugins/libimhex/source/helpers/utils.cpp | 8 +- source/helpers/loader_script_handler.cpp | 2 +- source/views/view_pattern_editor.cpp | 84 ++++++++++++++----- source/window/window.cpp | 13 +++ 13 files changed, 102 insertions(+), 29 deletions(-) diff --git a/include/views/view_pattern_editor.hpp b/include/views/view_pattern_editor.hpp index 7b7117d7e..2e243d958 100644 --- a/include/views/view_pattern_editor.hpp +++ b/include/views/view_pattern_editor.hpp @@ -26,12 +26,14 @@ namespace hex { private: pl::PatternLanguage *m_patternLanguageRuntime; - std::vector m_possiblePatternFiles; - int m_selectedPatternFile = 0; + std::vector m_possiblePatternFiles; + u32 m_selectedPatternFile = 0; bool m_runAutomatically = false; bool m_evaluatorRunning = false; bool m_hasUnevaluatedChanges = false; + bool m_acceptPatternWindowOpen = false; + TextEditor m_textEditor; std::vector> m_console; diff --git a/include/window.hpp b/include/window.hpp index 143b4f46a..fded346e1 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -64,6 +65,8 @@ namespace hex { ImGui::Texture m_logoTexture; std::filesystem::path m_safetyBackupPath; + + std::list m_popupsToOpen; }; } \ 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 599cc6cc7..41dc6add5 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -79,6 +79,8 @@ namespace hex::plugin::builtin { { "hex.common.dont_show_again", "Nicht mehr anzeigen" }, { "hex.common.link", "Link" }, { "hex.common.file", "Datei" }, + { "hex.common.open", "Öffnen" }, + { "hex.common.browse", "Druchsuchen..." }, { "hex.view.bookmarks.name", "Lesezeichen" }, { "hex.view.bookmarks.default_title", "Lesezeichen [0x{0:X} - 0x{1:X}]" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index e622216af..25388cbad 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -79,6 +79,8 @@ namespace hex::plugin::builtin { { "hex.common.dont_show_again", "Don't show again" }, { "hex.common.link", "Link" }, { "hex.common.file", "File" }, + { "hex.common.open", "Open" }, + { "hex.common.browse", "Browse..." }, { "hex.view.bookmarks.name", "Bookmarks" }, { "hex.view.bookmarks.default_title", "Bookmark [0x{0:X} - 0x{1:X}]" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index ef9874df1..3a9b12fc8 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -78,6 +78,8 @@ namespace hex::plugin::builtin { { "hex.common.dont_show_again", "Non mostrare di nuovo" }, { "hex.common.link", "Link" }, { "hex.common.file", "File" }, + //{ "hex.common.open", "Open" }, + //{ "hex.common.browse", "Browse..." }, { "hex.view.bookmarks.name", "Segnalibri" }, { "hex.view.bookmarks.default_title", "Segnalibro [0x{0:X} - 0x{1:X}]" }, diff --git a/plugins/builtin/source/lang/zh_CN.cpp b/plugins/builtin/source/lang/zh_CN.cpp index 0db97ef9d..40f811e86 100644 --- a/plugins/builtin/source/lang/zh_CN.cpp +++ b/plugins/builtin/source/lang/zh_CN.cpp @@ -79,6 +79,8 @@ namespace hex::plugin::builtin { { "hex.common.dont_show_again", "不要再次显示" }, { "hex.common.link", "链接" }, { "hex.common.file", "文件" }, + //{ "hex.common.open", "Open" }, + //{ "hex.common.browse", "Browse..." }, { "hex.view.bookmarks.name", "书签" }, { "hex.view.bookmarks.default_title", "书签 [0x{0:X} - 0x{1:X}]" }, diff --git a/plugins/libimhex/include/hex/api/event.hpp b/plugins/libimhex/include/hex/api/event.hpp index 9fa59569c..d7fb66c34 100644 --- a/plugins/libimhex/include/hex/api/event.hpp +++ b/plugins/libimhex/include/hex/api/event.hpp @@ -111,11 +111,12 @@ namespace hex { EVENT_DEF(RequestOpenWindow, std::string); EVENT_DEF(RequestSelectionChange, Region); EVENT_DEF(RequestAddBookmark, ImHexApi::Bookmarks::Entry); - EVENT_DEF(RequestAppendPatternLanguageCode, std::string); + EVENT_DEF(RequestSetPatternLanguageCode, std::string); EVENT_DEF(RequestChangeWindowTitle, std::string); EVENT_DEF(RequestCloseImHex, bool); EVENT_DEF(RequestOpenFile, std::string); EVENT_DEF(RequestChangeTheme, u32); + EVENT_DEF(RequestOpenPopup, std::string); EVENT_DEF(QuerySelection, Region&); diff --git a/plugins/libimhex/include/hex/helpers/utils.hpp b/plugins/libimhex/include/hex/helpers/utils.hpp index 1ec756570..ae930fb96 100644 --- a/plugins/libimhex/include/hex/helpers/utils.hpp +++ b/plugins/libimhex/include/hex/helpers/utils.hpp @@ -179,7 +179,7 @@ namespace hex { Folder }; - void openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback); + void openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath = {}); float float16ToFloat32(u16 float16); diff --git a/plugins/libimhex/source/helpers/magic.cpp b/plugins/libimhex/source/helpers/magic.cpp index 852f32d31..96563f007 100644 --- a/plugins/libimhex/source/helpers/magic.cpp +++ b/plugins/libimhex/source/helpers/magic.cpp @@ -74,7 +74,7 @@ namespace hex::magic { auto magicFiles = getMagicFiles(); if (magicFiles.has_value()) { - magic_t ctx = magic_open(MAGIC_MIME); + magic_t ctx = magic_open(MAGIC_MIME_TYPE); ON_SCOPE_EXIT { magic_close(ctx); }; if (magic_load(ctx, magicFiles->c_str()) == 0) diff --git a/plugins/libimhex/source/helpers/utils.cpp b/plugins/libimhex/source/helpers/utils.cpp index ae072ea41..9a7ac7433 100644 --- a/plugins/libimhex/source/helpers/utils.cpp +++ b/plugins/libimhex/source/helpers/utils.cpp @@ -184,20 +184,20 @@ namespace hex { } - void openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback) { + void openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath) { NFD::Init(); nfdchar_t *outPath; nfdresult_t result; switch (mode) { case DialogMode::Open: - result = NFD::OpenDialog(outPath, validExtensions.data(), validExtensions.size(), nullptr); + result = NFD::OpenDialog(outPath, validExtensions.data(), validExtensions.size(), defaultPath.c_str()); break; case DialogMode::Save: - result = NFD::SaveDialog(outPath, validExtensions.data(), validExtensions.size(), nullptr); + result = NFD::SaveDialog(outPath, validExtensions.data(), validExtensions.size(), defaultPath.c_str()); break; case DialogMode::Folder: - result = NFD::PickFolder(outPath, nullptr); + result = NFD::PickFolder(outPath, defaultPath.c_str()); break; default: __builtin_unreachable(); } diff --git a/source/helpers/loader_script_handler.cpp b/source/helpers/loader_script_handler.cpp index d2c582cdd..3f0df3e09 100644 --- a/source/helpers/loader_script_handler.cpp +++ b/source/helpers/loader_script_handler.cpp @@ -165,7 +165,7 @@ namespace hex { code += "};\n"; - EventManager::post(code); + EventManager::post(code); Py_RETURN_NONE; } diff --git a/source/views/view_pattern_editor.cpp b/source/views/view_pattern_editor.cpp index dd43f0531..1c6d76591 100644 --- a/source/views/view_pattern_editor.cpp +++ b/source/views/view_pattern_editor.cpp @@ -17,6 +17,7 @@ namespace hex { using namespace hex::literals; + namespace fs = std::filesystem; static const TextEditor::LanguageDefinition& PatternLanguage() { static bool initialized = false; @@ -96,18 +97,16 @@ namespace hex { this->parsePattern(this->m_textEditor.GetText().data()); }); - EventManager::subscribe(this, [this](std::string code) { - this->m_textEditor.InsertText("\n"); + EventManager::subscribe(this, [this](std::string code) { + this->m_textEditor.SelectAll(); + this->m_textEditor.Delete(); this->m_textEditor.InsertText(code); }); EventManager::subscribe(this, [this](const std::string &path) { - if (this->m_textEditor.GetText().find_first_not_of(" \f\n\r\t\v") != std::string::npos) - return; - pl::Preprocessor preprocessor; - if (ImHexApi::Provider::isValid()) + if (!ImHexApi::Provider::isValid()) return; std::string mimeType = magic::getMIMEType(ImHexApi::Provider::get()); @@ -127,6 +126,7 @@ namespace hex { std::error_code errorCode; for (const auto &dir : hex::getPath(ImHexPath::Patterns)) { for (auto &entry : std::filesystem::directory_iterator(dir, errorCode)) { + foundCorrectType = false; if (!entry.is_regular_file()) continue; @@ -137,14 +137,15 @@ namespace hex { preprocessor.preprocess(file.readString()); if (foundCorrectType) - this->m_possiblePatternFiles.push_back(entry.path().string()); + this->m_possiblePatternFiles.push_back(entry.path()); } } if (!this->m_possiblePatternFiles.empty()) { this->m_selectedPatternFile = 0; - View::doLater([] { ImGui::OpenPopup("hex.view.pattern.accept_pattern"_lang); }); + EventManager::post("hex.view.pattern.accept_pattern"_lang); + this->m_acceptPatternWindowOpen = true; } }); @@ -174,7 +175,7 @@ namespace hex { EventManager::unsubscribe(this); EventManager::unsubscribe(this); - EventManager::unsubscribe(this); + EventManager::unsubscribe(this); EventManager::unsubscribe(this); EventManager::unsubscribe(this); } @@ -182,9 +183,19 @@ namespace hex { void ViewPatternEditor::drawMenu() { if (ImGui::BeginMenu("hex.menu.file"_lang)) { if (ImGui::MenuItem("hex.view.pattern.menu.file.load_pattern"_lang)) { - hex::openFileBrowser("hex.view.pattern.open_pattern"_lang, DialogMode::Open, { { "Pattern File", "hexpat" } }, [this](auto path) { - this->loadPatternFile(path); - }); + + this->m_selectedPatternFile = 0; + this->m_possiblePatternFiles.clear(); + + for (auto &imhexPath : hex::getPath(ImHexPath::Patterns)) { + for (auto &entry: fs::recursive_directory_iterator(imhexPath)) { + if (entry.is_regular_file() && entry.path().extension() == ".hexpat") { + this->m_possiblePatternFiles.push_back(entry.path()); + } + } + } + + View::doLater([]{ ImGui::OpenPopup("hex.view.pattern.menu.file.load_pattern"_lang); }); } ImGui::EndMenu(); } @@ -270,7 +281,7 @@ namespace hex { } void ViewPatternEditor::drawAlwaysVisible() { - if (ImGui::BeginPopupModal("hex.view.pattern.accept_pattern"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { + if (ImGui::BeginPopupModal("hex.view.pattern.accept_pattern"_lang, &this->m_acceptPatternWindowOpen, ImGuiWindowFlags_AlwaysAutoResize)) { ImGui::TextWrapped("%s", static_cast("hex.view.pattern.accept_pattern.desc"_lang)); std::vector entries; @@ -280,19 +291,23 @@ namespace hex { entries[i] = std::filesystem::path(this->m_possiblePatternFiles[i]).filename().string(); } - ImGui::ListBox("hex.view.pattern.accept_pattern.pattern_language"_lang, &this->m_selectedPatternFile, [](void *data, int id, const char** outText) -> bool { - auto &entries = *static_cast*>(data); + if (ImGui::BeginListBox("##patterns_accept", ImVec2(-FLT_MIN, 0))) { - *outText = entries[id].c_str(); + u32 index = 0; + for (auto &path : this->m_possiblePatternFiles) { + if (ImGui::Selectable(path.filename().string().c_str(), index == this->m_selectedPatternFile)) + this->m_selectedPatternFile = index; + index++; + } - return true; - }, &entries, entries.size(), 4); + ImGui::EndListBox(); + } ImGui::NewLine(); ImGui::Text("%s", static_cast("hex.view.pattern.accept_pattern.question"_lang)); confirmButtons("hex.common.yes"_lang, "hex.common.no"_lang, [this]{ - this->loadPatternFile(this->m_possiblePatternFiles[this->m_selectedPatternFile]); + this->loadPatternFile(this->m_possiblePatternFiles[this->m_selectedPatternFile].string()); ImGui::CloseCurrentPopup(); }, []{ ImGui::CloseCurrentPopup(); @@ -303,6 +318,37 @@ namespace hex { ImGui::EndPopup(); } + + if (ImGui::BeginPopupModal("hex.view.pattern.menu.file.load_pattern"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { + + if (ImGui::BeginListBox("##patterns", ImVec2(-FLT_MIN, 0))) { + + u32 index = 0; + for (auto &path : this->m_possiblePatternFiles) { + if (ImGui::Selectable(path.filename().string().c_str(), index == this->m_selectedPatternFile)) + this->m_selectedPatternFile = index; + index++; + } + + ImGui::EndListBox(); + } + + if (ImGui::Button("hex.common.open"_lang)) { + this->loadPatternFile(this->m_possiblePatternFiles[this->m_selectedPatternFile].string()); + ImGui::CloseCurrentPopup(); + } + + ImGui::SameLine(); + + if (ImGui::Button("hex.common.browse"_lang)) { + hex::openFileBrowser("hex.view.pattern.open_pattern"_lang, DialogMode::Open, { { "Pattern File", "hexpat" } }, [this](auto path) { + this->loadPatternFile(path); + ImGui::CloseCurrentPopup(); + }); + } + + ImGui::EndPopup(); + } } diff --git a/source/window/window.cpp b/source/window/window.cpp index 4a6540a30..ac2f3a5b2 100644 --- a/source/window/window.cpp +++ b/source/window/window.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -222,6 +223,10 @@ namespace hex { } }); + EventManager::subscribe(this, [this](auto name){ + this->m_popupsToOpen.push_back(name); + }); + for (const auto &path : hex::getPath(ImHexPath::Config)) { if (auto filePath = std::filesystem::path(path) / CrashBackupFileName; std::filesystem::exists(filePath)) { this->m_safetyBackupPath = filePath; @@ -263,6 +268,7 @@ namespace hex { EventManager::unsubscribe(this); EventManager::unsubscribe(this); EventManager::unsubscribe(this); + EventManager::unsubscribe(this); ImGui::UnloadImage(this->m_bannerTexture); ImGui::UnloadImage(this->m_logoTexture); @@ -452,6 +458,13 @@ namespace hex { ImGui::EndPopup(); } + + for (auto it = this->m_popupsToOpen.begin(); it != this->m_popupsToOpen.end(); it++) { + if (ImGui::IsPopupOpen(it->c_str())) + this->m_popupsToOpen.erase(it); + else + ImGui::OpenPopup(it->c_str()); + } } void Window::frame() {