From c4dc150ed77538b6cdda1d3c7ed7e785da100637 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 5 Feb 2024 17:23:13 +0000 Subject: [PATCH] HexEditor: Extract some content-width calculations We repeat the same calculations a lot, and it's easy to make mistakes. m_address_bar_width and offset_margin_width() have basically the same purpose, but both are a little wrong. This removes the former, but we'll get to the latter soon. --- Userland/Applications/HexEditor/HexEditor.cpp | 29 +++++++++---------- Userland/Applications/HexEditor/HexEditor.h | 5 +++- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index 10e4a75e8a..af97dc22a7 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -235,9 +235,9 @@ bool HexEditor::copy_selected_hex_to_clipboard_as_c_code() void HexEditor::set_bytes_per_row(size_t bytes_per_row) { m_bytes_per_row = bytes_per_row; - auto newWidth = offset_margin_width() + (m_bytes_per_row * cell_width()) + 2 * m_padding + (m_bytes_per_row * character_width()) + 4 * m_padding; - auto newHeight = total_rows() * line_height() + 2 * m_padding; - set_content_size({ static_cast(newWidth), static_cast(newHeight) }); + auto new_width = offset_area_width() + hex_area_width() + text_area_width(); + auto new_height = total_rows() * line_height() + 2 * m_padding; + set_content_size({ static_cast(new_width), static_cast(new_height) }); update(); } @@ -246,9 +246,9 @@ void HexEditor::set_content_length(size_t length) if (length == m_content_length) return; m_content_length = length; - auto newWidth = offset_margin_width() + (m_bytes_per_row * cell_width()) + 2 * m_padding + (m_bytes_per_row * character_width()) + 4 * m_padding; - auto newHeight = total_rows() * line_height() + 2 * m_padding; - set_content_size({ static_cast(newWidth), static_cast(newHeight) }); + auto new_width = offset_area_width() + hex_area_width() + text_area_width(); + auto new_height = total_rows() * line_height() + 2 * m_padding; + set_content_size({ static_cast(new_width), static_cast(new_height) }); } Optional HexEditor::get_byte(size_t position) @@ -276,14 +276,14 @@ Optional HexEditor::offset_at(Gfx::IntPoint position) con auto absolute_x = horizontal_scrollbar().value() + position.x(); auto absolute_y = vertical_scrollbar().value() + position.y(); - auto hex_start_x = frame_thickness() + m_address_bar_width; + auto hex_start_x = frame_thickness() + offset_area_width(); auto hex_start_y = frame_thickness() + m_padding; - auto hex_end_x = static_cast(hex_start_x + bytes_per_row() * cell_width()); + auto hex_end_x = hex_start_x + hex_area_width(); auto hex_end_y = static_cast(hex_start_y + m_padding + total_rows() * line_height()); - auto text_start_x = static_cast(frame_thickness() + m_address_bar_width + 2 * m_padding + bytes_per_row() * cell_width()); + auto text_start_x = hex_start_x + hex_area_width(); auto text_start_y = frame_thickness() + m_padding; - auto text_end_x = static_cast(text_start_x + bytes_per_row() * character_width()); + auto text_end_x = text_start_x + text_area_width(); auto text_end_y = static_cast(text_start_y + m_padding + total_rows() * line_height()); // Hexadecimal display @@ -390,7 +390,7 @@ void HexEditor::scroll_position_into_view(size_t position) size_t y = position / bytes_per_row(); size_t x = position % bytes_per_row(); Gfx::IntRect rect { - static_cast(frame_thickness() + offset_margin_width() + x * cell_width() + 2 * m_padding), + static_cast(frame_thickness() + offset_area_width() + m_padding + x * cell_width()), static_cast(frame_thickness() + m_padding + y * line_height()), static_cast(cell_width()), static_cast(line_height() - m_line_spacing) @@ -590,15 +590,14 @@ void HexEditor::paint_event(GUI::PaintEvent& event) Gfx::IntRect offset_clip_rect { 0, vertical_scrollbar().value(), - m_address_bar_width - m_padding, + offset_area_width(), height() - height_occupied_by_horizontal_scrollbar() //(total_rows() * line_height()) + 5 }; painter.fill_rect(offset_clip_rect, palette().ruler()); painter.draw_line(offset_clip_rect.top_right(), offset_clip_rect.bottom_right(), palette().ruler_border()); - auto margin_and_hex_width = static_cast(offset_margin_width() + m_bytes_per_row * cell_width() + 3 * m_padding); - painter.draw_line({ margin_and_hex_width, 0 }, - { margin_and_hex_width, vertical_scrollbar().value() + (height() - height_occupied_by_horizontal_scrollbar()) }, + painter.draw_line({ offset_area_width() + hex_area_width(), 0 }, + { offset_area_width() + hex_area_width(), vertical_scrollbar().value() + (height() - height_occupied_by_horizontal_scrollbar()) }, palette().ruler_border()); size_t view_height = (height() - height_occupied_by_horizontal_scrollbar()); diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h index 01daa120dc..44d245c7df 100644 --- a/Userland/Applications/HexEditor/HexEditor.h +++ b/Userland/Applications/HexEditor/HexEditor.h @@ -102,7 +102,6 @@ private: RefPtr m_edit_annotation_action; RefPtr m_delete_annotation_action; - static constexpr int m_address_bar_width = 90; static constexpr int m_padding = 5; void scroll_position_into_view(size_t position); @@ -113,6 +112,10 @@ private: size_t cell_width() const { return character_width() * 3; } size_t offset_margin_width() const { return 80; } + int offset_area_width() const { return offset_margin_width() + m_padding; } + int hex_area_width() const { return m_padding + m_bytes_per_row * cell_width() + m_padding; } + int text_area_width() const { return m_padding + m_bytes_per_row * character_width() + m_padding; } + struct OffsetData { size_t offset; EditMode panel;