Browse Source

Enforce minimum panel sizes

tznind 2 years ago
parent
commit
8626c4e2d7
2 changed files with 21 additions and 15 deletions
  1. 19 13
      Terminal.Gui/Views/SplitContainer.cs
  2. 2 2
      UnitTests/SplitContainerTests.cs

+ 19 - 13
Terminal.Gui/Views/SplitContainer.cs

@@ -33,8 +33,8 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// </summary>
 		public View Panel1 { get; set; } // TODO: Should not be public set, should be helpers for this
 		public View Panel1 { get; set; } // TODO: Should not be public set, should be helpers for this
 
 
-		
-		public int Panel1MinSize { get; set; }
+
+		public int Panel1MinSize { get; set; } = 1;
 		public ustring Panel1Title { get; set; } = string.Empty;
 		public ustring Panel1Title { get; set; } = string.Empty;
 
 
 		/// <summary>
 		/// <summary>
@@ -46,7 +46,7 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// </summary>
 		public View Panel2 { get; set; } // TODO: Should not be public set, should be helpers for this
 		public View Panel2 { get; set; } // TODO: Should not be public set, should be helpers for this
 
 
-		public int Panel2MinSize { get; set; }
+		public int Panel2MinSize { get; set; } = 1;
 		public ustring Panel2Title { get; set; } = string.Empty;
 		public ustring Panel2Title { get; set; } = string.Empty;
 
 
 		private Pos splitterDistance = Pos.Percent (50);
 		private Pos splitterDistance = Pos.Percent (50);
@@ -348,31 +348,37 @@ namespace Terminal.Gui {
 			if (!IsInitialized) {
 			if (!IsInitialized) {
 				return pos;
 				return pos;
 			}
 			}
+			
+			var panel1MinSize = Panel1MinSize;
+			var panel2MinSize = Panel2MinSize;
+
+
+			// if there is a border then there is less space
+			// for the panels so we need to make size restrictions
+			// tighter.
+			if(HasBorder()) {
+				panel1MinSize++;
+				panel2MinSize++;
+			}
 
 
 			var availableSpace = Orientation == Orientation.Horizontal ? this.Bounds.Height : this.Bounds.Width;
 			var availableSpace = Orientation == Orientation.Horizontal ? this.Bounds.Height : this.Bounds.Width;
 
 
 			var idealPosition = pos.Anchor (availableSpace);
 			var idealPosition = pos.Anchor (availableSpace);
 
 
 			// bad position because not enough space for Panel1
 			// bad position because not enough space for Panel1
-			if (idealPosition < Panel1MinSize) {
+			if (idealPosition < panel1MinSize) {
 
 
 				// TODO: we should preserve Absolute/Percent status here not just force it to absolute
 				// TODO: we should preserve Absolute/Percent status here not just force it to absolute
-				return (Pos)Math.Min (Panel1MinSize, availableSpace);
-			}
-
-			// if there is a border then 2 screen units are taken occupied
-			// by the border around the edge (one on left, one on right).
-			if (HasBorder ()) {
-				availableSpace -= 2;
+				return (Pos)Math.Min (panel1MinSize, availableSpace);
 			}
 			}
 
 
 			// bad position because not enough space for Panel2
 			// bad position because not enough space for Panel2
-			if (availableSpace - idealPosition <= Panel2MinSize) {
+			if (availableSpace - idealPosition <= panel2MinSize) {
 
 
 				// TODO: we should preserve Absolute/Percent status here not just force it to absolute
 				// TODO: we should preserve Absolute/Percent status here not just force it to absolute
 
 
 				// +1 is to allow space for the splitter
 				// +1 is to allow space for the splitter
-				return (Pos)Math.Max (availableSpace - (Panel2MinSize + 1), 0);
+				return (Pos)Math.Max (availableSpace - (panel2MinSize + 1), 0);
 			}
 			}
 
 
 			// this splitter position is fine, there is enough space for everyone
 			// this splitter position is fine, there is enough space for everyone

+ 2 - 2
UnitTests/SplitContainerTests.cs

@@ -260,7 +260,7 @@ namespace UnitTests {
 			splitContainer.SplitterDistance = 2;
 			splitContainer.SplitterDistance = 2;
 
 
 			// Should bound the value to the minimum distance
 			// Should bound the value to the minimum distance
-			Assert.Equal (5, splitContainer.SplitterDistance);
+			Assert.Equal (6, splitContainer.SplitterDistance);
 
 
 			splitContainer.Redraw (splitContainer.Bounds);
 			splitContainer.Redraw (splitContainer.Bounds);
 
 
@@ -348,7 +348,7 @@ namespace UnitTests {
 			splitContainer.SplitterDistance = 8;
 			splitContainer.SplitterDistance = 8;
 
 
 			// Should bound the value to the minimum distance
 			// Should bound the value to the minimum distance
-			Assert.Equal (3, splitContainer.SplitterDistance);
+			Assert.Equal (4, splitContainer.SplitterDistance);
 
 
 			splitContainer.Redraw (splitContainer.Bounds);
 			splitContainer.Redraw (splitContainer.Bounds);