diff --git a/plugins/builtin/include/content/views/view_hex_editor.hpp b/plugins/builtin/include/content/views/view_hex_editor.hpp index e272562c9..e9f8bc6af 100644 --- a/plugins/builtin/include/content/views/view_hex_editor.hpp +++ b/plugins/builtin/include/content/views/view_hex_editor.hpp @@ -76,6 +76,8 @@ namespace hex::plugin::builtin { PerProvider> m_selectionStart, m_selectionEnd; PerProvider m_scrollPosition; + + PerProvider> m_foregroundHighlights, m_backgroundHighlights; }; } \ No newline at end of file diff --git a/plugins/builtin/include/content/views/view_patches.hpp b/plugins/builtin/include/content/views/view_patches.hpp index 9f8bdfd79..d8f5b051d 100644 --- a/plugins/builtin/include/content/views/view_patches.hpp +++ b/plugins/builtin/include/content/views/view_patches.hpp @@ -15,9 +15,11 @@ namespace hex::plugin::builtin { ~ViewPatches() override = default; void drawContent() override; + void drawAlwaysVisible() override; private: u64 m_selectedPatch = 0x00; + PerProvider m_numPatches; }; } \ No newline at end of file diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 12f2857f9..d76a548fe 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -505,40 +505,50 @@ namespace hex::plugin::builtin { /* Hex Editor */ ViewHexEditor::ViewHexEditor() : View("hex.builtin.view.hex_editor.name") { - this->m_hexEditor.setForegroundHighlightCallback([](u64 address, const u8 *data, size_t size) -> std::optional { + this->m_hexEditor.setForegroundHighlightCallback([this](u64 address, const u8 *data, size_t size) -> std::optional { + if (auto highlight = this->m_foregroundHighlights->find(address); highlight != this->m_foregroundHighlights->end()) + return highlight->second; + std::optional result; for (const auto &[id, callback] : ImHexApi::HexEditor::impl::getForegroundHighlightingFunctions()) { if (auto color = callback(address, data, size, result.has_value()); color.has_value()) result = color; } - if (result.has_value()) - return result; - - for (const auto &[id, highlighting] : ImHexApi::HexEditor::impl::getForegroundHighlights()) { - if (highlighting.getRegion().overlaps({ address, size })) - return highlighting.getColor(); + if (!result.has_value()) { + for (const auto &[id, highlighting] : ImHexApi::HexEditor::impl::getForegroundHighlights()) { + if (highlighting.getRegion().overlaps({ address, size })) + return highlighting.getColor(); + } } - return std::nullopt; + if (result.has_value()) + this->m_foregroundHighlights->insert({ address, result.value() }); + + return result; }); - this->m_hexEditor.setBackgroundHighlightCallback([](u64 address, const u8 *data, size_t size) -> std::optional { + this->m_hexEditor.setBackgroundHighlightCallback([this](u64 address, const u8 *data, size_t size) -> std::optional { + if (auto highlight = this->m_backgroundHighlights->find(address); highlight != this->m_backgroundHighlights->end()) + return highlight->second; + std::optional result; for (const auto &[id, callback] : ImHexApi::HexEditor::impl::getBackgroundHighlightingFunctions()) { if (auto color = callback(address, data, size, result.has_value()); color.has_value()) - return color.value(); + result = color; + } + + if (!result.has_value()) { + for (const auto &[id, highlighting] : ImHexApi::HexEditor::impl::getBackgroundHighlights()) { + if (highlighting.getRegion().overlaps({ address, size })) + return highlighting.getColor(); + } } if (result.has_value()) - return result; + this->m_backgroundHighlights->insert({ address, result.value() }); - for (const auto &[id, highlighting] : ImHexApi::HexEditor::impl::getBackgroundHighlights()) { - if (highlighting.getRegion().overlaps({ address, size })) - return highlighting.getColor(); - } - - return std::nullopt; + return result; }); this->m_hexEditor.setTooltipCallback([](u64 address, const u8 *data, size_t size) { @@ -573,8 +583,10 @@ namespace hex::plugin::builtin { } ViewHexEditor::~ViewHexEditor() { + EventManager::unsubscribe(this); EventManager::unsubscribe(this); EventManager::unsubscribe(this); + EventManager::unsubscribe(this); } void ViewHexEditor::drawPopup() { @@ -931,6 +943,11 @@ namespace hex::plugin::builtin { ImHexApi::HexEditor::clearSelection(); }); + EventManager::subscribe(this, [this]{ + this->m_foregroundHighlights->clear(); + this->m_backgroundHighlights->clear(); + }); + ProjectFile::registerPerProviderHandler({ .basePath = "custom_encoding.tbl", .required = false, diff --git a/plugins/builtin/source/content/views/view_patches.cpp b/plugins/builtin/source/content/views/view_patches.cpp index 32b6f89f2..341810ae0 100644 --- a/plugins/builtin/source/content/views/view_patches.cpp +++ b/plugins/builtin/source/content/views/view_patches.cpp @@ -117,4 +117,14 @@ namespace hex::plugin::builtin { ImGui::End(); } + void ViewPatches::drawAlwaysVisible() { + if (auto provider = ImHexApi::Provider::get(); provider != nullptr) { + const auto &patches = provider->getPatches(); + if (this->m_numPatches.get(provider) != patches.size()) { + this->m_numPatches.get(provider) = patches.size(); + EventManager::post(); + } + } + } + } \ No newline at end of file