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

HexEditor: Save and reuse vertical positions when painting rows

Rather than repeatedly calculating the same y offset over and over,
calculate it once per row, and re-use that result.
This commit is contained in:
Sam Atkins 2024-02-02 20:33:33 +00:00 committed by Sam Atkins
parent 4af565362c
commit 60f52aa0ef

View file

@ -607,10 +607,13 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
size_t max_row = min(total_rows(), min_row + ceil_div(view_height, line_height())); // if above calculated rows, use calculated rows size_t max_row = min(total_rows(), min_row + ceil_div(view_height, line_height())); // if above calculated rows, use calculated rows
for (size_t row = min_row; row < max_row; row++) { for (size_t row = min_row; row < max_row; row++) {
int row_text_y = frame_thickness() + m_padding + row * line_height();
int row_background_y = row_text_y - m_line_spacing / 2;
// Paint offsets // Paint offsets
Gfx::IntRect side_offset_rect { Gfx::IntRect side_offset_rect {
frame_thickness() + m_padding, frame_thickness() + m_padding,
static_cast<int>(frame_thickness() + m_padding + row * line_height()), row_text_y,
width() - width_occupied_by_vertical_scrollbar(), width() - width_occupied_by_vertical_scrollbar(),
height() - height_occupied_by_horizontal_scrollbar() height() - height_occupied_by_horizontal_scrollbar()
}; };
@ -634,10 +637,10 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
auto const annotation = m_document->annotations().closest_annotation_at(byte_position); auto const annotation = m_document->annotations().closest_annotation_at(byte_position);
Gfx::IntRect hex_display_rect_high_nibble { Gfx::IntRect hex_display_rect_high_nibble {
frame_thickness() + offset_margin_width() + column * cell_width() + 2 * m_padding, static_cast<int>(frame_thickness() + offset_margin_width() + column * cell_width() + 2 * m_padding),
frame_thickness() + m_padding + row * line_height(), row_text_y,
character_width(), static_cast<int>(character_width()),
line_height() - m_line_spacing static_cast<int>(line_height() - m_line_spacing),
}; };
Gfx::IntRect hex_display_rect_low_nibble { Gfx::IntRect hex_display_rect_low_nibble {
@ -648,10 +651,10 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
}; };
Gfx::IntRect background_rect { Gfx::IntRect background_rect {
frame_thickness() + offset_margin_width() + column * cell_width() + 1 * m_padding, static_cast<int>(frame_thickness() + offset_margin_width() + column * cell_width() + 1 * m_padding),
frame_thickness() + m_line_spacing / 2 + row * line_height(), row_background_y,
cell_width(), static_cast<int>(cell_width()),
line_height() static_cast<int>(line_height()),
}; };
auto line = String::formatted("{:02X}", cell.value).release_value_but_fixme_should_propagate_errors(); auto line = String::formatted("{:02X}", cell.value).release_value_but_fixme_should_propagate_errors();
@ -698,17 +701,17 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
painter.fill_rect(background_rect, background_color_hex); painter.fill_rect(background_rect, background_color_hex);
Gfx::IntRect text_display_rect { Gfx::IntRect text_display_rect {
frame_thickness() + offset_margin_width() + bytes_per_row() * cell_width() + column * character_width() + 4 * m_padding, static_cast<int>(frame_thickness() + offset_margin_width() + bytes_per_row() * cell_width() + column * character_width() + 4 * m_padding),
frame_thickness() + m_padding + row * line_height(), row_text_y,
character_width(), static_cast<int>(character_width()),
line_height() - m_line_spacing static_cast<int>(line_height() - m_line_spacing),
}; };
auto draw_cursor_rect = [&]() { auto draw_cursor_rect = [&]() {
if (byte_position == m_position) { if (byte_position == m_position) {
Gfx::IntRect cursor_position_rect { Gfx::IntRect cursor_position_rect {
(m_edit_mode == EditMode::Hex) ? static_cast<int>(hex_display_rect_high_nibble.left() + m_cursor_at_low_nibble * character_width()) : text_display_rect.left(), (m_edit_mode == EditMode::Hex) ? static_cast<int>(hex_display_rect_high_nibble.left() + m_cursor_at_low_nibble * character_width()) : text_display_rect.left(),
static_cast<int>(frame_thickness() + m_line_spacing / 2 + row * line_height()), row_background_y,
static_cast<int>(character_width()), static_cast<int>(character_width()),
static_cast<int>(line_height()) static_cast<int>(line_height())
}; };
@ -728,10 +731,10 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
} }
Gfx::IntRect text_background_rect { Gfx::IntRect text_background_rect {
frame_thickness() + offset_margin_width() + bytes_per_row() * cell_width() + column * character_width() + 4 * m_padding, static_cast<int>(frame_thickness() + offset_margin_width() + bytes_per_row() * cell_width() + column * character_width() + 4 * m_padding),
frame_thickness() + m_line_spacing / 2 + row * line_height(), row_background_y,
character_width(), static_cast<int>(character_width()),
line_height() static_cast<int>(line_height()),
}; };
painter.fill_rect(text_background_rect, background_color_text); painter.fill_rect(text_background_rect, background_color_text);