Skip to content

Commit

Permalink
Fixes TreeItem minimum width calculations
Browse files Browse the repository at this point in the history
1. Adds missing margins to `TreeItem::get_minimum_size`.
2. Sets minimum size dirty flags when an item is (un)collapsed.
3. Fixes depth calculation in `Tree::get_column_minimum_width` that was using
the next visible item's depth when calculating the current item's minimum width.

Fixes godotengine#84015.
  • Loading branch information
chybby committed Jan 30, 2024
1 parent 51991e2 commit 3c1f1d6
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions scene/gui/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,15 @@ void TreeItem::set_collapsed(bool p_collapsed) {
return;
}
collapsed = p_collapsed;

for (int i = 0; i < cells.size(); i++) {
cells.write[i].cached_minimum_size_dirty = true;
}

for (int i = 0; i < tree->columns.size(); i++) {
tree->columns.write[i].cached_minimum_width_dirty = true;
}

TreeItem *ci = tree->selected_item;
if (ci) {
while (ci && ci != this) {
Expand Down Expand Up @@ -1483,6 +1492,14 @@ Size2 TreeItem::get_minimum_size(int p_column) {
if (cell.cached_minimum_size_dirty) {
Size2 size;

size.width += parent_tree->theme_cache.inner_item_margin_left + parent_tree->theme_cache.inner_item_margin_right;

if (p_column == 0 && !(disable_folding || parent_tree->hide_folding)) {
size.width += parent_tree->theme_cache.item_margin;
} else {
size.width += parent_tree->theme_cache.h_separation;
}

// Text.
if (!cell.text.is_empty()) {
if (cell.dirty) {
Expand Down Expand Up @@ -4683,8 +4700,19 @@ int Tree::get_column_minimum_width(int p_column) const {
int depth = 0;
TreeItem *next;
for (TreeItem *item = get_root(); item; item = next) {
// Get the item minimum size.
Size2 item_size = item->get_minimum_size(p_column);
if (p_column == 0) {
item_size.width += theme_cache.item_margin * depth;
} else {
item_size.width += theme_cache.h_separation;
}

// Check if the item is wider.
min_width = MAX(min_width, item_size.width);

// Compute the depth of the next item.
next = item->get_next_visible();
// Compute the depth in tree.
if (next && p_column == 0) {
if (next->get_parent() == item) {
depth += 1;
Expand All @@ -4696,17 +4724,6 @@ int Tree::get_column_minimum_width(int p_column) const {
}
}
}

// Get the item minimum size.
Size2 item_size = item->get_minimum_size(p_column);
if (p_column == 0) {
item_size.width += theme_cache.item_margin * depth;
} else {
item_size.width += theme_cache.h_separation;
}

// Check if the item is wider.
min_width = MAX(min_width, item_size.width);
}
}

Expand Down

0 comments on commit 3c1f1d6

Please sign in to comment.