Skip to content

Commit d4b7c06

Browse files
committed
Add three-state color resolution to TableControl
null = inherit from container, Color.Default = use theme (default), explicit color = use as-is. Fixes WithBackgroundColor(null) not inheriting container background. Builder color params now accept nullable Color?.
1 parent d946731 commit d4b7c06

2 files changed

Lines changed: 59 additions & 34 deletions

File tree

SharpConsoleUI/Builders/TableControlBuilder.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public sealed class TableControlBuilder
2626
private readonly List<TableRow> _rows = new();
2727
private BorderStyle _borderStyle = BorderStyle.Single;
2828
private Color? _borderColor;
29-
private Color? _headerBackgroundColor;
30-
private Color? _headerForegroundColor;
29+
private Color? _headerBackgroundColor = Color.Default;
30+
private Color? _headerForegroundColor = Color.Default;
3131
private bool _showHeader = true;
3232
private bool _showRowSeparators = false;
3333
private bool _useSafeBorder = false;
@@ -44,8 +44,8 @@ public sealed class TableControlBuilder
4444
private string? _name;
4545
private object? _tag;
4646
private StickyPosition _stickyPosition = StickyPosition.None;
47-
private Color? _backgroundColor;
48-
private Color? _foregroundColor;
47+
private Color? _backgroundColor = Color.Default;
48+
private Color? _foregroundColor = Color.Default;
4949

5050
#region Column Configuration
5151

@@ -387,25 +387,25 @@ public TableControlBuilder StickyBottom()
387387
/// <summary>
388388
/// Sets the background color.
389389
/// </summary>
390-
public TableControlBuilder WithBackgroundColor(Color color)
390+
public TableControlBuilder WithBackgroundColor(Color? color)
391391
{
392392
_backgroundColor = color;
393393
return this;
394394
}
395395

396396
/// <summary>
397-
/// Sets the foreground (text) color.
397+
/// Sets the foreground (text) color. Pass null to inherit from container.
398398
/// </summary>
399-
public TableControlBuilder WithForegroundColor(Color color)
399+
public TableControlBuilder WithForegroundColor(Color? color)
400400
{
401401
_foregroundColor = color;
402402
return this;
403403
}
404404

405405
/// <summary>
406-
/// Sets both foreground and background colors.
406+
/// Sets both foreground and background colors. Pass null to inherit from container.
407407
/// </summary>
408-
public TableControlBuilder WithColors(Color foreground, Color background)
408+
public TableControlBuilder WithColors(Color? foreground, Color? background)
409409
{
410410
_foregroundColor = foreground;
411411
_backgroundColor = background;

SharpConsoleUI/Controls/TableControl.cs

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class TableControl : IWindowControl, IDOMPaintable, IMouseAwareControl
3030

3131
private HorizontalAlignment _horizontalAlignment = HorizontalAlignment.Left;
3232
private VerticalAlignment _verticalAlignment = VerticalAlignment.Top;
33-
private Color? _backgroundColorValue;
34-
private Color? _foregroundColorValue;
33+
private Color? _backgroundColorValue = Color.Default;
34+
private Color? _foregroundColorValue = Color.Default;
3535
private Margin _margin = new Margin(0, 0, 0, 0);
3636
private StickyPosition _stickyPosition = StickyPosition.None;
3737
private bool _visible = true;
@@ -43,8 +43,8 @@ public class TableControl : IWindowControl, IDOMPaintable, IMouseAwareControl
4343
private List<TableRow> _rows = new();
4444
private BorderStyle _borderStyle = BorderStyle.Single;
4545
private Color? _borderColorValue;
46-
private Color? _headerBackgroundColorValue;
47-
private Color? _headerForegroundColorValue;
46+
private Color? _headerBackgroundColorValue = Color.Default;
47+
private Color? _headerForegroundColorValue = Color.Default;
4848
private bool _showHeader = true;
4949
private bool _showRowSeparators = false;
5050
private bool _useSafeBorder = false;
@@ -485,41 +485,66 @@ public void SetData(IEnumerable<TableRow> rows)
485485

486486
#region Color Resolution Methods
487487

488+
/// <summary>
489+
/// Three-state resolution: null = inherit from container,
490+
/// Color.Default = use theme, explicit color = use as-is.
491+
/// </summary>
488492
private Color ResolveBackgroundColor(Color defaultBg)
489493
{
490-
var theme = Container?.GetConsoleWindowSystem?.Theme;
491-
return _backgroundColorValue
492-
?? theme?.TableBackgroundColor
493-
?? Container?.BackgroundColor
494-
?? defaultBg;
494+
if (_backgroundColorValue == null)
495+
return Container?.BackgroundColor ?? defaultBg;
496+
if (_backgroundColorValue.Value == Color.Default)
497+
{
498+
var theme = Container?.GetConsoleWindowSystem?.Theme;
499+
return theme?.TableBackgroundColor
500+
?? Container?.BackgroundColor
501+
?? defaultBg;
502+
}
503+
return _backgroundColorValue.Value;
495504
}
496505

497506
private Color ResolveForegroundColor(Color defaultFg)
498507
{
499-
var theme = Container?.GetConsoleWindowSystem?.Theme;
500-
return _foregroundColorValue
501-
?? theme?.TableForegroundColor
502-
?? Container?.ForegroundColor
503-
?? defaultFg;
508+
if (_foregroundColorValue == null)
509+
return Container?.ForegroundColor ?? defaultFg;
510+
if (_foregroundColorValue.Value == Color.Default)
511+
{
512+
var theme = Container?.GetConsoleWindowSystem?.Theme;
513+
return theme?.TableForegroundColor
514+
?? Container?.ForegroundColor
515+
?? defaultFg;
516+
}
517+
return _foregroundColorValue.Value;
504518
}
505519

506520
private Color ResolveHeaderBackgroundColor()
507521
{
508-
var theme = Container?.GetConsoleWindowSystem?.Theme;
509-
return _headerBackgroundColorValue
510-
?? theme?.TableHeaderBackgroundColor
511-
?? theme?.TableBackgroundColor
512-
?? Container?.BackgroundColor
513-
?? Color.Black;
522+
if (_headerBackgroundColorValue == null)
523+
return Container?.BackgroundColor ?? Color.Black;
524+
if (_headerBackgroundColorValue.Value == Color.Default)
525+
{
526+
var theme = Container?.GetConsoleWindowSystem?.Theme;
527+
return theme?.TableHeaderBackgroundColor
528+
?? theme?.TableBackgroundColor
529+
?? Container?.BackgroundColor
530+
?? Color.Black;
531+
}
532+
return _headerBackgroundColorValue.Value;
514533
}
515534

516535
private Color ResolveHeaderForegroundColor()
517536
{
518-
var theme = Container?.GetConsoleWindowSystem?.Theme;
519-
return _headerForegroundColorValue
520-
?? theme?.TableHeaderForegroundColor
521-
?? theme?.TableForegroundColor
522-
?? Color.White;
537+
if (_headerForegroundColorValue == null)
538+
return Container?.ForegroundColor ?? Color.White;
539+
if (_headerForegroundColorValue.Value == Color.Default)
540+
{
541+
var theme = Container?.GetConsoleWindowSystem?.Theme;
542+
return theme?.TableHeaderForegroundColor
543+
?? theme?.TableForegroundColor
544+
?? Container?.ForegroundColor
545+
?? Color.White;
546+
}
547+
return _headerForegroundColorValue.Value;
523548
}
524549

525550
private Color ResolveBorderColor()

0 commit comments

Comments
 (0)