Added support for coloring cells

This commit is contained in:
tznind
2021-06-28 18:16:48 +01:00
parent 5cef7f7663
commit b3ce131fbf
2 changed files with 27 additions and 5 deletions

View File

@@ -406,12 +406,13 @@ namespace Terminal.Gui {
// Set color scheme based on whether the current cell is the selected one
bool isSelectedCell = IsSelected(current.Column.Ordinal,rowToRender);
Driver.SetAttribute (isSelectedCell ? ColorScheme.HotFocus : ColorScheme.Normal);
var val = Table.Rows [rowToRender][current.Column];
// Render the (possibly truncated) cell value
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));
@@ -1134,6 +1135,12 @@ namespace Terminal.Gui {
/// </summary>
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>
/// Defines the format for values e.g. "yyyy-MM-dd" for dates
/// </summary>

View File

@@ -24,6 +24,8 @@ namespace UICatalog.Scenarios {
private MenuItem miFullRowSelect;
private MenuItem miExpandLastColumn;
ColorScheme redColorScheme;
public override void Setup ()
{
Win.Title = this.GetName();
@@ -60,8 +62,6 @@ namespace UICatalog.Scenarios {
});
Top.Add (menu);
var statusBar = new StatusBar (new StatusItem [] {
new StatusItem(Key.F2, "~F2~ OpenExample", () => OpenExample(true)),
new StatusItem(Key.F3, "~F3~ CloseExample", () => CloseExample()),
@@ -88,6 +88,13 @@ namespace UICatalog.Scenarios {
tableView.KeyPress += TableViewKeyPress;
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 ()
@@ -268,7 +275,15 @@ namespace UICatalog.Scenarios {
// align positive values left
TextAlignment.Left:
// 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);