From b4fde7201328fb73e7789ebf8bf6ee8c47b4cb64 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 30 Mar 2020 19:57:44 +0200 Subject: LibGUI: Brighten icons when hovering items in item views View classes now track their hovered item and paint them in a slightly brighter shade to liven up the user interface. :^) --- Libraries/LibGUI/AbstractView.cpp | 11 ++++++++++- Libraries/LibGUI/AbstractView.h | 2 ++ Libraries/LibGUI/ColumnsView.cpp | 11 ++++++++--- Libraries/LibGUI/ItemView.cpp | 9 +++++++-- Libraries/LibGUI/TableView.cpp | 10 +++++++--- Libraries/LibGUI/TreeView.cpp | 8 ++++++-- 6 files changed, 40 insertions(+), 11 deletions(-) (limited to 'Libraries') diff --git a/Libraries/LibGUI/AbstractView.cpp b/Libraries/LibGUI/AbstractView.cpp index 3842ae117f..bc06ba4b2c 100644 --- a/Libraries/LibGUI/AbstractView.cpp +++ b/Libraries/LibGUI/AbstractView.cpp @@ -193,7 +193,16 @@ void AbstractView::mousedown_event(MouseEvent& event) void AbstractView::mousemove_event(MouseEvent& event) { - if (!model() || !m_might_drag) + if (!model()) + return ScrollableWidget::mousemove_event(event); + + auto hovered_index = index_at_event_position(event.position()); + if (m_hovered_index != hovered_index) { + m_hovered_index = hovered_index; + update(); + } + + if (!m_might_drag) return ScrollableWidget::mousemove_event(event); if (!(event.buttons() & MouseButton::Left) || m_selection.is_empty()) { diff --git a/Libraries/LibGUI/AbstractView.h b/Libraries/LibGUI/AbstractView.h index 5e041a1d64..dfedbd7614 100644 --- a/Libraries/LibGUI/AbstractView.h +++ b/Libraries/LibGUI/AbstractView.h @@ -96,6 +96,8 @@ protected: Gfx::Point m_left_mousedown_position; bool m_might_drag { false }; + ModelIndex m_hovered_index; + private: RefPtr m_model; OwnPtr m_editing_delegate; diff --git a/Libraries/LibGUI/ColumnsView.cpp b/Libraries/LibGUI/ColumnsView.cpp index 3e8da13ff3..b4feee1c38 100644 --- a/Libraries/LibGUI/ColumnsView.cpp +++ b/Libraries/LibGUI/ColumnsView.cpp @@ -128,9 +128,14 @@ void ColumnsView::paint_event(PaintEvent& event) auto icon = model()->data(index, Model::Role::Icon); Gfx::Rect icon_rect = { column_x + icon_spacing(), 0, icon_size(), icon_size() }; icon_rect.center_vertically_within(row_rect); - if (icon.is_icon()) - if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) - painter.blit(icon_rect.location(), *bitmap, bitmap->rect()); + if (icon.is_icon()) { + if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) { + if (m_hovered_index.is_valid() && m_hovered_index.parent() == index.parent() && m_hovered_index.row() == index.row()) + painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect()); + else + painter.blit(icon_rect.location(), *bitmap, bitmap->rect()); + } + } Gfx::Rect text_rect = { icon_rect.right() + 1 + icon_spacing(), row * item_height(), diff --git a/Libraries/LibGUI/ItemView.cpp b/Libraries/LibGUI/ItemView.cpp index 42b4effe72..f3b33fd561 100644 --- a/Libraries/LibGUI/ItemView.cpp +++ b/Libraries/LibGUI/ItemView.cpp @@ -299,8 +299,13 @@ void ItemView::paint_event(PaintEvent& event) get_item_rects(item_index, font, item_text, item_rect, icon_rect, text_rect); if (icon.is_icon()) { - if (auto bitmap = icon.as_icon().bitmap_for_size(icon_rect.width())) - painter.draw_scaled_bitmap(icon_rect, *bitmap, bitmap->rect()); + if (auto bitmap = icon.as_icon().bitmap_for_size(icon_rect.width())) { + if (m_hovered_index.is_valid() && m_hovered_index == model_index) { + painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect()); + } else { + painter.blit(icon_rect.location(), *bitmap, bitmap->rect()); + } + } } Color text_color; diff --git a/Libraries/LibGUI/TableView.cpp b/Libraries/LibGUI/TableView.cpp index 3522f08464..30204b6087 100644 --- a/Libraries/LibGUI/TableView.cpp +++ b/Libraries/LibGUI/TableView.cpp @@ -26,7 +26,6 @@ #include #include -#include #include #include #include @@ -35,6 +34,7 @@ #include #include #include +#include namespace GUI { @@ -119,8 +119,12 @@ void TableView::paint_event(PaintEvent& event) if (data.is_bitmap()) { painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect()); } else if (data.is_icon()) { - if (auto bitmap = data.as_icon().bitmap_for_size(16)) - painter.blit(cell_rect.location(), *bitmap, bitmap->rect()); + if (auto bitmap = data.as_icon().bitmap_for_size(16)) { + if (m_hovered_index.is_valid() && cell_index.row() == m_hovered_index.row()) + painter.blit_brightened(cell_rect.location(), *bitmap, bitmap->rect()); + else + painter.blit(cell_rect.location(), *bitmap, bitmap->rect()); + } } else { Color text_color; if (is_selected_row) diff --git a/Libraries/LibGUI/TreeView.cpp b/Libraries/LibGUI/TreeView.cpp index bbc6ef97e5..93604b0ba6 100644 --- a/Libraries/LibGUI/TreeView.cpp +++ b/Libraries/LibGUI/TreeView.cpp @@ -260,8 +260,12 @@ void TreeView::paint_event(PaintEvent& event) Gfx::Rect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() }; auto icon = model.data(index, Model::Role::Icon); if (icon.is_icon()) { - if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) - painter.blit(icon_rect.location(), *bitmap, bitmap->rect()); + if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) { + if (m_hovered_index.is_valid() && m_hovered_index.parent() == index.parent() && m_hovered_index.row() == index.row()) + painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect()); + else + painter.blit(icon_rect.location(), *bitmap, bitmap->rect()); + } } Gfx::Rect text_rect = { icon_rect.right() + 1 + icon_spacing(), rect.y(), -- cgit v1.2.3