diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index 9281bbe830f..fe82bec33af 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; }