Browse Source

Fix subcontainers in SplitContainerNesting example not having Titles

tznind 2 years ago
parent
commit
86564d5340
2 changed files with 49 additions and 19 deletions
  1. 37 10
      Terminal.Gui/Views/SplitContainer.cs
  2. 12 9
      UICatalog/Scenarios/SplitContainerNesting.cs

+ 37 - 10
Terminal.Gui/Views/SplitContainer.cs

@@ -1,6 +1,7 @@
 using NStack;
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using Terminal.Gui.Graphs;
 
 namespace Terminal.Gui {
@@ -104,17 +105,17 @@ namespace Terminal.Gui {
 					contentArea = new Rect(
 						contentArea.X + 1,
 						contentArea.Y + 1,
-						contentArea.Width - 2,
-						contentArea.Height - 2);
+						Math.Max (0, contentArea.Width - 2),
+						Math.Max (0, contentArea.Height - 2));
 				}
-				else if(HasAnyTitles())
+				else if(HasAnyTitles() && IsRootSplitContainer())
 				{
 					// TODO: Bound with Max/Min
 					contentArea = new Rect(
 						contentArea.X,
 						contentArea.Y + 1,
 						contentArea.Width,
-						contentArea.Height - 1);
+						Math.Max(0,contentArea.Height - 1));
 				}
 
 				Setup (contentArea);
@@ -155,6 +156,8 @@ namespace Terminal.Gui {
 		/// <inheritdoc/>
 		public override void Redraw (Rect bounds)
 		{
+			var childTitles = new List<ChildSplitterLine> ();
+
 			Driver.SetAttribute (ColorScheme.Normal);
 			Clear ();
 			base.Redraw (bounds);
@@ -174,7 +177,7 @@ namespace Terminal.Gui {
 					lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Height + 1, Orientation.Vertical, IntegratedBorder);
 				}
 
-				foreach (var line in allLines)
+				foreach (var line in allLines.Where(l=>l.Visible))
 				{
 					bool isRoot = line == splitterLine;
 
@@ -192,7 +195,9 @@ namespace Terminal.Gui {
 						}
 						length += 2;
 
-						// TODO: Render this title too
+						childTitles.Add (
+							new ChildSplitterLine(line));
+						
 					}
 
 					lc.AddLine(origin,length,line.Orientation,IntegratedBorder);
@@ -207,8 +212,12 @@ namespace Terminal.Gui {
 				line.DrawSplitterSymbol ();
 			}
 
+			foreach(var child in childTitles) {
+				child.DrawTitles ();
+			}
+
 			// Draw Titles over Border
-			var screen = ViewToScreen (bounds);
+			var screen = ViewToScreen (new Rect(0,0,bounds.Width,1));
 			if (Panel1.Visible && Panel1Title.Length > 0) {
 				Driver.SetAttribute (Panel1.HasFocus ? ColorScheme.HotNormal : ColorScheme.Normal);
 				Driver.DrawWindowTitle (new Rect (screen.X, screen.Y, Panel1.Frame.Width, 0), Panel1Title, 0, 0, 0, 0);
@@ -226,12 +235,12 @@ namespace Terminal.Gui {
 				if (Panel2.Visible && Panel2Title?.Length > 0) {
 
 					Driver.SetAttribute (Panel2.HasFocus ? ColorScheme.HotNormal : ColorScheme.Normal);
-					Driver.DrawWindowTitle (new Rect (screen.X + 1, screen.Y, Panel2.Bounds.Width, 1), Panel2Title, 0, 0, 0, 0);
+					Driver.DrawWindowTitle (new Rect (screen.X, screen.Y, Panel2.Bounds.Width, 1), Panel2Title, 0, 0, 0, 0);
 				}
 			} else {
 				if (Panel2.Visible && Panel2Title?.Length > 0) {
 					Driver.SetAttribute (Panel2.HasFocus ? ColorScheme.HotNormal : ColorScheme.Normal);
-					Driver.DrawWindowTitle (new Rect (screen.X + 1, screen.Y, Panel2.Bounds.Width, 1), Panel2Title, 0, 0, 0, 0);
+					Driver.DrawWindowTitle (new Rect (screen.X, screen.Y, Panel2.Bounds.Width, 1), Panel2Title, 0, 0, 0, 0);
 				}
 			}
 		}
@@ -384,7 +393,6 @@ namespace Terminal.Gui {
 			// this splitter position is fine, there is enough space for everyone
 			return pos;
 		}
-		
 		private class SplitContainerLineView : LineView {
 			public SplitContainer Parent { get; private set; }
 
@@ -615,6 +623,25 @@ namespace Terminal.Gui {
 			return Panel1Title.Length > 0 || Panel2Title.Length > 0;
 
 		}
+
+		private class ChildSplitterLine {
+
+			readonly SplitContainerLineView currentLine;
+			internal ChildSplitterLine (SplitContainerLineView currentLine)
+			{
+				this.currentLine = currentLine;
+			}
+
+			internal void DrawTitles ()
+			{
+				if(currentLine.Orientation == Orientation.Horizontal) 
+				{
+					var screenRect = currentLine.ViewToScreen (
+						new Rect(0,0,currentLine.Frame.Width,currentLine.Frame.Height));
+					Driver.DrawWindowTitle (screenRect, currentLine.Parent.Panel2Title, 0, 0, 0, 0);
+				}
+			}
+		}
 	}
 
 	/// <summary>

+ 12 - 9
UICatalog/Scenarios/SplitContainerNesting.cs

@@ -95,15 +95,13 @@ namespace UICatalog.Scenarios {
 				return;
 			}
 
-			var root = CreateSplitContainer (startHorizontal ?
+			var root = CreateSplitContainer (1,startHorizontal ?
 					Terminal.Gui.Graphs.Orientation.Horizontal :
 					Terminal.Gui.Graphs.Orientation.Vertical, false);
 
 			root.Panel1.Add (CreateTextView (1));
-			root.Panel1Title = titles ? "Panel 1" : string.Empty;
-
 			root.Panel2.Add (CreateTextView (2));
-			root.Panel2Title = titles ? "Panel 2" : string.Empty;
+			
 
 			root.IntegratedBorder = border ? BorderStyle.Rounded : BorderStyle.None;
 
@@ -162,31 +160,34 @@ namespace UICatalog.Scenarios {
 			// we can split Panel1
 			var tv = (TextView)to.Panel1.Subviews.Single ();
 
-			var newContainer = CreateSplitContainer (to.Orientation, true);
+			panelsCreated++;
+
+			var newContainer = CreateSplitContainer (panelsCreated, to.Orientation, true);
 
 			to.Remove (to.Panel1);
 			to.Add (newContainer);
 			to.Panel1 = newContainer;
 
 			newContainer.Panel1.Add (tv);
-			newContainer.Panel2.Add (CreateTextView (++panelsCreated));
+			newContainer.Panel2.Add (CreateTextView (panelsCreated));
 		}
 		private void SplitRight(SplitContainer to)
 		{
 			// we can split Panel2
 			var tv = (TextView)to.Panel2.Subviews.Single ();
+			panelsCreated++;
 
-			var newContainer = CreateSplitContainer (to.Orientation, true);
+			var newContainer = CreateSplitContainer (panelsCreated, to.Orientation, true);
 
 			to.Remove (to.Panel2);
 			to.Add (newContainer);
 			to.Panel2 = newContainer;
 
 			newContainer.Panel2.Add (tv);
-			newContainer.Panel1.Add (CreateTextView (++panelsCreated));
+			newContainer.Panel1.Add (CreateTextView (panelsCreated));
 		}
 
-		private SplitContainer CreateSplitContainer (Orientation orientation, bool flip)
+		private SplitContainer CreateSplitContainer (int titleNumber, Orientation orientation, bool flip)
 		{
 			var toReturn = new SplitContainer {
 				Width = Dim.Fill (),
@@ -200,6 +201,8 @@ namespace UICatalog.Scenarios {
 					Orientation.Horizontal :
 					Orientation.Vertical;
 			}
+			toReturn.Panel1Title = cbTitles.Checked ? $"Panel {titleNumber}" : string.Empty;
+			toReturn.Panel2Title = cbTitles.Checked ? $"Panel {titleNumber+1}" : string.Empty;
 
 			return toReturn;
 		}