From 8024b0a186d2f0c1badf0c3a27da9087f39c25e7 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 7 Oct 2022 22:13:25 +0200 Subject: [PATCH] sys: Improve drag scrolling behaviour in hex editor view --- .../source/content/views/view_hex_editor.cpp | 25 ++++++++++--------- 1 file changed, 13 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 1921dd435667a..ec3552712f0b0 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -1017,20 +1017,21 @@ namespace hex::plugin::builtin { // Scroll to the cursor if it's either at the top or bottom edge of the screen if (this->m_shouldScrollToSelection && this->isSelectionValid()) { // Make sure simply clicking on a byte at the edge of the screen won't cause scrolling - if ((ImGui::IsMouseDown(ImGuiMouseButton_Left) && providerData.selectionStart != providerData.selectionEnd)) { - auto fractionPerLine = 1.0 / (this->m_visibleRowCount + 1); + if ((ImGui::IsMouseDown(ImGuiMouseButton_Left) && providerData.selectionStart != providerData.selectionEnd && providerData.selectionEnd.has_value())) { + auto scrollPerLine = ImGui::GetScrollMaxY() / (provider->getSize() / (long double)(this->m_bytesPerRow)); - if (y == (u64(clipper.DisplayStart) + 3)) { - if (i128(*providerData.selectionEnd - provider->getBaseAddress() - provider->getCurrentPageAddress()) <= (i64(clipper.DisplayStart + 3) * this->m_bytesPerRow)) { - this->m_shouldScrollToSelection = false; - ImGui::SetScrollHereY(fractionPerLine * 5); + auto scrollPos = ImGui::GetScrollY(); + auto scrollUpStartPos = scrollPos + scrollPerLine * 2; + auto scrollDownStartPos = scrollPos + ImGui::GetWindowHeight() - scrollPerLine * 4; - } - } else if (y == (u64(clipper.DisplayStart) - 3)) { - if (i128(*providerData.selectionEnd - provider->getBaseAddress() - provider->getCurrentPageAddress()) >= (i64(clipper.DisplayEnd - 3) * this->m_bytesPerRow)) { - this->m_shouldScrollToSelection = false; - ImGui::SetScrollHereY(fractionPerLine * (this->m_visibleRowCount - 1)); - } + auto cursorPos = float(u64(*providerData.selectionEnd / this->m_bytesPerRow)) * CharacterSize.y; + + if (cursorPos <= scrollUpStartPos && y == u64(clipper.DisplayStart)) { + this->m_shouldScrollToSelection = false; + ImGui::SetScrollHereY(0.1F); + } else if (cursorPos >= scrollDownStartPos && y == u64(clipper.DisplayEnd - 1)) { + this->m_shouldScrollToSelection = false; + ImGui::SetScrollHereY(0.95F); } }