diff --git a/src/client/spark/view/Controls/DIYGrid/CellRender/TextCell.cs b/src/client/spark/view/Controls/DIYGrid/CellRender/TextCell.cs index 882289093..834916025 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellRender/TextCell.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellRender/TextCell.cs @@ -8,178 +8,263 @@ using System.Windows.Media; using FontFamily = System.Windows.Media.FontFamily; namespace mbts.ui.controls { - partial class DIYGrid : Control { - public class TextCell : FrameworkElement { - // these fields each represent properties that are accessed frequently, - // they are stored locally to reduce repetative DependencyProperty lookups - Thickness _padding; - HorizontalAlignment _horizontal_alignment; - Brush _background, _foreground; - double _glyph_width, _font_size; - string _text; - GlyphRun _glyph_run; - GlyphContext _glyph_context; + class TextCell : FrameworkElement { + // these fields each represent properties that are accessed frequently, + // they are stored locally to reduce repetative DependencyProperty lookups + Thickness _padding => _gcr.padding; + Brush _background, _foreground, _border_brush_backing; + double _glyph_width => _gcr.glyph_run.AdvanceWidths.Sum(); + GlyphRun _glyph_run => _gcr.glyph_run; + Pen _border_stroke_pen_backing; + GlyphContentRect _gcr = new GlyphContentRect(); - static TextCell() { - WPFHelper.override_metadata( - HorizontalAlignmentProperty, - FrameworkPropertyMetadataOptions.AffectsArrange | + Brush _border_brush { + get => _border_brush_backing; + set { + _border_brush_backing = value; + _border_stroke_pen_backing = null; + } + } + Thickness _border_thickness { + get => _gcr.border_thickness; + set { + _gcr.border_thickness = new Thickness(value.Left); + _border_stroke_pen_backing = null; + } + } + Pen _border_stroke_pen => + // border_thickness.Left is used as the uniform border thickness. This can be changed if need be. + _border_stroke_pen_backing == null && _border_brush != null && _border_thickness.Left > 0 ? + (_border_stroke_pen_backing = _border_stroke_pen_backing = new Pen(_border_brush, _border_thickness.Left)) : + _border_stroke_pen_backing; + + static TextCell() { + WPFHelper.override_metadata( + HorizontalAlignmentProperty, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._gcr.horizontal_alignment = v, HorizontalAlignment.Left); + WPFHelper.override_metadata( + WidthProperty, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._gcr.width = v, + double.NaN); + } + + public TextCell() { + _gcr.min_width = MinWidth; + _gcr.width = Width; + _gcr.max_width = MaxWidth; + _gcr.min_height = MinHeight; + _gcr.height = Height; + _gcr.max_height = Height; + _gcr.padding = Padding; + _gcr.border_thickness = BorderThickness; + _gcr.horizontal_alignment = HorizontalAlignment; + _gcr.horizontal_content_alignment = HorizontalContentAlignment; + _gcr.vertical_alignment = VerticalAlignment; + _gcr.vertical_content_alignment = VerticalContentAlignment; + _background = Background; + _foreground = Foreground; + _border_brush = BorderBrush; + _gcr.font_size = FontSize; + if (Text is string str && !string.IsNullOrEmpty(str)) + _gcr.text_lines = new[] { str }; + _gcr.glyph_context = GlyphContext.create(FontFamily, FontWeight); + } + + #region BorderThickness + public static readonly DependencyProperty BorderThicknessProperty = + Control.BorderThicknessProperty.AddOwner(typeof(TextCell), + new FrameworkPropertyMetadata( + new Thickness(0), + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsArrange, + (d, e) => ((TextCell)d)._gcr.border_thickness = (Thickness)e.NewValue)); + + public Thickness BorderThickness { + get => (Thickness)GetValue(BorderThicknessProperty); + set => SetValue(BorderThicknessProperty, value); + } + #endregion + + #region BorderBrush + public static readonly DependencyProperty BorderBrushProperty = + Control.BorderBrushProperty.AddOwner(typeof(TextCell), + new FrameworkPropertyMetadata( + null, + FrameworkPropertyMetadataOptions.AffectsRender, + (d, e) => ((TextCell)d)._border_brush = e.NewValue as Brush)); + + public Brush BorderBrush { + get => (Brush)GetValue(BorderBrushProperty); + set => SetValue(BorderBrushProperty, value); + } + #endregion + + #region HorizontalContentAlignment + public static readonly DependencyProperty HorizontalContentAlignmentProperty = + Control.HorizontalContentAlignmentProperty.AddOwner(typeof(TextCell), + new FrameworkPropertyMetadata( + HorizontalAlignment.Center, + FrameworkPropertyMetadataOptions.AffectsArrange, + (d, e) => { + if (e.OldValue is HorizontalAlignment oldha) { + if (oldha == HorizontalAlignment.Right) { + + } + } + ((TextCell)d)._gcr.horizontal_content_alignment = (HorizontalAlignment)e.NewValue; + })); + + public HorizontalAlignment HorizontalContentAlignment { + get => (HorizontalAlignment)GetValue(HorizontalContentAlignmentProperty); + set => SetValue(HorizontalContentAlignmentProperty, value); + } + #endregion + + #region VerticalContentAlignment + public static readonly DependencyProperty VerticalContentAlignmentProperty = Control.VerticalContentAlignmentProperty.AddOwner(typeof(TextCell), + new FrameworkPropertyMetadata( + VerticalAlignment.Center, + FrameworkPropertyMetadataOptions.AffectsArrange, + (d, e) => ((TextCell)d)._gcr.vertical_content_alignment = (VerticalAlignment)e.NewValue)); + + public VerticalAlignment VerticalContentAlignment { + get => (VerticalAlignment)GetValue(VerticalContentAlignmentProperty); + set => SetValue(VerticalContentAlignmentProperty, value); + } + #endregion + + #region Background + public static readonly DependencyProperty BackgroundProperty = + WPFHelper.create_dp( + nameof(Background), + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._background = v, + Brushes.Transparent); + + public Brush Background { get => GetValue(BackgroundProperty) as Brush; set => SetValue(BackgroundProperty, value); } + #endregion + + #region Foreground + public static readonly DependencyProperty ForegroundProperty = + WPFHelper.create_dp( + nameof(Foreground), + FrameworkPropertyMetadataOptions.AffectsRender, + (p, v) => p._foreground = v, + Brushes.Black); + + public Brush Foreground { get => (Brush)GetValue(ForegroundProperty); set => SetValue(ForegroundProperty, value); } + #endregion + + #region FontWeight + public static readonly DependencyProperty FontWeightProperty = + TextElement.FontWeightProperty.AddOwner( + typeof(TextCell), + new FrameworkPropertyMetadata( + FontWeights.Normal, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, - (o, v) => { - o._horizontal_alignment = v; - o._glyph_run = null; - }, - HorizontalAlignment.Left); - WPFHelper.override_metadata( - WidthProperty, - FrameworkPropertyMetadataOptions.AffectsArrange | + WPFHelper.prop_changed_callback( + (p, v) => p._gcr.glyph_context = p._gcr.glyph_context.with(v)))); + + public FontWeight FontWeight { get => (FontWeight)GetValue(FontWeightProperty); set => SetValue(FontWeightProperty, value); } + #endregion + + #region FontFamily + public static readonly DependencyProperty FontFamilyProperty = + TextElement.FontFamilyProperty.AddOwner( + typeof(TextCell), + new FrameworkPropertyMetadata( + TextUtils.StandardFontFamily, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, - (o, v) => o._glyph_run = null, - double.NaN); - } + WPFHelper.prop_changed_callback( + (p, v) => p._gcr.glyph_context = p._gcr.glyph_context.with(v)))); - public TextCell() { - _background = Background; - _foreground = Foreground; - _horizontal_alignment = HorizontalAlignment; - _padding = Padding; - _font_size = FontSize; - _text = Text; - _glyph_context = GlyphContext.create(FontFamily, FontWeight); - } + public FontFamily FontFamily { get => GetValue(FontFamilyProperty) as FontFamily; set => SetValue(FontFamilyProperty, value); } + #endregion - #region Background - public static readonly DependencyProperty BackgroundProperty = - WPFHelper.create_dp( - nameof(Background), - FrameworkPropertyMetadataOptions.AffectsRender, - (p, v) => p._background = v, - null); - - public Brush Background { get => (Brush)GetValue(BackgroundProperty); set => SetValue(BackgroundProperty, value); } - #endregion - - #region Foreground - public static readonly DependencyProperty ForegroundProperty = - WPFHelper.create_dp( - nameof(Foreground), - FrameworkPropertyMetadataOptions.AffectsRender, - (p, v) => p._foreground = v, - Brushes.Black); - - public Brush Foreground { get => (Brush)GetValue(ForegroundProperty); set => SetValue(ForegroundProperty, value); } - #endregion - - #region FontFamily - public static readonly DependencyProperty FontFamilyProperty = - TextElement.FontFamilyProperty.AddOwner( - typeof(TextCell), - new FrameworkPropertyMetadata( - TextUtils.StandardFontFamily, - FrameworkPropertyMetadataOptions.AffectsMeasure | - FrameworkPropertyMetadataOptions.AffectsRender, - WPFHelper.prop_changed_callback( - (p, v) => { - p._glyph_context = p._glyph_context.with(v); - p._glyph_run = null; - }))); - - public FontFamily FontFamily { get => GetValue(FontFamilyProperty) as FontFamily; set => SetValue(FontFamilyProperty, value); } - #endregion - - #region FontWeight - public static readonly DependencyProperty FontWeightProperty = - TextElement.FontWeightProperty.AddOwner( - typeof(TextCell), - new FrameworkPropertyMetadata( - FontWeights.Normal, - FrameworkPropertyMetadataOptions.AffectsMeasure | - FrameworkPropertyMetadataOptions.AffectsRender, - WPFHelper.prop_changed_callback( - (p, v) => { - p._glyph_context = p._glyph_context.with(v); - p._glyph_run = null; - }))); - - public FontWeight FontWeight { get => (FontWeight)GetValue(FontWeightProperty); set => SetValue(FontWeightProperty, value); } - #endregion - - #region FontSize - public static readonly DependencyProperty FontSizeProperty = - TextElement.FontSizeProperty.AddOwner( - typeof(TextCell), - new FrameworkPropertyMetadata( - FontSizes.Small, - FrameworkPropertyMetadataOptions.AffectsMeasure | - FrameworkPropertyMetadataOptions.AffectsRender | - FrameworkPropertyMetadataOptions.Inherits, - WPFHelper.prop_changed_callback( - (p, v) => { - p._font_size = v; - p._glyph_run = null; - }))); - - public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); } - #endregion - - #region Padding - public static readonly DependencyProperty PaddingProperty = - WPFHelper.create_dp( - nameof(Padding), + #region FontSize + public static readonly DependencyProperty FontSizeProperty = + TextElement.FontSizeProperty.AddOwner( + typeof(TextCell), + new FrameworkPropertyMetadata( + FontSizes.Small, FrameworkPropertyMetadataOptions.AffectsMeasure | - FrameworkPropertyMetadataOptions.AffectsRender, - (p, v) => { - p._padding = v; - p._glyph_run = null; - }, DefaultCellPadding); + FrameworkPropertyMetadataOptions.AffectsRender | + FrameworkPropertyMetadataOptions.Inherits, + WPFHelper.prop_changed_callback( + (p, v) => p._gcr.font_size = v))); - public Thickness Padding { get => (Thickness)GetValue(PaddingProperty); set => SetValue(PaddingProperty, value); } - #endregion + public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); } + #endregion - #region Text - public static readonly DependencyProperty TextProperty = - WPFHelper.create_dp( - nameof(Text), - FrameworkPropertyMetadataOptions.AffectsMeasure | - FrameworkPropertyMetadataOptions.AffectsRender, - (p, v) => { - p._text = v; - p._glyph_run = null; - }); + #region Padding + public static readonly DependencyProperty PaddingProperty = + WPFHelper.create_dp( + nameof(Padding), + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (p, v) => p._gcr.padding = v, + DIYGrid.DefaultCellPadding); - public string Text { get => GetValue(TextProperty) as string; set => SetValue(TextProperty, value); } - #endregion + public Thickness Padding { get => (Thickness)GetValue(PaddingProperty); set => SetValue(PaddingProperty, value); } + #endregion - protected override Size ArrangeOverride(Size finalSize) { - if (_glyph_run == null) - _set_glyph(finalSize); - return finalSize; + #region Text + public static readonly DependencyProperty TextProperty = + WPFHelper.create_dp( + nameof(Text), + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (p, v) => p._gcr.text_lines = string.IsNullOrEmpty(v) ? null : new[] { v }, string.Empty); + + public string Text { get => GetValue(TextProperty) as string; set => SetValue(TextProperty, value); } + #endregion + + protected override Size ArrangeOverride(Size finalSize) => finalSize; + + protected override Size MeasureOverride(Size availableSize) { + _gcr.available_size = availableSize; + if (double.IsInfinity(_gcr.layout_size.Height) || double.IsInfinity(_gcr.layout_size.Width)) { + _gcr.remeasure(); } + return _gcr.layout_size; + } - protected override Size MeasureOverride(Size availableSize) { - if (_glyph_run == null) - _set_glyph(availableSize); - return new Size(Math.Max(availableSize.Width, _padding.Left + _glyph_width + _padding.Right), - Math.Max(availableSize.Height, _padding.Top + _font_size + _padding.Bottom)); + double bl_x = 0; + protected override void OnRender(DrawingContext drawingContext) { + var rs = RenderSize; + var rect = new Rect(0, 0, rs.Width, rs.Height); + var bg = Background; + if (bg != null) { + drawingContext.DrawRectangle(bg, _border_stroke_pen, rect); } + if (_glyph_run != null) { + var new_x = _glyph_run.BaselineOrigin.X; + if (Math.Abs(bl_x - new_x) > 5) { - void _set_glyph(Size size) { - _glyph_run = _glyph_context.get_glyph_run(_text, _font_size, size, _horizontal_alignment, VerticalAlignment.Center, _padding); - _glyph_width = _glyph_run == null ? 0 : _glyph_run.AdvanceWidths.Sum(); - } + if (new_x < bl_x) { - protected override void OnRender(DrawingContext drawingContext) { - var rs = RenderSize; - var rect = new Rect(0, 0, rs.Width, rs.Height); - if (_background != null) - drawingContext.DrawRectangle(_background, null, rect); - if (_glyph_run != null) { - drawingContext.PushClip(new RectangleGeometry(rect)); - drawingContext.DrawGlyphRun(_foreground, _glyph_run); - drawingContext.Pop(); + } } + if (Text == "-3,375.000") { + + } + bl_x = new_x; + drawingContext.PushClip(new RectangleGeometry(rect)); + drawingContext.DrawGlyphRun(_foreground, _glyph_run); + drawingContext.Pop(); + } + if (bg != null) { + var bgo = DIYGrid.GetBackgroundOverlay(this); + if (bgo != null) + drawingContext.DrawRectangle(bgo, null, rect); } } }