Merged in badge-style-row-height-unification (pull request #35)

SPARK-1204 Ladder/Badge - row height needs to fit different badge styles

Approved-by: Eric Harding <eric.harding@bluetradesys.com>
This commit is contained in:
John Colvin committed 2018-06-07 16:36:03 +00:00
commit 699736ff52
14 files changed
+660 -213

No files matched your search

@@ -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;
}
}
@@ -54,5 +54,33 @@
rv |= try_convert(args[1], out arg2);
return rv;
}
public static bool try_convert_args<T1, T2, T3>(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<T1, T2, T3, T4>(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;
}
}
}
+191 -112
View File
@@ -1,141 +1,220 @@
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<bts.config.System>().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<double, Badge>(
MinWidthProperty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._gcr.min_width = v,
0);
// Width
WPFHelper.override_metadata<double, Badge>(
WidthProperty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._gcr.width = v,
double.NaN);
// Max Width
WPFHelper.override_metadata<double, Badge>(
MaxWidthProperty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._gcr.max_width = v,
double.MaxValue);
// Min Height
WPFHelper.override_metadata<double, Badge>(
MinHeightProperty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._gcr.min_height = v,
0);
// Height
WPFHelper.override_metadata<double, Badge>(
HeightProperty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._gcr.height = v,
double.NaN);
// Max Height
WPFHelper.override_metadata<double, Badge>(
MaxHeightProperty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._gcr.max_height = v,
double.MaxValue);
// FontSize
WPFHelper.override_metadata<double, Badge>(
FontSizeProperty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._gcr.font_size = v,
DefaultFontSize);
// Foreground
WPFHelper.override_metadata<Brush, Badge>(
ForegroundProperty,
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._foreground = v,
Brushes.White);
// Background
WPFHelper.override_metadata<Brush, Badge>(
BackgroundProperty,
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._background = v,
Brushes.Transparent);
// BorderThickness
WPFHelper.override_metadata<Thickness, Badge>(
BorderThicknessProperty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._gcr.border_thickness = v,
DefaultBorderThickness);
// BorderBrush
WPFHelper.override_metadata<Brush, Badge>(
BorderBrushProperty,
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._border_brush = v,
Brushes.Black);
// FontFamily
WPFHelper.override_metadata<FontFamily, Badge>(
FontFamilyProperty,
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._gcr.glyph_context = b._gcr.glyph_context.with(v),
DefaultFontFamily);
// HoriontalAlignment
WPFHelper.override_metadata<HorizontalAlignment, Badge>(
HorizontalAlignmentProperty,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._gcr.horizontal_alignment = v,
HorizontalAlignment.Stretch);
// HoriontalContentAlignment
WPFHelper.override_metadata<HorizontalAlignment, Badge>(
HorizontalContentAlignmentProperty,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._gcr.horizontal_content_alignment = v,
HorizontalAlignment.Center);
// VerticalAlignment
WPFHelper.override_metadata<VerticalAlignment, Badge>(
VerticalAlignmentProperty,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._gcr.vertical_alignment = v,
VerticalAlignment.Center);
// VerticalContentAlignment
WPFHelper.override_metadata<VerticalAlignment, Badge>(
VerticalContentAlignmentProperty,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._gcr.vertical_content_alignment = v,
VerticalAlignment.Center);
// Padding
WPFHelper.override_metadata<Thickness, Badge>(
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.horizontal_content_alignment = HorizontalContentAlignment;
_gcr.vertical_alignment = VerticalAlignment;
_gcr.vertical_content_alignment = VerticalContentAlignment;
_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<IReadOnlyList<string>, Badge>(
nameof(TextLines),
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(b, v) => b._gcr.text_lines = v,
new string[0]);
public IReadOnlyList<string> TextLines {
get => GetValue(TextLinesProperty) as IReadOnlyList<string>;
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<double, Badge>(nameof(CornerRadius), DefaultCornerRadius);
static PropertyChangedCallback format_text(Action<Badge,FormattedText> 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;
}
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();
}
}
@@ -15,7 +15,7 @@
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<c:Badge Background="{Binding Metadata, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource OrderTypeBrushConverter}}" Width="15" Height="10" HorizontalAlignment="Left" />
<c:Badge Background="{Binding Metadata, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource OrderTypeBrushConverter}}" BorderThickness="1" Width="15" Height="10" HorizontalAlignment="Left" />
<TextBlock Text="{Binding OrderType, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource OrderTypeDisplayTextConverter}}" Margin="20,0,0,0" HorizontalAlignment="Left" />
</Grid>
</Border>
@@ -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:
@@ -67,19 +67,16 @@ 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);
}
public void update() {
// update brushes
Color color;
@@ -107,17 +104,8 @@ namespace mbts.spark {
}
private void _set_badge_text() {
if (_orders.Count > 0) {
switch (_current_badge_style) {
case LadderSettings.BadgeStyle.Alternate:
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) {
@@ -138,7 +126,6 @@ namespace mbts.spark {
public uint security_key => _parent.security.Id;
public double price { get => double.IsNaN(_phantom_price) ? _real_price : _phantom_price; }
public double real_price => _real_price;
public Side side {
get {
var distinct_sides = _orders.Values.Select(o => o.side()).Distinct();
@@ -149,55 +136,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<OrderRecordImmutable> _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<string> text_lines { get; private set; }
public bool has_parent => _orders.Values.Any( order => order.has_parent());
public bool has_phantom_price => !double.IsNaN(_phantom_price);
@@ -254,4 +195,4 @@ namespace mbts.spark {
return $"Price: {this.price} Order Count: {this.orders.Count()}";
}
}
}
}
@@ -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
};
@@ -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<LadderSettings> {
readonly Property<LadderSettings.BadgeStyle> _badge_style;
readonly Property<uint> _alternate_order_size;
readonly DelegateProperty<double> _row_height;
LadderSettings _current;
LadderColumnSettingsVM
_orders = new OrdersColumnSettingsVM(),
@@ -30,6 +31,13 @@ namespace mbts.spark {
_badge_style = backing<LadderSettings.BadgeStyle>(nameof(badge_style));
_badge_style.Changed += _badge_style_Changed;
_alternate_order_size = backing<uint>(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<LadderColumnSettingsVM> 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<ColorPaletteVM> foreground_palettes { get; }
public ObservableCollection<ColorPaletteVM> background_palettes { get; }
public event Action<double, double> font_size_changed;
public virtual string sample_text => "999-56789.01234";
@@ -178,7 +184,9 @@ namespace mbts.spark {
public class OrdersColumnSettingsVM : LadderColumnSettingsVM, ITemplateSelectorKey {
public OrdersColumnSettingsVM() : base("Orders") { }
public override string sample_text => "12/50";
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;
@@ -213,4 +221,4 @@ namespace mbts.spark {
};
}
}
}
}
@@ -11,6 +11,7 @@
TextElement.FontSize="{x:Static u:FontSizes.Small}"
>
<UserControl.Resources>
<c:BadgeTextLinesConverter x:Key="BadgeTextLinesConverter"/>
<u:KeyedTemplateSelector x:Key="ColumnSampleTemplateSelector">
<u:KeyedTemplateSelector.Default>
<DataTemplate>
@@ -28,18 +29,23 @@
<u:TemplateSelectorKeyValue Key="{x:Static usp:LadderSettingsView.OrdersColumnSampleTemplateKey}">
<DataTemplate>
<c:Badge
Width="50"
Height="20"
Background="{Binding badge_background}"
BorderBrush="{Binding badge_border}"
BorderThickness="3"
FontFamily="{Binding font_family}"
FontSize="{Binding font_size}"
FontWeight="{Binding font_weight}"
Foreground="{Binding foreground, Converter={StaticResource ColorToBrushConverter}}"
Text="{Binding sample_text}"
VerticalAlignment="Center"
/>
Width="{x:Static c:Badge.DefaultWidth}"
>
<c:Badge.TextLines>
<MultiBinding Converter="{StaticResource BadgeTextLinesConverter}">
<Binding Path="sample_side"/>
<Binding Path="sample_quantity"/>
<Binding Path="sample_filled"/>
<Binding Path="DataContext.badge_style" RelativeSource="{RelativeSource AncestorType=usp:LadderSettingsView}"/>
</MultiBinding>
</c:Badge.TextLines>
</c:Badge>
</DataTemplate>
</u:TemplateSelectorKeyValue>
</u:KeyedTemplateSelector>
@@ -82,7 +88,7 @@
<ItemsControl Grid.Row="1" ItemsSource="{Binding column_items}" Margin="8,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Height="27">
<Grid Height="{Binding DataContext.row_height, Mode=OneWay, RelativeSource={RelativeSource AncestorType=usp:LadderSettingsView}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="c0"/>
<ColumnDefinition SharedSizeGroup="c1"/>
@@ -124,7 +130,7 @@
<c:FontPicker SelectedFontSize="{Binding font_size}" Typeface="{Binding typeface}"/>
</c:PopupControl>
<Border Grid.Column="4" Background="{Binding background, Converter={StaticResource ColorToBrushConverter}}" BorderBrush="Black" BorderThickness="2,0,0,0" Padding="4,0">
<ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource ColumnSampleTemplateSelector}"/>
<ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource ColumnSampleTemplateSelector}" VerticalContentAlignment="Center"/>
</Border>
<Rectangle Grid.Column="1" Grid.ColumnSpan="5" Fill="DimGray" Height="1" HorizontalAlignment="Stretch" VerticalAlignment="Bottom"/>
</Grid>
@@ -448,15 +448,9 @@
<Setter Property="FontFamily" Value="{Binding font_family}"/>
<Setter Property="FontSize" Value="{Binding font_size}"/>
<Setter Property="Foreground" Value="{Binding foreground}"/>
<Setter Property="Text" Value="{Binding text}" />
<Setter Property="TopText" Value="{Binding top_line}" />
<Setter Property="BottomText" Value="{Binding bottom_line}" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="Width" Value="63" />
<Setter Property="Height" Value="21" />
<Setter Property="Margin" Value="4,0,0,0" />
<Setter Property="TextLines" Value="{Binding text_lines}" />
<Setter Property="Width" Value="{x:Static controls:Badge.DefaultWidth}"/>
<Setter Property="ToolTip" Value="{Binding description}" />
<Setter Property="CornerRadius" Value="1" />
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="ContextMenu">
<Setter.Value>
@@ -507,4 +501,4 @@
</Button>
</Grid>
</AdornerDecorator>
</UserControl>
</UserControl>
@@ -1,20 +1,61 @@
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 {
// 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<string> 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<double> 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
+65 -3
View File
@@ -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,64 @@ namespace mbts.ui.utility.text {
return true;
}
#region MultiLine
public bool arrange_glyph_run(
IReadOnlyList<string> 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);
bool _try_get_glyph_params_left_aligned(
IReadOnlyList<string> 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<int, (ushort index, double width)> _get_glyph_char_map(GlyphTypeface glyph_typeface) =>
_glyph_char_maps.GetOrAdd(glyph_typeface, gt => new Lazy<ReadOnlyDictionary<int, (ushort index, double width)>>(() => {
var widths = glyph_typeface.AdvanceWidths;
@@ -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; }
}
/// <summary>
/// Uses a set of standard r/w properties to measure and arrange the layout of a rectangular border-region with content inside.
/// </summary>
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(
_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;
}
}
void _set<T>(ref T field, T value) =>
_set(ref field, value, EqualityComparer<T>.Default);
void _set<T>(ref T field, T value, IEqualityComparer<T> comparer) {
if (!comparer.Equals(field, value)) {
field = value;
_dirty = true;
}
}
static readonly IEqualityComparer<Size> _size_comparer = EqualityComparer.Create<Size>((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<string> _text_lines;
GlyphContext _glyph_context;
GlyphRun _glyph_run;
public GlyphContext glyph_context { get => _glyph_context; set => _set(ref _glyph_context, value); }
public IReadOnlyList<string> 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<T>(ref T field, T value) =>
_set(ref field, value, EqualityComparer<T>.Default);
void _set<T>(ref T field, T value, IEqualityComparer<T> 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);
}
}
}
@@ -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(
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) {
//
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);
}
}
}