From 8ba47facf6c9c41169333e5034ef4a1007fd09fd Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 27 Jul 2021 17:23:52 +0200 Subject: [PATCH] LibGUI: Add ModelRole::IconOpacity and support it in all views :^) This role allows you to specify a custom opacity for icon painting. Note that the opacity is not in effect when the item is either selected and/or hovered. --- Userland/Libraries/LibGUI/ColumnsView.cpp | 3 ++- Userland/Libraries/LibGUI/IconView.cpp | 3 ++- Userland/Libraries/LibGUI/ListView.cpp | 6 ++++-- Userland/Libraries/LibGUI/ModelRole.h | 1 + Userland/Libraries/LibGUI/TableView.cpp | 3 ++- Userland/Libraries/LibGUI/TreeView.cpp | 14 +++++++++----- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibGUI/ColumnsView.cpp b/Userland/Libraries/LibGUI/ColumnsView.cpp index 7e0c7e8e7d..ccd9740f8a 100644 --- a/Userland/Libraries/LibGUI/ColumnsView.cpp +++ b/Userland/Libraries/LibGUI/ColumnsView.cpp @@ -118,7 +118,8 @@ void ColumnsView::paint_event(PaintEvent& event) } else 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()); + auto opacity = index.data(ModelRole::IconOpacity).as_float_or(1.0f); + painter.blit(icon_rect.location(), *bitmap, bitmap->rect(), opacity); } } } diff --git a/Userland/Libraries/LibGUI/IconView.cpp b/Userland/Libraries/LibGUI/IconView.cpp index 745d497a9e..87d17b573b 100644 --- a/Userland/Libraries/LibGUI/IconView.cpp +++ b/Userland/Libraries/LibGUI/IconView.cpp @@ -539,7 +539,8 @@ void IconView::paint_event(PaintEvent& event) } else if (m_hovered_index.is_valid() && m_hovered_index == item_data.index) { painter.blit_brightened(destination.location(), *bitmap, bitmap->rect()); } else { - painter.blit(destination.location(), *bitmap, bitmap->rect()); + auto opacity = item_data.index.data(ModelRole::IconOpacity).as_float_or(1.0f); + painter.blit(destination.location(), *bitmap, bitmap->rect(), opacity); } } } diff --git a/Userland/Libraries/LibGUI/ListView.cpp b/Userland/Libraries/LibGUI/ListView.cpp index 86bd0b6a1d..e97e87fa8e 100644 --- a/Userland/Libraries/LibGUI/ListView.cpp +++ b/Userland/Libraries/LibGUI/ListView.cpp @@ -118,8 +118,10 @@ void ListView::paint_list_item(Painter& painter, int row_index, int painted_item if (data.is_bitmap()) { painter.blit(row_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(row_rect.location(), *bitmap, bitmap->rect()); + if (auto bitmap = data.as_icon().bitmap_for_size(16)) { + auto opacity = index.data(ModelRole::IconOpacity).as_float_or(1.0f); + painter.blit(row_rect.location(), *bitmap, bitmap->rect(), opacity); + } } else { Color text_color; if (is_selected_row) diff --git a/Userland/Libraries/LibGUI/ModelRole.h b/Userland/Libraries/LibGUI/ModelRole.h index 28f2ce6a37..6ec3b7a019 100644 --- a/Userland/Libraries/LibGUI/ModelRole.h +++ b/Userland/Libraries/LibGUI/ModelRole.h @@ -14,6 +14,7 @@ enum class ModelRole { ForegroundColor, BackgroundColor, Icon, + IconOpacity, Font, MimeData, TextAlignment, diff --git a/Userland/Libraries/LibGUI/TableView.cpp b/Userland/Libraries/LibGUI/TableView.cpp index 9a904bec49..a8fadf5a6b 100644 --- a/Userland/Libraries/LibGUI/TableView.cpp +++ b/Userland/Libraries/LibGUI/TableView.cpp @@ -121,7 +121,8 @@ void TableView::paint_event(PaintEvent& event) } else 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()); + auto opacity = cell_index.data(ModelRole::IconOpacity).as_float_or(1.0f); + painter.blit(cell_rect.location(), *bitmap, bitmap->rect(), opacity); } } } else { diff --git a/Userland/Libraries/LibGUI/TreeView.cpp b/Userland/Libraries/LibGUI/TreeView.cpp index 021097f556..8219467f11 100644 --- a/Userland/Libraries/LibGUI/TreeView.cpp +++ b/Userland/Libraries/LibGUI/TreeView.cpp @@ -298,8 +298,10 @@ void TreeView::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)) { + auto opacity = cell_index.data(ModelRole::IconOpacity).as_float_or(1.0f); + painter.blit(cell_rect.location(), *bitmap, bitmap->rect(), opacity); + } } else { auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft); draw_item_text(painter, cell_index, is_selected_row, cell_rect, data.to_string(), font_for_index(cell_index), text_alignment, Gfx::TextElision::Right); @@ -318,10 +320,12 @@ void TreeView::paint_event(PaintEvent& event) auto icon = index.data(ModelRole::Icon); 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()) + 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()); + } else { + auto opacity = index.data(ModelRole::IconOpacity).as_float_or(1.0f); + painter.blit(icon_rect.location(), *bitmap, bitmap->rect(), opacity); + } } } draw_item_text(painter, index, is_selected_row, text_rect, index.data().to_string(), font_for_index(index), Gfx::TextAlignment::Center, Gfx::TextElision::None);