Skip to content

Commit

Permalink
HexEditor: Reduce code duplication when handling key down events
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
supercomputer7 authored and awesomekling committed Feb 20, 2022
1 parent 410254d commit c8e691f
Showing 1 changed file with 24 additions and 35 deletions.
59 changes: 24 additions & 35 deletions Userland/Applications/HexEditor/HexEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit c8e691f

Please sign in to comment.