From a39c921421f021bf55e0180aaabc0b451b467808 Mon Sep 17 00:00:00 2001 From: Samu698 Date: Sun, 13 Mar 2022 19:08:10 +0100 Subject: [PATCH] HexEditor: Selection follows cursor while pressing shift This patch makes the HexEditor behaviour similar to the one of the text editor, this can be seen by pressing shift and the arrow keys --- Userland/Applications/HexEditor/HexEditor.cpp | 58 ++++++++----------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index 9281bbe830..fe82bec33a 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -377,8 +377,17 @@ 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; + auto move_and_update_cursor_by = [&](i64 cursor_location_change) { + size_t new_position = m_position + cursor_location_change; + if (event.modifiers() & Mod_Shift) { + size_t selection_pivot = m_position == m_selection_end ? m_selection_start : m_selection_end; + m_position = new_position; + m_selection_start = selection_pivot; + m_selection_end = m_position; + if (m_selection_start > m_selection_end) + swap(m_selection_start, m_selection_end); + } else + m_selection_start = m_selection_end = m_position = new_position; m_cursor_at_low_nibble = false; reset_cursor_blink_state(); scroll_position_into_view(m_position); @@ -386,64 +395,47 @@ void HexEditor::keydown_event(GUI::KeyEvent& event) 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()) { - advance_cursor_backwards(bytes_per_row()); - } + if (m_position >= bytes_per_row()) + move_and_update_cursor_by(-bytes_per_row()); return; } if (event.key() == KeyCode::Key_Down) { - if (m_position + bytes_per_row() < m_document->size()) { - advance_cursor_forward(bytes_per_row()); - } + if (m_position + bytes_per_row() < m_document->size()) + move_and_update_cursor_by(bytes_per_row()); return; } if (event.key() == KeyCode::Key_Left) { - if (m_position >= 1) { - advance_cursor_backwards(1); - } + if (m_position >= 1) + move_and_update_cursor_by(-1); return; } if (event.key() == KeyCode::Key_Right) { - if (m_position + 1 < m_document->size()) { - advance_cursor_forward(1); - } + if (m_position + 1 < m_document->size()) + move_and_update_cursor_by(1); return; } if (event.key() == KeyCode::Key_Backspace) { - if (m_position > 0) { - advance_cursor_backwards(1); - } + if (m_position > 0) + move_and_update_cursor_by(-1); return; } if (event.key() == KeyCode::Key_PageUp) { auto cursor_location_change = min(bytes_per_row() * visible_content_rect().height(), m_position); - if (cursor_location_change > 0) { - advance_cursor_backwards(cursor_location_change); - } + if (cursor_location_change > 0) + move_and_update_cursor_by(-cursor_location_change); return; } if (event.key() == KeyCode::Key_PageDown) { auto cursor_location_change = min(bytes_per_row() * visible_content_rect().height(), m_document->size() - m_position); - if (cursor_location_change > 0) { - advance_cursor_forward(cursor_location_change); - } + if (cursor_location_change > 0) + move_and_update_cursor_by(cursor_location_change); return; }