Skip to content

Commit

Permalink
Adds TreeView MaxDepth (gui-cs#2652)
Browse files Browse the repository at this point in the history
  • Loading branch information
tznind committed May 21, 2023
1 parent 8287acb commit f5ca5b0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Terminal.Gui/Core/Trees/Branch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ public virtual void FetchChildren ()
return;
}

var children = tree.TreeBuilder.GetChildren (this.Model) ?? Enumerable.Empty<T> ();
IEnumerable<T> children;

if (Depth >= tree.MaxDepth) {
children = Enumerable.Empty<T> ();
}
else {
children = tree.TreeBuilder.GetChildren (this.Model) ?? Enumerable.Empty<T> ();
}

this.ChildBranches = children.ToDictionary (k => k, val => new Branch<T> (tree, this, val));
}

Expand Down
5 changes: 5 additions & 0 deletions Terminal.Gui/Views/TreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public class TreeView<T> : View, ITreeView where T : class {
/// <value></value>
public bool MultiSelect { get; set; } = true;

/// <summary>
/// Maximum number of nodes that can be expanded in any given branch.
/// </summary>
public int MaxDepth { get; set; } = 100;

/// <summary>
/// True makes a letter key press navigate to the next visible branch that begins with
/// that letter/digit.
Expand Down
93 changes: 93 additions & 0 deletions UnitTests/Views/TreeViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,99 @@ public void TestTreeViewColor ()
new [] { tv.ColorScheme.Normal, pink });
}

[Fact, AutoInitShutdown]
public void TestBottomlessTreeView_MaxDepth_5 ()
{
var tv = new TreeView<string> () { Width = 20, Height = 10 };

tv.TreeBuilder = new DelegateTreeBuilder<string> (
(s) => new [] { (int.Parse (s) + 1).ToString () }
);

tv.AddObject ("1");
tv.ColorScheme = new ColorScheme ();

tv.LayoutSubviews ();
tv.Redraw (tv.Bounds);

// Nothing expanded
TestHelpers.AssertDriverContentsAre (
@"└+1
", output);
tv.MaxDepth = 5;
tv.ExpandAll ();

tv.Redraw (tv.Bounds);

// Normal drawing of the tree view
TestHelpers.AssertDriverContentsAre (
@"
└-1
└-2
└-3
└-4
└-5
└─6
", output);
Assert.False (tv.CanExpand ("6"));
Assert.False (tv.IsExpanded ("6"));

tv.Collapse("6");

Assert.False (tv.CanExpand ("6"));
Assert.False (tv.IsExpanded ("6"));

tv.Collapse ("5");

Assert.True (tv.CanExpand ("5"));
Assert.False (tv.IsExpanded ("5"));

tv.Redraw (tv.Bounds);

// Normal drawing of the tree view
TestHelpers.AssertDriverContentsAre (
@"
└-1
└-2
└-3
└-4
└+5
", output);
}

[Fact, AutoInitShutdown]
public void TestBottomlessTreeView_MaxDepth_3 ()
{
var tv = new TreeView<string> () { Width = 20, Height = 10 };

tv.TreeBuilder = new DelegateTreeBuilder<string> (
(s) => new [] { (int.Parse (s) + 1).ToString () }
);

tv.AddObject ("1");
tv.ColorScheme = new ColorScheme ();

tv.LayoutSubviews ();
tv.Redraw (tv.Bounds);

// Nothing expanded
TestHelpers.AssertDriverContentsAre (
@"└+1
", output);
tv.MaxDepth = 3;
tv.ExpandAll ();
tv.Redraw (tv.Bounds);

// Normal drawing of the tree view
TestHelpers.AssertDriverContentsAre (
@"
└-1
└-2
└-3
└─4
", output);
}

[Fact, AutoInitShutdown]
public void TestTreeView_Filter ()
{
Expand Down

0 comments on commit f5ca5b0

Please sign in to comment.