From d19d62b1fc20d01860de1b821d2f688537386337 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 23 Feb 2024 18:31:42 +0100 Subject: [PATCH] impr: Remember find popup input per provider Closes #1567 --- .../hex_editor/popup_hex_editor_find.hpp | 7 +-- .../hex_editor/popup_hex_editor_find.cpp | 50 ++++++++++++------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/plugins/builtin/include/content/popups/hex_editor/popup_hex_editor_find.hpp b/plugins/builtin/include/content/popups/hex_editor/popup_hex_editor_find.hpp index 1ab18f2e8..a5b205c28 100644 --- a/plugins/builtin/include/content/popups/hex_editor/popup_hex_editor_find.hpp +++ b/plugins/builtin/include/content/popups/hex_editor/popup_hex_editor_find.hpp @@ -19,9 +19,8 @@ namespace hex::plugin::builtin { void drawSearchDirectionButtons(); void drawTabContents(); - std::optional findByteSequence(const std::vector &sequence); + std::optional findByteSequence(const std::vector &sequence) const; - std::string m_inputString; std::vector m_searchByteSequence; std::optional m_foundRegion = std::nullopt; @@ -46,9 +45,11 @@ namespace hex::plugin::builtin { String }; + static PerProvider s_inputString; + static PerProvider s_searchMode; + std::atomic m_stringEncoding = Encoding::UTF8; std::atomic m_stringEndianness = Endianness::Little; - std::atomic m_searchMode = SearchMode::ByteSequence; TaskHolder m_searchTask; diff --git a/plugins/builtin/source/content/popups/hex_editor/popup_hex_editor_find.cpp b/plugins/builtin/source/content/popups/hex_editor/popup_hex_editor_find.cpp index 58e14e844..bfded1755 100644 --- a/plugins/builtin/source/content/popups/hex_editor/popup_hex_editor_find.cpp +++ b/plugins/builtin/source/content/popups/hex_editor/popup_hex_editor_find.cpp @@ -13,6 +13,9 @@ namespace hex::plugin::builtin { + PerProvider PopupFind::s_inputString; + PerProvider PopupFind::s_searchMode; + PopupFind::PopupFind(ViewHexEditor *editor) noexcept { EventRegionSelected::subscribe(this, [this](Region region) { m_foundRegion = region; @@ -28,15 +31,16 @@ namespace hex::plugin::builtin { void PopupFind::draw(ViewHexEditor *editor) { ImGui::TextUnformatted("hex.builtin.view.hex_editor.menu.file.search"_lang); + auto lastMode = *s_searchMode; if (ImGui::BeginTabBar("##find_tabs")) { if (ImGui::BeginTabItem("hex.builtin.view.hex_editor.search.hex"_lang)) { - m_searchMode = SearchMode::ByteSequence; + s_searchMode = SearchMode::ByteSequence; this->drawTabContents(); ImGui::EndTabItem(); } if (ImGui::BeginTabItem("hex.builtin.view.hex_editor.search.string"_lang)) { - m_searchMode = SearchMode::String; + s_searchMode = SearchMode::String; this->drawTabContents(); ImGui::EndTabItem(); } @@ -44,6 +48,11 @@ namespace hex::plugin::builtin { ImGui::EndTabBar(); } + if (lastMode != *s_searchMode) { + m_requestFocus = true; + s_inputString->clear(); + } + const auto doSearch = [this, editor](auto &) { auto region = this->findByteSequence(m_searchByteSequence); @@ -124,7 +133,7 @@ namespace hex::plugin::builtin { ImGuiInputTextFlags searchInputFlags = 0; // Set search input icon and flags - switch (m_searchMode) { + switch (*s_searchMode) { case SearchMode::ByteSequence: searchInputIcon = ICON_VS_SYMBOL_NUMERIC; searchInputFlags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll | @@ -134,23 +143,25 @@ namespace hex::plugin::builtin { searchInputIcon = ICON_VS_SYMBOL_KEY; searchInputFlags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll; break; + default: + break; } // Draw search input - if (ImGuiExt::InputTextIcon("##input", searchInputIcon, m_inputString, searchInputFlags)) { - if (!m_inputString.empty()) { + if (ImGuiExt::InputTextIcon("##input", searchInputIcon, s_inputString, searchInputFlags)) { + if (!s_inputString->empty()) { m_requestSearch = true; m_searchBackwards = ImGui::GetIO().KeyShift; } } // Draw search direction buttons - ImGui::BeginDisabled(m_inputString.empty()); + ImGui::BeginDisabled(s_inputString->empty()); this->drawSearchDirectionButtons(); ImGui::EndDisabled(); // Draw search options for string search - if (m_searchMode == SearchMode::String) { + if (*s_searchMode == SearchMode::String) { if (ImGui::BeginCombo("hex.builtin.view.hex_editor.search.string.encoding"_lang, Lang(EncodingNames[std::to_underlying(m_stringEncoding.load())]))) { if (ImGui::Selectable(Lang(EncodingNames[0]), m_stringEncoding == Encoding::UTF8)) { m_stringEncoding = Encoding::UTF8; @@ -191,7 +202,7 @@ namespace hex::plugin::builtin { } } - std::optional PopupFind::findByteSequence(const std::vector &sequence) { + std::optional PopupFind::findByteSequence(const std::vector &sequence) const { auto provider = ImHexApi::Provider::get(); prv::ProviderReader reader(provider); @@ -244,23 +255,21 @@ namespace hex::plugin::builtin { } }; - switch (m_searchMode) { + switch (*s_searchMode) { case SearchMode::ByteSequence: { - m_searchByteSequence = crypt::decode16(m_inputString); - } + m_searchByteSequence = crypt::decode16(s_inputString); break; - + } case SearchMode::String: { switch (m_stringEncoding) { case Encoding::UTF8: { - std::copy(m_inputString.data(), m_inputString.data() + m_inputString.size(), + std::copy_n(s_inputString->data(), s_inputString->size(), std::back_inserter(m_searchByteSequence)); - } break; - + } case Encoding::UTF16: { std::wstring_convert, char16_t> convert16; - auto utf16 = convert16.from_bytes(m_inputString); + auto utf16 = convert16.from_bytes(s_inputString); for (auto &c: utf16) { swapEndianness(c, Encoding::UTF16, m_stringEndianness); @@ -269,12 +278,11 @@ namespace hex::plugin::builtin { std::copy(reinterpret_cast(utf16.data()), reinterpret_cast(utf16.data() + utf16.size()), std::back_inserter(m_searchByteSequence)); - } break; - + } case Encoding::UTF32: { std::wstring_convert, char32_t> convert32; - auto utf32 = convert32.from_bytes(m_inputString); + auto utf32 = convert32.from_bytes(s_inputString); for (auto &c: utf32) { swapEndianness(c, Encoding::UTF32, m_stringEndianness); @@ -283,10 +291,14 @@ namespace hex::plugin::builtin { std::copy(reinterpret_cast(utf32.data()), reinterpret_cast(utf32.data() + utf32.size()), std::back_inserter(m_searchByteSequence)); + break; } + default: break; } + break; } + default: break; } }