From 410e17aec5abc003189750b600720392f0d70571 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 5 Jun 2018 15:37:17 -0400 Subject: [PATCH 1/3] SPARK-1204 Ladder/Badge - row height needs to fit different badge styles --- .../Modules/Trading/LadderWindowFactory.cs | 1 + .../core/GlobalExtensions/ObjectHelper.cs | 28 ++ src/client/spark/view/Controls/Badge.cs | 287 +++++++++++------- .../spark/view/Spark/Core/OrderUtils.cs | 9 + .../spark/view/Spark/Ladder/LadderOrderVM.cs | 69 +---- .../view/Spark/Ladder/LadderSettingsVM.cs | 38 ++- .../view/Spark/Ladder/LadderSettingsView.xaml | 22 +- .../spark/view/Spark/Ladder/LadderView.xaml | 11 +- .../spark/view/Spark/Ladder/LaderRender.cs | 48 ++- src/client/spark/view/Utility/GlyphContext.cs | 69 ++++- .../spark/view/Utility/IVisualContentRect.cs | 185 +++++++++++ src/client/spark/view/Utility/WPFHelper.cs | 92 ++++++ 12 files changed, 652 insertions(+), 207 deletions(-) create mode 100644 src/client/spark/view/Utility/IVisualContentRect.cs diff --git a/src/client/spark/Modules/Trading/LadderWindowFactory.cs b/src/client/spark/Modules/Trading/LadderWindowFactory.cs index c1d1234e8..2d5e32ec0 100644 --- a/src/client/spark/Modules/Trading/LadderWindowFactory.cs +++ b/src/client/spark/Modules/Trading/LadderWindowFactory.cs @@ -77,6 +77,7 @@ namespace bts.shell { s.ask_break_line = LadderSettings.Default.ask_break_line; if (s.mid_break_line == null) s.mid_break_line = LadderSettings.Default.mid_break_line; + s.row_height = LadderRender.measure_row_height(s); return s; } } diff --git a/src/client/spark/core/GlobalExtensions/ObjectHelper.cs b/src/client/spark/core/GlobalExtensions/ObjectHelper.cs index 8d78666f0..0f55e3163 100644 --- a/src/client/spark/core/GlobalExtensions/ObjectHelper.cs +++ b/src/client/spark/core/GlobalExtensions/ObjectHelper.cs @@ -54,5 +54,33 @@ rv |= try_convert(args[1], out arg2); return rv; } + + public static bool try_convert_args(object[] args, out T1 arg1, out T2 arg2, out T3 arg3) { + if (args == null || args.Length != 3) { + arg1 = default; + arg2 = default; + arg3 = default; + return false; + } + bool rv = try_convert(args[0], out arg1); + rv |= try_convert(args[1], out arg2); + rv |= try_convert(args[2], out arg3); + return rv; + } + + public static bool try_convert_args(object[] args, out T1 arg1, out T2 arg2, out T3 arg3, out T4 arg4) { + if (args == null || args.Length != 4) { + arg1 = default; + arg2 = default; + arg3 = default; + arg4 = default; + return false; + } + bool rv = try_convert(args[0], out arg1); + rv |= try_convert(args[1], out arg2); + rv |= try_convert(args[2], out arg3); + rv |= try_convert(args[3], out arg4); + return rv; + } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/Badge.cs b/src/client/spark/view/Controls/Badge.cs index 8782d2e87..7369f4934 100644 --- a/src/client/spark/view/Controls/Badge.cs +++ b/src/client/spark/view/Controls/Badge.cs @@ -1,141 +1,204 @@ -using mbts.spark; -using mbts.ui.extensions; +using bts.api; +using mbts.spark; +using mbts.ui.utility.text; using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Globalization; using System.Windows; using System.Windows.Controls; -using System.Windows.Input; +using System.Windows.Data; using System.Windows.Media; -using static mbts.config.Configuration; +using FontFamily = System.Windows.Media.FontFamily; namespace mbts.ui.controls { - public class Badge: Control { - readonly static double _dpi = get().dpi_ratio; + public class Badge : Control { + public const double LINE_SPACING = 1; + // Storing the defaults here for readability + // Warning: these properties are used to calculate the ladder's row_height. Please change the values below if you want to change properties that affect the layout. + // ** - value is not the actual PropertyMetadata.DefaultValue + public static readonly Thickness + DefaultMargin = new Thickness(4, 2, 4, 2), // ** + DefaultPadding = new Thickness(1), + DefaultBorderThickness = new Thickness(2); + public static readonly double + DefaultCornerRadius = 1, + DefaultFontSize = 10, + DefaultWidth = 63; // ** + public static readonly FontFamily DefaultFontFamily = new FontFamily("Consolas"); + + readonly GlyphContentRect _gcr = new GlyphContentRect(); + Brush _background, _foreground, _border_brush; - Typeface _face; - FormattedText _center_text; - FormattedText _top_text; - FormattedText _bottom_text; static Badge() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(typeof(Badge))); - WidthProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(15.0)); - HeightProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(10.0)); - FontSizeProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(10.0, FrameworkPropertyMetadataOptions.AffectsRender /*| FrameworkPropertyMetadataOptions.AffectsMeasure*/, update_text)); - ForegroundProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(Brushes.White, update_text)); - BackgroundProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender)); - BorderThicknessProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(new Thickness(1))); - BorderBrushProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(Brushes.Black)); - FontFamilyProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(new FontFamily("Consolas"), FrameworkPropertyMetadataOptions.AffectsRender /*| FrameworkPropertyMetadataOptions.AffectsMeasure*/, update_text)); - - //TextProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, update_text)); - //TopTextProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, update_text)); - //BottomTextProperty.OverrideMetadata(typeof(Badge), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, update_text)); + // MinWidth + WPFHelper.override_metadata( + MinWidthProperty, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._gcr.min_width = v, + 0); + // Width + WPFHelper.override_metadata( + WidthProperty, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._gcr.width = v, + double.NaN); + // Max Width + WPFHelper.override_metadata( + MaxWidthProperty, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._gcr.max_width = v, + double.MaxValue); + // Min Height + WPFHelper.override_metadata( + MinHeightProperty, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._gcr.min_height = v, + 0); + // Height + WPFHelper.override_metadata( + HeightProperty, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._gcr.height = v, + double.NaN); + // Max Height + WPFHelper.override_metadata( + MaxHeightProperty, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._gcr.max_height = v, + double.MaxValue); + // FontSize + WPFHelper.override_metadata( + FontSizeProperty, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._gcr.font_size = v, + DefaultFontSize); + // Foreground + WPFHelper.override_metadata( + ForegroundProperty, + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._foreground = v, + Brushes.White); + // Background + WPFHelper.override_metadata( + BackgroundProperty, + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._background = v, + Brushes.Transparent); + // BorderThickness + WPFHelper.override_metadata( + BorderThicknessProperty, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._gcr.border_thickness = v, + DefaultBorderThickness); + // BorderBrush + WPFHelper.override_metadata( + BorderBrushProperty, + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._border_brush = v, + Brushes.Black); + // FontFamily + WPFHelper.override_metadata( + FontFamilyProperty, + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._gcr.glyph_context = b._gcr.glyph_context.with(v), + DefaultFontFamily); + // HoriontalAlignment + WPFHelper.override_metadata( + HorizontalAlignmentProperty, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._gcr.horizontal_alignment = v, + HorizontalAlignment.Stretch); + // VerticalAlignment + WPFHelper.override_metadata( + VerticalAlignmentProperty, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._gcr.vertical_alignment = v, + VerticalAlignment.Center); + // Padding + WPFHelper.override_metadata( + PaddingProperty, + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (p, v) => p._gcr.padding = v, + DefaultPadding); } - public string Text { - get { return (string)GetValue(TextProperty); } - set { SetValue(TextProperty, value); } - } - public static readonly DependencyProperty TextProperty = - DependencyProperty.Register("Text", typeof(string), typeof(Badge), new PropertyMetadata(string.Empty, - format_text((b,v) => b._center_text= v))); - - - public string TopText { - get { return (string)GetValue(TopTextProperty); } - set { SetValue(TopTextProperty, value); } - } - public static readonly DependencyProperty TopTextProperty = - DependencyProperty.Register(nameof(TopText), typeof(string), typeof(Badge), new PropertyMetadata(string.Empty, - format_text((b, v) => b._top_text = v))); - - public string BottomText { - get { return (string)GetValue(BottomTextProperty); } - set { SetValue(BottomTextProperty, value); } + public Badge() { + _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.vertical_alignment = VerticalAlignment; + _background = Background; + _foreground = Foreground; + _border_brush = BorderBrush; + _gcr.font_size = FontSize; + _gcr.text_lines = TextLines; + _gcr.glyph_context = GlyphContext.create(FontFamily, FontWeight); } - public static readonly DependencyProperty BottomTextProperty = - DependencyProperty.Register(nameof(BottomText), typeof(string), typeof(Badge), new PropertyMetadata(string.Empty, - format_text((b,v)=>b._bottom_text=v))); + public static readonly DependencyProperty TextLinesProperty = + WPFHelper.create_dp, Badge>( + nameof(TextLines), + FrameworkPropertyMetadataOptions.AffectsMeasure | + FrameworkPropertyMetadataOptions.AffectsRender, + (b, v) => b._gcr.text_lines = v, + new string[0]); + + public IReadOnlyList TextLines { + get => GetValue(TextLinesProperty) as IReadOnlyList; + set => SetValue(TextLinesProperty, value); + } public double CornerRadius { get { return (double)GetValue(CornerRadiusProperty); } set { SetValue(CornerRadiusProperty, value); } } + public static readonly DependencyProperty CornerRadiusProperty = - DependencyProperty.Register("CornerRadius", typeof(double), typeof(Badge), new PropertyMetadata(3.0)); + WPFHelper.create_dp(nameof(CornerRadius), DefaultCornerRadius); - static PropertyChangedCallback format_text(Action f) { - return (d, e) => { - Badge b = (Badge)d; - ensure_fontface(b); - string text = e.NewValue as string; - if (string.IsNullOrEmpty(text)) f(b, null); - f(b, new FormattedText(text, - System.Globalization.CultureInfo.CurrentCulture, - FlowDirection.LeftToRight, - b._face, b.FontSize, b.Foreground, _dpi)); - b.InvalidateVisual(); - }; + protected override Size ArrangeOverride(Size finalSize) => finalSize; + + protected override Size MeasureOverride(Size availableSize) { + _gcr.available_size = availableSize; + return _gcr.layout_size_plus_margin(Margin); } - static FormattedText format_text(Badge badge, DependencyProperty source) { - FormattedText result = null; - var value = badge.GetValue(source); - format_text((b, v) => result = v)(badge, new DependencyPropertyChangedEventArgs(source, value, value)); - return result; - } - - static void update_text(DependencyObject d, DependencyPropertyChangedEventArgs args) { - // Force formatted text regeneration when font/color changes - Badge badge = (Badge)d; - badge._face = null; - ensure_fontface(badge); - badge._center_text = format_text(badge, TextProperty); - badge._top_text = format_text(badge, TopTextProperty); - badge._bottom_text = format_text(badge, BottomTextProperty); - } - - private static void ensure_fontface(Badge b) { - b._face = b._face ?? new Typeface(b.FontFamily, b.FontStyle, b.FontWeight, b.FontStretch); - } - - //protected override Size MeasureOverride(Size availableSize) { - // Double calc_height = 0.0; - // Double calc_width = 0.0; - // Size size = new Size(0, 0); - - // Size center_size = new Size(_center_text.Width, _center_text.Height); - // Size top_size = new Size(_top_text.Width, _top_text.Height); - // Size bottom_size = new Size(_bottom_text.Width, _bottom_text.Height); - - // calc_width = Math.Max(center_size.Width, top_size.Width); - // calc_width = Math.Max(calc_width, bottom_size.Width); - - // calc_height += center_size.Height; - // calc_height += top_size.Height; - // calc_height += bottom_size.Height; - - // size.Width = calc_width; - // size.Height = calc_height; - - // return size; - - //} protected override void OnRender(DrawingContext dc) { base.OnRender(dc); double border_thickness = BorderThickness.Top; var pen = new Pen(BorderBrush, border_thickness); - dc.DrawRoundedRectangle(Background, pen, new Rect(0, 0 + border_thickness, ActualWidth, ActualHeight - 2 * border_thickness), CornerRadius, CornerRadius); - if (_center_text != null) - dc.DrawText(_center_text, new Point((ActualWidth-_center_text.Width)/2, (ActualHeight-_center_text.Height)/2)); - if (_top_text != null) - dc.DrawText(_top_text, new Point(border_thickness, border_thickness)); - if (_bottom_text != null) - dc.DrawText(_bottom_text, new Point(border_thickness, ActualHeight - _bottom_text.Height - border_thickness)); + dc.DrawRoundedRectangle(Background, pen, new Rect(0, border_thickness, _gcr.layout_size.Width, _gcr.layout_size.Height), CornerRadius, CornerRadius); + var gr = _gcr.glyph_run; + if (gr != null) + dc.DrawGlyphRun(_foreground, gr); } - } + } + + class BadgeTextLinesConverter : IMultiValueConverter { + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { + if (ObjectHelper.try_convert_args(values, out Side side, out uint quantity, out uint filled, out mbts.spark.LadderSettings.BadgeStyle style)) + return BadgeRender.format(side, quantity, filled, style); + else return new string[0]; + } + + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotImplementedException(); + } } \ No newline at end of file diff --git a/src/client/spark/view/Spark/Core/OrderUtils.cs b/src/client/spark/view/Spark/Core/OrderUtils.cs index 241f681af..c2ce9379c 100644 --- a/src/client/spark/view/Spark/Core/OrderUtils.cs +++ b/src/client/spark/view/Spark/Core/OrderUtils.cs @@ -149,6 +149,15 @@ namespace mbts.spark { } } + public static string abbreviate(this Side side) { + switch (side) { + case Side.Buy: return "B"; + case Side.Sell: return "S"; + case Side.Undefined: + default: return "E"; + } + } + public static string order_sub_type_display_name(OrderSubType sub_type, bool use_long_name) { switch(sub_type) { case OrderSubType.HedgingLeg: diff --git a/src/client/spark/view/Spark/Ladder/LadderOrderVM.cs b/src/client/spark/view/Spark/Ladder/LadderOrderVM.cs index 4e3f92183..7689ae6f8 100644 --- a/src/client/spark/view/Spark/Ladder/LadderOrderVM.cs +++ b/src/client/spark/view/Spark/Ladder/LadderOrderVM.cs @@ -60,16 +60,14 @@ namespace mbts.spark { FontWeight get_font_weight(LadderSettings s) => ui.utility.text.TextUtils.from_open_type_weight_safe(s.orders_column.font_weight); double get_font_size(LadderSettings s) => s.orders_column.font_size; Brush get_foreground(LadderSettings s) => s.orders_column.foreground.rgb_brush(); - _text = _bottom_line = _top_line = string.Empty; + // + add_order(o); _dispose = Disposable.Create( - _bs.Subscribe( badge => { + _bs.Subscribe(badge => { _current_badge_style = badge; _set_badge_text(); }) ); - - // - add_order(o); } @@ -98,18 +96,8 @@ namespace mbts.spark { } private void _set_badge_text() { - if (_orders.Count > 0) { - switch (_current_badge_style) { - case LadderSettings.BadgeStyle.Alternate: - //text = $"{_buy_sell_indicator}:{filled} - top_line = $"{_buy_sell_indicator}:{filled}"; - bottom_line = $"W:{ working_quantity}"; - break; - case LadderSettings.BadgeStyle.Normal: - default: - text = $"{filled}/{quantity}"; break; - } - } + text_lines = BadgeRender.format(side, quantity, filled, _current_badge_style); + RaisePropertyChanged(nameof(text_lines)); } public void add_order(OrderRecordImmutable new_order) { @@ -137,54 +125,9 @@ namespace mbts.spark { } } - string _buy_sell_indicator { - get { - switch (side) { - case Side.Buy: return "B"; - case Side.Sell: return "S"; - case Side.Undefined: - default: - return "E"; - } - } - } - IEnumerable _parent_and_child_not_sharing_slot() => _orders.Values.Where(o => !_orders.ContainsKey(o.ParentKey)); - private string _text; - public string text { - get { return _text; } - private set { - _text = value; - if (value != string.Empty) { - top_line = string.Empty; - bottom_line = string.Empty; - } - RaisePropertyChanged(); } - } - - private string _top_line; - public string top_line { - get { return _top_line; } - private set { - _top_line = value; - if (value != string.Empty) { - text = string.Empty; - } - RaisePropertyChanged(); - } - } - private string _bottom_line; - public string bottom_line { - get { return _bottom_line; } - private set { - _bottom_line = value; - if (value != string.Empty) { - text = string.Empty; - } - RaisePropertyChanged(); - } - } + public IReadOnlyList text_lines { get; private set; } public string summary => _orders.Values.Select(order => order.short_description(_parent.security.ContractSpec)).Aggregate((s1, s2) => $"{s1}{Environment.NewLine}{Environment.NewLine}{s2}"); public string description => _orders.Values.Select(order => order.full_description(_parent.security)).Aggregate((s1, s2) => $"{s1}{Environment.NewLine}{Environment.NewLine}{s2}"); diff --git a/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs b/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs index 8807a38d1..083224c94 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs +++ b/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs @@ -1,8 +1,8 @@ -using bts.spark; -using mbts.spark.utilities; +using bts.api; using mbts.ui; using mbts.ui.controls.color_picker; using mbts.ui.shell; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -14,6 +14,7 @@ namespace mbts.spark { class LadderSettingsVM : ViewModelBase, IWindowSettingsVM { readonly Property _badge_style; readonly Property _alternate_order_size; + readonly DelegateProperty _row_height; LadderSettings _current; LadderColumnSettingsVM _orders = new OrdersColumnSettingsVM(), @@ -30,6 +31,13 @@ namespace mbts.spark { _badge_style = backing(nameof(badge_style)); _badge_style.Changed += _badge_style_Changed; _alternate_order_size = backing(nameof(alternate_order_size)); + _row_height = backing(nameof(row_height), () => LadderRender.measure_row_height(get_result())); + _orders.font_size_changed += on_font_size_changed; + _bid.font_size_changed += on_font_size_changed; + _price.font_size_changed += on_font_size_changed; + _ask.font_size_changed += on_font_size_changed; + _last.font_size_changed += on_font_size_changed; + void on_font_size_changed(double oldval, double newval) => _row_height.refresh(); } private void _badge_style_Changed(LadderSettings.BadgeStyle oldval, LadderSettings.BadgeStyle newval) { @@ -38,6 +46,7 @@ namespace mbts.spark { } else if (newval == LadderSettings.BadgeStyle.Normal && _orders.font_size == 7) { _orders.font_size = 10; } + _row_height.refresh(); } public LadderSettings.BadgeStyle badge_style { @@ -50,6 +59,8 @@ namespace mbts.spark { set => _alternate_order_size.Value = value; } + public double row_height => _row_height.Value; + public IEnumerable column_items { get { yield return _orders; @@ -89,7 +100,6 @@ namespace mbts.spark { } public LadderSettings get_result() { - const double row_padding = 12; var clone = SparkUtils.clone_proto(_current); clone.badge_style = badge_style; clone.alternate_order_size = alternate_order_size; @@ -101,13 +111,7 @@ namespace mbts.spark { clone.bid_break_line = _bid_mkt_line.get_settings(); clone.ask_break_line = _ask_mkt_line.get_settings(); clone.mid_break_line = _mid_mkt_line.get_settings(); - clone.row_height = new[] { - clone.orders_column, - clone.bid_column, - clone.price_column, - clone.ask_column, - clone.last_column - }.Max(cs => cs.font_size) + row_padding; + clone.row_height = LadderRender.measure_row_height(clone); return clone; } @@ -131,6 +135,7 @@ namespace mbts.spark { _background = backing(nameof(background), Colors.White); _font_display_name = backing(nameof(font_display_name), () => TextUtils.display_name(font_family, font_weight, font_size), nameof(font_family), nameof(font_weight), nameof(font_size)); + _font_size.Changed += (oldval, newval) => font_size_changed?.Invoke(oldval, newval); } public string name { get; } @@ -145,6 +150,7 @@ namespace mbts.spark { public Color background { get => _background.Value; set => _background.Value = value; } public ObservableCollection foreground_palettes { get; } public ObservableCollection background_palettes { get; } + public event Action font_size_changed; public virtual string sample_text => "999-56789.01234"; @@ -176,9 +182,15 @@ namespace mbts.spark { } public class OrdersColumnSettingsVM : LadderColumnSettingsVM, ITemplateSelectorKey { - public OrdersColumnSettingsVM() : base("Orders") { } + public OrdersColumnSettingsVM() : base("Orders") { + sample_filled = 12; + sample_quantity = 50; + sample_side = Side.Buy; + } - public override string sample_text => "12/50"; + public uint sample_quantity { get; } + public uint sample_filled { get; } + public Side sample_side { get; } public Brush badge_border { get; } = new SolidColorBrush(BTSColors.MarketData.ask); public Brush badge_background { get; } = Brushes.White; @@ -213,4 +225,4 @@ namespace mbts.spark { }; } } -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml index 73b80eee7..ff513fe18 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml @@ -9,8 +9,10 @@ xmlns:usp="clr-namespace:mbts.ui.spark" xmlns:txt="clr-namespace:mbts.ui.utility.text" TextElement.FontSize="{x:Static u:FontSizes.Small}" + UseLayoutRounding="True" > + @@ -28,18 +30,24 @@ + Margin="{x:Static c:Badge.DefaultMargin}" + Width="{x:Static c:Badge.DefaultWidth}" + > + + + + + + + + + @@ -82,7 +90,7 @@ - + diff --git a/src/client/spark/view/Spark/Ladder/LadderView.xaml b/src/client/spark/view/Spark/Ladder/LadderView.xaml index c9c0bd8b1..3a4a0b813 100644 --- a/src/client/spark/view/Spark/Ladder/LadderView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderView.xaml @@ -448,15 +448,10 @@ - - - - - - - + + + - diff --git a/src/client/spark/view/Spark/Ladder/LaderRender.cs b/src/client/spark/view/Spark/Ladder/LaderRender.cs index dcd0291fe..410fb7eff 100644 --- a/src/client/spark/view/Spark/Ladder/LaderRender.cs +++ b/src/client/spark/view/Spark/Ladder/LaderRender.cs @@ -1,20 +1,66 @@ using bts.api; using mbts.co2; using mbts.ui; +using mbts.ui.controls; using mbts.ui.utility.text; using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; namespace mbts.spark { + static class BadgeRender { + // These fields are used outside of Badge.cs, to calculate row height. + // Until we integrate these values into the LadderSettings, + // Badges MUST USE these values for Margin, BorderThickness, and Padding, + // else row_height calculations become invalid (see "LadderRender.measure_row_height"). + + // IMPORTANT: the return values for this method MUST BE IN SYNC with the "format" method, else "measure_height" and "LadderRender.measure_row_height" will be broken. + static int _line_count_for_style(LadderSettings.BadgeStyle badge_style) => badge_style == LadderSettings.BadgeStyle.Alternate ? 2 : 1; + // IMPORTANT: at the risk of being redundant, see above comment. + public static IReadOnlyList format(Side side, uint quantity, uint filled, LadderSettings.BadgeStyle style) { + switch (style) { + case LadderSettings.BadgeStyle.Alternate: + return new[] { + $"{side.abbreviate()}:{filled}", + $"W:{quantity - filled}" + }; + default: return new[] { $"{filled}/{quantity}" }; + } + } + + public static double measure_height(LadderSettings.BadgeStyle badge_style, Thickness border_thickness, Thickness padding, double font_size) => + measure_height(_line_count_for_style(badge_style), border_thickness, padding, font_size); + + public static double measure_height(int line_count, Thickness border_thickness, Thickness padding, double font_size) => + WPFHelper.control_base_height(border_thickness, padding) + font_size * line_count + Badge.LINE_SPACING * (line_count - 1); + } + static class LadderRender { + // Note: the orders-column has a separate "row_padding" value: Badge.DefaultMargin + // (see how it is incorporated in measure_row_height() below) + public static readonly Thickness row_padding = new Thickness(6); + + // IMPORTANT: this method ONLY WORKS if Badges use the BadgeRender.margin/border_thickness/padding values. + // TODO: elminate the dependency on BadgeRender.margin/border_thickness/padding by integrating them into settings. + public static double measure_row_height(LadderSettings settings) => + Math.Max(new[] { + settings.bid_column.font_size, + settings.price_column.font_size, + settings.ask_column.font_size, + settings.last_column.font_size + }.Max() + row_padding.Top + row_padding.Bottom, + BadgeRender.measure_height( + settings.badge_style, Badge.DefaultBorderThickness, Badge.DefaultPadding, settings.orders_column.font_size + ) + Badge.DefaultMargin.Top + Badge.DefaultMargin.Bottom); + public static int index_of(double[] prices, double price, IComparer comparer) => Array.BinarySearch(prices, price, comparer); public static GlyphRun format_text(string text, int row, double row_height, LadderColumnSettings settings, GlyphContext glyph_context) => - glyph_context.get_glyph_run(text, settings.font_size, settings.width, row_height, HorizontalAlignment.Center, 0, VerticalAlignment.Center, row * row_height); + glyph_context.get_glyph_run(text, settings.font_size, settings.width, row_height, HorizontalAlignment.Center, 0, VerticalAlignment.Center, row * row_height, true); public static double[] gen_prices(double mid, int row_count, ContractSpecificationImmutable spec) { // require odd number diff --git a/src/client/spark/view/Utility/GlyphContext.cs b/src/client/spark/view/Utility/GlyphContext.cs index 2de1d2723..8a6feda20 100644 --- a/src/client/spark/view/Utility/GlyphContext.cs +++ b/src/client/spark/view/Utility/GlyphContext.cs @@ -53,7 +53,7 @@ namespace mbts.ui.utility.text { public GlyphRun get_glyph_run( string text, double font_size, double available_width, double available_height, - HorizontalAlignment x_alignment, double x_offset, VerticalAlignment y_alignment, double y_offset) { + HorizontalAlignment x_alignment, double x_offset, VerticalAlignment y_alignment, double y_offset, bool use_layout_rounding = false) { // if (string.IsNullOrEmpty(text)) return null; @@ -61,7 +61,7 @@ namespace mbts.ui.utility.text { 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); + var origin = _calculate_glyph_run_origin(font_size, available_width, available_height, glyph_width, x_alignment, x_offset, y_alignment, y_offset, use_layout_rounding); return new GlyphRun(_glyph_typeface, 0, false, font_size, HighDpiExtensions.fdpi_ratio, glyph_indices, origin, advance_widths, null, null, null, null, null, null); } @@ -87,7 +87,7 @@ namespace mbts.ui.utility.text { 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 ) { + HorizontalAlignment x_alignment, double x_offset, VerticalAlignment y_alignment, double y_offset, bool use_layout_rounding = false) { // double x, y; switch (x_alignment) { @@ -104,6 +104,10 @@ namespace mbts.ui.utility.text { default: y = available_height - 0.5 * (available_height - font_size); break; } y += y_offset; + if (use_layout_rounding) { + x = Math.Round(x); + y = Math.Round(y); + } return new Point(x, y); } @@ -148,6 +152,65 @@ namespace mbts.ui.utility.text { return true; } + #region MultiLine + public bool arrange_glyph_run( + IReadOnlyList lines, double line_spacing, TextAlignment text_alignment, double font_size, + out ushort[] glyph_indices, out double[] advance_widths, out Point[] glyph_offsets, out double glyph_width, out double glyph_height) { + // + glyph_width = 0; + glyph_height = 0; + if (lines.is_null_or_empty()) { + glyph_indices = new ushort[0]; + advance_widths = new double[0]; + glyph_offsets = new Point[0]; + return false; + } + int size = lines.Sum(s => s.Length); + glyph_indices = new ushort[size]; + advance_widths = new double[size]; + glyph_offsets = new Point[size]; + switch (text_alignment) { + case TextAlignment.Left: + return _try_get_glyph_params_left_aligned(lines, line_spacing, font_size, ref glyph_indices, ref advance_widths, ref glyph_offsets, out glyph_width, out glyph_height); + case TextAlignment.Center: // TODO + case TextAlignment.Right: // TODO + default: goto case TextAlignment.Left; + } + } + + public GlyphRun from_arranged(double font_size, Point origin, ushort[] indices, double[] advance_widths, Point[] offsets) => + new GlyphRun(_glyph_typeface, 0, false, font_size, HighDpiExtensions.fdpi_ratio, indices, origin, advance_widths, offsets, null, null, null, null, null); + + // Note: 3 separate methods are used for left/center/right alignment for optimization purposes + bool _try_get_glyph_params_left_aligned( + IReadOnlyList lines, double line_spacing, double font_size, ref ushort[] glyph_indices, ref double[] advance_widths, ref Point[] glyph_offsets, out double glyph_width, out double glyph_height) { + // + glyph_width = 0; + glyph_height = 0; + double x_offset = 0; + int n = 0; + foreach (string text in lines) { + double line_width = 0; + foreach (char c in text) { + if (!_char_map.TryGetValue(c, out (ushort index, double width) gc)) + return false; + glyph_indices[n] = gc.index; + double w = gc.width * font_size; + advance_widths[n] = w; + line_width += w; + glyph_offsets[n] = new Point(x_offset, glyph_height); + n++; + } + x_offset -= line_width; + if (line_width > glyph_width) + glyph_width = line_width; + glyph_height += (font_size + line_spacing); + } + glyph_height -= line_spacing; // remove the last line space + return true; + } + #endregion + static ReadOnlyDictionary _get_glyph_char_map(GlyphTypeface glyph_typeface) => _glyph_char_maps.GetOrAdd(glyph_typeface, gt => new Lazy>(() => { var widths = glyph_typeface.AdvanceWidths; diff --git a/src/client/spark/view/Utility/IVisualContentRect.cs b/src/client/spark/view/Utility/IVisualContentRect.cs new file mode 100644 index 000000000..a3ddd1bb5 --- /dev/null +++ b/src/client/spark/view/Utility/IVisualContentRect.cs @@ -0,0 +1,185 @@ +using mbts.ui.utility.text; +using mbts.utils; +using System.Collections.Generic; +using System.Windows; +using System.Windows.Media; + +namespace mbts.ui { + interface IVisualContentRect { + Size layout_size { get; } + Rect content_area { get; } + } + + // the "Control" in 'IControlVisualContentRect' is a double entendre: + // it hosts a set of input-properties inspired by the System.Windows.Control class, + // and it gives the caller "control" to modify the layout and content_area. + interface IControlVisualContentRect : IVisualContentRect { + Thickness border_thickness { get; set; } + Thickness padding { get; set; } + HorizontalAlignment horizontal_alignment { get; set; } + HorizontalAlignment horizontal_content_alignment { get; set; } + VerticalAlignment vertical_alignment { get; set; } + VerticalAlignment vertical_content_alignment { get; set; } + double content_width { get; set; } + double content_height { get; set; } + double min_width { get; set; } + double width { get; set; } + double max_width { get; set; } + double min_height { get; set; } + double height { get; set; } + double max_height { get; set; } + Size available_size { get; set; } + } + + /// + /// Uses a set of standard r/w properties to measure and arrange the layout of a rectangular border-region with content inside. + /// + class VisualContentRect : IControlVisualContentRect { + Thickness + _padding, + _border_thickness; + HorizontalAlignment + _horizontal_alignment, + _horizontal_content_alignment = HorizontalAlignment.Center; + VerticalAlignment + _vertical_alignment, + _vertical_content_alignment = VerticalAlignment.Center; + double + _content_width, + _content_height, + _min_width, + _width, + _max_width, + _min_height, + _height, + _max_height; + Size _available_size, _layout_size; + Rect _content_area; + bool _dirty; + + public Thickness border_thickness { get => _border_thickness; set => _set(ref _border_thickness, value); } + public Thickness padding { get => _padding; set => _set(ref _padding, value); } + public HorizontalAlignment horizontal_alignment { get => _horizontal_alignment; set => _set(ref _horizontal_alignment, value); } + public HorizontalAlignment horizontal_content_alignment { get => _horizontal_content_alignment; set => _set(ref _horizontal_content_alignment, value); } + public VerticalAlignment vertical_alignment { get => _vertical_alignment; set => _set(ref _vertical_alignment, value); } + public VerticalAlignment vertical_content_alignment { get => _vertical_content_alignment; set => _set(ref _vertical_content_alignment, value); } + public double content_width { get => _content_width; set => _set(ref _content_width, value); } + public double content_height { get => _content_height; set => _set(ref _content_height, value); } + public double min_width { get => _min_width; set => _set(ref _min_width, value); } + public double width { get => _width; set => _set(ref _width, value); } + public double max_width { get => _max_width; set => _set(ref _max_width, value); } + public double min_height { get => _min_height; set => _set(ref _min_height, value); } + public double height { get => _height; set => _set(ref _height, value); } + public double max_height { get => _max_height; set => _set(ref _max_height, value); } + public Size available_size { get => _available_size; set => _set(ref _available_size, value, _size_comparer); } + public Size layout_size { get { _ensure_current(); return _layout_size; } } + public Rect content_area { get { _ensure_current(); return _content_area; } } + + void _ensure_current() { + if (_dirty) { + WPFHelper.measure_visual_content( + ref _border_thickness, ref _padding, ref _horizontal_alignment, ref _horizontal_content_alignment, ref _vertical_alignment, ref _vertical_content_alignment, + ref _min_width, ref _width, ref _max_width, ref _min_height, ref _height, ref _max_height, + ref _content_width, ref _content_height, ref _available_size, out _content_area, out _layout_size, true); + _dirty = false; + } + } + + void _set(ref T field, T value) => + _set(ref field, value, EqualityComparer.Default); + + void _set(ref T field, T value, IEqualityComparer comparer) { + if (!comparer.Equals(field, value)) { + field = value; + _dirty = true; + } + } + + static readonly IEqualityComparer _size_comparer = EqualityComparer.Create((a, b) => a == b); + } + + // this class was designed to organize/contain the littany of properties needed to arrange glyph content + // in addition to the usual-suspects (width, horizontalalignment, etc.), it stores the glyph-arrays: glyph_indices, advance_widths, and glyph_offsets + class GlyphContentRect : IControlVisualContentRect { + readonly VisualContentRect _vcr = new VisualContentRect(); + ushort[] _glyph_indices; + double[] _advance_widths; + Point[] _glyph_offsets; + bool _arrange_dirty; + + // fields of public properties + double _font_size, _line_spacing; + IReadOnlyList _text_lines; + GlyphContext _glyph_context; + GlyphRun _glyph_run; + + public GlyphContext glyph_context { get => _glyph_context; set => _set(ref _glyph_context, value); } + public IReadOnlyList text_lines { get => _text_lines; set => _set(ref _text_lines, value); } + public double font_size { get => _font_size; set => _set(ref _font_size, value); } + public double line_spacing { get => _line_spacing; set => _set(ref _line_spacing, value); } + public Thickness border_thickness { get => _vcr.border_thickness; set => _vcr.border_thickness = value; } + public Thickness padding { get => _vcr.padding; set => _vcr.padding = value; } + public HorizontalAlignment horizontal_alignment { get => _vcr.horizontal_alignment; set => _vcr.horizontal_alignment = value; } + public HorizontalAlignment horizontal_content_alignment { get => _vcr.horizontal_content_alignment; set => _vcr.horizontal_content_alignment = value; } + public VerticalAlignment vertical_alignment { get => _vcr.vertical_alignment; set => _vcr.vertical_alignment = value; } + public VerticalAlignment vertical_content_alignment { get => _vcr.vertical_content_alignment; set => _vcr.vertical_content_alignment = value; } + public double content_width { get { _ensure_current(); return _vcr.content_width; } set { /*TODO: maybe add a Clip property and set it here?*/ } } + public double content_height { get { _ensure_current(); return _vcr.content_height; } set { /*TODO: maybe add a Clip property and set it here?*/ } } + public double min_width { get => _vcr.min_width; set => _vcr.min_width = value; } + public double width { get => _vcr.width; set => _vcr.width = value; } + public double max_width { get => _vcr.max_width; set => _vcr.max_width = value; } + public double min_height { get => _vcr.min_height; set => _vcr.min_height = value; } + public double height { get => _vcr.height; set => _vcr.height = value; } + public double max_height { get => _vcr.max_height; set => _vcr.max_height = value; } + public Size available_size { get => _vcr.available_size; set => _vcr.available_size = value; } + public Size layout_size { get { _ensure_current(); return _vcr.layout_size; } } + public Rect content_area { get { _ensure_current(); return _vcr.content_area; } } + public GlyphRun glyph_run { + get { + if (_text_lines.is_null_or_empty()) + return null; + _ensure_current(); + Point pt = _vcr.content_area.BottomLeft; + if (_glyph_run == null) { + _glyph_run = _glyph_context.from_arranged(_font_size, pt, _glyph_indices, _advance_widths, _glyph_offsets); + } else if (_glyph_run.BaselineOrigin != pt) { + _glyph_run.BaselineOrigin = pt; + } + return _glyph_run; + } + } + + void _ensure_current() { + if (_arrange_dirty) { + if (_text_lines.is_null_or_empty()) { + _vcr.content_width = 0; + _vcr.content_height = 0; + } else { + _glyph_context.arrange_glyph_run(_text_lines, _line_spacing, TextAlignment.Left, _font_size, out _glyph_indices, out _advance_widths, out _glyph_offsets, out double glyph_width, out double glyph_height); + _vcr.content_width = glyph_width; + _vcr.content_height = glyph_height; + } + _glyph_run = null; + _arrange_dirty = false; + } + } + + void _set(ref T field, T value) => + _set(ref field, value, EqualityComparer.Default); + + void _set(ref T field, T value, IEqualityComparer comparer) { + if (!comparer.Equals(field, value)) { + field = value; + _arrange_dirty = true; + } + } + } + + static class VisualContentRectExtensions { + // since the Margin is an external factor - an amount of space outside of the control's render-region - it is not included directly in the layout_size + public static Size layout_size_plus_margin(this IVisualContentRect vcr, Thickness margin) { + var sz = vcr.layout_size; + return new Size(margin.Left + sz.Width + margin.Right, margin.Top + sz.Height + margin.Bottom); + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Utility/WPFHelper.cs b/src/client/spark/view/Utility/WPFHelper.cs index bcc813476..1c08abd6c 100644 --- a/src/client/spark/view/Utility/WPFHelper.cs +++ b/src/client/spark/view/Utility/WPFHelper.cs @@ -1,3 +1,4 @@ +using mbts.spark.utilities; using mbts.utils; using System; using System.Collections; @@ -222,5 +223,96 @@ namespace mbts.ui { public static double control_base_height(Thickness border_thickness, Thickness padding) => border_thickness.Top + padding.Top + padding.Bottom + border_thickness.Bottom; + + public static void measure_visual_content( + ref Thickness border_thickness, + ref Thickness padding, + ref HorizontalAlignment x_align, + ref HorizontalAlignment x_content_align, + ref VerticalAlignment y_align, + ref VerticalAlignment y_content_align, + ref double min_width, + ref double width, + ref double max_width, + ref double min_height, + ref double height, + ref double max_height, + ref double content_width, + ref double content_height, + ref Size available_size, + out Rect content_area, + out Size resultant_size, + bool use_layout_rounding = false) { + // + double result_width, content_left; + if (!double.IsNaN(width)) { + result_width = MathUtils.within_range(min_width, max_width, width); + switch (x_content_align) { + case HorizontalAlignment.Left: content_left = border_thickness.Left + padding.Left; break; + case HorizontalAlignment.Center: content_left = 0.5 * (result_width - content_width); break; + case HorizontalAlignment.Right: content_left = result_width - border_thickness.Right - padding.Right; break; + default: goto case HorizontalAlignment.Center; + } + } else { + switch (x_align) { + case HorizontalAlignment.Left: + case HorizontalAlignment.Center: + case HorizontalAlignment.Right: + result_width = MathUtils.within_range(min_width, max_height, border_thickness.Left + padding.Left + content_width + padding.Right + border_thickness.Right); + content_left = border_thickness.Left + padding.Left; + break; + case HorizontalAlignment.Stretch: { + result_width = MathUtils.within_range(min_width, max_height, available_size.Width); + switch (x_content_align) { + case HorizontalAlignment.Left: content_left = border_thickness.Left + padding.Left; break; + case HorizontalAlignment.Center: content_left = 0.5 * (result_width - content_width); break; + case HorizontalAlignment.Right: content_left = result_width - content_width - border_thickness.Right - padding.Right; break; + default: goto case HorizontalAlignment.Center; + } + } + break; + default: goto case HorizontalAlignment.Center; + } + } + double result_height, content_top; + if (!double.IsNaN(height)) { + result_height = MathUtils.within_range(min_height, max_height, height); + switch (y_content_align) { + case VerticalAlignment.Top: content_top = border_thickness.Top + padding.Top; break; + case VerticalAlignment.Center: content_top = 0.5 * (result_height - content_height); break; + case VerticalAlignment.Bottom: content_top = result_height - border_thickness.Bottom - padding.Bottom; break; + default: goto case VerticalAlignment.Center; + } + } else { + switch (y_align) { + case VerticalAlignment.Top: + case VerticalAlignment.Center: + case VerticalAlignment.Bottom: + result_height = border_thickness.Top + padding.Top + content_height + padding.Bottom + border_thickness.Bottom; + content_top = border_thickness.Top + padding.Top; + break; + case VerticalAlignment.Stretch: + result_height = available_size.Height; + switch (y_content_align) { + case VerticalAlignment.Top: content_top = border_thickness.Top + padding.Top; break; + case VerticalAlignment.Center: content_top = 0.5 * (result_height - content_height); break; + case VerticalAlignment.Bottom: content_top = result_height - content_height - border_thickness.Bottom - padding.Bottom; break; + default: goto case VerticalAlignment.Center; + } + break; + default: goto case VerticalAlignment.Center; + } + } + if (use_layout_rounding) { + content_left = Math.Round(content_left); + content_top = Math.Round(content_top); + content_width = Math.Round(content_width); + content_height = Math.Round(content_height); + result_width = Math.Round(result_width); + result_height = Math.Round(result_height); + } + content_area = new Rect(content_left, content_top, content_width, content_height); + resultant_size = new Size(result_width, result_height); + } } } \ No newline at end of file From 837aa51adf1cb49847884ece955c2af5278da249 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 5 Jun 2018 18:02:32 -0400 Subject: [PATCH 2/3] SPARK-1204 pr edits --- .../view/Controls/Order/OrderTypeItem.xaml | 2 +- .../view/Spark/Ladder/LadderSettingsVM.cs | 12 +++----- .../view/Spark/Ladder/LadderSettingsView.xaml | 1 - .../spark/view/Spark/Ladder/LaderRender.cs | 5 ---- src/client/spark/view/Utility/GlyphContext.cs | 1 - .../spark/view/Utility/IVisualContentRect.cs | 6 ++-- src/client/spark/view/Utility/WPFHelper.cs | 30 +++++++++---------- 7 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/client/spark/view/Controls/Order/OrderTypeItem.xaml b/src/client/spark/view/Controls/Order/OrderTypeItem.xaml index 6d6759ed9..2eafa6a61 100644 --- a/src/client/spark/view/Controls/Order/OrderTypeItem.xaml +++ b/src/client/spark/view/Controls/Order/OrderTypeItem.xaml @@ -15,7 +15,7 @@ - + diff --git a/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs b/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs index 083224c94..9f8d70c32 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs +++ b/src/client/spark/view/Spark/Ladder/LadderSettingsVM.cs @@ -182,15 +182,11 @@ namespace mbts.spark { } public class OrdersColumnSettingsVM : LadderColumnSettingsVM, ITemplateSelectorKey { - public OrdersColumnSettingsVM() : base("Orders") { - sample_filled = 12; - sample_quantity = 50; - sample_side = Side.Buy; - } + public OrdersColumnSettingsVM() : base("Orders") { } - public uint sample_quantity { get; } - public uint sample_filled { get; } - public Side sample_side { get; } + public uint sample_quantity { get; } = 50; + public uint sample_filled { get; } = 12; + public Side sample_side { get; } = Side.Buy; public Brush badge_border { get; } = new SolidColorBrush(BTSColors.MarketData.ask); public Brush badge_background { get; } = Brushes.White; diff --git a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml index ff513fe18..ee52e4c3e 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml @@ -9,7 +9,6 @@ xmlns:usp="clr-namespace:mbts.ui.spark" xmlns:txt="clr-namespace:mbts.ui.utility.text" TextElement.FontSize="{x:Static u:FontSizes.Small}" - UseLayoutRounding="True" > diff --git a/src/client/spark/view/Spark/Ladder/LaderRender.cs b/src/client/spark/view/Spark/Ladder/LaderRender.cs index 410fb7eff..8458c5702 100644 --- a/src/client/spark/view/Spark/Ladder/LaderRender.cs +++ b/src/client/spark/view/Spark/Ladder/LaderRender.cs @@ -12,11 +12,6 @@ using System.Windows.Media; namespace mbts.spark { static class BadgeRender { - // These fields are used outside of Badge.cs, to calculate row height. - // Until we integrate these values into the LadderSettings, - // Badges MUST USE these values for Margin, BorderThickness, and Padding, - // else row_height calculations become invalid (see "LadderRender.measure_row_height"). - // IMPORTANT: the return values for this method MUST BE IN SYNC with the "format" method, else "measure_height" and "LadderRender.measure_row_height" will be broken. static int _line_count_for_style(LadderSettings.BadgeStyle badge_style) => badge_style == LadderSettings.BadgeStyle.Alternate ? 2 : 1; // IMPORTANT: at the risk of being redundant, see above comment. diff --git a/src/client/spark/view/Utility/GlyphContext.cs b/src/client/spark/view/Utility/GlyphContext.cs index 8a6feda20..5ca1ccb2f 100644 --- a/src/client/spark/view/Utility/GlyphContext.cs +++ b/src/client/spark/view/Utility/GlyphContext.cs @@ -181,7 +181,6 @@ namespace mbts.ui.utility.text { public GlyphRun from_arranged(double font_size, Point origin, ushort[] indices, double[] advance_widths, Point[] offsets) => new GlyphRun(_glyph_typeface, 0, false, font_size, HighDpiExtensions.fdpi_ratio, indices, origin, advance_widths, offsets, null, null, null, null, null); - // Note: 3 separate methods are used for left/center/right alignment for optimization purposes bool _try_get_glyph_params_left_aligned( IReadOnlyList lines, double line_spacing, double font_size, ref ushort[] glyph_indices, ref double[] advance_widths, ref Point[] glyph_offsets, out double glyph_width, out double glyph_height) { // diff --git a/src/client/spark/view/Utility/IVisualContentRect.cs b/src/client/spark/view/Utility/IVisualContentRect.cs index a3ddd1bb5..ad99873df 100644 --- a/src/client/spark/view/Utility/IVisualContentRect.cs +++ b/src/client/spark/view/Utility/IVisualContentRect.cs @@ -78,9 +78,9 @@ namespace mbts.ui { void _ensure_current() { if (_dirty) { WPFHelper.measure_visual_content( - ref _border_thickness, ref _padding, ref _horizontal_alignment, ref _horizontal_content_alignment, ref _vertical_alignment, ref _vertical_content_alignment, - ref _min_width, ref _width, ref _max_width, ref _min_height, ref _height, ref _max_height, - ref _content_width, ref _content_height, ref _available_size, out _content_area, out _layout_size, true); + _border_thickness, _padding, _horizontal_alignment, _horizontal_content_alignment, _vertical_alignment, _vertical_content_alignment, + _min_width, _width, _max_width, _min_height, _height, _max_height, + _content_width, _content_height, _available_size, out _content_area, out _layout_size, true); _dirty = false; } } diff --git a/src/client/spark/view/Utility/WPFHelper.cs b/src/client/spark/view/Utility/WPFHelper.cs index 1c08abd6c..c6e2bfd3e 100644 --- a/src/client/spark/view/Utility/WPFHelper.cs +++ b/src/client/spark/view/Utility/WPFHelper.cs @@ -225,21 +225,21 @@ namespace mbts.ui { border_thickness.Top + padding.Top + padding.Bottom + border_thickness.Bottom; public static void measure_visual_content( - ref Thickness border_thickness, - ref Thickness padding, - ref HorizontalAlignment x_align, - ref HorizontalAlignment x_content_align, - ref VerticalAlignment y_align, - ref VerticalAlignment y_content_align, - ref double min_width, - ref double width, - ref double max_width, - ref double min_height, - ref double height, - ref double max_height, - ref double content_width, - ref double content_height, - ref Size available_size, + Thickness border_thickness, + Thickness padding, + HorizontalAlignment x_align, + HorizontalAlignment x_content_align, + VerticalAlignment y_align, + VerticalAlignment y_content_align, + double min_width, + double width, + double max_width, + double min_height, + double height, + double max_height, + double content_width, + double content_height, + Size available_size, out Rect content_area, out Size resultant_size, bool use_layout_rounding = false) { From 8d0e7bc46726ea204b612a25328a655eb03c8136 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 7 Jun 2018 12:35:03 -0400 Subject: [PATCH 3/3] pr finishing touch --- src/client/spark/view/Controls/Badge.cs | 18 +++++++++++++++++- .../view/Spark/Ladder/LadderOrdersHost.cs | 3 ++- .../view/Spark/Ladder/LadderSettingsView.xaml | 3 +-- .../spark/view/Spark/Ladder/LadderView.xaml | 1 - 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/client/spark/view/Controls/Badge.cs b/src/client/spark/view/Controls/Badge.cs index 7369f4934..143c6db5a 100644 --- a/src/client/spark/view/Controls/Badge.cs +++ b/src/client/spark/view/Controls/Badge.cs @@ -118,6 +118,13 @@ namespace mbts.ui.controls { FrameworkPropertyMetadataOptions.AffectsRender, (o, v) => o._gcr.horizontal_alignment = v, HorizontalAlignment.Stretch); + // HoriontalContentAlignment + WPFHelper.override_metadata( + HorizontalContentAlignmentProperty, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._gcr.horizontal_content_alignment = v, + HorizontalAlignment.Center); // VerticalAlignment WPFHelper.override_metadata( VerticalAlignmentProperty, @@ -125,6 +132,13 @@ namespace mbts.ui.controls { FrameworkPropertyMetadataOptions.AffectsRender, (o, v) => o._gcr.vertical_alignment = v, VerticalAlignment.Center); + // VerticalContentAlignment + WPFHelper.override_metadata( + VerticalContentAlignmentProperty, + FrameworkPropertyMetadataOptions.AffectsArrange | + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._gcr.vertical_content_alignment = v, + VerticalAlignment.Center); // Padding WPFHelper.override_metadata( PaddingProperty, @@ -144,7 +158,9 @@ namespace mbts.ui.controls { _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; @@ -178,7 +194,7 @@ namespace mbts.ui.controls { protected override Size MeasureOverride(Size availableSize) { _gcr.available_size = availableSize; - return _gcr.layout_size_plus_margin(Margin); + return _gcr.layout_size; } protected override void OnRender(DrawingContext dc) { diff --git a/src/client/spark/view/Spark/Ladder/LadderOrdersHost.cs b/src/client/spark/view/Spark/Ladder/LadderOrdersHost.cs index 17ff06716..3abbb66a9 100644 --- a/src/client/spark/view/Spark/Ladder/LadderOrdersHost.cs +++ b/src/client/spark/view/Spark/Ladder/LadderOrdersHost.cs @@ -143,8 +143,9 @@ namespace mbts.spark { return price_cell.panel; else if (_price_list != null && !double.IsNaN(_row_height)) { var panel = new StackPanel() { + Margin = Badge.DefaultMargin, Orientation = Orientation.Horizontal, - MaxWidth = _col_width, + MaxWidth = _col_width //MaxHeight = _row_height, //ClipToBounds = true }; diff --git a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml index ee52e4c3e..130c0e8f3 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderSettingsView.xaml @@ -35,7 +35,6 @@ FontSize="{Binding font_size}" FontWeight="{Binding font_weight}" Foreground="{Binding foreground, Converter={StaticResource ColorToBrushConverter}}" - Margin="{x:Static c:Badge.DefaultMargin}" Width="{x:Static c:Badge.DefaultWidth}" > @@ -131,7 +130,7 @@ - + diff --git a/src/client/spark/view/Spark/Ladder/LadderView.xaml b/src/client/spark/view/Spark/Ladder/LadderView.xaml index 2b9145bbb..4cb0bd7d5 100644 --- a/src/client/spark/view/Spark/Ladder/LadderView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderView.xaml @@ -450,7 +450,6 @@ -