refactor: Add get bg/fg delegates to RenderColumnBase

These colors are frequently functions of the rows; now such cases don't require
implementation in classes that derive from RenderColumnBase
This commit is contained in:
John Colvin committed 2022-08-07 13:51:52 -04:00
1 parent eb150ba82c
commit e15cdfd76e
2 files changed
+35 -11

No files matched your search

@@ -23,7 +23,6 @@ namespace mbts.ui.controls {
public TColKey key { get; }
public IDataGridColumnFilter filter_model { get; }
public string filter_template => DataGridFilterKnownTemplates.DiscreteValues;
public Brush background { get; set; }
public VerticalAlignment vertical_alignment { get; set; } = VerticalAlignment.Center;
public Thickness padding { get; set; } = DefaultCellPadding;
public GlyphContext glyph_context { get; set; } = TextUtils.StandardGlyphContext;
@@ -32,7 +31,7 @@ namespace mbts.ui.controls {
Side side = _get_side(row);
CellRender.draw_text(
dc, glyph_context, font_size, side.ToString(), position, row_height,
_get_foreground(side), background, horizontal_alignment, vertical_alignment, padding);
_get_foreground(side), background_brush, horizontal_alignment, vertical_alignment, padding);
}
static Brush _get_foreground(Side side) =>
@@ -23,17 +23,19 @@ namespace mbts.ui.controls {
public class RenderTextColumn<TRowVM> : RenderColumnBase<TRowVM>, IRenderColumn<TRowVM>, IFilterableColumn {
protected readonly Func<TRowVM, string> _get_text;
protected readonly Func<TRowVM, Brush> _get_background_brush;
protected readonly Func<TRowVM, Brush> _get_foreground_brush;
public RenderTextColumn(
Func<TRowVM, string> get_text,
IDataGridColumnFilter filter = null,
string filter_template = null,
Func<TRowVM, Brush> get_brush = null)
Func<TRowVM, Brush> get_background = null,
Func<TRowVM, Brush> get_foreground = null)
: base() {
_get_text = get_text;
if (get_brush is not null) {
var get_row_brush = get_brush;
get_brush = r => {
if (get_background is not null) {
var get_row_brush = get_background;
get_background = r => {
var row_brush = get_row_brush(r);
if (row_brush is null) {
return background_brush;
@@ -49,9 +51,11 @@ namespace mbts.ui.controls {
return row_color.blend(col_color).to_brush();
};
}
_get_background_brush = get_brush ?? new Func<TRowVM, Brush>(_ => {
_get_background_brush = get_background ?? new Func<TRowVM, Brush>(_ => {
return background_brush;
});
_get_foreground_brush = get_foreground
?? (_ => font_brush);
this.filter_model = filter;
this.filter_template = filter_template;
font_params_changed += refresh_glyph_context;
@@ -68,16 +72,37 @@ namespace mbts.ui.controls {
public override void draw(TRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) =>
CellRender.draw_text(
dc, glyph_context, font_size, _get_text(row), position, row_height,
font_brush, _get_background_brush(row), horizontal_alignment, vertical_alignment, padding);
dc,
glyph_context,
font_size,
_get_text(row),
position,
row_height,
_get_foreground_brush(row),
_get_background_brush(row),
horizontal_alignment,
vertical_alignment,
padding);
}
public class RenderTextColumn<TRowVM, TColKey> : RenderTextColumn<TRowVM>, IKeyedColumn<TColKey> {
public RenderTextColumn(TColKey key, Func<TRowVM, string> get_text, IDataGridColumnFilter filter, string filter_template)
: base(get_text, filter, filter_template) => this.key = key;
public RenderTextColumn(TColKey key, Func<TRowVM, string> get_text, IDataGridColumnFilter filter, string filter_template, Func<TRowVM, Brush> get_brush)
: base(get_text, filter, filter_template, get_brush) => this.key = key;
public RenderTextColumn(
TColKey key,
Func<TRowVM, string> get_text,
IDataGridColumnFilter filter,
string filter_template,
Func<TRowVM, Brush> get_background = null,
Func<TRowVM, Brush> get_foreground = null)
: base(
get_text,
filter,
filter_template,
get_background: get_background,
get_foreground: get_foreground) =>
this.key = key;
public TColKey key { get; }
}