diff options
author | Liav A <liavalb@gmail.com> | 2022-02-19 16:52:09 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-20 15:44:36 +0100 |
commit | c8e691f917d396dea43d5a62bd94ac5a76442518 (patch) | |
tree | 8935e86dba208437fb79e433020ea148d0e3a300 /Userland | |
parent | 410254dbe2924112aca0ff2a1ed8c735ceb9ee52 (diff) | |
download | serenity-c8e691f917d396dea43d5a62bd94ac5a76442518.zip |
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.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/HexEditor/HexEditor.cpp | 59 |
1 files 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<u8>(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; } |