From 0ea7a871ce923dfb13d36e7f06e283b0ac24d886 Mon Sep 17 00:00:00 2001 From: Thomas Nind <31306100+tznind@users.noreply.github.com> Date: Sun, 21 May 2023 22:26:50 +0100 Subject: [PATCH] Adds TreeView MaxDepth (#2651) --- Terminal.Gui/Views/TreeView/Branch.cs | 9 ++- Terminal.Gui/Views/TreeView/TreeView.cs | 5 ++ UnitTests/Views/TreeViewTests.cs | 95 ++++++++++++++++++++++++- 3 files changed, 107 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Views/TreeView/Branch.cs b/Terminal.Gui/Views/TreeView/Branch.cs index 795e06268..aeb556288 100644 --- a/Terminal.Gui/Views/TreeView/Branch.cs +++ b/Terminal.Gui/Views/TreeView/Branch.cs @@ -61,8 +61,15 @@ namespace Terminal.Gui { return; } - var children = tree.TreeBuilder.GetChildren (this.Model) ?? Enumerable.Empty (); + IEnumerable children; + if (Depth >= tree.MaxDepth) { + children = Enumerable.Empty (); + } + else { + children = tree.TreeBuilder.GetChildren (this.Model) ?? Enumerable.Empty (); + } + this.ChildBranches = children.ToDictionary (k => k, val => new Branch (tree, this, val)); } diff --git a/Terminal.Gui/Views/TreeView/TreeView.cs b/Terminal.Gui/Views/TreeView/TreeView.cs index d4e7f4bea..14645539d 100644 --- a/Terminal.Gui/Views/TreeView/TreeView.cs +++ b/Terminal.Gui/Views/TreeView/TreeView.cs @@ -85,6 +85,11 @@ namespace Terminal.Gui { /// public bool MultiSelect { get; set; } = true; + /// + /// Maximum number of nodes that can be expanded in any given branch. + /// + public int MaxDepth { get; set; } = 100; + /// /// True makes a letter key press navigate to the next visible branch that begins with /// that letter/digit. diff --git a/UnitTests/Views/TreeViewTests.cs b/UnitTests/Views/TreeViewTests.cs index 585b41b64..0644fac65 100644 --- a/UnitTests/Views/TreeViewTests.cs +++ b/UnitTests/Views/TreeViewTests.cs @@ -131,7 +131,7 @@ namespace Terminal.Gui.ViewsTests { { var tree = CreateTree (out Factory f, out Car car1, out Car car2); tree.BeginInit (); tree.EndInit (); - + // control only allows 1 row to be viewed at once tree.Bounds = new Rect (0, 0, 20, 1); @@ -902,6 +902,99 @@ namespace Terminal.Gui.ViewsTests { new [] { tv.ColorScheme.Normal, pink }); } + [Fact, AutoInitShutdown] + public void TestBottomlessTreeView_MaxDepth_5 () + { + var tv = new TreeView () { Width = 20, Height = 10 }; + + tv.TreeBuilder = new DelegateTreeBuilder ( + (s) => new [] { (int.Parse (s) + 1).ToString () } + ); + + tv.AddObject ("1"); + tv.ColorScheme = new ColorScheme (); + + tv.LayoutSubviews (); + tv.Draw (); + + // Nothing expanded + TestHelpers.AssertDriverContentsAre ( +@"└+1 +", output); + tv.MaxDepth = 5; + tv.ExpandAll (); + + tv.Draw (); + + // 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.Draw (); + + // 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 () { Width = 20, Height = 10 }; + + tv.TreeBuilder = new DelegateTreeBuilder ( + (s) => new [] { (int.Parse (s) + 1).ToString () } + ); + + tv.AddObject ("1"); + tv.ColorScheme = new ColorScheme (); + + tv.LayoutSubviews (); + tv.Draw (); + + // Nothing expanded + TestHelpers.AssertDriverContentsAre ( +@"└+1 +", output); + tv.MaxDepth = 3; + tv.ExpandAll (); + tv.Draw (); + + // Normal drawing of the tree view + TestHelpers.AssertDriverContentsAre ( +@" +└-1 + └-2 + └-3 + └─4 +", output); + } + [Fact, AutoInitShutdown] public void TestTreeView_Filter () {