Skip to content

Commit

Permalink
Added CollapseAll.
Browse files Browse the repository at this point in the history
  • Loading branch information
grokys committed Mar 14, 2024
1 parent 6e53f61 commit d73b294
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void Dispose()
}

public void Collapse(IndexPath index) => GetOrCreateRows().Collapse(index);
public void CollapseAll() => GetOrCreateRows().CollapseAll();
public void Expand(IndexPath index) => GetOrCreateRows().Expand(index);
public void ExpandAll() => GetOrCreateRows().ExpandRecursive(null);
public void ExpandRecursive(Func<TModel, bool> filter) => GetOrCreateRows().ExpandRecursive(filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,31 @@ public void Collapse(IndexPath index)
}
}

internal void CollapseAll()
{
static void Collapse(IReadOnlyList<HierarchicalRow<TModel>> rows)
{
for (var i = 0; i < rows.Count; ++i)
{
var row = rows[i];

if (row.Children is { } children)
Collapse(children);

row.IsExpanded = false;
}
}

_ignoreCollectionChanges = true;

try { Collapse(_roots); }
finally { _ignoreCollectionChanges = false; }

_flattenedRows.Clear();
InitializeRows();
CollectionChanged?.Invoke(this, CollectionExtensions.ResetEvent);
}

public (int index, double y) GetRowAt(double y)
{
if (MathUtilities.IsZero(y))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,28 @@ public void ExpandAll_Expands_All_Rows(bool sorted)

Assert.Equal(65, target.Rows.Count);
}

[AvaloniaTheory(Timeout = 10000)]
[InlineData(false)]
[InlineData(true)]
public void CollapseAll_Collapses_All_Rows(bool sorted)
{
var data = CreateData(5, 3, 3);
var target = CreateTarget(data, sorted);

// We need to expand before we can collapse.
target.ExpandAll();
Assert.Equal(65, target.Rows.Count);

// Now we can test collapsing.
target.CollapseAll();
Assert.Equal(5, target.Rows.Count);

// Ensure that nested rows were collapsed, i.e. only the first level of rows is
// visible after expanding now.
target.Expand(0);
Assert.Equal(8, target.Rows.Count);
}
}

public class ExpansionBinding
Expand Down

0 comments on commit d73b294

Please sign in to comment.