From 7c8da47f6ec3519282e383e6d408024a849ada95 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 28 Mar 2018 14:07:41 -0400 Subject: [PATCH] DIYGrid style changes --- .../Market/MarketView/MarketViewRow.cs | 40 +++--------------- .../Modules/Market/MarketView/MarketViewVM.cs | 4 +- .../view/Controls/DIYGrid/CellProperty.cs | 42 +++++++++++++++++++ .../view/Controls/DIYGrid/DIYColumnHeaders.cs | 13 ++++-- .../spark/view/Controls/DIYGrid/DIYGrid.cs | 2 +- .../Controls/DIYGrid/DIYGridResources.xaml | 20 ++++++--- .../spark/view/Controls/DIYGrid/DIYRowBase.cs | 28 ++++++++++--- 7 files changed, 98 insertions(+), 51 deletions(-) create mode 100644 src/client/spark/view/Controls/DIYGrid/CellProperty.cs diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs index 7a1e5c8c9..49849355a 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewRow.cs @@ -40,9 +40,9 @@ namespace mbts.spark { _orders_model = orders_model; string format_price(double price) => security.ContractSpec.format(price); _position = new CellProperty(0); - _bid_price = new CellProperty(format_price, double.NaN); + _bid_price = new CellProperty(format_price, BTSColors.header_text_brush, double.NaN); _bid_size = new CellProperty(0); - _ask_price = new CellProperty(format_price, double.NaN); + _ask_price = new CellProperty(format_price, BTSColors.header_text_brush, double.NaN); _ask_size = new CellProperty(0); _last_price = new CellProperty(format_price, double.NaN); _last_size = new CellProperty(0); @@ -63,8 +63,8 @@ namespace mbts.spark { _working_best_ask = new CellProperty(format_price, double.NaN); _working_bids_text = new CellProperty(string.Empty); _working_asks_text = new CellProperty(string.Empty); - _security_formatted_text = TextUtils.GetFormattedText(smd.security.ToString(null)); - _security_key_formatted_text = TextUtils.GetFormattedText(smd.security.Id.ToString()); + _security_formatted_text = TextUtils.GetFormattedText(smd.security.ToString(null), FontSizes.Small); + _security_key_formatted_text = TextUtils.GetFormattedText(smd.security.Id.ToString(), FontSizes.Small); // _dispose = Disposable.Create( _orders_model.on_security_orders(security.Id).ObserveOnDispatcher().Subscribe(_on_orders) @@ -240,37 +240,7 @@ namespace mbts.spark { _working_bids_text.on_next_distinct(OrderUtils.order_collection_summary(_working_bids, (x, y) => x.CompareTo(y), security.ContractSpec), ref dirty); _working_asks_text.on_next_distinct(OrderUtils.order_collection_summary(_working_asks, (x, y) => y.CompareTo(x), security.ContractSpec), ref dirty); } - #endregion - - class CellProperty : IObserver { - readonly Func _format; - FormattedText _formatted_text; - - public CellProperty(T initial_value = default) - : this(t => t.ToString(), initial_value) { } - - public CellProperty(Func format, T initial_value = default) { - _format = format; - this.value = initial_value; - } - - public T value { get; private set; } - public FormattedText formatted_text => - _formatted_text ?? (_formatted_text = TextUtils.GetFormattedText(_format(value))); - - public void on_next_distinct(T value, ref bool dirty) { - if (!value.Equals(this.value)) { - OnNext(value); - dirty = true; - } - } - public void OnCompleted() { } - public void OnError(Exception error) { } - public void OnNext(T value) { - this.value = value; - _formatted_text = null; - } - } + #endregion } } } \ No newline at end of file diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs index 2771d3d20..9fa835edd 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewVM.cs @@ -43,8 +43,8 @@ namespace mbts.spark { columns.Add(new DIYGrid.SimpleTextColumn(r => r[MarketViewView.Columns.Position]) { Header = "Pos", original_width = 70 }); columns.Add(new DIYGrid.SimpleTextColumn(r => r[MarketViewView.Columns.WorkingBids]) { Header = "Working", original_width = 70 }); columns.Add(new DIYGrid.SimpleTextColumn(r => r[MarketViewView.Columns.BidSize]) { Header = "Qty", original_width = 70 }); - columns.Add(new DIYGrid.SimpleTextColumn(r => r[MarketViewView.Columns.Bid]) { Header = "Bid", original_width = 80 }); - columns.Add(new DIYGrid.SimpleTextColumn(r => r[MarketViewView.Columns.Ask]) { Header = "Ask", original_width = 80 }); + columns.Add(new DIYGrid.SimpleTextColumn(r => r[MarketViewView.Columns.Bid], BTSColors.MarketData.bid_brush) { Header = "Bid", original_width = 80 }); + columns.Add(new DIYGrid.SimpleTextColumn(r => r[MarketViewView.Columns.Ask], BTSColors.MarketData.ask_brush) { Header = "Ask", original_width = 80 }); columns.Add(new DIYGrid.SimpleTextColumn(r => r[MarketViewView.Columns.AskSize]) { Header = "Qty", original_width = 70 }); columns.Add(new DIYGrid.SimpleTextColumn(r => r[MarketViewView.Columns.WorkingAsks]) { Header = "Working", original_width = 70 }); columns.Add(new DIYGrid.SimpleTextColumn(r => r[MarketViewView.Columns.Settlement]) { Header = "Settlement", original_width = 70 }); diff --git a/src/client/spark/view/Controls/DIYGrid/CellProperty.cs b/src/client/spark/view/Controls/DIYGrid/CellProperty.cs new file mode 100644 index 000000000..125bf5dd2 --- /dev/null +++ b/src/client/spark/view/Controls/DIYGrid/CellProperty.cs @@ -0,0 +1,42 @@ +using mbts.ui; +using mbts.ui.utility.text; +using System; +using System.Windows; +using System.Windows.Media; + +namespace mbts.spark { + class CellProperty : IObserver { + readonly Func _format; + readonly Brush _foreground; + FormattedText _formatted_text; + + public CellProperty(T initial_value = default) + : this(t => t.ToString(), SystemColors.ControlTextBrush, initial_value) { } + + public CellProperty(Func format, T initial_value = default) + : this(format, SystemColors.ControlTextBrush, initial_value) { } + + public CellProperty(Func format, Brush foreground, T initial_value = default) { + _format = format; + _foreground = foreground; + value = initial_value; + } + + public T value { get; private set; } + public FormattedText formatted_text => + _formatted_text ?? (_formatted_text = TextUtils.GetFormattedText(_format(value), FontSizes.Small, _foreground)); + + public void on_next_distinct(T value, ref bool dirty) { + if (!value.Equals(this.value)) { + OnNext(value); + dirty = true; + } + } + public void OnCompleted() { } + public void OnError(Exception error) { } + public void OnNext(T value) { + this.value = value; + _formatted_text = null; + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/DIYColumnHeaders.cs b/src/client/spark/view/Controls/DIYGrid/DIYColumnHeaders.cs index 51530e030..45ba4c9d4 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYColumnHeaders.cs +++ b/src/client/spark/view/Controls/DIYGrid/DIYColumnHeaders.cs @@ -12,18 +12,25 @@ namespace mbts.ui.controls { public partial class DIYGrid { public interface IColumnBase { void draw(TRowVM row, DrawingContext drawing_context, double left, double unclipped_left, double row_height, double padding, out double resultant_width); + Brush background { get; } } // To include extra data with your column type you are encouraged to derive from DIYGridColumn public abstract class ColumnBase : GridViewColumn { - public ColumnBase() { + protected ColumnBase(Brush background = null) { + this.background = background ?? Brushes.Transparent; original_width = double.NaN; } + public double original_width { get; set; } public bool is_collapsed { get; set; } + public Brush background { get; } } public abstract class ColumnBase : ColumnBase, IColumnBase { + protected ColumnBase(Brush background = null) + : base(background) { } + public abstract void draw(TRowVM row, DrawingContext drawing_context, double left, double unclipped_left, double row_height, double padding, out double total_width); } @@ -31,8 +38,8 @@ namespace mbts.ui.controls { public class SimpleTextColumn : ColumnBase, IColumnBase { readonly Func _get_formatted_text; - public SimpleTextColumn(Func get_formatted_text) => - _get_formatted_text = get_formatted_text; + public SimpleTextColumn(Func get_formatted_text, Brush background = null) + : base(background) => _get_formatted_text = get_formatted_text; public override void draw(TRowVM row, DrawingContext drawing_context, double left, double unclipped_left, double row_height, double padding, out double total_width) { // draw text centered vertially diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs index e704b0076..e6e0d8eb1 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs +++ b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs @@ -332,7 +332,7 @@ namespace mbts.ui.controls { set { SetValue(ColumnHeightProperty, value); } } public static readonly DependencyProperty ColumnHeightProperty = - DependencyProperty.Register("ColumnHeight", typeof(double), typeof(DIYGrid), new FrameworkPropertyMetadata(25.0, column_height_changed)); + DependencyProperty.Register("ColumnHeight", typeof(double), typeof(DIYGrid), new FrameworkPropertyMetadata(17.0, column_height_changed)); public IEnumerable Rows { get { return (IEnumerable)GetValue(RowsProperty); } set { SetValue(RowsProperty, value); } diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml b/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml index 6814d2edc..40dd6da80 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml +++ b/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml @@ -3,15 +3,18 @@ xmlns:mbts="clr-namespace:mbts.ui" xmlns:ui="clr-namespace:mbts.ui" xmlns:bts="clr-namespace:mbts.ui.controls"> + + + - + \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/DIYRowBase.cs b/src/client/spark/view/Controls/DIYGrid/DIYRowBase.cs index 12bc6933b..569e70e91 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYRowBase.cs +++ b/src/client/spark/view/Controls/DIYGrid/DIYRowBase.cs @@ -208,7 +208,7 @@ namespace mbts.ui.controls { #endregion } - public class Row : RowBase where TRowVM : IRowVM { + public class Row : RowBase where TRowVM : class, IRowVM { const double _padding = 5.0; TRowVM _row; @@ -216,15 +216,33 @@ namespace mbts.ui.controls { DataContextChanged += (s, e) => { if (e.OldValue is TRowVM old_row) old_row.invalidated -= InvalidateVisual; - _row = (TRowVM)e.NewValue; - _row.invalidated += InvalidateVisual; + _row = e.NewValue as TRowVM; + if (_row != null) + _row.invalidated += InvalidateVisual; }; } protected override void OnRender(DrawingContext dc) { if (_grid == null) return; - // Fill with transparent for hit testing - dc.DrawRectangle(new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)), null, new Rect(0, 0, _column_geometry.Sum(g => g.width), RenderSize.Height)); + // batch background draws + using (var col_en = _column_geometry.GetEnumerator()) { + Brush curr_brush = Brushes.Transparent; + double x = 0, curr_dist = 0, height = RenderSize.Height; + while (col_en.MoveNext()) { + ColumnBase col = col_en.Current.column; + if (col.background.Equals(curr_brush)) + curr_dist += col.ActualWidth; + else { + draw_background_part(); + x += curr_dist; + curr_brush = col.background; + curr_dist = col.ActualWidth; + } + } + void draw_background_part() => + dc.DrawRectangle(curr_brush, null, new Rect(x, 0, curr_dist, height)); + } + if (_row == null) return; string last_group = ""; foreach (var cell in _column_geometry) {