From 4ba12e9c2306606bcb8ac14617f5698c506342e3 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 2 Sep 2020 21:21:24 +0200 Subject: 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. --- Libraries/LibGUI/ListView.cpp | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'Libraries/LibGUI/ListView.cpp') 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; -- cgit v1.2.3