1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 20:58:11 +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:
Andreas Kling 2020-09-02 21:21:24 +02:00
parent 274a09246b
commit 4ba12e9c23
5 changed files with 10 additions and 30 deletions

View file

@ -234,16 +234,6 @@ void AbstractView::set_hovered_index(const ModelIndex& index)
if (m_hovered_index == index)
return;
m_hovered_index = index;
if (m_hovered_index.is_valid())
m_last_valid_hovered_index = m_hovered_index;
update();
}
void AbstractView::set_last_valid_hovered_index(const ModelIndex& index)
{
if (m_last_valid_hovered_index == index)
return;
m_last_valid_hovered_index = index;
update();
}

View file

@ -104,8 +104,6 @@ public:
NonnullRefPtr<Gfx::Font> font_for_index(const ModelIndex&) const;
void set_last_valid_hovered_index(const ModelIndex&);
void set_key_column_and_sort_order(int column, SortOrder);
int key_column() const { return m_key_column; }
@ -153,7 +151,6 @@ protected:
bool m_might_drag { false };
ModelIndex m_hovered_index;
ModelIndex m_last_valid_hovered_index;
int m_key_column { -1 };
SortOrder m_sort_order;

View file

@ -196,9 +196,6 @@ void ComboBox::open()
Gfx::IntRect list_window_rect { my_screen_rect.bottom_left(), size };
list_window_rect.intersect(Desktop::the().rect().shrunken(0, taskbar_height + menubar_height + offset));
if (m_list_view->hover_highlighting())
m_list_view->set_last_valid_hovered_index({});
m_editor->set_has_visible_list(true);
m_editor->set_focus(true);
m_list_window->set_rect(list_window_rect);

View file

@ -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;

View file

@ -69,6 +69,7 @@ private:
virtual void paint_event(PaintEvent&) override;
virtual void keydown_event(KeyEvent&) override;
virtual void resize_event(ResizeEvent&) override;
virtual void mousemove_event(MouseEvent&) override;
Gfx::IntRect content_rect(int row) const;
int item_count() const;