mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-29 04:37:55 +00:00
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.
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user