Browse Source

Vamp up split container nesting

tznind 2 years ago
parent
commit
7abf2d7966
2 changed files with 94 additions and 25 deletions
  1. 2 2
      Terminal.Gui/Views/SplitContainer.cs
  2. 92 23
      UICatalog/Scenarios/SplitContainerNesting.cs

+ 2 - 2
Terminal.Gui/Views/SplitContainer.cs

@@ -31,7 +31,7 @@ namespace Terminal.Gui {
 		/// <see cref="SplitContainer"/> if further splitter subdivisions are
 		/// desired (e.g. to create a resizeable grid.
 		/// </summary>
-		public View Panel1 { get; private set; }
+		public View Panel1 { get; set; } // TODO: Should not be public set, should be helpers for this
 
 		
 		public int Panel1MinSize { get; set; }
@@ -44,7 +44,7 @@ namespace Terminal.Gui {
 		/// <see cref="SplitContainer"/> if further splitter subdivisions are
 		/// desired (e.g. to create a resizeable grid.
 		/// </summary>
-		public View Panel2 { get; private set; }
+		public View Panel2 { get; set; } // TODO: Should not be public set, should be helpers for this
 
 		public int Panel2MinSize { get; set; }
 		public ustring Panel2Title { get; set; } = string.Empty;

+ 92 - 23
UICatalog/Scenarios/SplitContainerNesting.cs

@@ -1,6 +1,8 @@
 using System;
 using System.ComponentModel;
+using System.Linq;
 using Terminal.Gui;
+using Terminal.Gui.Graphs;
 
 namespace UICatalog.Scenarios {
 	[ScenarioMetadata (Name: "Split Container Nesting", Description: "Nest SplitContainers")]
@@ -36,17 +38,17 @@ namespace UICatalog.Scenarios {
 
 
 			cbHorizontal = new CheckBox ("Horizontal") {
-				X = Pos.Right (textField) +1
+				X = Pos.Right (textField) + 1
 			};
 			cbHorizontal.Toggled += (s) => SetupSplitContainer ();
 
 			cbBorder = new CheckBox ("Border") {
-				X = Pos.Right (cbHorizontal)+1
+				X = Pos.Right (cbHorizontal) + 1
 			};
 			cbBorder.Toggled += (s) => SetupSplitContainer ();
-			
+
 			cbTitles = new CheckBox ("Titles") {
-				X = Pos.Right (cbBorder)+1
+				X = Pos.Right (cbBorder) + 1
 			};
 			cbTitles.Toggled += (s) => SetupSplitContainer ();
 
@@ -90,27 +92,14 @@ namespace UICatalog.Scenarios {
 				return;
 			}
 
-			var root = new SplitContainer {
-				Width = Dim.Fill (),
-				Height = Dim.Fill (),
-				Orientation = startHorizontal ?
+			var root = CreateSplitContainer (startHorizontal ?
 					Terminal.Gui.Graphs.Orientation.Horizontal :
-					Terminal.Gui.Graphs.Orientation.Vertical,
-			};
-			root.Panel1.Add (new TextView {
-				Width = Dim.Fill (),
-				Height = Dim.Fill (),
-				Text = new string ('1', 10000),
-				AllowsTab = false,
-			});
+					Terminal.Gui.Graphs.Orientation.Vertical, false);
+
+			root.Panel1.Add (CreateTextView (1));
 			root.Panel1Title = titles ? "Panel 1" : string.Empty;
 
-			root.Panel2.Add (new TextView {
-				Width = Dim.Fill (),
-				Height = Dim.Fill (),
-				Text = new string ('2', 10000),
-				AllowsTab = false,
-			});
+			root.Panel2.Add (CreateTextView (2));
 			root.Panel2Title = titles ? "Panel 2" : string.Empty;
 
 			root.IntegratedBorder = border ? BorderStyle.Rounded : BorderStyle.None;
@@ -124,7 +113,7 @@ namespace UICatalog.Scenarios {
 
 			if (numberOfPanels > 2) {
 
-				// TODO: Add more
+				AddMorePanels (root, 2, numberOfPanels);
 			}
 
 			if (loaded) {
@@ -132,6 +121,86 @@ namespace UICatalog.Scenarios {
 			}
 		}
 
+		private View CreateTextView (int number)
+		{
+			return new TextView {
+				Width = Dim.Fill (),
+				Height = Dim.Fill (),
+				Text = number.ToString ().Repeat (1000),
+				AllowsTab = false,
+				WordWrap = true,
+			};
+		}
+
+		private void AddMorePanels (SplitContainer to, int done, int numberOfPanels)
+		{
+			if (done == numberOfPanels) {
+				return;
+			}
+
+			View toSplit;
+
+			if (!(to.Panel1 is SplitContainer)) {
+
+				// we can split Panel1
+				var tv = (TextView)to.Panel1.Subviews.Single ();
+
+				var newContainer = CreateSplitContainer (to.Orientation, true);
+
+				to.Remove (to.Panel1);
+				to.Add (newContainer);
+				to.Panel1 = newContainer;
+
+				newContainer.Panel1.Add (tv);
+				newContainer.Panel2.Add (CreateTextView (++done));
+
+				AddMorePanels (to, done, numberOfPanels);
+			} else
+			if (!(to.Panel2 is SplitContainer)) {
+				// we can split Panel2
+				var tv = (TextView)to.Panel2.Subviews.Single ();
+
+				var newContainer = CreateSplitContainer (to.Orientation, true);
+
+				to.Remove (to.Panel2);
+				to.Add (newContainer);
+				to.Panel2 = newContainer;
+
+				newContainer.Panel1.Add (tv);
+				newContainer.Panel2.Add (CreateTextView (++done));
+
+				AddMorePanels (to, done, numberOfPanels);
+			} else {
+				// Both Panel1 and Panel2 are already SplitContainer	
+
+				// So split one of the children
+				if(done % 2 == 0) {
+					AddMorePanels ((SplitContainer)to.Panel1, done, numberOfPanels);
+				} else {
+
+					AddMorePanels ((SplitContainer)to.Panel2, done, numberOfPanels);
+				}
+			}
+		}
+
+		private SplitContainer CreateSplitContainer (Orientation orientation, bool flip)
+		{
+			var toReturn = new SplitContainer {
+				Width = Dim.Fill (),
+				Height = Dim.Fill (),
+				// flip the orientation
+				Orientation = orientation
+			};
+
+			if (flip) {
+				toReturn.Orientation = toReturn.Orientation == Orientation.Vertical ?
+					Orientation.Horizontal :
+					Orientation.Vertical;
+			}
+
+			return toReturn;
+		}
+
 		private int GetNumberOfPanels ()
 		{
 			if (int.TryParse (textField.Text.ToString (), out var panels) && panels >= 0) {