From c8e691f917d396dea43d5a62bd94ac5a76442518 Mon Sep 17 00:00:00 2001 From: Liav A Date: Sat, 19 Feb 2022 16:52:09 +0200 Subject: [PATCH] HexEditor: Reduce code duplication when handling key down events Instead of having the same update block for each event we can use lambda functions to help updating the cursor when handling key down events. --- Userland/Applications/HexEditor/HexEditor.cpp | 59 ++++++++----------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index 1106057b11..7d9b7f60d3 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -378,67 +378,56 @@ void HexEditor::keydown_event(GUI::KeyEvent& event) { dbgln_if(HEX_DEBUG, "HexEditor::keydown_event key={}", static_cast(event.key())); + auto update_cursor_on_change = [&]() { + m_selection_start = m_selection_end = m_position; + m_cursor_at_low_nibble = false; + reset_cursor_blink_state(); + scroll_position_into_view(m_position); + update(); + update_status(); + }; + + auto advance_cursor_backwards = [this, update_cursor_on_change](size_t cursor_location_change) -> void { + m_position -= cursor_location_change; + update_cursor_on_change(); + }; + + auto advance_cursor_forward = [this, update_cursor_on_change](size_t cursor_location_change) -> void { + m_position += cursor_location_change; + update_cursor_on_change(); + }; + if (event.key() == KeyCode::Key_Up) { if (m_position >= bytes_per_row()) { - m_position -= bytes_per_row(); - m_selection_start = m_selection_end = m_position; - m_cursor_at_low_nibble = false; - reset_cursor_blink_state(); - scroll_position_into_view(m_position); - update(); - update_status(); + advance_cursor_backwards(bytes_per_row()); } return; } if (event.key() == KeyCode::Key_Down) { if (m_position + bytes_per_row() < m_document->size()) { - m_position += bytes_per_row(); - m_selection_start = m_selection_end = m_position; - m_cursor_at_low_nibble = false; - reset_cursor_blink_state(); - scroll_position_into_view(m_position); - update(); - update_status(); + advance_cursor_forward(bytes_per_row()); } return; } if (event.key() == KeyCode::Key_Left) { if (m_position >= 1) { - m_position--; - m_selection_start = m_selection_end = m_position; - m_cursor_at_low_nibble = false; - reset_cursor_blink_state(); - scroll_position_into_view(m_position); - update(); - update_status(); + advance_cursor_backwards(1); } return; } if (event.key() == KeyCode::Key_Right) { if (m_position + 1 < m_document->size()) { - m_position++; - m_selection_start = m_selection_end = m_position; - m_cursor_at_low_nibble = false; - reset_cursor_blink_state(); - scroll_position_into_view(m_position); - update(); - update_status(); + advance_cursor_forward(1); } return; } if (event.key() == KeyCode::Key_Backspace) { if (m_position > 0) { - m_position--; - m_selection_start = m_selection_end = m_position; - m_cursor_at_low_nibble = false; - reset_cursor_blink_state(); - scroll_position_into_view(m_position); - update(); - update_status(); + advance_cursor_backwards(1); } return; }