diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 6394e6c6e..11d8c1c74 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -88,12 +88,14 @@ namespace hex::plugin::builtin { } } + bool isOffsetValid = m_newAddress <= ImHexApi::Provider::get()->getActualSize(); + bool executeGoto = false; - if (ImGui::IsItemFocused() && ImGui::IsKeyPressed(ImGuiKey_Enter)) { + if (isOffsetValid && ImGui::IsItemFocused() && ImGui::IsKeyPressed(ImGuiKey_Enter)) { executeGoto = true; } - ImGui::BeginDisabled(!m_newAddress.has_value()); + ImGui::BeginDisabled(!m_newAddress.has_value() || !isOffsetValid); { 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; @@ -163,10 +165,18 @@ namespace hex::plugin::builtin { ImGui::EndTabItem(); } - if (ImGui::Button("hex.builtin.view.hex_editor.select.select"_lang) || (ImGui::IsItemFocused() && (ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Enter)))) { - editor->setSelection(m_region.getStartAddress(), m_region.getEndAddress()); - editor->jumpToSelection(); + const auto provider = ImHexApi::Provider::get(); + bool isOffsetValid = m_region.getStartAddress() <= m_region.getEndAddress() && + m_region.getEndAddress() < provider->getActualSize(); + + ImGui::BeginDisabled(!isOffsetValid); + { + if (ImGui::Button("hex.builtin.view.hex_editor.select.select"_lang) || (ImGui::IsItemFocused() && (ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Enter)))) { + editor->setSelection(m_region.getStartAddress(), m_region.getEndAddress()); + editor->jumpToSelection(); + } } + ImGui::EndDisabled(); ImGui::EndTabBar(); } diff --git a/plugins/ui/source/ui/hex_editor.cpp b/plugins/ui/source/ui/hex_editor.cpp index f67692365..c619d7718 100644 --- a/plugins/ui/source/ui/hex_editor.cpp +++ b/plugins/ui/source/ui/hex_editor.cpp @@ -837,9 +837,20 @@ namespace hex::ui { m_provider->setCurrentPage(m_provider->getPageOfAddress(newSelection.address).value_or(0)); const auto pageAddress = m_provider->getCurrentPageAddress() + m_provider->getBaseAddress(); + const auto targetRowNumber = (newSelection.getStartAddress() - pageAddress) / m_bytesPerRow; - m_scrollPosition = (newSelection.getStartAddress() - pageAddress) / m_bytesPerRow; - m_scrollPosition -= m_visibleRowCount * m_jumpPivot; + // Calculate the current top and bottom row numbers of the viewport + ImS64 currentTopRow = m_scrollPosition; + ImS64 currentBottomRow = m_scrollPosition + m_visibleRowCount - 3; + + // Check if the targetRowNumber is outside the current visible range + if (ImS64(targetRowNumber) < currentTopRow) { + // If target is above the current view, scroll just enough to bring it into view at the top + m_scrollPosition = targetRowNumber - m_visibleRowCount * m_jumpPivot; + } else if (ImS64(targetRowNumber) > currentBottomRow) { + // If target is below the current view, scroll just enough to bring it into view at the bottom + m_scrollPosition = targetRowNumber - (m_visibleRowCount - 3); + } m_jumpPivot = 0.0F; }