Skip to content

Commit 967eec1

Browse files
committed
GTreeView: Add expand/collapse buttons to items with children.
1 parent eb182bc commit 967eec1

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed
14 Bytes
Loading

Base/res/icons/treeview-expand.png

16 Bytes
Loading

LibGUI/GTreeView.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void GTreeView::traverse_in_paint_order(Callback callback) const
174174
auto node_text = model.data(index, GModel::Role::Display).to_string();
175175
Rect rect = {
176176
x_offset, y_offset,
177-
icon_size() + icon_spacing() + font().width(node_text), item_height()
177+
toggle_size() + icon_spacing() + icon_size() + icon_spacing() + font().width(node_text), item_height()
178178
};
179179
if (rect.intersects(visible_content_rect)) {
180180
if (callback(index, rect, indent_level) == IterationDecision::Abort)
@@ -219,7 +219,7 @@ void GTreeView::paint_event(GPaintEvent& event)
219219
auto icon = model.data(index, GModel::Role::Icon);
220220
if (icon.is_icon()) {
221221
if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size()))
222-
painter.blit(rect.location(), *bitmap, bitmap->rect());
222+
painter.blit(icon_rect.location(), *bitmap, bitmap->rect());
223223
}
224224
Rect text_rect = {
225225
icon_rect.right() + 1 + icon_spacing(), rect.y(),
@@ -245,6 +245,18 @@ void GTreeView::paint_event(GPaintEvent& event)
245245
}
246246
index_at_indent = parent_of_index_at_indent;
247247
}
248+
249+
if (model.row_count(index) > 0) {
250+
int toggle_x = indent_width_in_pixels() * indent_level - icon_size() / 2 - 3;
251+
Rect toggle_rect = { toggle_x, rect.y(), toggle_size(), toggle_size() };
252+
toggle_rect.center_vertically_within(rect);
253+
auto& metadata = ensure_metadata_for_index(index);
254+
if (metadata.open)
255+
painter.blit(toggle_rect.location(), *m_collapse_bitmap, m_collapse_bitmap->rect());
256+
else
257+
painter.blit(toggle_rect.location(), *m_expand_bitmap, m_expand_bitmap->rect());
258+
}
259+
248260
return IterationDecision::Continue;
249261
});
250262
}

LibGUI/GTreeView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class GTreeView : public GAbstractView {
2121
int indent_width_in_pixels() const { return 16; }
2222
int icon_size() const { return 16; }
2323
int icon_spacing() const { return 4; }
24+
int toggle_size() const { return 9; }
2425

2526
template<typename Callback>
2627
void traverse_in_paint_order(Callback) const;

0 commit comments

Comments
 (0)