Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jump scroll offset #1660

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions plugins/builtin/source/content/views/view_hex_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)))) {
WerWolv marked this conversation as resolved.
Show resolved Hide resolved
editor->setSelection(m_region.getStartAddress(), m_region.getEndAddress());
editor->jumpToSelection();
}
}
ImGui::EndDisabled();

ImGui::EndTabBar();
}
Expand Down
17 changes: 14 additions & 3 deletions plugins/ui/source/ui/hex_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

m_scrollPosition = (newSelection.getStartAddress() - pageAddress) / m_bytesPerRow;
m_scrollPosition -= m_visibleRowCount * m_jumpPivot;
const auto targetRowNumber = (newSelection.getStartAddress() - pageAddress) / m_bytesPerRow;

// 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;
}
Expand Down