1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:27:45 +00:00

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.
This commit is contained in:
Sam Atkins 2024-02-05 17:23:13 +00:00 committed by Sam Atkins
parent 4dc3816853
commit c4dc150ed7
2 changed files with 18 additions and 16 deletions

View file

@ -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) void HexEditor::set_bytes_per_row(size_t bytes_per_row)
{ {
m_bytes_per_row = 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 new_width = offset_area_width() + hex_area_width() + text_area_width();
auto newHeight = total_rows() * line_height() + 2 * m_padding; auto new_height = total_rows() * line_height() + 2 * m_padding;
set_content_size({ static_cast<int>(newWidth), static_cast<int>(newHeight) }); set_content_size({ static_cast<int>(new_width), static_cast<int>(new_height) });
update(); update();
} }
@ -246,9 +246,9 @@ void HexEditor::set_content_length(size_t length)
if (length == m_content_length) if (length == m_content_length)
return; return;
m_content_length = length; 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 new_width = offset_area_width() + hex_area_width() + text_area_width();
auto newHeight = total_rows() * line_height() + 2 * m_padding; auto new_height = total_rows() * line_height() + 2 * m_padding;
set_content_size({ static_cast<int>(newWidth), static_cast<int>(newHeight) }); set_content_size({ static_cast<int>(new_width), static_cast<int>(new_height) });
} }
Optional<u8> HexEditor::get_byte(size_t position) Optional<u8> HexEditor::get_byte(size_t position)
@ -276,14 +276,14 @@ Optional<HexEditor::OffsetData> HexEditor::offset_at(Gfx::IntPoint position) con
auto absolute_x = horizontal_scrollbar().value() + position.x(); auto absolute_x = horizontal_scrollbar().value() + position.x();
auto absolute_y = vertical_scrollbar().value() + position.y(); 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_start_y = frame_thickness() + m_padding;
auto hex_end_x = static_cast<int>(hex_start_x + bytes_per_row() * cell_width()); auto hex_end_x = hex_start_x + hex_area_width();
auto hex_end_y = static_cast<int>(hex_start_y + m_padding + total_rows() * line_height()); auto hex_end_y = static_cast<int>(hex_start_y + m_padding + total_rows() * line_height());
auto text_start_x = static_cast<int>(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_start_y = frame_thickness() + m_padding;
auto text_end_x = static_cast<int>(text_start_x + bytes_per_row() * character_width()); auto text_end_x = text_start_x + text_area_width();
auto text_end_y = static_cast<int>(text_start_y + m_padding + total_rows() * line_height()); auto text_end_y = static_cast<int>(text_start_y + m_padding + total_rows() * line_height());
// Hexadecimal display // Hexadecimal display
@ -390,7 +390,7 @@ void HexEditor::scroll_position_into_view(size_t position)
size_t y = position / bytes_per_row(); size_t y = position / bytes_per_row();
size_t x = position % bytes_per_row(); size_t x = position % bytes_per_row();
Gfx::IntRect rect { Gfx::IntRect rect {
static_cast<int>(frame_thickness() + offset_margin_width() + x * cell_width() + 2 * m_padding), static_cast<int>(frame_thickness() + offset_area_width() + m_padding + x * cell_width()),
static_cast<int>(frame_thickness() + m_padding + y * line_height()), static_cast<int>(frame_thickness() + m_padding + y * line_height()),
static_cast<int>(cell_width()), static_cast<int>(cell_width()),
static_cast<int>(line_height() - m_line_spacing) static_cast<int>(line_height() - m_line_spacing)
@ -590,15 +590,14 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
Gfx::IntRect offset_clip_rect { Gfx::IntRect offset_clip_rect {
0, 0,
vertical_scrollbar().value(), vertical_scrollbar().value(),
m_address_bar_width - m_padding, offset_area_width(),
height() - height_occupied_by_horizontal_scrollbar() //(total_rows() * line_height()) + 5 height() - height_occupied_by_horizontal_scrollbar() //(total_rows() * line_height()) + 5
}; };
painter.fill_rect(offset_clip_rect, palette().ruler()); painter.fill_rect(offset_clip_rect, palette().ruler());
painter.draw_line(offset_clip_rect.top_right(), offset_clip_rect.bottom_right(), palette().ruler_border()); painter.draw_line(offset_clip_rect.top_right(), offset_clip_rect.bottom_right(), palette().ruler_border());
auto margin_and_hex_width = static_cast<int>(offset_margin_width() + m_bytes_per_row * cell_width() + 3 * m_padding); painter.draw_line({ offset_area_width() + hex_area_width(), 0 },
painter.draw_line({ margin_and_hex_width, 0 }, { offset_area_width() + hex_area_width(), vertical_scrollbar().value() + (height() - height_occupied_by_horizontal_scrollbar()) },
{ margin_and_hex_width, vertical_scrollbar().value() + (height() - height_occupied_by_horizontal_scrollbar()) },
palette().ruler_border()); palette().ruler_border());
size_t view_height = (height() - height_occupied_by_horizontal_scrollbar()); size_t view_height = (height() - height_occupied_by_horizontal_scrollbar());

View file

@ -102,7 +102,6 @@ private:
RefPtr<GUI::Action> m_edit_annotation_action; RefPtr<GUI::Action> m_edit_annotation_action;
RefPtr<GUI::Action> m_delete_annotation_action; RefPtr<GUI::Action> m_delete_annotation_action;
static constexpr int m_address_bar_width = 90;
static constexpr int m_padding = 5; static constexpr int m_padding = 5;
void scroll_position_into_view(size_t position); void scroll_position_into_view(size_t position);
@ -113,6 +112,10 @@ private:
size_t cell_width() const { return character_width() * 3; } size_t cell_width() const { return character_width() * 3; }
size_t offset_margin_width() const { return 80; } 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 { struct OffsetData {
size_t offset; size_t offset;
EditMode panel; EditMode panel;