diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs index 686d0e0d2..057db4866 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs @@ -80,7 +80,6 @@ namespace mbts.spark { _inside_bids.ObserveOnDispatcher().Subscribe(v => _animation.animate_working_bids_inside(v.newval)), _inside_asks.ObserveOnDispatcher().Subscribe(v => _animation.animate_working_asks_inside(v.newval)), _day_change.ObserveOnDispatcher().Subscribe(v => _animation.animate_day_change(v.newval > v.oldval)), - _orders_model.on_security_orders(security.Id).ObserveOnDispatcher().Subscribe(_on_orders), Disposable.Create(() => _animation.AnimationsChanged -= on_animations_changed) ); } diff --git a/src/client/spark/view/Controls/DIYGrid/CellRender/CellRender.cs b/src/client/spark/view/Controls/DIYGrid/CellRender/CellRender.cs index df3a652d1..3bb9945f3 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellRender/CellRender.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellRender/CellRender.cs @@ -1,5 +1,6 @@ using mbts.ui.utility.text; using System; +using System.Linq; using System.Windows; using System.Windows.Media; @@ -34,26 +35,18 @@ namespace mbts.ui.controls { DrawingContext dc, GlyphContext glyph_context, double font_size, string text, ColumnGeometry position, double row_height, Brush foreground, Brush background, HorizontalAlignment x_align, VerticalAlignment y_align, Thickness padding) { // - var gt = glyph_context.get_glyph_text(text, font_size, position.left, 0, position.left + position.width, row_height, x_align, y_align, padding); - draw_text(dc, gt, position, padding, row_height, foreground, background); - } - - public static void draw_text(DrawingContext dc, GlyphText glyph_text, ColumnGeometry position, Thickness padding, double row_height, Brush foreground, Brush background) { - using (draw_with_clip(dc, glyph_text.width, glyph_text.font_size, padding, row_height, position)) { - if (background != null) - dc.DrawRectangle(background, null, new Rect(position.left, 0, position.width, row_height)); - dc.draw_glyph_run_anti_alias(foreground, glyph_text); + if (!string.IsNullOrEmpty(text)) { + var gr = glyph_context.get_glyph_run(text, font_size, position.unclipped_left, 0, position.right, row_height, x_align, y_align, padding); + draw_text(dc, gr, gr.AdvanceWidths.Sum(), font_size, position, padding, row_height, foreground, background); } } - public static Size measure_text_cell(FrameworkElement cell, GlyphText glyph_text, Thickness padding, Size available_size) { - double x = padding.Left + padding.Right, y = padding.Top + padding.Bottom; - if (glyph_text != null) { - x += glyph_text.width; - y += glyph_text.font_size; + public static void draw_text(DrawingContext dc, GlyphRun glyph_run, double glyph_width, double font_size, ColumnGeometry position, Thickness padding, double row_height, Brush foreground, Brush background) { + using (draw_with_clip(dc, glyph_width, font_size, padding, row_height, position)) { + if (background != null) + dc.DrawRectangle(background, null, new Rect(position.left, 0, position.width, row_height)); + dc.DrawGlyphRun(foreground, glyph_run); } - // stretch if smaller than container - return new Size(Math.Max(x, available_size.Width), Math.Max(y, available_size.Height)); } /// diff --git a/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs b/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs index 0eb49b333..b35971ee3 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellRender/HyperlinkCell.cs @@ -2,6 +2,7 @@ using mbts.ui.utility.text; using System; using System.ComponentModel; +using System.Linq; using System.Windows; using System.Windows.Documents; using System.Windows.Input; @@ -16,16 +17,22 @@ namespace mbts.ui.controls { Thickness _padding; HorizontalAlignment _horizontal_alignment; Brush _background, _foreground; - double _font_size; + double _glyph_width, _font_size; string _text; + GlyphRun _glyph_run; GlyphContext _glyph_context; - GlyphText _glyph_text; bool _is_mouse_over_text, _can_execute; Pen _underline_pen; static HyperlinkCell() => - WPFHelper.override_metadata( - HorizontalAlignmentProperty, FrameworkPropertyMetadataOptions.AffectsRender, (o, v) => o._horizontal_alignment = v, HorizontalAlignment.Left); + WPFHelper.override_metadata( + HorizontalAlignmentProperty, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => { + o._horizontal_alignment = v; + o._glyph_run = null; + }, HorizontalAlignment.Left); public HyperlinkCell() { _background = Background; @@ -76,7 +83,7 @@ namespace mbts.ui.controls { WPFHelper.prop_changed_callback( (p, v) => { p._glyph_context = p._glyph_context.with(v); - p._set_glyph_text(); + p._glyph_run = null; }))); public FontWeight FontWeight { get => (FontWeight)GetValue(FontWeightProperty); set => SetValue(FontWeightProperty, value); } @@ -93,7 +100,7 @@ namespace mbts.ui.controls { WPFHelper.prop_changed_callback( (p, v) => { p._glyph_context = p._glyph_context.with(v); - p._set_glyph_text(); + p._glyph_run = null; }))); public FontFamily FontFamily { get => GetValue(FontFamilyProperty) as FontFamily; set => SetValue(FontFamilyProperty, value); } @@ -111,7 +118,7 @@ namespace mbts.ui.controls { WPFHelper.prop_changed_callback( (p, v) => { p._font_size = v; - p._set_glyph_text(); + p._glyph_run = null; }))); public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); } @@ -125,7 +132,7 @@ namespace mbts.ui.controls { FrameworkPropertyMetadataOptions.AffectsRender, (p, v) => { p._padding = v; - p._set_glyph_text(); + p._glyph_run = null; }, DefaultCellPadding); public Thickness Padding { get => (Thickness)GetValue(PaddingProperty); set => SetValue(PaddingProperty, value); } @@ -139,7 +146,7 @@ namespace mbts.ui.controls { FrameworkPropertyMetadataOptions.AffectsRender, (o, v) => { o._text = v; - o._set_glyph_text(); + o._glyph_run = null; }, string.Empty); public string Text { get => GetValue(TextProperty) as string; set => SetValue(TextProperty, value); } @@ -243,25 +250,39 @@ namespace mbts.ui.controls { protected override void OnMouseEnter(MouseEventArgs e) => _check_mouse_over_text(e); protected override void OnMouseLeave(MouseEventArgs e) => _check_mouse_over_text(e); - protected override Size ArrangeOverride(Size finalSize) => finalSize; - protected override Size MeasureOverride(Size availableSize) => - CellRender.measure_text_cell(this, _glyph_text, _padding, availableSize); + protected override Size ArrangeOverride(Size finalSize) { + if (_glyph_run == null) + _set_glyph(finalSize); + return finalSize; + } - void _set_glyph_text() => - _glyph_text = _glyph_context.get_glyph_text(_text, _font_size, DesiredSize, _horizontal_alignment, VerticalAlignment.Center, _padding); + 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)); + } + + 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(); + } protected override void OnRender(DrawingContext drawingContext) { - var rect = new Rect(0, 0, RenderSize.Width, RenderSize.Height); + var rs = RenderSize; + var rect = new Rect(0, 0, rs.Width, rs.Height); var bg = Background; if (bg != null) drawingContext.DrawRectangle(bg, null, rect); - if (_glyph_text != null) { - drawingContext.draw_glyph_run_anti_alias(_foreground, _glyph_text); + if (_glyph_run != null) { + drawingContext.PushClip(new RectangleGeometry(rect)); + drawingContext.DrawGlyphRun(_foreground, _glyph_run); if (_is_mouse_over_text) { // draw underline - var pt = new Point(Padding.Left, 0.5 * (ActualHeight + _glyph_text.font_size) + 1); - drawingContext.DrawLine(_underline_pen, pt, new Point(pt.X + _glyph_text.width, pt.Y)); + var pt = new Point(Padding.Left, 0.5 * (ActualHeight + _font_size) + 1); + drawingContext.DrawLine(_underline_pen, pt, new Point(pt.X + _glyph_width, pt.Y)); } + drawingContext.Pop(); } var bgo = GetBackgroundOverlay(this); if (bgo != null) @@ -271,11 +292,11 @@ namespace mbts.ui.controls { void _check_mouse_over_text(MouseEventArgs e) { var pt = e.GetPosition(this); bool over_text = - _glyph_text != null && + _glyph_run != null && pt.X >= _padding.Left && - pt.X <= _padding.Left + _glyph_text.width && + pt.X <= _padding.Left + _glyph_width && pt.Y >= _padding.Top && - pt.Y <= _padding.Top + _glyph_text.height; + pt.Y <= _padding.Top + _font_size; if (_is_mouse_over_text != over_text) _toggle_mouse_over_text(); } diff --git a/src/client/spark/view/Controls/DIYGrid/CellRender/PriceCell.cs b/src/client/spark/view/Controls/DIYGrid/CellRender/PriceCell.cs index 115515593..52578cdf8 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellRender/PriceCell.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellRender/PriceCell.cs @@ -1,6 +1,9 @@ using bts.api; using mbts.spark; using mbts.ui.utility.text; +using System; +using System.Collections.Generic; +using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; @@ -15,14 +18,21 @@ namespace mbts.ui.controls { Thickness _padding; HorizontalAlignment _horizontal_alignment; Brush _background, _foreground; - double _font_size, _value; - ContractSpecificationImmutable _spec; + double _glyph_width, _font_size, _value; + GlyphRun _glyph_run; GlyphContext _glyph_context; - GlyphText _glyph_text; + ContractSpecificationImmutable _spec; static PriceCell() => - WPFHelper.override_metadata( - HorizontalAlignmentProperty, FrameworkPropertyMetadataOptions.AffectsRender, (o, v) => o._horizontal_alignment = v, HorizontalAlignment.Left); + WPFHelper.override_metadata( + HorizontalAlignmentProperty, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => { + o._horizontal_alignment = v; + o._glyph_run = null; + }, + HorizontalAlignment.Left); public PriceCell() { _background = Background; @@ -68,7 +78,7 @@ namespace mbts.ui.controls { WPFHelper.prop_changed_callback( (p, v) => { p._glyph_context = p._glyph_context.with(v); - p._set_glyph_text(); + p._glyph_run = null; }))); public FontWeight FontWeight { get => (FontWeight)GetValue(FontWeightProperty); set => SetValue(FontWeightProperty, value); } @@ -85,7 +95,7 @@ namespace mbts.ui.controls { WPFHelper.prop_changed_callback( (p, v) => { p._glyph_context = p._glyph_context.with(v); - p._set_glyph_text(); + p._glyph_run = null; }))); public FontFamily FontFamily { get => GetValue(FontFamilyProperty) as FontFamily; set => SetValue(FontFamilyProperty, value); } @@ -103,7 +113,7 @@ namespace mbts.ui.controls { WPFHelper.prop_changed_callback( (p, v) => { p._font_size = v; - p._set_glyph_text(); + p._glyph_run = null; }))); public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); } @@ -117,7 +127,7 @@ namespace mbts.ui.controls { FrameworkPropertyMetadataOptions.AffectsRender, (p, v) => { p._padding = v; - p._set_glyph_text(); + p._glyph_run = null; }, DefaultCellPadding); public Thickness Padding { get => (Thickness)GetValue(PaddingProperty); set => SetValue(PaddingProperty, value); } @@ -127,10 +137,11 @@ namespace mbts.ui.controls { public static readonly DependencyProperty ValueProperty = WPFHelper.create_dp( nameof(Value), - FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, (p, v) => { p._value = v; - p._set_glyph_text(); + p._glyph_run = null; }, double.NaN); public double Value { get => (double)GetValue(ValueProperty); set => SetValue(ValueProperty, value); } @@ -144,7 +155,7 @@ namespace mbts.ui.controls { FrameworkPropertyMetadataOptions.AffectsRender, (p, v) => { p._spec = v; - p._set_glyph_text(); + p._glyph_run = null; }, SecurityDataHelper.Defaults.ContractSpecificationImmutable); public ContractSpecificationImmutable ContractSpec { @@ -153,18 +164,29 @@ namespace mbts.ui.controls { } #endregion - void _set_glyph_text() => - _glyph_text = _glyph_context.get_glyph_text(_spec.format(_value), _font_size, DesiredSize, _horizontal_alignment, VerticalAlignment.Center, _padding); + protected override Size ArrangeOverride(Size finalSize) { + if (_glyph_run == null) + _set_glyph(finalSize); + return finalSize; + } - protected override Size ArrangeOverride(Size finalSize) => finalSize; - protected override Size MeasureOverride(Size availableSize) => - CellRender.measure_text_cell(this, _glyph_text, _padding, availableSize); + 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)); + } + + void _set_glyph(Size size) { + _glyph_run = _glyph_context.get_glyph_run(_spec.format(_value), _font_size, size, _horizontal_alignment, VerticalAlignment.Center, _padding); + _glyph_width = _glyph_run == null ? 0 : _glyph_run.AdvanceWidths.Sum(); + } protected override void OnRender(DrawingContext drawingContext) { if (_background != null) drawingContext.DrawRectangle(_background, null, new Rect(RenderSize)); - if (_glyph_text != null) - drawingContext.draw_glyph_run_anti_alias(_foreground, _glyph_text); + if (_glyph_run != null) + drawingContext.DrawGlyphRun(_foreground, _glyph_run); } } } diff --git a/src/client/spark/view/Controls/DIYGrid/CellRender/SizeCell.cs b/src/client/spark/view/Controls/DIYGrid/CellRender/SizeCell.cs index 2327bc634..07126eb1b 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellRender/SizeCell.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellRender/SizeCell.cs @@ -1,4 +1,6 @@ using mbts.ui.utility.text; +using System; +using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; @@ -7,19 +9,27 @@ using FontFamily = System.Windows.Media.FontFamily; namespace mbts.ui.controls { public partial class DIYGrid : Control { - class SizeCell : FrameworkElement { + internal class SizeCell : 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 _font_size, _value; + double _glyph_width, _font_size; + ushort _value; + GlyphRun _glyph_run; GlyphContext _glyph_context; - GlyphText _glyph_text; static SizeCell() => WPFHelper.override_metadata( - HorizontalAlignmentProperty, FrameworkPropertyMetadataOptions.AffectsRender, (o, v) => o._horizontal_alignment = v, HorizontalAlignment.Left); + HorizontalAlignmentProperty, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => { + o._horizontal_alignment = v; + o._glyph_run = null; + }, + HorizontalAlignment.Left); public SizeCell() { _background = Background; @@ -53,23 +63,6 @@ namespace mbts.ui.controls { public Brush Foreground { get => (Brush)GetValue(ForegroundProperty); set => SetValue(ForegroundProperty, value); } #endregion - #region FontWeight - public static readonly DependencyProperty FontWeightProperty = - TextElement.FontWeightProperty.AddOwner( - typeof(SizeCell), - new FrameworkPropertyMetadata( - FontWeights.Normal, - FrameworkPropertyMetadataOptions.AffectsMeasure | - FrameworkPropertyMetadataOptions.AffectsRender, - WPFHelper.prop_changed_callback( - (p, v) => { - p._glyph_context = p._glyph_context.with(v); - p._set_glyph_text(); - }))); - - public FontWeight FontWeight { get => (FontWeight)GetValue(FontWeightProperty); set => SetValue(FontWeightProperty, value); } - #endregion - #region FontFamily public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner( @@ -81,12 +74,29 @@ namespace mbts.ui.controls { WPFHelper.prop_changed_callback( (p, v) => { p._glyph_context = p._glyph_context.with(v); - p._set_glyph_text(); + 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(SizeCell), + 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( @@ -99,7 +109,7 @@ namespace mbts.ui.controls { WPFHelper.prop_changed_callback( (p, v) => { p._font_size = v; - p._set_glyph_text(); + p._glyph_run = null; }))); public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); } @@ -113,37 +123,49 @@ namespace mbts.ui.controls { FrameworkPropertyMetadataOptions.AffectsRender, (p, v) => { p._padding = v; - p._set_glyph_text(); + p._glyph_run = null; }, DefaultCellPadding); public Thickness Padding { get => (Thickness)GetValue(PaddingProperty); set => SetValue(PaddingProperty, value); } #endregion - #region Value + #region Size public static readonly DependencyProperty ValueProperty = WPFHelper.create_dp( nameof(Value), - FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, (p, v) => { p._value = v; - p._set_glyph_text(); - }); + p._glyph_run = null; + }, 0); public ushort Value { get => (ushort)GetValue(ValueProperty); set => SetValue(ValueProperty, value); } #endregion - void _set_glyph_text() => - _glyph_text = _glyph_context.get_glyph_text(_value.ToString(), _font_size, DesiredSize, _horizontal_alignment, VerticalAlignment.Center, _padding); + protected override Size ArrangeOverride(Size finalSize) { + if (_glyph_run == null) + _set_glyph(finalSize); + return finalSize; + } - protected override Size ArrangeOverride(Size finalSize) => finalSize; - protected override Size MeasureOverride(Size availableSize) => - CellRender.measure_text_cell(this, _glyph_text, _padding, availableSize); + 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)); + } + + void _set_glyph(Size size) { + _glyph_run = _glyph_context.get_glyph_run(_value.ToString(), _font_size, size, _horizontal_alignment, VerticalAlignment.Center, _padding); + _glyph_width = _glyph_run == null ? 0 : _glyph_run.AdvanceWidths.Sum(); + } protected override void OnRender(DrawingContext drawingContext) { if (_background != null) drawingContext.DrawRectangle(_background, null, new Rect(RenderSize)); - if (_glyph_text != null) - drawingContext.draw_glyph_run_anti_alias(_foreground, _glyph_text); + if (_glyph_run != null) + drawingContext.DrawGlyphRun(_foreground, _glyph_run); } } } diff --git a/src/client/spark/view/Controls/DIYGrid/CellRender/TextCell.cs b/src/client/spark/view/Controls/DIYGrid/CellRender/TextCell.cs index fd82a554d..86e43f13b 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellRender/TextCell.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellRender/TextCell.cs @@ -1,4 +1,6 @@ using mbts.ui.utility.text; +using System; +using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; @@ -13,14 +15,21 @@ namespace mbts.ui.controls { Thickness _padding; HorizontalAlignment _horizontal_alignment; Brush _background, _foreground; - double _font_size; + double _glyph_width, _font_size; string _text; + GlyphRun _glyph_run; GlyphContext _glyph_context; - GlyphText _glyph_text; static TextCell() => WPFHelper.override_metadata( - HorizontalAlignmentProperty, FrameworkPropertyMetadataOptions.AffectsRender, (o, v) => o._horizontal_alignment = v, HorizontalAlignment.Left); + HorizontalAlignmentProperty, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => { + o._horizontal_alignment = v; + o._glyph_run = null; + }, + HorizontalAlignment.Left); public TextCell() { _background = Background; @@ -65,7 +74,7 @@ namespace mbts.ui.controls { WPFHelper.prop_changed_callback( (p, v) => { p._glyph_context = p._glyph_context.with(v); - p._set_glyph_text(); + p._glyph_run = null; }))); public FontFamily FontFamily { get => GetValue(FontFamilyProperty) as FontFamily; set => SetValue(FontFamilyProperty, value); } @@ -82,7 +91,7 @@ namespace mbts.ui.controls { WPFHelper.prop_changed_callback( (p, v) => { p._glyph_context = p._glyph_context.with(v); - p._set_glyph_text(); + p._glyph_run = null; }))); public FontWeight FontWeight { get => (FontWeight)GetValue(FontWeightProperty); set => SetValue(FontWeightProperty, value); } @@ -100,7 +109,7 @@ namespace mbts.ui.controls { WPFHelper.prop_changed_callback( (p, v) => { p._font_size = v; - p._set_glyph_text(); + p._glyph_run = null; }))); public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); } @@ -114,7 +123,7 @@ namespace mbts.ui.controls { FrameworkPropertyMetadataOptions.AffectsRender, (p, v) => { p._padding = v; - p._set_glyph_text(); + p._glyph_run = null; }, DefaultCellPadding); public Thickness Padding { get => (Thickness)GetValue(PaddingProperty); set => SetValue(PaddingProperty, value); } @@ -124,27 +133,39 @@ namespace mbts.ui.controls { public static readonly DependencyProperty TextProperty = WPFHelper.create_dp( nameof(Text), - FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, (p, v) => { p._text = v; - p._set_glyph_text(); + p._glyph_run = null; }); public string Text { get => GetValue(TextProperty) as string; set => SetValue(TextProperty, value); } #endregion - - void _set_glyph_text() => - _glyph_text = _glyph_context.get_glyph_text(_text, _font_size, DesiredSize, _horizontal_alignment, VerticalAlignment.Center, _padding); - protected override Size ArrangeOverride(Size finalSize) => finalSize; - protected override Size MeasureOverride(Size availableSize) => - CellRender.measure_text_cell(this, _glyph_text, _padding, availableSize); + protected override Size ArrangeOverride(Size finalSize) { + if (_glyph_run == null) + _set_glyph(finalSize); + return finalSize; + } + + 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)); + } + + 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(); + } protected override void OnRender(DrawingContext drawingContext) { if (_background != null) drawingContext.DrawRectangle(_background, null, new Rect(RenderSize)); - if (_glyph_text != null) - drawingContext.draw_glyph_run_anti_alias(_foreground, _glyph_text); + if (_glyph_run != null) + drawingContext.DrawGlyphRun(_foreground, _glyph_run); } } } diff --git a/src/client/spark/view/Spark/Shell/OrderBook/OrderBookColumns.cs b/src/client/spark/view/Spark/Shell/OrderBook/OrderBookColumns.cs index b2b96d4e2..401900572 100644 --- a/src/client/spark/view/Spark/Shell/OrderBook/OrderBookColumns.cs +++ b/src/client/spark/view/Spark/Shell/OrderBook/OrderBookColumns.cs @@ -37,6 +37,7 @@ namespace mbts.spark { } static RenderUtils() => _working_order_brush.Freeze(); + public static GlyphContext get_glyph_context(OrderBookRowVM r) => r.is_child ? _child_glyph_context : TextUtils.StandardGlyphContext; public static Brush get_side_brush(OrderBookRowVM r) => r.side == Side.Buy ? BTSColors.MarketData.bid : BTSColors.MarketData.ask; public static BindingBase get_font_weight_binding() => new Binding(nameof(OrderBookRowVM.is_child)) { Converter = _font_weight_converter }; @@ -92,7 +93,7 @@ namespace mbts.spark { : base(Columns.Filled, "Filled Qty", nameof(OrderBookRowVM.filled_quantity), DefaultColumnWidths.Size, filter, DataGridFilterKnownTemplates.UIntMinMax) { } public void draw(OrderBookRowVM row, DrawingContext dc, ColumnGeometry position, double row_height) => - CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), RenderUtils.font_size, row.quantity.ToString(), position, RowHeight, + CellRender.draw_text(dc, RenderUtils.get_glyph_context(row), RenderUtils.font_size, row.filled_quantity.ToString(), position, RowHeight, RenderUtils.get_side_brush(row), null, HorizontalAlignment.Right, VerticalAlignment.Center, DefaultCellPadding); } diff --git a/src/client/spark/view/Spark/Shell/OrderBook/OrderBookView.xaml b/src/client/spark/view/Spark/Shell/OrderBook/OrderBookView.xaml index 3eba612a9..a31f752e1 100644 --- a/src/client/spark/view/Spark/Shell/OrderBook/OrderBookView.xaml +++ b/src/client/spark/view/Spark/Shell/OrderBook/OrderBookView.xaml @@ -12,7 +12,8 @@ xmlns:order="clr-namespace:bts.api.orderman;assembly=spark_api" mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="1080" - FocusManager.IsFocusScope="True"> + FocusManager.IsFocusScope="True" + Background="{StaticResource CommonBackground}"> @@ -54,19 +55,20 @@ - - - - - - - - + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/src/client/spark/view/Spark/Shell/OrderBook/OrderBookView.xaml.cs b/src/client/spark/view/Spark/Shell/OrderBook/OrderBookView.xaml.cs index 59b3f1ef8..a409a64fd 100644 --- a/src/client/spark/view/Spark/Shell/OrderBook/OrderBookView.xaml.cs +++ b/src/client/spark/view/Spark/Shell/OrderBook/OrderBookView.xaml.cs @@ -8,11 +8,10 @@ namespace mbts.spark { public const string SubTypeFilterTemplateKey = "SubTypeFilterTemplate", ExpandCollapseToggleStyleKey = "ExpandCollapseToggleStyle"; - public const double RowHeight = 18; + public const double RowHeight = 17; OrderBookVM _vm; //DataGridEx _grid; public OrderBookView(OrderBookVM vm) { - UseLayoutRounding = true; InitializeComponent(); _vm = vm; DataContext = vm; diff --git a/src/client/spark/view/Spark/Shell/OrderBook/OrderBookWindow.cs b/src/client/spark/view/Spark/Shell/OrderBook/OrderBookWindow.cs index 31fc5f178..b05667103 100644 --- a/src/client/spark/view/Spark/Shell/OrderBook/OrderBookWindow.cs +++ b/src/client/spark/view/Spark/Shell/OrderBook/OrderBookWindow.cs @@ -10,7 +10,7 @@ using System.Windows; using System.Windows.Media; namespace mbts.spark { - internal class OrderBookWindow : SparkWindow { + internal class OrderBookWindow : SparkWindow, ISharedWindow { const string _window_type = "OrderBook", ORDERBOOK_THREAD_ID = "OrderBook"; OrderBookWindowFactory.OrderBookStartupData _state; @@ -32,7 +32,6 @@ namespace mbts.spark { _model = model; _factory = factory; _state = state; - thread_init(); } public override OrderBookWindowFactory.OrderBookStartupData SaveState => _state = diff --git a/src/client/spark/view/Utility/GlyphContext.cs b/src/client/spark/view/Utility/GlyphContext.cs index 21b976dc4..2de1d2723 100644 --- a/src/client/spark/view/Utility/GlyphContext.cs +++ b/src/client/spark/view/Utility/GlyphContext.cs @@ -8,21 +8,6 @@ using System.Windows; using System.Windows.Media; namespace mbts.ui.utility.text { - // thin wrapper around a GlyphRun that stores useful size/alignment info - class GlyphText { - public readonly GlyphRun run; - public readonly double width, height, font_size; - public readonly Rect alignment_box; - - public GlyphText(GlyphRun run, double width, double font_size) { - this.run = run; - this.width = width; - this.font_size = font_size; - height = font_size; - alignment_box = new Rect(0, 0, width, height); - } - } - /// /// Reduces the performance overhead of creating GlyphRuns of a particular GlyphTypeface by mapping all chars to glyph indices/widths upon construction. /// Note: Useful in situations where Glyphtypeface will never or rarely change. The overhead, in this case, is spent creating the map in the constructor. /// @@ -80,31 +65,13 @@ namespace mbts.ui.utility.text { return new GlyphRun(_glyph_typeface, 0, false, font_size, HighDpiExtensions.fdpi_ratio, glyph_indices, origin, advance_widths, null, null, null, null, null, null); } - public GlyphText get_glyph_text(string text, double font_size, Size available_size, HorizontalAlignment x_alignment, VerticalAlignment y_alignment, Thickness padding) => - get_glyph_text(text, font_size, available_size.Width, available_size.Height, x_alignment, y_alignment, padding); + public GlyphRun get_glyph_run(string text, double font_size, Size available_size, HorizontalAlignment x_alignment, VerticalAlignment y_alignment, Thickness padding) => + get_glyph_run(text, font_size, available_size.Width, available_size.Height, x_alignment, y_alignment, padding); - public GlyphText get_glyph_text(string text, double font_size, double available_width, double available_height, HorizontalAlignment x_alignment, VerticalAlignment y_alignment, Thickness padding) => - get_glyph_text(text, font_size, 0, 0, available_width, available_height, x_alignment, y_alignment, padding); + public GlyphRun get_glyph_run(string text, double font_size, double available_width, double available_height, HorizontalAlignment x_alignment, VerticalAlignment y_alignment, Thickness padding) => + get_glyph_run(text, font_size, 0, 0, available_width, available_height, x_alignment, y_alignment, padding); - public GlyphText get_glyph_text( - string text, double font_size, double available_width, double available_height, - HorizontalAlignment x_alignment, double x_offset, VerticalAlignment y_alignment, double y_offset) { - // - if (string.IsNullOrEmpty(text)) - return null; - var glyph_indices = new ushort[text.Length]; - var advance_widths = new double[text.Length]; - if (!_try_get_glyph_params(text, font_size, ref glyph_indices, ref advance_widths, out double glyph_width)) - return null; - var origin = _calculate_glyph_run_origin(font_size, available_width, available_height, glyph_width, x_alignment, x_offset, y_alignment, y_offset); - return new GlyphText( - new GlyphRun(_glyph_typeface, 0, false, font_size, HighDpiExtensions.fdpi_ratio, glyph_indices, origin, advance_widths, null, null, null, null, null, null), - glyph_width, font_size); - } - - // since no clipping/resizinig is done at this level, the appropriate padding property with respect to x_alignment is translated into a x_offset. - // y_alignment will be treated in the same manner (see the switch-cases code for details). - public GlyphText get_glyph_text( + public GlyphRun get_glyph_run( string text, double font_size, double left, double top, double right, double bottom, HorizontalAlignment x_alignment, VerticalAlignment y_alignment, Thickness padding) { // @@ -115,49 +82,9 @@ namespace mbts.ui.utility.text { if (!_try_get_glyph_params(text, font_size, ref glyph_indices, ref advance_widths, out double glyph_width)) return null; var origin = _calculate_glyph_run_origin(font_size, left, top, right, bottom, glyph_width, x_alignment, y_alignment, padding); - return new GlyphText( - new GlyphRun(_glyph_typeface, 0, false, font_size, HighDpiExtensions.fdpi_ratio, glyph_indices, origin, advance_widths, null, null, null, null, null, null), - glyph_width, font_size); + return new GlyphRun(_glyph_typeface, 0, false, font_size, HighDpiExtensions.fdpi_ratio, glyph_indices, origin, advance_widths, null, null, null, null, null, null); } - public IEnumerable get_glyph_text_lines(IReadOnlyList lines, - double font_size, double line_spacing, double available_width, double available_height, - HorizontalAlignment x_alignment, double x_offset, VerticalAlignment v_alignment, double y_offset) { - // ultimately, each line with will be formatted with "Top" alignment, - // if v_alignment is Center/Bottom, measure text height and adjust y_offset to effectively align the text accordingly. - switch (v_alignment) { - case VerticalAlignment.Center: { - double text_height = get_text_height(); - double adjustment = 0.5 * (available_height - text_height); - y_offset += adjustment; - } - break; - case VerticalAlignment.Bottom: { - double text_height = get_text_height(); - double adjustment = available_height - text_height; - y_offset += adjustment; - } - break; - } - - foreach (string l in lines) { - yield return get_glyph_text(l, font_size, available_width, available_height, x_alignment, x_offset, VerticalAlignment.Top, y_offset); - y_offset += line_spacing + font_size; - } - double get_text_height() => - lines.Count * font_size + (lines.Count - 1) * line_spacing; - } - - public double measure_width(string text, double font_size) { - double glyph_width = 0; - foreach (char c in text) { - if (!_char_map.TryGetValue(c, out (ushort index, double width) gc)) - return double.NaN; - glyph_width += gc.width * font_size; - } - return glyph_width; - } - static Point _calculate_glyph_run_origin( double font_size, double available_width, double available_height, double glyph_width, HorizontalAlignment x_alignment, double x_offset, VerticalAlignment y_alignment, double y_offset ) { diff --git a/src/client/spark/view/Utility/TextUtils.cs b/src/client/spark/view/Utility/TextUtils.cs index c20f1144a..9bc84615f 100644 --- a/src/client/spark/view/Utility/TextUtils.cs +++ b/src/client/spark/view/Utility/TextUtils.cs @@ -73,28 +73,6 @@ namespace mbts.ui.utility.text { public static FontWeight from_open_type_weight_safe(int weight_value) => FontWeight.FromOpenTypeWeight(MathUtils.within_range(1, 999, weight_value)); - // https://stackoverflow.com/questions/4095750/wpf-4-0-drawing-aliased-formattedtext-glyphrun - public static GuidelineSet get_anti_alias_guidelines(Rect alignment_box) { - double half_stroke_thickness = 0.5; - // Create a guidelines set - GuidelineSet guidelines = new GuidelineSet(); - guidelines.GuidelinesX.Add(alignment_box.Left + half_stroke_thickness); - guidelines.GuidelinesX.Add(alignment_box.Right + half_stroke_thickness); - guidelines.GuidelinesY.Add(alignment_box.Top + half_stroke_thickness); - guidelines.GuidelinesY.Add(alignment_box.Bottom + half_stroke_thickness); - return guidelines; - } - - public static void draw_glyph_run_anti_alias(this DrawingContext dc, Brush foreground, GlyphText glyph_text) => - _draw_glyph_run_anti_alias(dc, foreground, glyph_text.run, glyph_text.alignment_box); - - static void _draw_glyph_run_anti_alias(DrawingContext dc, Brush foreground, GlyphRun glyph_run, Rect alignment_box) { - //var anti_alias_guidelines = get_anti_alias_guidelines(alignment_box); - //dc.PushGuidelineSet(anti_alias_guidelines); - dc.DrawGlyphRun(foreground, glyph_run); - //dc.Pop(); - } - public static string InsertText(string current_text, string insertion_text, int insertion_index, int selection_start, int selection_length) { if (selection_length > 0) current_text = current_text.Remove(selection_start, selection_length);