Skip to content

Commit

Permalink
feat: Display pattern description on the accept pattern popup
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Aug 3, 2024
1 parent e726fce commit 35739d6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
55 changes: 42 additions & 13 deletions plugins/builtin/include/content/views/view_pattern_editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,43 @@ namespace hex::plugin::builtin {

ImGuiExt::TextFormattedWrapped("{}", static_cast<const char *>("hex.builtin.view.pattern_editor.accept_pattern.desc"_lang));

std::vector<std::string> entries;
entries.resize(m_view->m_possiblePatternFiles.get(provider).size());
if (ImGui::BeginListBox("##patterns_accept", ImVec2(400_scaled, 0))) {
u32 index = 0;
for (const auto &[path, author, description] : m_view->m_possiblePatternFiles.get(provider)) {
auto fileName = wolv::util::toUTF8String(path.filename());

for (u32 i = 0; i < entries.size(); i++) {
entries[i] = wolv::util::toUTF8String(m_view->m_possiblePatternFiles.get(provider)[i].filename());
}
std::string displayValue;
if (!description.empty()) {
displayValue = fmt::format("{} ({})", description, fileName);
} else {
displayValue = fileName;
}

if (ImGui::BeginListBox("##patterns_accept", ImVec2(-FLT_MIN, 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))
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());

Expand All @@ -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] {
Expand Down Expand Up @@ -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<pl::PatternLanguage> m_editorRuntime;

std::mutex m_possiblePatternFilesMutex;
PerProvider<std::vector<std::fs::path>> m_possiblePatternFiles;
PerProvider<std::vector<PossiblePattern>> m_possiblePatternFiles;
bool m_runAutomatically = false;
bool m_triggerEvaluation = false;
std::atomic<bool> m_triggerAutoEvaluate = false;
Expand Down
25 changes: 22 additions & 3 deletions plugins/builtin/source/content/views/view_pattern_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -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) {
Expand Down

0 comments on commit 35739d6

Please sign in to comment.