1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:07:35 +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

@ -92,6 +92,7 @@ int main(int argc, char** argv)
auto& bottom_splitter = main_widget.add<GUI::VerticalSplitter>();
auto& tree_view = bottom_splitter.add<GUI::TreeView>();
tree_view.set_should_fill_selected_rows(true);
tree_view.set_column_headers_visible(true);
tree_view.set_model(profile->model());

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,6 +293,8 @@ void TreeView::paint_event(PaintEvent& event)
}
Gfx::IntRect row_rect { 0, rect.y(), row_width, rect.height() };
if (!is_selected_row || should_fill_selected_rows())
painter.fill_rect(row_rect, background_color);
int x_offset = 0;
@ -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();

View file

@ -48,6 +48,9 @@ public:
Function<void(const ModelIndex&, const bool)> on_toggle;
void set_should_fill_selected_rows(bool fill) { m_should_fill_selected_rows = fill; }
bool should_fill_selected_rows() const { return m_should_fill_selected_rows; }
protected:
TreeView();
@ -84,6 +87,8 @@ private:
RefPtr<Gfx::Bitmap> m_expand_bitmap;
RefPtr<Gfx::Bitmap> m_collapse_bitmap;
bool m_should_fill_selected_rows { false };
};
}