1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:48:12 +00:00

LibGUI: Shrink the default selection rect of TreeView items

Instead of filling the whole row with selection color, only fill behind
the text. This gives a snugger, more focused appearance.

For embedders that want the entire row to get filled with the selection
color when selected, they can opt in to the old behavior by calling
TreeView::set_should_fill_selected_rows(). This is used by Profiler.
This commit is contained in:
Andreas Kling 2020-10-27 20:33:30 +01:00
parent df98c9ebbe
commit 0391806eec
3 changed files with 23 additions and 6 deletions

View file

@ -264,7 +264,7 @@ void TreeView::paint_event(PaintEvent& event)
bool is_selected_row = selection().contains(index);
Color text_color = palette().color(foreground_role());
if (is_selected_row)
if (is_selected_row && should_fill_selected_rows())
text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
Color background_color;
@ -293,7 +293,9 @@ void TreeView::paint_event(PaintEvent& event)
}
Gfx::IntRect row_rect { 0, rect.y(), row_width, rect.height() };
painter.fill_rect(row_rect, background_color);
if (!is_selected_row || should_fill_selected_rows())
painter.fill_rect(row_rect, background_color);
int x_offset = 0;
for (int column_index = 0; column_index < model.column_count(); ++column_index) {
@ -325,6 +327,13 @@ void TreeView::paint_event(PaintEvent& event)
} else {
// It's the tree column!
Gfx::IntRect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() };
Gfx::IntRect text_rect = {
icon_rect.right() + 1 + icon_spacing(), rect.y(),
rect.width() - icon_size() - icon_spacing(), rect.height()
};
painter.fill_rect(text_rect, background_color);
auto icon = index.data(ModelRole::Icon);
if (icon.is_icon()) {
if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) {
@ -334,11 +343,13 @@ void TreeView::paint_event(PaintEvent& event)
painter.blit(icon_rect.location(), *bitmap, bitmap->rect());
}
}
Gfx::IntRect text_rect = {
icon_rect.right() + 1 + icon_spacing(), rect.y(),
rect.width() - icon_size() - icon_spacing(), rect.height()
};
draw_item_text(painter, index, is_selected_row, text_rect, index.data().to_string(), font_for_index(index), Gfx::TextAlignment::Center, Gfx::TextElision::None);
if (is_focused() && index == cursor_index()) {
painter.draw_rect(text_rect, palette().color(background_role()));
painter.draw_focus_rect(text_rect, palette().focus_outline());
}
auto index_at_indent = index;
for (int i = indent_level; i > 0; --i) {
auto parent_of_index_at_indent = index_at_indent.parent();