Skip to content

Commit d73b294

Browse files
committed
Added CollapseAll.
1 parent 6e53f61 commit d73b294

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/Avalonia.Controls.TreeDataGrid/HierarchicalTreeDataGridSource.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public void Dispose()
105105
}
106106

107107
public void Collapse(IndexPath index) => GetOrCreateRows().Collapse(index);
108+
public void CollapseAll() => GetOrCreateRows().CollapseAll();
108109
public void Expand(IndexPath index) => GetOrCreateRows().Expand(index);
109110
public void ExpandAll() => GetOrCreateRows().ExpandRecursive(null);
110111
public void ExpandRecursive(Func<TModel, bool> filter) => GetOrCreateRows().ExpandRecursive(filter);

src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/HierarchicalRows.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,31 @@ public void Collapse(IndexPath index)
131131
}
132132
}
133133

134+
internal void CollapseAll()
135+
{
136+
static void Collapse(IReadOnlyList<HierarchicalRow<TModel>> rows)
137+
{
138+
for (var i = 0; i < rows.Count; ++i)
139+
{
140+
var row = rows[i];
141+
142+
if (row.Children is { } children)
143+
Collapse(children);
144+
145+
row.IsExpanded = false;
146+
}
147+
}
148+
149+
_ignoreCollectionChanges = true;
150+
151+
try { Collapse(_roots); }
152+
finally { _ignoreCollectionChanges = false; }
153+
154+
_flattenedRows.Clear();
155+
InitializeRows();
156+
CollectionChanged?.Invoke(this, CollectionExtensions.ResetEvent);
157+
}
158+
134159
public (int index, double y) GetRowAt(double y)
135160
{
136161
if (MathUtilities.IsZero(y))

tests/Avalonia.Controls.TreeDataGrid.Tests/HierarchicalTreeDataGridSourceTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,28 @@ public void ExpandAll_Expands_All_Rows(bool sorted)
467467

468468
Assert.Equal(65, target.Rows.Count);
469469
}
470+
471+
[AvaloniaTheory(Timeout = 10000)]
472+
[InlineData(false)]
473+
[InlineData(true)]
474+
public void CollapseAll_Collapses_All_Rows(bool sorted)
475+
{
476+
var data = CreateData(5, 3, 3);
477+
var target = CreateTarget(data, sorted);
478+
479+
// We need to expand before we can collapse.
480+
target.ExpandAll();
481+
Assert.Equal(65, target.Rows.Count);
482+
483+
// Now we can test collapsing.
484+
target.CollapseAll();
485+
Assert.Equal(5, target.Rows.Count);
486+
487+
// Ensure that nested rows were collapsed, i.e. only the first level of rows is
488+
// visible after expanding now.
489+
target.Expand(0);
490+
Assert.Equal(8, target.Rows.Count);
491+
}
470492
}
471493

472494
public class ExpansionBinding

0 commit comments

Comments
 (0)