1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 13:45:07 +00:00

LibGUI: Add a cursor to AbstractView, separate from the selection

Views now have a cursor index (retrievable via cursor_index()) which
is separate from the selection.

Until now, we've been using the first entry in the selection as
"the cursor", which gets messy whenever you want to select more than
one index in the model.

When setting the cursor, the selection is implicitly updated as well
to maintain the old behavior (for the most part.)

Going forward, this will make it much easier to implement things like
shift-select (extend selection from cursor) and such. :^)
This commit is contained in:
Andreas Kling 2020-08-27 18:36:31 +02:00
parent 76a0acb5bc
commit 9cf37901cd
8 changed files with 79 additions and 49 deletions

View file

@ -219,7 +219,7 @@ void AbstractView::mousedown_event(MouseEvent& event)
// We might be starting a drag, so don't throw away other selected items yet.
m_might_drag = true;
} else {
set_selection(index);
set_cursor(index, SelectionUpdate::Set);
}
update();
@ -424,4 +424,29 @@ void AbstractView::set_key_column_and_sort_order(int column, SortOrder sort_orde
update();
}
void AbstractView::set_cursor(ModelIndex index, SelectionUpdate selection_update)
{
if (m_cursor_index == index)
return;
if (!model()) {
m_cursor_index = {};
return;
}
if (model()->is_valid(index)) {
if (selection_update == SelectionUpdate::Set)
selection().set(index);
else if (selection_update == SelectionUpdate::Ctrl)
selection().toggle(index);
// FIXME: Support the other SelectionUpdate types
m_cursor_index = index;
// FIXME: We should scroll into view both vertically *and* horizontally.
scroll_into_view(index, false, true);
update();
}
}
}