summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI/ListView.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-09-02 21:21:24 +0200
committerAndreas Kling <kling@serenityos.org>2020-09-02 21:29:31 +0200
commit4ba12e9c2306606bcb8ac14617f5698c506342e3 (patch)
tree04fe1403ac7f2cf43ec4b65a1b6baf9ae25aedcd /Libraries/LibGUI/ListView.cpp
parent274a09246b7bc96845574995407b4300418acade (diff)
downloadserenity-4ba12e9c2306606bcb8ac14617f5698c506342e3.zip
LibGUI: Simplify ListView hover highlighting
Instead of tracking the last valid hovered index, just hook the mousemove event and make the cursor follow the hover when it changes.
Diffstat (limited to 'Libraries/LibGUI/ListView.cpp')
-rw-r--r--Libraries/LibGUI/ListView.cpp23
1 files changed, 9 insertions, 14 deletions
diff --git a/Libraries/LibGUI/ListView.cpp b/Libraries/LibGUI/ListView.cpp
index c1d69fb3e9..50e3449f05 100644
--- a/Libraries/LibGUI/ListView.cpp
+++ b/Libraries/LibGUI/ListView.cpp
@@ -127,11 +127,7 @@ void ListView::paint_event(PaintEvent& event)
int painted_item_index = 0;
for (int row_index = 0; row_index < model()->row_count(); ++row_index) {
- bool is_selected_row;
- if (hover_highlighting() && m_last_valid_hovered_index.is_valid())
- is_selected_row = row_index == m_last_valid_hovered_index.row();
- else
- is_selected_row = selection().contains_row(row_index);
+ bool is_selected_row = selection().contains_row(row_index);
int y = painted_item_index * item_height();
@@ -185,20 +181,23 @@ int ListView::item_count() const
return model()->row_count();
}
+void ListView::mousemove_event(MouseEvent& event)
+{
+ auto previous_hovered_index = m_hovered_index;
+ AbstractView::mousemove_event(event);
+ if (hover_highlighting() && previous_hovered_index != m_hovered_index)
+ set_cursor(m_hovered_index, SelectionUpdate::Set);
+}
+
void ListView::keydown_event(KeyEvent& event)
{
if (!model())
return;
- auto& model = *this->model();
SelectionUpdate selection_update = SelectionUpdate::Set;
ModelIndex new_index;
if (event.key() == KeyCode::Key_Return) {
- if (hover_highlighting() && m_last_valid_hovered_index.is_valid()) {
- auto new_index = model.index(m_last_valid_hovered_index.row(), m_last_valid_hovered_index.column());
- selection().set(new_index);
- }
activate_selected();
return;
}
@@ -267,15 +266,11 @@ void ListView::move_cursor(CursorMovement movement, SelectionUpdate selection_up
new_index = model.index(model.row_count() - 1, 0);
break;
case CursorMovement::PageUp: {
- if (hover_highlighting())
- set_last_valid_hovered_index({});
int items_per_page = visible_content_rect().height() / item_height();
new_index = model.index(max(0, cursor_index().row() - items_per_page), cursor_index().column());
break;
}
case CursorMovement::PageDown: {
- if (hover_highlighting())
- set_last_valid_hovered_index({});
int items_per_page = visible_content_rect().height() / item_height();
new_index = model.index(min(model.row_count() - 1, cursor_index().row() + items_per_page), cursor_index().column());
break;