From ca3f21f27362c1c36842196d5ebe803508fb3460 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 5 Feb 2024 17:31:08 +0000 Subject: [PATCH] HexEditor: Account for padding in offset_at() Compare the x position with the start of the hex characters, which is m_padding pixels to the right of hex_start_x. (And the same for the text characters.) If the position is within the padding area, clamp it so it's on the nearest character. This was previously covered-up somewhat by using the buggy m_address_bar_width value to calculate the start and end x positions of these areas. Fixing that in the previous commit made this more obvious. --- Userland/Applications/HexEditor/HexEditor.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index af97dc22a7..b11aba9b39 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -291,7 +291,11 @@ Optional HexEditor::offset_at(Gfx::IntPoint position) con if (absolute_x < hex_start_x || absolute_y < hex_start_y) return {}; - auto byte_x = (absolute_x - hex_start_x) / cell_width(); + auto hex_text_start_x = hex_start_x + m_padding; + auto hex_text_end_x = hex_end_x - m_padding; + absolute_x = clamp(absolute_x, hex_text_start_x, hex_text_end_x); + + auto byte_x = (absolute_x - hex_text_start_x) / cell_width(); auto byte_y = (absolute_y - hex_start_y) / line_height(); auto offset = (byte_y * m_bytes_per_row) + byte_x; @@ -306,7 +310,11 @@ Optional HexEditor::offset_at(Gfx::IntPoint position) con if (absolute_x < hex_start_x || absolute_y < hex_start_y) return {}; - auto byte_x = (absolute_x - text_start_x) / character_width(); + auto text_text_start_x = text_start_x + m_padding; + auto text_text_end_x = text_end_x - m_padding; + absolute_x = clamp(absolute_x, text_text_start_x, text_text_end_x); + + auto byte_x = (absolute_x - text_text_start_x) / character_width(); auto byte_y = (absolute_y - text_start_y) / line_height(); auto offset = (byte_y * m_bytes_per_row) + byte_x;