mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-08 12:19:37 +03:00
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
This commit is contained in:
parent
61b8834b1a
commit
a39c921421
Notes:
sideshowbarker
2024-07-19 17:07:03 +09:00
Author: https://github.com/samu698 Commit: https://github.com/SerenityOS/serenity/commit/a39c921421f Pull-request: https://github.com/SerenityOS/serenity/pull/13031
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user