From 35739d6d0d2cf649d1a72f3a0dc5dc78ae2099c5 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 3 Aug 2024 18:15:30 +0200 Subject: [PATCH] feat: Display pattern description on the accept pattern popup --- .../content/views/view_pattern_editor.hpp | 57 ++++++++++++++----- .../content/views/view_pattern_editor.cpp | 25 +++++++- 2 files changed, 65 insertions(+), 17 deletions(-) diff --git a/plugins/builtin/include/content/views/view_pattern_editor.hpp b/plugins/builtin/include/content/views/view_pattern_editor.hpp index 369dd7722..12686e3f7 100644 --- a/plugins/builtin/include/content/views/view_pattern_editor.hpp +++ b/plugins/builtin/include/content/views/view_pattern_editor.hpp @@ -88,21 +88,43 @@ namespace hex::plugin::builtin { ImGuiExt::TextFormattedWrapped("{}", static_cast("hex.builtin.view.pattern_editor.accept_pattern.desc"_lang)); - std::vector entries; - entries.resize(m_view->m_possiblePatternFiles.get(provider).size()); - - for (u32 i = 0; i < entries.size(); i++) { - entries[i] = wolv::util::toUTF8String(m_view->m_possiblePatternFiles.get(provider)[i].filename()); - } - - if (ImGui::BeginListBox("##patterns_accept", ImVec2(-FLT_MIN, 0))) { + if (ImGui::BeginListBox("##patterns_accept", ImVec2(400_scaled, 0))) { u32 index = 0; - for (auto &path : m_view->m_possiblePatternFiles.get(provider)) { - if (ImGui::Selectable(wolv::util::toUTF8String(path.filename()).c_str(), index == m_selectedPatternFile, ImGuiSelectableFlags_DontClosePopups)) + for (const auto &[path, author, description] : m_view->m_possiblePatternFiles.get(provider)) { + auto fileName = wolv::util::toUTF8String(path.filename()); + + std::string displayValue; + if (!description.empty()) { + displayValue = fmt::format("{} ({})", description, fileName); + } else { + displayValue = fileName; + } + + if (ImGui::Selectable(displayValue.c_str(), index == m_selectedPatternFile, ImGuiSelectableFlags_DontClosePopups)) m_selectedPatternFile = index; + if (ImGui::IsItemHovered(ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayNormal)) { + if (ImGui::BeginTooltip()) { + ImGui::TextUnformatted(fileName.c_str()); + + if (!author.empty()) { + ImGui::SameLine(); + ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical); + ImGui::SameLine(); + ImGui::TextUnformatted(author.c_str()); + } + + if (!description.empty()) { + ImGui::Separator(); + ImGui::TextUnformatted(description.c_str()); + } + + ImGui::EndTooltip(); + } + } + if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) - m_view->loadPatternFile(m_view->m_possiblePatternFiles.get(provider)[m_selectedPatternFile], provider); + m_view->loadPatternFile(m_view->m_possiblePatternFiles.get(provider)[m_selectedPatternFile].path, provider); ImGuiExt::InfoTooltip(wolv::util::toUTF8String(path).c_str()); @@ -117,11 +139,12 @@ namespace hex::plugin::builtin { } ImGui::NewLine(); - ImGui::TextUnformatted("hex.builtin.view.pattern_editor.accept_pattern.question"_lang); + ImGuiExt::TextUnformattedCentered("hex.builtin.view.pattern_editor.accept_pattern.question"_lang); + ImGui::NewLine(); ImGuiExt::ConfirmButtons("hex.ui.common.yes"_lang, "hex.ui.common.no"_lang, [this, provider] { - m_view->loadPatternFile(m_view->m_possiblePatternFiles.get(provider)[m_selectedPatternFile], provider); + m_view->loadPatternFile(m_view->m_possiblePatternFiles.get(provider)[m_selectedPatternFile].path, provider); this->close(); }, [this] { @@ -175,10 +198,16 @@ namespace hex::plugin::builtin { u32 color; }; + struct PossiblePattern { + std::fs::path path; + std::string author; + std::string description; + }; + std::unique_ptr m_editorRuntime; std::mutex m_possiblePatternFilesMutex; - PerProvider> m_possiblePatternFiles; + PerProvider> m_possiblePatternFiles; bool m_runAutomatically = false; bool m_triggerEvaluation = false; std::atomic m_triggerAutoEvaluate = false; diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index cbfa7464d..dd8b812c1 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -1340,9 +1340,9 @@ namespace hex::plugin::builtin { pl::PatternLanguage runtime; ContentRegistry::PatternLanguage::configureRuntime(runtime, provider); - auto mimeType = magic::getMIMEType(provider, 0, 100_KiB, true); - bool foundCorrectType = false; + + auto mimeType = magic::getMIMEType(provider, 0, 100_KiB, true); runtime.addPragma("MIME", [&mimeType, &foundCorrectType](const pl::PatternLanguage &runtime, const std::string &value) { hex::unused(runtime); @@ -1414,6 +1414,18 @@ namespace hex::plugin::builtin { return true; }); + std::string author; + runtime.addPragma("author", [&author](pl::PatternLanguage &, const std::string &value) -> bool { + author = value; + return true; + }); + + std::string description; + runtime.addPragma("description", [&description](pl::PatternLanguage &, const std::string &value) -> bool { + description = value; + return true; + }); + m_possiblePatternFiles.get(provider).clear(); bool popupOpen = false; @@ -1430,6 +1442,9 @@ namespace hex::plugin::builtin { if (!file.isValid()) continue; + author.clear(); + description.clear(); + auto result = runtime.preprocessString(file.readString(), pl::api::Source::DefaultSource); if (!result.has_value()) { log::warn("Failed to preprocess file {} during MIME analysis", entry.path().string()); @@ -1439,7 +1454,11 @@ namespace hex::plugin::builtin { { std::scoped_lock lock(m_possiblePatternFilesMutex); - m_possiblePatternFiles.get(provider).push_back(entry.path()); + m_possiblePatternFiles.get(provider).emplace_back( + entry.path(), + std::move(author), + std::move(description) + ); } if (!popupOpen) {