summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI/AbstractView.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-08-27 18:36:31 +0200
committerAndreas Kling <kling@serenityos.org>2020-08-27 18:36:31 +0200
commit9cf37901cd4166117f8ed7d381b59da243ea7443 (patch)
tree3025c74ca786b2a7b1438045562a631844c58138 /Libraries/LibGUI/AbstractView.cpp
parent76a0acb5bcb2fae9e7a08ea8de02e9873708c255 (diff)
downloadserenity-9cf37901cd4166117f8ed7d381b59da243ea7443.zip
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. :^)
Diffstat (limited to 'Libraries/LibGUI/AbstractView.cpp')
-rw-r--r--Libraries/LibGUI/AbstractView.cpp27
1 files changed, 26 insertions, 1 deletions
diff --git a/Libraries/LibGUI/AbstractView.cpp b/Libraries/LibGUI/AbstractView.cpp
index d6fe8c77a7..8f299b247e 100644
--- a/Libraries/LibGUI/AbstractView.cpp
+++ b/Libraries/LibGUI/AbstractView.cpp
@@ -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();
+ }
+}
+
}