Explorar o código

Added support for coloring cells

tznind %!s(int64=4) %!d(string=hai) anos
pai
achega
b3ce131fbf
Modificáronse 2 ficheiros con 27 adicións e 5 borrados
  1. 9 2
      Terminal.Gui/Views/TableView.cs
  2. 18 3
      UICatalog/Scenarios/TableEditor.cs

+ 9 - 2
Terminal.Gui/Views/TableView.cs

@@ -406,12 +406,13 @@ namespace Terminal.Gui {
 				// Set color scheme based on whether the current cell is the selected one
 				// Set color scheme based on whether the current cell is the selected one
 				bool isSelectedCell = IsSelected(current.Column.Ordinal,rowToRender);
 				bool isSelectedCell = IsSelected(current.Column.Ordinal,rowToRender);
 
 
-				Driver.SetAttribute (isSelectedCell ? ColorScheme.HotFocus : ColorScheme.Normal);
-
 				var val = Table.Rows [rowToRender][current.Column];
 				var val = Table.Rows [rowToRender][current.Column];
 
 
 				// Render the (possibly truncated) cell value
 				// Render the (possibly truncated) cell value
 				var representation = GetRepresentation(val,colStyle);
 				var representation = GetRepresentation(val,colStyle);
+				var scheme = (colStyle?.ColorGetter?.Invoke(val)) ?? ColorScheme;
+
+				Driver.SetAttribute (isSelectedCell ? scheme.HotFocus : scheme.Normal);
 				
 				
 				Driver.AddStr (TruncateOrPad(val,representation, current.Width, colStyle));
 				Driver.AddStr (TruncateOrPad(val,representation, current.Width, colStyle));
 				
 				
@@ -1134,6 +1135,12 @@ namespace Terminal.Gui {
 			/// </summary>
 			/// </summary>
 			public Func<object, string> RepresentationGetter;
 			public Func<object, string> RepresentationGetter;
 
 
+			/// <summary>
+			/// Defines a delegate for returning a custom color scheme per cell based on cell values.
+			/// Return null for the default
+			/// </summary>
+			public Func<object, ColorScheme> ColorGetter;
+
 			/// <summary>
 			/// <summary>
 			/// Defines the format for values e.g. "yyyy-MM-dd" for dates
 			/// Defines the format for values e.g. "yyyy-MM-dd" for dates
 			/// </summary>
 			/// </summary>

+ 18 - 3
UICatalog/Scenarios/TableEditor.cs

@@ -24,6 +24,8 @@ namespace UICatalog.Scenarios {
 		private MenuItem miFullRowSelect;
 		private MenuItem miFullRowSelect;
 		private MenuItem miExpandLastColumn;
 		private MenuItem miExpandLastColumn;
 
 
+		ColorScheme redColorScheme;
+
 		public override void Setup ()
 		public override void Setup ()
 		{
 		{
 			Win.Title = this.GetName();
 			Win.Title = this.GetName();
@@ -60,8 +62,6 @@ namespace UICatalog.Scenarios {
 			});
 			});
 			Top.Add (menu);
 			Top.Add (menu);
 
 
-
-
 			var statusBar = new StatusBar (new StatusItem [] {
 			var statusBar = new StatusBar (new StatusItem [] {
 				new StatusItem(Key.F2, "~F2~ OpenExample", () => OpenExample(true)),
 				new StatusItem(Key.F2, "~F2~ OpenExample", () => OpenExample(true)),
 				new StatusItem(Key.F3, "~F3~ CloseExample", () => CloseExample()),
 				new StatusItem(Key.F3, "~F3~ CloseExample", () => CloseExample()),
@@ -88,6 +88,13 @@ namespace UICatalog.Scenarios {
 			tableView.KeyPress += TableViewKeyPress;
 			tableView.KeyPress += TableViewKeyPress;
 
 
 			SetupScrollBar();
 			SetupScrollBar();
+
+			redColorScheme = new ColorScheme(){
+				Disabled = Colors.Base.Disabled,
+				HotFocus = Colors.Base.HotFocus,
+				Focus = Colors.Base.Focus,
+				Normal = Application.Driver.MakeAttribute(Color.Red,Color.Blue)
+			};
 		}
 		}
 
 
 		private void SetupScrollBar ()
 		private void SetupScrollBar ()
@@ -268,7 +275,15 @@ namespace UICatalog.Scenarios {
 								// align positive values left
 								// align positive values left
 								TextAlignment.Left:
 								TextAlignment.Left:
 								// not a double
 								// not a double
-								TextAlignment.Left
+								TextAlignment.Left,
+				
+				ColorGetter = (v)=>v is double d ? 
+								// color 0 and negative values red
+								d <= 0.0000001 ? redColorScheme : 
+								// use normal scheme for positive values
+								null:
+								// not a double
+								null
 			};
 			};
 			
 			
 			tableView.Style.ColumnStyles.Add(tableView.Table.Columns["DateCol"],dateFormatStyle);
 			tableView.Style.ColumnStyles.Add(tableView.Table.Columns["DateCol"],dateFormatStyle);