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.
This commit is contained in:
Sam Atkins 2024-02-05 17:31:08 +00:00 committed by Sam Atkins
parent c4dc150ed7
commit ca3f21f273
Notes: sideshowbarker 2024-07-17 08:35:21 +09:00

View file

@ -291,7 +291,11 @@ Optional<HexEditor::OffsetData> 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::OffsetData> 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;