2
0
Thomas Nind 2 жил өмнө
parent
commit
f5ca5b0781

+ 8 - 1
Terminal.Gui/Core/Trees/Branch.cs

@@ -61,8 +61,15 @@ namespace Terminal.Gui.Trees {
 				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));
 		}
 

+ 5 - 0
Terminal.Gui/Views/TreeView.cs

@@ -85,6 +85,11 @@ namespace Terminal.Gui {
 		/// <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.

+ 93 - 0
UnitTests/Views/TreeViewTests.cs

@@ -908,6 +908,99 @@ namespace Terminal.Gui.ViewTests {
 				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 ()
 		{