Browse Source

TrySplitPanel now migrates PanelTitle to children

tznind 2 years ago
parent
commit
a470451555
2 changed files with 44 additions and 21 deletions
  1. 38 12
      Terminal.Gui/Views/SplitContainer.cs
  2. 6 9
      UICatalog/Scenarios/SplitContainerNesting.cs

+ 38 - 12
Terminal.Gui/Views/SplitContainer.cs

@@ -248,7 +248,8 @@ namespace Terminal.Gui {
 		/// is already a <see cref="SplitContainer"/> then returns false.
 		/// is already a <see cref="SplitContainer"/> then returns false.
 		/// </summary>
 		/// </summary>
 		/// <remarks>After successful splitting, the returned container's <see cref="Panel1"/> 
 		/// <remarks>After successful splitting, the returned container's <see cref="Panel1"/> 
-		/// will contain the original content (if any) while <see cref="Panel2"/> will be empty and available
+		/// will contain the original content and <see cref="Panel1Title"/> (if any) while
+		/// <see cref="Panel2"/> will be empty and available for adding to.
 		/// for adding to.</remarks>
 		/// for adding to.</remarks>
 		/// <param name="result">The new <see cref="SplitContainer"/> now showing in 
 		/// <param name="result">The new <see cref="SplitContainer"/> now showing in 
 		/// <see cref="Panel1"/> or the existing one if it was already been converted before.</param>
 		/// <see cref="Panel1"/> or the existing one if it was already been converted before.</param>
@@ -257,10 +258,22 @@ namespace Terminal.Gui {
 		/// <see cref="SplitContainer"/></returns>
 		/// <see cref="SplitContainer"/></returns>
 		public bool TrySplitPanel1(out SplitContainer result)
 		public bool TrySplitPanel1(out SplitContainer result)
 		{
 		{
-			return TrySplit (
-				() => this.Panel1,
-				(n) => this.Panel1 = n,
+			// when splitting a panel into 2 sub panels we will need to migrate
+			// the title too
+			var title = Panel1Title;
+
+			bool returnValue = TrySplit (
+				this.Panel1,
+				(newSplitContainer) => {
+					this.Panel1 = newSplitContainer;
+					
+					// Move title to new container
+					Panel1Title = string.Empty;
+					newSplitContainer.Panel1Title = title;
+				},
 				out result);
 				out result);
+			
+			return returnValue;
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
@@ -269,7 +282,8 @@ namespace Terminal.Gui {
 		/// is already a <see cref="SplitContainer"/> then returns false.
 		/// is already a <see cref="SplitContainer"/> then returns false.
 		/// </summary>
 		/// </summary>
 		/// <remarks>After successful splitting, the returned container's <see cref="Panel1"/> 
 		/// <remarks>After successful splitting, the returned container's <see cref="Panel1"/> 
-		/// will contain the original content (if any) while <see cref="Panel2"/> will be empty and available
+		/// will contain the original content and <see cref="Panel2Title"/> (if any) while
+		/// <see cref="Panel2"/> will be empty and available for adding to.
 		/// for adding to.</remarks>
 		/// for adding to.</remarks>
 		/// <param name="result">The new <see cref="SplitContainer"/> now showing in 
 		/// <param name="result">The new <see cref="SplitContainer"/> now showing in 
 		/// <see cref="Panel2"/> or the existing one if it was already been converted before.</param>
 		/// <see cref="Panel2"/> or the existing one if it was already been converted before.</param>
@@ -278,19 +292,31 @@ namespace Terminal.Gui {
 		/// <see cref="SplitContainer"/></returns>
 		/// <see cref="SplitContainer"/></returns>
 		public bool TrySplitPanel2 (out SplitContainer result)
 		public bool TrySplitPanel2 (out SplitContainer result)
 		{
 		{
-			return TrySplit (
-				() => this.Panel2,
-				(n) => this.Panel2 = n,
+			// when splitting a panel into 2 sub panels we will need to migrate
+			// the title too
+			var title = Panel2Title;
+
+			bool returnValue = TrySplit (
+				this.Panel2,
+				(newSplitContainer) => {
+					this.Panel2 = newSplitContainer;
+
+					// Move title to new container
+					Panel2Title = string.Empty;
+
+					// Content always goes into Panel1 of the new container
+					// so that is where the title goes too
+					newSplitContainer.Panel1Title = title;
+				},
 				out result);
 				out result);
+
+			return returnValue;
 		}
 		}
 		private bool TrySplit(
 		private bool TrySplit(
-			Func<View> getter,
+			View toMove,
 			Action<SplitContainer> newSplitContainerSetter,
 			Action<SplitContainer> newSplitContainerSetter,
 			out SplitContainer result)
 			out SplitContainer result)
 		{
 		{
-			// Get the current panel contents (Panel1 or Panel2)
-			var toMove = getter();
-
 			if (toMove is SplitContainer existing) {
 			if (toMove is SplitContainer existing) {
 				result = existing;
 				result = existing;
 				return false;
 				return false;

+ 6 - 9
UICatalog/Scenarios/SplitContainerNesting.cs

@@ -2,6 +2,7 @@
 using System.ComponentModel;
 using System.ComponentModel;
 using System.Linq;
 using System.Linq;
 using System.Threading;
 using System.Threading;
+using System.Xml.Linq;
 using Terminal.Gui;
 using Terminal.Gui;
 using Terminal.Gui.Graphs;
 using Terminal.Gui.Graphs;
 
 
@@ -174,8 +175,9 @@ namespace UICatalog.Scenarios {
 
 
 			panelsCreated++;
 			panelsCreated++;
 
 
-			// we can split Panel1
-			SetPanelTitles (newContainer, panelsCreated);
+			// During splitting the old Title will have been migrated to Panel1 so we only need
+			// to set the Title on Panel2 (the one that gets our new TextView)
+			newContainer.Panel2Title = cbTitles.Checked ? $"Panel {panelsCreated}" : string.Empty;
 
 
 			// Flip orientation
 			// Flip orientation
 			newContainer.Orientation = newContainer.Orientation == Orientation.Vertical ?
 			newContainer.Orientation = newContainer.Orientation == Orientation.Vertical ?
@@ -185,12 +187,6 @@ namespace UICatalog.Scenarios {
 			newContainer.Panel2.Add (CreateTextView (panelsCreated));
 			newContainer.Panel2.Add (CreateTextView (panelsCreated));
 		}
 		}
 
 
-		private void SetPanelTitles (SplitContainer container, int containerNumber)
-		{
-			container.Panel1Title = cbTitles.Checked ? $"Panel {containerNumber}" : string.Empty;
-			container.Panel2Title = cbTitles.Checked ? $"Panel {containerNumber + 1}" : string.Empty;
-		}
-
 		private SplitContainer CreateSplitContainer (int titleNumber, Orientation orientation)
 		private SplitContainer CreateSplitContainer (int titleNumber, Orientation orientation)
 		{
 		{
 			var toReturn = new SplitContainer {
 			var toReturn = new SplitContainer {
@@ -200,7 +196,8 @@ namespace UICatalog.Scenarios {
 				Orientation = orientation
 				Orientation = orientation
 			};
 			};
 
 
-			SetPanelTitles (toReturn, titleNumber);
+			toReturn.Panel1Title = cbTitles.Checked ? $"Panel {titleNumber}" : string.Empty;
+			toReturn.Panel2Title = cbTitles.Checked ? $"Panel {titleNumber + 1}" : string.Empty;
 
 
 			return toReturn;
 			return toReturn;
 		}
 		}