diff options
author | Samu698 <samu2altervista@gmail.com> | 2022-03-13 19:08:10 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-14 22:48:16 +0100 |
commit | a39c921421f021bf55e0180aaabc0b451b467808 (patch) | |
tree | 63e19803c1ff37d769714152d43a4c415a2cd5bf /Userland | |
parent | 61b8834b1ad8b1b5eb472bb0cf6711585e2b671e (diff) | |
download | serenity-a39c921421f021bf55e0180aaabc0b451b467808.zip |
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
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/HexEditor/HexEditor.cpp | 58 |
1 files 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<u8>(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; } |