Browse Source

Add InsertTile method

tznind 2 years ago
parent
commit
a50f9cd2de
2 changed files with 47 additions and 0 deletions
  1. 28 0
      Terminal.Gui/Views/SplitView.cs
  2. 19 0
      UnitTests/SplitViewTests.cs

+ 28 - 0
Terminal.Gui/Views/SplitView.cs

@@ -116,6 +116,34 @@ namespace Terminal.Gui {
 			LayoutSubviews ();
 			LayoutSubviews ();
 		}
 		}
 
 
+		/// <summary>
+		/// Adds a new <see cref="Tile"/> to the collection at <paramref name="idx"/>.
+		/// This will also add another splitter line
+		/// </summary>
+		/// <param name="idx"></param>
+		/// <exception cref="NotImplementedException"></exception>
+		internal void InsertTile (int idx)
+		{
+			var oldTiles = Tiles.ToArray ();
+			RebuildForTileCount (oldTiles.Length + 1);
+
+			for(int i=0;i<tiles.Count;i++) {
+				
+				if(i != idx) {
+					var oldTile = oldTiles [i > idx ? i - 1 : i];
+
+					// remove the new empty View
+					Remove (tiles [i].View);
+					
+					// restore old Tile and View
+					tiles [i] = oldTile;
+					Add (tiles [i].View);
+				}
+			}
+			SetNeedsDisplay ();
+			LayoutSubviews ();
+		}
+
 		/// <summary>
 		/// <summary>
 		/// Orientation of the dividing line (Horizontal or Vertical).
 		/// Orientation of the dividing line (Horizontal or Vertical).
 		/// </summary>
 		/// </summary>

+ 19 - 0
UnitTests/SplitViewTests.cs

@@ -379,8 +379,27 @@ namespace UnitTests {
 └──┴──────┘";
 └──┴──────┘";
 
 
 			TestHelpers.AssertDriverContentsAre (looksLike, output);
 			TestHelpers.AssertDriverContentsAre (looksLike, output);
+		}
+
+		[Fact, AutoInitShutdown]
+		public void TestSplitView_InsertPanelAtStart ()
+		{
+			var splitContainer = Get11By3SplitView (out var line, true);
+			SetInputFocusLine (splitContainer);
+
+			splitContainer.InsertTile (0);
 
 
+			splitContainer.Redraw (splitContainer.Bounds);
+
+			// so should ignore the 2 distance and stick to 6
+			string looksLike =
+@"
+┌──┬───┬──┐
+│  │111│22│
+└──┴───┴──┘";
+			TestHelpers.AssertDriverContentsAre (looksLike, output);
 		}
 		}
+
 		[Fact, AutoInitShutdown]
 		[Fact, AutoInitShutdown]
 		public void TestSplitView_Horizontal_Focused ()
 		public void TestSplitView_Horizontal_Focused ()
 		{
 		{