LibGUI: Constrain relative cursor delta to valid range

This commit is contained in:
Tom 2020-12-30 22:15:41 -07:00 committed by Andreas Kling
parent 0c57e16ce4
commit eabbe03b97
Notes: sideshowbarker 2024-07-19 00:19:07 +09:00

View file

@ -213,7 +213,19 @@ void ListView::move_cursor_relative(int steps, SelectionUpdate selection_update)
auto& model = *this->model();
ModelIndex new_index;
if (cursor_index().is_valid()) {
new_index = model.index(cursor_index().row() + steps, cursor_index().column());
auto row = cursor_index().row();
if (steps > 0) {
if (row + steps >= model.row_count())
row = model.row_count() - 1;
else
row += steps;
} else if (steps < 0) {
if (row < -steps)
row = 0;
else
row += steps;
}
new_index = model.index(row, cursor_index().column());
} else {
new_index = model.index(0, 0);
}