1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 19:57:41 +00:00

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.
This commit is contained in:
Andreas Kling 2021-07-27 17:23:52 +02:00
parent 2d1eff01a2
commit 8ba47facf6
6 changed files with 20 additions and 10 deletions

View file

@ -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()) { } 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()); painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect());
} else { } 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);
} }
} }
} }

View file

@ -539,7 +539,8 @@ void IconView::paint_event(PaintEvent& event)
} else if (m_hovered_index.is_valid() && m_hovered_index == item_data.index) { } else if (m_hovered_index.is_valid() && m_hovered_index == item_data.index) {
painter.blit_brightened(destination.location(), *bitmap, bitmap->rect()); painter.blit_brightened(destination.location(), *bitmap, bitmap->rect());
} else { } 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);
} }
} }
} }

View file

@ -118,8 +118,10 @@ void ListView::paint_list_item(Painter& painter, int row_index, int painted_item
if (data.is_bitmap()) { if (data.is_bitmap()) {
painter.blit(row_rect.location(), data.as_bitmap(), data.as_bitmap().rect()); painter.blit(row_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
} else if (data.is_icon()) { } else if (data.is_icon()) {
if (auto bitmap = data.as_icon().bitmap_for_size(16)) if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
painter.blit(row_rect.location(), *bitmap, bitmap->rect()); auto opacity = index.data(ModelRole::IconOpacity).as_float_or(1.0f);
painter.blit(row_rect.location(), *bitmap, bitmap->rect(), opacity);
}
} else { } else {
Color text_color; Color text_color;
if (is_selected_row) if (is_selected_row)

View file

@ -14,6 +14,7 @@ enum class ModelRole {
ForegroundColor, ForegroundColor,
BackgroundColor, BackgroundColor,
Icon, Icon,
IconOpacity,
Font, Font,
MimeData, MimeData,
TextAlignment, TextAlignment,

View file

@ -121,7 +121,8 @@ void TableView::paint_event(PaintEvent& event)
} else if (m_hovered_index.is_valid() && cell_index.row() == m_hovered_index.row()) { } else if (m_hovered_index.is_valid() && cell_index.row() == m_hovered_index.row()) {
painter.blit_brightened(cell_rect.location(), *bitmap, bitmap->rect()); painter.blit_brightened(cell_rect.location(), *bitmap, bitmap->rect());
} else { } 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 { } else {

View file

@ -298,8 +298,10 @@ void TreeView::paint_event(PaintEvent& event)
if (data.is_bitmap()) { if (data.is_bitmap()) {
painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect()); painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
} else if (data.is_icon()) { } else if (data.is_icon()) {
if (auto bitmap = data.as_icon().bitmap_for_size(16)) if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
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 { } else {
auto text_alignment = cell_index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft); 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); 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); auto icon = index.data(ModelRole::Icon);
if (icon.is_icon()) { if (icon.is_icon()) {
if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) { 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()); painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect());
else } 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);
}
} }
} }
draw_item_text(painter, index, is_selected_row, text_rect, index.data().to_string(), font_for_index(index), Gfx::TextAlignment::Center, Gfx::TextElision::None); draw_item_text(painter, index, is_selected_row, text_rect, index.data().to_string(), font_for_index(index), Gfx::TextAlignment::Center, Gfx::TextElision::None);