1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 08:28:11 +00:00

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.
This commit is contained in:
Liav A 2022-02-19 16:52:09 +02:00 committed by Andreas Kling
parent 410254dbe2
commit c8e691f917

View file

@ -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;
}