Skip to content

Commit

Permalink
HexEditor: Move drawing cursor under a lambda and reuse it
Browse files Browse the repository at this point in the history
This is a cleanup patch dones for paint event for HexEditor, moving
repeated chunks of code under a common lambda.
  • Loading branch information
tetektoza committed Dec 18, 2023
1 parent d99af64 commit 59cdad6
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions Userland/Applications/HexEditor/HexEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,17 +684,27 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
}
painter.fill_rect(background_rect, background_color_hex);

if (m_edit_mode == EditMode::Hex) {
Gfx::IntRect text_display_rect {
frame_thickness() + offset_margin_width() + bytes_per_row() * cell_width() + j * character_width() + 4 * m_padding,
frame_thickness() + m_padding + i * line_height(),
character_width(),
line_height() - m_line_spacing
};

auto draw_cursor_rect = [&]() {
if (byte_position == m_position) {
Gfx::IntRect cursor_position_rect {
static_cast<int>(hex_display_rect_high_nibble.left() + m_cursor_at_low_nibble * character_width()),
(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 + i * line_height()),
static_cast<int>(character_width()),
static_cast<int>(line_height())
};
painter.fill_rect(cursor_position_rect, palette().black());
}
}
};

if (m_edit_mode == EditMode::Hex)
draw_cursor_rect();

if (byte_position == m_position && !edited_flag) {
painter.draw_text(hex_display_rect_high_nibble, high_nibble, Gfx::TextAlignment::TopLeft, m_cursor_at_low_nibble ? text_color_hex : palette().selection_text());
Expand All @@ -704,12 +714,6 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
painter.draw_text(hex_display_rect_low_nibble, low_nibble, Gfx::TextAlignment::TopLeft, text_color_hex);
}

Gfx::IntRect text_display_rect {
frame_thickness() + offset_margin_width() + bytes_per_row() * cell_width() + j * character_width() + 4 * m_padding,
frame_thickness() + m_padding + i * line_height(),
character_width(),
line_height() - m_line_spacing
};
Gfx::IntRect text_background_rect {
frame_thickness() + offset_margin_width() + bytes_per_row() * cell_width() + j * character_width() + 4 * m_padding,
frame_thickness() + m_line_spacing / 2 + i * line_height(),
Expand All @@ -720,17 +724,8 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
painter.fill_rect(text_background_rect, background_color_text);
auto character = String::formatted("{:c}", isprint(cell_value) ? cell_value : '.').release_value_but_fixme_should_propagate_errors();

if (m_edit_mode == EditMode::Text) {
if (byte_position == m_position) {
Gfx::IntRect cursor_position_rect {
text_display_rect.left(),
static_cast<int>(frame_thickness() + m_line_spacing / 2 + i * line_height()),
static_cast<int>(character_width()),
static_cast<int>(line_height())
};
painter.fill_rect(cursor_position_rect, palette().black());
}
}
if (m_edit_mode == EditMode::Text)
draw_cursor_rect();

if (byte_position == m_position)
painter.draw_text(text_display_rect, character, Gfx::TextAlignment::TopLeft, palette().selection_text());
Expand Down

0 comments on commit 59cdad6

Please sign in to comment.