From 3794aa425d7ecdacca68081cfb8e19652574928a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 23 Apr 2024 23:26:58 +0200 Subject: [PATCH] feat: Display destination address in goto popup --- .../source/content/views/view_hex_editor.cpp | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index f5a1415c8..c1fdce62c 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -59,38 +59,55 @@ namespace hex::plugin::builtin { ImGui::SetKeyboardFocusHere(); m_requestFocus = false; } - if (ImGuiExt::InputTextIcon("##input", ICON_VS_SYMBOL_OPERATOR, m_input, ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll)) { + if (ImGuiExt::InputTextIcon("##input", ICON_VS_SYMBOL_OPERATOR, m_input)) { if (auto result = m_evaluator.evaluate(m_input); result.has_value()) { const auto inputResult = result.value(); - u64 newAddress = 0x00; - auto provider = ImHexApi::Provider::get(); switch (m_mode) { case Mode::Absolute: { - newAddress = inputResult; + m_newAddress = inputResult; } break; case Mode::Relative: { const auto selection = editor->getSelection(); - newAddress = selection.getStartAddress() + inputResult; + m_newAddress = selection.getStartAddress() + inputResult; } break; case Mode::Begin: { - newAddress = provider->getBaseAddress() + provider->getCurrentPageAddress() + inputResult; + m_newAddress = provider->getBaseAddress() + provider->getCurrentPageAddress() + inputResult; } break; case Mode::End: { - newAddress = provider->getActualSize() - inputResult; + m_newAddress = provider->getActualSize() - inputResult; } break; } - - editor->setSelection(newAddress, newAddress); - editor->jumpToSelection(); + } else { + m_newAddress.reset(); } } + bool executeGoto = false; + if (ImGui::IsItemFocused() && ImGui::IsKeyPressed(ImGuiKey_Enter)) { + executeGoto = true; + } + + ImGui::BeginDisabled(!m_newAddress.has_value()); + { + const auto label = hex::format("{} {}", "hex.builtin.view.hex_editor.menu.file.goto"_lang, m_newAddress.has_value() ? hex::format("0x{:08X}", *m_newAddress) : "???"); + const auto buttonWidth = ImGui::GetWindowWidth() - ImGui::GetStyle().WindowPadding.x * 2; + if (ImGuiExt::DimmedButton(label.c_str(), ImVec2(buttonWidth, 0))) { + executeGoto = true; + } + } + ImGui::EndDisabled(); + + if (executeGoto && m_newAddress.has_value()) { + editor->setSelection(*m_newAddress, *m_newAddress); + editor->jumpToSelection(); + } + ImGui::EndTabBar(); } } @@ -104,9 +121,10 @@ namespace hex::plugin::builtin { }; Mode m_mode = Mode::Absolute; + std::optional m_newAddress; bool m_requestFocus = true; - std::string m_input; + std::string m_input = "0x"; wolv::math_eval::MathEvaluator m_evaluator; }; @@ -1101,7 +1119,7 @@ namespace hex::plugin::builtin { ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.resize" }, ICON_VS_ARROW_BOTH, 1700, Shortcut::None, [this] { auto provider = ImHexApi::Provider::get(); - this->openPopup(provider->getBaseAddress()); + this->openPopup(provider->getActualSize()); }, [] { return ImHexApi::Provider::isValid() && ImHexApi::Provider::get()->isResizable(); });