2
0
Эх сурвалжийг харах

Merge pull request #2495 from tznind/table-cell-draw-fix

Fixes #2494 rendering empty space when using cell specific styles
Tig 2 жил өмнө
parent
commit
6463aab244

+ 19 - 8
Terminal.Gui/Views/TableView.cs

@@ -640,7 +640,7 @@ namespace Terminal.Gui {
 		private string TruncateOrPad (object originalCellValue, string representation, int availableHorizontalSpace, ColumnStyle colStyle)
 		private string TruncateOrPad (object originalCellValue, string representation, int availableHorizontalSpace, ColumnStyle colStyle)
 		{
 		{
 			if (string.IsNullOrEmpty (representation))
 			if (string.IsNullOrEmpty (representation))
-				return representation;
+				return new string(' ',availableHorizontalSpace);
 
 
 			// if value is not wide enough
 			// if value is not wide enough
 			if (representation.Sum (c => Rune.ColumnWidth (c)) < availableHorizontalSpace) {
 			if (representation.Sum (c => Rune.ColumnWidth (c)) < availableHorizontalSpace) {
@@ -1493,9 +1493,11 @@ namespace Terminal.Gui {
 		/// <returns></returns>
 		/// <returns></returns>
 		private IEnumerable<ColumnToRender> CalculateViewport (Rect bounds, int padding = 1)
 		private IEnumerable<ColumnToRender> CalculateViewport (Rect bounds, int padding = 1)
 		{
 		{
-			if (TableIsNullOrInvisible ())
-				yield break;
+			if (TableIsNullOrInvisible ()) {
+				return Enumerable.Empty<ColumnToRender> ();
+			}	
 
 
+			var toReturn = new List<ColumnToRender> ();
 			int usedSpace = 0;
 			int usedSpace = 0;
 
 
 			//if horizontal space is required at the start of the line (before the first header)
 			//if horizontal space is required at the start of the line (before the first header)
@@ -1557,13 +1559,22 @@ namespace Terminal.Gui {
 
 
 				usedSpace += colWidth;
 				usedSpace += colWidth;
 
 
+				// required for if we end up here because first == true i.e. we have a single massive width (overspilling bounds) column to present
+				colWidth = Math.Min (availableHorizontalSpace, colWidth);
+				var isVeryLast = lastColumn == col;
+
 				// there is space
 				// there is space
-				yield return new ColumnToRender (col, startingIdxForCurrentHeader,
-					// required for if we end up here because first == true i.e. we have a single massive width (overspilling bounds) column to present
-					Math.Min (availableHorizontalSpace, colWidth),
-					lastColumn == col);
+				toReturn.Add(new ColumnToRender (col, startingIdxForCurrentHeader, colWidth, isVeryLast));
 				first = false;
 				first = false;
 			}
 			}
+
+			if(Style.ExpandLastColumn)
+			{
+				var last = toReturn.Last ();
+				last.Width = Math.Max (last.Width, availableHorizontalSpace - last.X);
+			}
+
+			return toReturn;
 		}
 		}
 
 
 		private bool ShouldRenderHeaders ()
 		private bool ShouldRenderHeaders ()
@@ -1876,7 +1887,7 @@ namespace Terminal.Gui {
 			/// The width that the column should occupy as calculated by <see cref="CalculateViewport(Rect, int)"/>.  Note that this includes
 			/// The width that the column should occupy as calculated by <see cref="CalculateViewport(Rect, int)"/>.  Note that this includes
 			/// space for padding i.e. the separator between columns.
 			/// space for padding i.e. the separator between columns.
 			/// </summary>
 			/// </summary>
-			public int Width { get; }
+			public int Width { get; internal set; }
 
 
 			/// <summary>
 			/// <summary>
 			/// True if this column is the very last column in the <see cref="Table"/> (not just the last visible column)
 			/// True if this column is the very last column in the <see cref="Table"/> (not just the last visible column)

+ 96 - 32
UnitTests/Views/TableViewTests.cs

@@ -267,7 +267,7 @@ namespace Terminal.Gui.ViewTests {
 			// ensure that TableView has the input focus
 			// ensure that TableView has the input focus
 			Application.Top.Add (tableView);
 			Application.Top.Add (tableView);
 			Application.Begin (Application.Top);
 			Application.Begin (Application.Top);
-			
+
 			Application.Top.FocusFirst ();
 			Application.Top.FocusFirst ();
 			Assert.True (tableView.HasFocus);
 			Assert.True (tableView.HasFocus);
 
 
@@ -453,7 +453,7 @@ namespace Terminal.Gui.ViewTests {
 			Assert.Equal (new Point (8, 3), selected [5]);
 			Assert.Equal (new Point (8, 3), selected [5]);
 		}
 		}
 
 
-		[Fact]
+		[Fact, AutoInitShutdown]
 		public void TableView_ExpandLastColumn_True ()
 		public void TableView_ExpandLastColumn_True ()
 		{
 		{
 			var tv = SetUpMiniTable ();
 			var tv = SetUpMiniTable ();
@@ -476,7 +476,7 @@ namespace Terminal.Gui.ViewTests {
 		}
 		}
 
 
 
 
-		[Fact]
+		[Fact, AutoInitShutdown]
 		public void TableView_ExpandLastColumn_False ()
 		public void TableView_ExpandLastColumn_False ()
 		{
 		{
 			var tv = SetUpMiniTable ();
 			var tv = SetUpMiniTable ();
@@ -498,7 +498,7 @@ namespace Terminal.Gui.ViewTests {
 			Application.Shutdown ();
 			Application.Shutdown ();
 		}
 		}
 
 
-		[Fact]
+		[Fact, AutoInitShutdown]
 		public void TableView_ExpandLastColumn_False_ExactBounds ()
 		public void TableView_ExpandLastColumn_False_ExactBounds ()
 		{
 		{
 			var tv = SetUpMiniTable ();
 			var tv = SetUpMiniTable ();
@@ -560,7 +560,7 @@ namespace Terminal.Gui.ViewTests {
 			Assert.Equal ("R0C0", activatedValue);
 			Assert.Equal ("R0C0", activatedValue);
 		}
 		}
 
 
-		[Fact]
+		[Fact, AutoInitShutdown]
 		public void TableViewMultiSelect_CannotFallOffLeft ()
 		public void TableViewMultiSelect_CannotFallOffLeft ()
 		{
 		{
 			var tv = SetUpMiniTable ();
 			var tv = SetUpMiniTable ();
@@ -583,7 +583,7 @@ namespace Terminal.Gui.ViewTests {
 
 
 			Application.Shutdown ();
 			Application.Shutdown ();
 		}
 		}
-		[Fact]
+		[Fact, AutoInitShutdown]
 		public void TableViewMultiSelect_CannotFallOffRight ()
 		public void TableViewMultiSelect_CannotFallOffRight ()
 		{
 		{
 			var tv = SetUpMiniTable ();
 			var tv = SetUpMiniTable ();
@@ -606,7 +606,7 @@ namespace Terminal.Gui.ViewTests {
 
 
 			Application.Shutdown ();
 			Application.Shutdown ();
 		}
 		}
-		[Fact]
+		[Fact, AutoInitShutdown]
 		public void TableViewMultiSelect_CannotFallOffBottom ()
 		public void TableViewMultiSelect_CannotFallOffBottom ()
 		{
 		{
 			var tv = SetUpMiniTable ();
 			var tv = SetUpMiniTable ();
@@ -631,7 +631,7 @@ namespace Terminal.Gui.ViewTests {
 			Application.Shutdown ();
 			Application.Shutdown ();
 		}
 		}
 
 
-		[Fact]
+		[Fact, AutoInitShutdown]
 		public void TableViewMultiSelect_CannotFallOffTop ()
 		public void TableViewMultiSelect_CannotFallOffTop ()
 		{
 		{
 			var tv = SetUpMiniTable ();
 			var tv = SetUpMiniTable ();
@@ -728,7 +728,7 @@ namespace Terminal.Gui.ViewTests {
 			Assert.Contains (new Point (0, 2), selected);
 			Assert.Contains (new Point (0, 2), selected);
 		}
 		}
 
 
-		[Theory]
+		[Theory, AutoInitShutdown]
 		[InlineData (false)]
 		[InlineData (false)]
 		[InlineData (true)]
 		[InlineData (true)]
 		public void TableView_ColorTests_FocusedOrNot (bool focused)
 		public void TableView_ColorTests_FocusedOrNot (bool focused)
@@ -772,7 +772,7 @@ namespace Terminal.Gui.ViewTests {
 			Application.Shutdown ();
 			Application.Shutdown ();
 		}
 		}
 
 
-		[Theory]
+		[Theory, AutoInitShutdown]
 		[InlineData (false)]
 		[InlineData (false)]
 		[InlineData (true)]
 		[InlineData (true)]
 		public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
 		public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
@@ -821,7 +821,7 @@ namespace Terminal.Gui.ViewTests {
 		}
 		}
 
 
 
 
-		[Theory]
+		[Theory, AutoInitShutdown]
 		[InlineData (false)]
 		[InlineData (false)]
 		[InlineData (true)]
 		[InlineData (true)]
 		public void TableView_ColorsTest_RowColorGetter (bool focused)
 		public void TableView_ColorsTest_RowColorGetter (bool focused)
@@ -912,7 +912,7 @@ namespace Terminal.Gui.ViewTests {
 			Application.Shutdown ();
 			Application.Shutdown ();
 		}
 		}
 
 
-		[Theory]
+		[Theory, AutoInitShutdown]
 		[InlineData (false)]
 		[InlineData (false)]
 		[InlineData (true)]
 		[InlineData (true)]
 		public void TableView_ColorsTest_ColorGetter (bool focused)
 		public void TableView_ColorsTest_ColorGetter (bool focused)
@@ -1023,7 +1023,6 @@ namespace Terminal.Gui.ViewTests {
 			tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
 			tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
 			tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
 			tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
 
 
-			GraphViewTests.InitFakeDriver ();
 			tv.ColorScheme = Colors.Base;
 			tv.ColorScheme = Colors.Base;
 			return tv;
 			return tv;
 		}
 		}
@@ -1055,10 +1054,9 @@ namespace Terminal.Gui.ViewTests {
 			Assert.Equal (1, tableView.RowOffset);
 			Assert.Equal (1, tableView.RowOffset);
 		}
 		}
 
 
-		[Fact]
+		[Fact, AutoInitShutdown]
 		public void ScrollRight_SmoothScrolling ()
 		public void ScrollRight_SmoothScrolling ()
 		{
 		{
-			GraphViewTests.InitFakeDriver ();
 
 
 			var tableView = new TableView ();
 			var tableView = new TableView ();
 			tableView.BeginInit (); tableView.EndInit ();
 			tableView.BeginInit (); tableView.EndInit ();
@@ -1123,11 +1121,9 @@ namespace Terminal.Gui.ViewTests {
 			Application.Shutdown ();
 			Application.Shutdown ();
 		}
 		}
 
 
-		[Fact]
+		[Fact, AutoInitShutdown]
 		public void ScrollRight_WithoutSmoothScrolling ()
 		public void ScrollRight_WithoutSmoothScrolling ()
 		{
 		{
-			GraphViewTests.InitFakeDriver ();
-
 			var tableView = new TableView ();
 			var tableView = new TableView ();
 			tableView.BeginInit (); tableView.EndInit ();
 			tableView.BeginInit (); tableView.EndInit ();
 			tableView.ColorScheme = Colors.TopLevel;
 			tableView.ColorScheme = Colors.TopLevel;
@@ -1193,7 +1189,7 @@ namespace Terminal.Gui.ViewTests {
 		{
 		{
 			var tableView = new TableView ();
 			var tableView = new TableView ();
 			tableView.BeginInit (); tableView.EndInit ();
 			tableView.BeginInit (); tableView.EndInit ();
-			
+
 			tableView.ColorScheme = Colors.TopLevel;
 			tableView.ColorScheme = Colors.TopLevel;
 
 
 			// 3 columns are visible
 			// 3 columns are visible
@@ -1746,14 +1742,15 @@ namespace Terminal.Gui.ViewTests {
 			tableView.EnsureSelectedCellIsVisible ();
 			tableView.EnsureSelectedCellIsVisible ();
 			Assert.Equal (smooth ? 1 : 3, tableView.ColumnOffset);
 			Assert.Equal (smooth ? 1 : 3, tableView.ColumnOffset);
 		}
 		}
-		[Fact]
+		
+		[Fact, AutoInitShutdown]
 		public void LongColumnTest ()
 		public void LongColumnTest ()
 		{
 		{
-			GraphViewTests.InitFakeDriver ();
-
 			var tableView = new TableView ();
 			var tableView = new TableView ();
-			tableView.BeginInit (); tableView.EndInit ();
-			
+
+			Application.Top.Add(tableView);
+			Application.Begin(Application.Top);
+
 			tableView.ColorScheme = Colors.TopLevel;
 			tableView.ColorScheme = Colors.TopLevel;
 
 
 			// 25 characters can be printed into table
 			// 25 characters can be printed into table
@@ -1791,16 +1788,16 @@ namespace Terminal.Gui.ViewTests {
 			var style = tableView.Style.GetOrCreateColumnStyle (dt.Columns [2]);
 			var style = tableView.Style.GetOrCreateColumnStyle (dt.Columns [2]);
 
 
 			// one way the API user can fix this for long columns
 			// one way the API user can fix this for long columns
-			// is to specify a max width for the column
+			// is to specify a MinAcceptableWidth for the column
 			style.MaxWidth = 10;
 			style.MaxWidth = 10;
 
 
 			tableView.LayoutSubviews ();
 			tableView.LayoutSubviews ();
 			tableView.Redraw (tableView.Bounds);
 			tableView.Redraw (tableView.Bounds);
 			expected =
 			expected =
 				@"
 				@"
-│A│B│Very Long       
+│A│B│Very Long Column
 ├─┼─┼───────────────────┤
 ├─┼─┼───────────────────┤
-│1│2│aaaaaaaaaa         
+│1│2│aaaaaaaaaaaaaaaaaaa
 │1│2│aaa                │
 │1│2│aaa                │
 ";
 ";
 			TestHelpers.AssertDriverContentsAre (expected, output);
 			TestHelpers.AssertDriverContentsAre (expected, output);
@@ -1854,8 +1851,9 @@ namespace Terminal.Gui.ViewTests {
 
 
 			// Now test making the width too small for the MinAcceptableWidth
 			// Now test making the width too small for the MinAcceptableWidth
 			// the Column won't fit so should not be rendered
 			// the Column won't fit so should not be rendered
-			Application.Shutdown ();
-			GraphViewTests.InitFakeDriver ();
+			var driver = ((FakeDriver)Application.Driver);
+			driver.UpdateOffScreen();
+			
 
 
 			tableView.Bounds = new Rect (0, 0, 9, 5);
 			tableView.Bounds = new Rect (0, 0, 9, 5);
 			tableView.LayoutSubviews ();
 			tableView.LayoutSubviews ();
@@ -1889,11 +1887,9 @@ namespace Terminal.Gui.ViewTests {
 		}
 		}
 
 
 
 
-		[Fact]
+		[Fact, AutoInitShutdown]
 		public void ScrollIndicators ()
 		public void ScrollIndicators ()
 		{
 		{
-			GraphViewTests.InitFakeDriver ();
-
 			var tableView = new TableView ();
 			var tableView = new TableView ();
 			tableView.BeginInit (); tableView.EndInit ();
 			tableView.BeginInit (); tableView.EndInit ();
 
 
@@ -1969,6 +1965,74 @@ namespace Terminal.Gui.ViewTests {
 			Application.Shutdown ();
 			Application.Shutdown ();
 		}
 		}
 
 
+		[Fact, AutoInitShutdown]
+		public void CellEventsBackgroundFill ()
+		{
+			var tv = new TableView () {
+				Width = 20,
+				Height = 4
+			};
+
+			var dt = new DataTable ();
+			dt.Columns.Add ("C1");
+			dt.Columns.Add ("C2");
+			dt.Columns.Add ("C3");
+
+			dt.Rows.Add ("Hello", DBNull.Value, "f");
+
+			tv.Table = dt;
+			tv.NullSymbol = string.Empty;
+
+			Application.Top.Add (tv);
+			Application.Begin (Application.Top);
+
+			tv.Redraw (tv.Bounds);
+
+			var expected =
+				@"
+┌─────┬──┬─────────┐
+│C1   │C2│C3       │
+├─────┼──┼─────────┤
+│Hello│  │f        │
+";
+
+			TestHelpers.AssertDriverContentsAre (expected, output);
+
+			var color = new Attribute (Color.Magenta, Color.BrightBlue);
+
+			var scheme = new ColorScheme {
+				Normal = color,
+				HotFocus = color,
+				Focus = color,
+				Disabled = color,
+				HotNormal = color,
+			};
+
+			// Now the thing we really want to test is the styles!
+			// All cells in the column have a column style that says
+			// the cell is pink!
+			foreach (DataColumn col in dt.Columns) {
+				var style = tv.Style.GetOrCreateColumnStyle (col);
+				style.ColorGetter = (e) => {
+					return scheme;
+				};
+
+			}
+
+
+			tv.Redraw (tv.Bounds);
+			expected =
+							@"
+00000000000000000000
+00000000000000000000
+00000000000000000000
+01111101101111111110
+";
+			TestHelpers.AssertDriverColorsAre (expected, new Attribute [] { tv.ColorScheme.Normal, color });
+
+
+		}
+
 		/// <summary>
 		/// <summary>
 		/// Builds a simple table of string columns with the requested number of columns and rows
 		/// Builds a simple table of string columns with the requested number of columns and rows
 		/// </summary>
 		/// </summary>