From c53f6ad906a2224248b21c726ee203d7ab065605 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 14 Oct 2019 16:01:24 -0500 Subject: [PATCH 1/4] finally remove mutable members from LadderSettings GetHashCode impl --- .../Modules/Trading/Ladder/LadderSettings.cs | 95 +++++-------------- 1 file changed, 22 insertions(+), 73 deletions(-) diff --git a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs index e1222b5af..f6942ccc0 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs @@ -23,6 +23,10 @@ namespace mbts.spark { // TODO: somehow allow windows that are using the default settings to still have individual column widths. [ProtoContract] class LadderColumnSettings : IEquatable { + // this class is 100% mutable (there are literally 0 readonly members), + // so GetHashCode must return the same hash for every instance. + static readonly int _type_hash = typeof(LadderColumnSettings).GetHashCode(); + public static LadderColumnSettings Default => new LadderColumnSettings { show = true, width = 50, @@ -74,14 +78,7 @@ namespace mbts.spark { other.show == show; public override bool Equals(object obj) => Equals(obj as LadderColumnSettings); - - public override int GetHashCode() => - width.GetHashCode() + - background.GetHashCode() + - foreground.GetHashCode() + - font_family.GetHashCode() + - font_size.GetHashCode() + - font_weight.GetHashCode(); + public override int GetHashCode() => _type_hash; public static bool operator ==(LadderColumnSettings x, LadderColumnSettings y) => x.safe_equals(y); public static bool operator !=(LadderColumnSettings x, LadderColumnSettings y) => !(x == y); @@ -89,6 +86,9 @@ namespace mbts.spark { [ProtoContract] class HistogramBarAnimation : IEquatable { + // this class is 100% mutable (there are literally 0 readonly members), + // so GetHashCode must return the same hash for every instance. + static readonly int _type_hash = typeof(HistogramBarAnimation).GetHashCode(); [ProtoMember(1)] public ColorShowProto bars { get; set; } [ProtoMember(2)] @@ -102,14 +102,14 @@ namespace mbts.spark { show_numbers == other.show_numbers; public override bool Equals(object obj) => Equals(obj as HistogramBarAnimation); - public override int GetHashCode() => - EqualityComparer.aggregate_hash( - bars.GetHashCode(), - show_numbers.GetHashCode()); + public override int GetHashCode() => _type_hash; } [ProtoContract] class VolumeColumnSettings : IEquatable { + // this class is 100% mutable (there are literally 0 readonly members), + // so GetHashCode must return the same hash for every instance. + static readonly int _type_hash = typeof(VolumeColumnSettings).GetHashCode(); public static VolumeColumnSettings Default { get { var gen = LadderColumnSettings.Default; @@ -149,19 +149,14 @@ namespace mbts.spark { show_ltq.Equals(other.show_ltq); public override bool Equals(object obj) => Equals(obj as VolumeColumnSettings); - - public override int GetHashCode() => - EqualityComparer.aggregate_hash( - general.GetHashCode(), - buy.GetHashCode(), - sell.GetHashCode(), - passive.GetHashCode(), - total.GetHashCode(), - show_ltq.GetHashCode()); + public override int GetHashCode() => _type_hash; } [ProtoContract] class LadderMarketBreakLine : IEquatable { + // this class is 100% mutable (there are literally 0 readonly members), + // so GetHashCode must return the same hash for every instance. + static readonly int _type_hash = typeof(LadderMarketBreakLine).GetHashCode(); [ProtoMember(1)] public bool show { get; set; } [ProtoMember(2)] @@ -184,8 +179,7 @@ namespace mbts.spark { Equals(obj as LadderMarketBreakLine); public override int GetHashCode() => - show.GetHashCode() + - (int)color; + _type_hash; } [ProtoContract] @@ -255,6 +249,9 @@ namespace mbts.spark { const double _default_order_entry_panel_width = 110.0; const double _default_zoom_factor = 1.0; const bool _default_show_warn_on_cross = true; + // this class is 100% mutable (there are literally 0 readonly members), + // so GetHashCode must return the same hash for every instance. + static readonly int _type_hash = typeof(LadderSettings).GetHashCode(); // Default proto constructor // Added this so that when clients upgrade to a version with new settings, @@ -590,55 +587,7 @@ namespace mbts.spark { public override bool Equals(object obj) => Equals(obj as LadderSettings); - // TODO: remove mutable props from hash logic (or conversely, make them immutable). - public override int GetHashCode() => - EqualityComparer.aggregate_hash( - row_height.GetHashCode(), - grid_line_color.GetHashCode(), - EqualityComparer.safe_get_hash_code(orders_column), - EqualityComparer.safe_get_hash_code(bid_column), - EqualityComparer.safe_get_hash_code(ask_column), - EqualityComparer.safe_get_hash_code(price_column), - EqualityComparer.safe_get_hash_code(last_column), - EqualityComparer.safe_get_hash_code(piq_column), - EqualityComparer.safe_get_hash_code(calculator_buttons), - scroll_bar_visible.GetHashCode(), - default_time_in_force.GetHashCode(), - size.GetHashCode(), - animate_recenter.GetHashCode(), - badge_style.GetHashCode(), - EqualityComparer.safe_get_hash_code(bid_break_line), - EqualityComparer.safe_get_hash_code(ask_break_line), - EqualityComparer.safe_get_hash_code(mid_break_line), - alternate_order_size.GetHashCode(), - EqualityComparer.safe_get_hash_code(session_low_line), - EqualityComparer.safe_get_hash_code(session_high_line), - EqualityComparer.safe_get_hash_code(session_range_fill), - EqualityComparer.safe_get_hash_code(volume_column), - EqualityComparer.safe_get_hash_code(size_display), - day_visible.GetHashCode(), - high_visible.GetHashCode(), - low_visible.GetHashCode(), - open_visible.GetHashCode(), - settle_visible.GetHashCode(), - volume_visible.GetHashCode(), - account_visible.GetHashCode(), - tif_visible.GetHashCode(), - order_type_visible.GetHashCode(), - account_position_visible.GetHashCode(), - size_presets_visible.GetHashCode(), - cancel_buys_visible.GetHashCode(), - cancel_sells_visible.GetHashCode(), - cancel_all_visible.GetHashCode(), - auto_clear_visible.GetHashCode(), - clear_visible.GetHashCode(), - order_entry_panel_initial_width.GetHashCode(), - zoom_slider_visible.GetHashCode(), - zoom_factor.GetHashCode(), - use_defaults.GetHashCode(), - favorite_algos.GetHashCode(), - net_change.GetHashCode(), - show_warn_on_cross.GetHashCode()); + public override int GetHashCode() => _type_hash; public struct WindowSpecifics { public readonly double order_entry_panel_width, zoom_factor; @@ -693,4 +642,4 @@ namespace mbts.spark { return source; } } -} +} \ No newline at end of file From 0772a13b09d09e3ef1859887647dd63b5f638320 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 14 Oct 2019 18:05:18 -0500 Subject: [PATCH 2/4] introduce ladder settings 'profile-equivalent' logic to avoid disconnecting ladders from profiles due to trivial settings changes --- .../Modules/Trading/Ladder/LadderSettings.cs | 124 ++++++++++-------- .../Trading/Ladder/LadderWindowFactory.cs | 13 +- .../Shell/Settings/ShellSettingsDialogVM.cs | 17 ++- 3 files changed, 90 insertions(+), 64 deletions(-) diff --git a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs index f6942ccc0..cc53844a7 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs @@ -249,6 +249,8 @@ namespace mbts.spark { const double _default_order_entry_panel_width = 110.0; const double _default_zoom_factor = 1.0; const bool _default_show_warn_on_cross = true; + public static readonly IEqualityComparer profile_equality_comparer = + EqualityComparer.Create(profile_equals); // this class is 100% mutable (there are literally 0 readonly members), // so GetHashCode must return the same hash for every instance. static readonly int _type_hash = typeof(LadderSettings).GetHashCode(); @@ -530,60 +532,76 @@ namespace mbts.spark { return s; } + /// + /// Determines whether or not the specified LadderSettings are 'profile equivalent.' + /// This means that 2 ladders, each with a different one of the specified settings, could share the same profile. + /// + /// + /// Note: the impetus for this version of equality is to avoid disconnecting a ladder from its profile due to 'trivial' settings changes. + /// Consider the '' property: + /// If a ladder is using a settings profile with Limit as the default algo, and the user decides to send a 'Market' order, + /// the ladder would suddenly be disconnected from the profile, because it's settings state would no longer be equal to the profile's settings, + /// since they changed the selected algo type to 'Market.' + /// To avoid such travesties, a separate equals logic is needed: this is called 'profile-equivalent' and is implemented in . + /// Devs: be sure to account for properties that are excluded in the 'profile-equals' logic in the [regular]'equals' logic. + public static bool profile_equals(LadderSettings a, LadderSettings b) => + a == null ? b == null : + b != null && + a.row_height == b.row_height && + a.grid_line_color == b.grid_line_color && + a.orders_column.safe_equals(b.orders_column) && + a.bid_column.safe_equals(b.bid_column) && + a.ask_column.safe_equals(b.ask_column) && + a.price_column.safe_equals(b.price_column) && + a.last_column.safe_equals(b.last_column) && + a.piq_column.safe_equals(b.piq_column) && + a.calculator_buttons.safe_equals(b.calculator_buttons, Enumerable.SequenceEqual) && + a.scroll_bar_visible == b.scroll_bar_visible && + a.default_account == b.default_account && + a.default_time_in_force == b.default_time_in_force && + a.size == b.size && + a.animate_recenter == b.animate_recenter && + a.badge_style == b.badge_style && + a.auto_clear == b.auto_clear && + a.bid_break_line.safe_equals(b.bid_break_line) && + a.ask_break_line.safe_equals(b.ask_break_line) && + a.mid_break_line.safe_equals(b.mid_break_line) && + a.alternate_order_size == b.alternate_order_size && + a.session_low_line.safe_equals(b.session_low_line) && + a.session_high_line.safe_equals(b.session_high_line) && + a.session_range_fill.safe_equals(b.session_range_fill) && + a.volume_column.safe_equals(b.volume_column) && + a.size_display.safe_equals(b.size_display) && + a.day_visible == b.day_visible && + a.high_visible == b.high_visible && + a.low_visible == b.low_visible && + a.open_visible == b.open_visible && + a.settle_visible == b.settle_visible && + a.volume_visible == b.volume_visible && + a.account_visible == b.account_visible && + a.tif_visible == b.tif_visible && + a.order_type_visible == b.order_type_visible && + a.account_position_visible == b.account_position_visible && + a.day_position_visible == b.day_position_visible && + a.raw_day_position_visible == b.raw_day_position_visible && + a.size_presets_visible == b.size_presets_visible && + a.cancel_buys_visible == b.cancel_buys_visible && + a.cancel_sells_visible == b.cancel_sells_visible && + a.cancel_all_visible == b.cancel_all_visible && + a.auto_clear_visible == b.auto_clear_visible && + a.clear_visible == b.clear_visible && + a.order_entry_panel_initial_width == b.order_entry_panel_initial_width && + a.zoom_slider_visible == b.zoom_slider_visible && + a.zoom_factor == b.zoom_factor && + a.use_defaults == b.use_defaults && + a.favorite_algos == b.favorite_algos && + a.net_change == b.net_change && + a.show_warn_on_cross == b.show_warn_on_cross && + a.mini_ob == b.mini_ob; + public bool Equals(LadderSettings other) => - other != null && - row_height == other.row_height && - grid_line_color == other.grid_line_color && - orders_column.safe_equals(other.orders_column) && - bid_column.safe_equals(other.bid_column) && - ask_column.safe_equals(other.ask_column) && - price_column.safe_equals(other.price_column) && - last_column.safe_equals(other.last_column) && - piq_column.safe_equals(other.piq_column) && - calculator_buttons.safe_equals(other.calculator_buttons, (a, b) => a.SequenceEqual(b)) && - scroll_bar_visible == other.scroll_bar_visible && - default_account == other.default_account && - default_algo == other.default_algo && - default_time_in_force == other.default_time_in_force && - size == other.size && - animate_recenter == other.animate_recenter && - badge_style == other.badge_style && - auto_clear == other.auto_clear && - bid_break_line.safe_equals(other.bid_break_line) && - ask_break_line.safe_equals(other.ask_break_line) && - mid_break_line.safe_equals(other.mid_break_line) && - alternate_order_size == other.alternate_order_size && - session_low_line.safe_equals(other.session_low_line) && - session_high_line.safe_equals(other.session_high_line) && - session_range_fill.safe_equals(other.session_range_fill) && - volume_column.safe_equals(other.volume_column) && - size_display.safe_equals(other.size_display) && - day_visible == other.day_visible && - high_visible == other.high_visible && - low_visible == other.low_visible && - open_visible == other.open_visible && - settle_visible == other.settle_visible && - volume_visible == other.volume_visible && - account_visible == other.account_visible && - tif_visible == other.tif_visible && - order_type_visible == other.order_type_visible && - account_position_visible == other.account_position_visible && - day_position_visible == other.day_position_visible && - raw_day_position_visible == other.raw_day_position_visible && - size_presets_visible == other.size_presets_visible && - cancel_buys_visible == other.cancel_buys_visible && - cancel_sells_visible == other.cancel_sells_visible && - cancel_all_visible == other.cancel_all_visible && - auto_clear_visible == other.auto_clear_visible && - clear_visible == other.clear_visible && - order_entry_panel_initial_width == other.order_entry_panel_initial_width && - zoom_slider_visible == other.zoom_slider_visible && - zoom_factor == other.zoom_factor && - use_defaults == other.use_defaults && - favorite_algos == other.favorite_algos && - net_change == other.net_change && - show_warn_on_cross == other.show_warn_on_cross && - mini_ob == other.mini_ob; + profile_equals(this, other) && + default_algo == other.default_algo; public override bool Equals(object obj) => Equals(obj as LadderSettings); diff --git a/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs b/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs index 0d1b3ac40..2f3b64e92 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs @@ -81,11 +81,9 @@ namespace bts.shell { if (!_settings_hub.current.ladder_profiles.TryGetFirst(lp => lp.id == id, out var result)) { // profile was deleted id = 0; - } else { - if (!s.Equals(result.value)) { - // ladder's settings diverged from the profile (this happens when the user changes settings outside of the settings dialog, such as column widths, left panel width, etc.) - id = 0; - } + } else if (!LadderSettings.profile_equals(s, result.value)) { + // ladder's settings diverged from the profile (this happens when the user changes settings outside of the settings dialog, such as column widths, left panel width, etc.) + id = 0; } } return new LadderSaveState { security_key = vm.security.Id, settings = s, settings_id = id }; @@ -149,7 +147,7 @@ namespace bts.shell { var curr = vm.get_save_state(); if (settings_id == 0) { last_value = curr; - } else if (!curr.Equals(last_value)) { + } else if (!LadderSettings.profile_equals(curr, last_value)) { settings_id = 0; last_value = curr; } @@ -163,7 +161,7 @@ namespace bts.shell { // not on a profile, don't respond to default settings changes return; } else if (us.ladder_profiles.TryGetFirst(p => p.id == settings_id, out var profile)) { - if (!last_value.Equals(vm.get_save_state())) { + if (!LadderSettings.profile_equals(last_value, vm.get_save_state())) { settings_id = 0; } else { settings_subject.OnNext(last_value = profile.value); @@ -211,6 +209,7 @@ namespace bts.shell { _ => _, vm.get_save_state(), UserSettings.FileExtensions.LADDER, + LadderSettings.profile_equals, settings_id); dialog_vm.applied += apply; // dialog diff --git a/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs b/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs index b51a4989a..6cc76049e 100644 --- a/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs +++ b/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs @@ -130,8 +130,10 @@ namespace mbts.spark { DialogSkirtItem _apply_item, _ok_item, _cancel_item; MenuItemVM _import_item, _export_item, _load_factory_item; - public ProfiledShellSettingsDialogVM(ISettingsProfileService profile_service, Func get_fallback, Func vm_factory, Func normalize, T init, string file_ext, uint settings_id = 0) { + public ProfiledShellSettingsDialogVM(ISettingsProfileService profile_service, Func get_fallback, Func vm_factory, Func normalize, T init, string file_ext, Func profile_equals = null, uint settings_id = 0) { _vm_factory = vm_factory; + if (profile_equals == null) + profile_equals = EqualityComparer.Default.Equals; // profile collections var skip_local = profiles.WrapSkip(1); var skip_local_wrapper = skip_local.WrapSelector( @@ -185,12 +187,19 @@ namespace mbts.spark { continue; i = ~i; var temp = ns.with(value: normalize(ns.value)); - skip_local_wrapper.Insert(i, temp); if (settings_id == temp.id) { - if (temp.value.Equals(init)) - selected_profile = skip_local[i]; settings_id = 0; + if (profile_equals(temp.value, init)) { + if (!temp.value.Equals(init)) { + // they aren't exactly equal (don't overwrite any props in 'init' - use it to create the vm) + temp = temp.with(value: init); + } + skip_local_wrapper.Insert(i, temp); + selected_profile = skip_local[i]; + continue; + } } + skip_local_wrapper.Insert(i, temp); } })); From c9da022c58215a76f5b4955f63edf3c946c05358 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 14 Oct 2019 22:54:23 -0500 Subject: [PATCH 3/4] SP-248 Ladder left panel scroll --- .../Modules/Trading/Ladder/LadderSettings.cs | 8 ++- .../Modules/Trading/Ladder/VM/LadderVM.cs | 42 ++++++++++++- .../Trading/Ladder/View/LadderView.xaml | 9 ++- .../Trading/Ladder/View/LadderView.xaml.cs | 40 +++++++++++- .../core/GlobalExtensions/DoubleHelper.cs | 31 +++++++++- .../spark/view/Controls/DIYGrid/DoubleUtil.cs | 2 + .../Converters/AnonymousValueConverter.cs | 3 + .../Extensions/FrameworkElementExtensions.cs | 23 +++++++ .../view/Extensions/ScrollViewerBehavior.cs | 61 +++++++++++++++++++ .../view/Extensions/UIElementExtensions.cs | 8 +++ 10 files changed, 221 insertions(+), 6 deletions(-) create mode 100644 src/client/spark/view/Extensions/ScrollViewerBehavior.cs diff --git a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs index cc53844a7..c025e90ed 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs @@ -496,6 +496,9 @@ namespace mbts.spark { [ProtoMember(55)] public LadderMiniOrderBookSettings mini_ob { get; set; } + [ProtoMember(57)] + public double left_panel_scroll_position { get; set; } + public void sanitize() => sanitize(this); @@ -601,7 +604,8 @@ namespace mbts.spark { public bool Equals(LadderSettings other) => profile_equals(this, other) && - default_algo == other.default_algo; + default_algo == other.default_algo && + left_panel_scroll_position == other.left_panel_scroll_position; public override bool Equals(object obj) => Equals(obj as LadderSettings); @@ -660,4 +664,4 @@ namespace mbts.spark { return source; } } -} \ No newline at end of file +} diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs index d5d1c38da..174a14b69 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderVM.cs @@ -35,7 +35,14 @@ namespace mbts.spark { _show_cancel_sells, _show_cancel_all, _show_zoom; readonly PropertyAutoWrapper _bid_background, _bid_foreground, _ask_background, _ask_foreground; readonly HashSet _sticky_algos = new HashSet(); - double _position, _day_position, _raw_day_position; + double + _position, + _day_position, + _raw_day_position, + _left_panel_extent_height, + _left_panel_view_port_height, + _left_panel_scroll_position, + _left_panel_scroll_position_target; bool _show_account_position, _show_day_position, _show_raw_day_position; ulong _misc_time = 0; ulong _last_trade_time = 0; @@ -76,6 +83,7 @@ namespace mbts.spark { _volume_column_width = backing(nameof(volume_column_width), ws.volume_column_width); _left_panel_width = backing(nameof(left_panel_width), ws.order_entry_panel_width); _zoom_factor = backing(nameof(zoom_factor), ws.zoom_factor); + _left_panel_scroll_position_target = init.left_panel_scroll_position; // remember: account_positions needs to subscribe to selected_account; both the sub and account_positions need to be disposed _volume = backing(nameof(volume), 0ul); _auto_clear = backing(nameof(auto_clear), init.auto_clear); @@ -333,6 +341,33 @@ namespace mbts.spark { public bool show_cancel_sells => _show_cancel_sells.view; public bool show_cancel_all => _show_cancel_all.view; public bool show_zoom => _show_zoom.view; + // left panel scroll props + public double left_panel_view_port_height { + get => _left_panel_view_port_height; + set { + using (detect_property_changes(nameof(left_panel_scroll_position))) + _left_panel_view_port_height = value; + } + } + public double left_panel_scroll_position { + get { + if (!_left_panel_scroll_position_target.is_nan()) { + if (_left_panel_scroll_position_target < _max_left_panel_scroll_position) { + _left_panel_scroll_position = _left_panel_scroll_position_target; + _left_panel_scroll_position_target = double.NaN; + } + } + return Math.Min(_left_panel_scroll_position, _left_panel_extent_height); + } + set => Set(ref _left_panel_scroll_position, value); + } + public double left_panel_extent_height { + get => _left_panel_extent_height; + set { + using (detect_property_changes(nameof(left_panel_scroll_position))) + Set(ref _left_panel_extent_height, value); + } + } public double left_panel_width { get => _left_panel_width.Value; set => _left_panel_width.set(value); } public double zoom_factor { get => _zoom_factor.Value; set => _zoom_factor.set(value); } public string position_display_text { @@ -364,6 +399,10 @@ namespace mbts.spark { public bool show_warn_on_cross_from_settings => current_settings.show_warn_on_cross; internal LadderModel model => _model; internal IObservable settings { get; } + + double _max_left_panel_scroll_position => + Math.Max(0, _left_panel_extent_height - _left_panel_view_port_height); + internal OrderRecordImmutable get_root_at_price((OrderRecordImmutable order, uint? _, AlgoData? __, AlgoDefinitionMetadata? meta) op) { var order = op.order; double p = order.price(); @@ -394,6 +433,7 @@ namespace mbts.spark { zoom_factor, piq_column_width)); rv.auto_clear = auto_clear; + rv.left_panel_scroll_position = left_panel_scroll_position; if (order_properties_vm != null) { rv.default_time_in_force = order_properties_vm.selected_time_in_force; if (order_properties_vm.selected_account.HasValue) diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml index b761e9b75..c0275583a 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml @@ -301,7 +301,14 @@ - + diff --git a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs index 015680d5c..5a6dced1a 100644 --- a/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs +++ b/src/client/spark/Modules/Trading/Ladder/View/LadderView.xaml.cs @@ -1,11 +1,16 @@ using bts; using bts.collections; using bts.utils; +using mbts.ui; using mbts.ui.converters; +using mbts.ui.extensions; using mbts.utils; +using System; +using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Input; @@ -80,6 +85,7 @@ namespace mbts.spark { _ladder.visible_columns_changed += () => _column_headers_presenter.Columns.SetItems( _ladder.visible_column_types.Select(t => ColumnFactory.create(_ladder, t))); this.vm = vm; + vm.SubscribeDisposed(_connect_left_panel_scroll()); DataContext = vm; DataContextChanged += datacontext_changed; _ui_scaler.MouseDoubleClick += _ui_scaler_MouseDoubleClick; @@ -87,7 +93,39 @@ namespace mbts.spark { this.PreviewMouseDown += LadderView_PreviewMouseDown; } - + IDisposable _connect_left_panel_scroll() { + // extent height + bool busy = false; + var extent_height_hook = _left_panel_scroll.ObserveProperty(ScrollViewer.ExtentHeightProperty, (s, e) => { + if (!busy) { + double scroll_pos = vm.left_panel_scroll_position; + busy = true; + Dispatcher.InvokeAsync(() => { + vm.left_panel_extent_height = _left_panel_scroll.ExtentHeight; + if (_left_panel_scroll.VerticalOffset != scroll_pos) + _left_panel_scroll.TryScrollToVerticalOffset(scroll_pos); + busy = false; + }, System.Windows.Threading.DispatcherPriority.ApplicationIdle); + } + }); + // view port height + _left_panel_scroll.Loaded += on_loaded; + var dispose = Disposable.CreateDisposeOnce(dispose_impl); + return Disposable.Create(dispose, vm.SubscribeDisposed(dispose)); + // local methods + void on_loaded(object sender, RoutedEventArgs e) { + if (_left_panel_scroll.descendants_of_type().TryGetFirst(sb => sb.Orientation == Orientation.Vertical, out var vscroll)) { + vscroll.SetBinding(ScrollBar.ViewportSizeProperty, new Binding(nameof(LadderVM.left_panel_view_port_height)) { + Converter = ValueConverter.create(d => new Size(0, d), sz => sz.Height), + Mode = BindingMode.OneWayToSource + }); + } + } + void dispose_impl() { + Disposable.dispose(ref extent_height_hook); + _left_panel_scroll.Loaded -= on_loaded; + } + } // (Left|Right) CTRL + Wheel to zoom // This is neat b/c the user doesn't even need to have the slider visible to zoom diff --git a/src/client/spark/core/GlobalExtensions/DoubleHelper.cs b/src/client/spark/core/GlobalExtensions/DoubleHelper.cs index 747747963..4b9292936 100644 --- a/src/client/spark/core/GlobalExtensions/DoubleHelper.cs +++ b/src/client/spark/core/GlobalExtensions/DoubleHelper.cs @@ -1,7 +1,36 @@ -using System.Linq; +using System; +using System.Linq; namespace mbts { public static class DoubleHelper { + public static bool any_nan_or_infinity(params double[] values) { + for (int i = 0; i < values.Length; i++) { + if (double.IsNaN(values[i]) || double.IsInfinity(values[i])) + return true; + } + return false; + } + + public static bool are_close(double v1, double v2, double threshold) => + !any_nan_or_infinity(v1, v2, threshold) && Math.Abs(threshold) > Math.Abs(v1 - v2); + + // bc double.IsNaN gets annoying write, and a lazy programmer is a happy programmer ;) + public static bool is_nan(this double value) => + double.IsNaN(value); + + /// + /// If the specified value is a , , or , returns the equivalent ; otherwise, returns . + /// + /// The value to convert to a . + /// The equivalent value if the cast succeeds, otherwise . + public static double nan_cast(object value) { + // devs: add numeric type checks as needed + if (value is double d) return d; + if (value is int i) return i; + if (value is uint u) return u; + return double.NaN; + } + public static bool IsNaNOrInfinity(double d) => double.IsNaN(d) || double.IsInfinity(d); public static bool IsNaNOrInfinityOr(double d, params double[] values) => IsNaNOrInfinity(d) || values.Contains(d); public static bool IsOneOf(double d, params double[] values) => values.Contains(d); diff --git a/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs b/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs index aa2019135..cce6cb9bc 100644 --- a/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs +++ b/src/client/spark/view/Controls/DIYGrid/DoubleUtil.cs @@ -4,6 +4,8 @@ using System.Linq; namespace mbts.ui.controls { static class DoubleUtil { public const int API_DECIMAL_PRECISION = 9; + // arbitrary small number that can be used to determine whether or not scroll changes are significant + public const double MIN_SCROLL_DELTA = 0.000015; /* smallest such that 1.0+DBL_EPSILON != 1.0 */ private const double EPSILON = 2.2204460492503131e-016; private const double BIG_EPSILON = EPSILON * 10; diff --git a/src/client/spark/view/Converters/AnonymousValueConverter.cs b/src/client/spark/view/Converters/AnonymousValueConverter.cs index 29ba7db43..20a67cbc9 100644 --- a/src/client/spark/view/Converters/AnonymousValueConverter.cs +++ b/src/client/spark/view/Converters/AnonymousValueConverter.cs @@ -11,6 +11,9 @@ namespace mbts.ui.converters { public static IValueConverter create(Func convert) => new AnonymousValueConverter(convert); + public static IValueConverter create(Func convert, Func convert_back) => + new AnonymousValueConverter(convert, convert_back); + public static IMultiValueConverter create(Func convert) => new AnonymousMultiValueConverter(convert); diff --git a/src/client/spark/view/Extensions/FrameworkElementExtensions.cs b/src/client/spark/view/Extensions/FrameworkElementExtensions.cs index 5e1fca80f..56bc99d47 100644 --- a/src/client/spark/view/Extensions/FrameworkElementExtensions.cs +++ b/src/client/spark/view/Extensions/FrameworkElementExtensions.cs @@ -9,6 +9,29 @@ using CompositeDisposable = System.Reactive.Disposables.CompositeDisposable; namespace mbts.ui.extensions { static class FrameworkElementHelper { + /// + /// Invokes the specified action immediately if the element is already loaded, otherwise hooks up to the event, + /// and [conditionally]invokes the specified action if returns true. + /// + /// The element that must be loaded before the specified action is invoked. + /// The action to invoke when the element is loaded. + /// If the element is not already loaded, this delegate determines whether or not to invoke the action in the Loaded event callback. + /// + public static void InvokeOnLoaded(this FrameworkElement element, Action action, Func should_still_execute) { + if (action == null) + throw new ArgumentNullException(nameof(action)); + if (element.IsLoaded) { + action(); + } else { + element.Loaded += on_loaded; + void on_loaded(object sender, RoutedEventArgs e) { + element.Loaded -= on_loaded; + if (should_still_execute()) + action(); + } + } + } + public static Point GetCenter(this FrameworkElement element) => new Point(element.ActualWidth * 0.5, element.ActualHeight * 0.5); diff --git a/src/client/spark/view/Extensions/ScrollViewerBehavior.cs b/src/client/spark/view/Extensions/ScrollViewerBehavior.cs new file mode 100644 index 000000000..e9db14fd4 --- /dev/null +++ b/src/client/spark/view/Extensions/ScrollViewerBehavior.cs @@ -0,0 +1,61 @@ +using mbts.ui.controls; +using mbts.ui.extensions; +using System.Windows; +using System.Windows.Controls; + +namespace mbts.ui { + static class ScrollViewerBehavior { + public static readonly DependencyProperty VerticalOffsetProperty = + DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ScrollViewerBehavior), new PropertyMetadata(double.NaN, OnVerticalOffsetChanged)); + + static void OnVerticalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { + if (d is ScrollViewer scroll) { + double voff = DoubleHelper.nan_cast(e.NewValue); + // Ignore small deltas to prevent strange layout rounding issues/loops. + // Be mindful that small deltas are not accumulated into a single larger delta. + if (!double.IsNaN(voff) && !DoubleHelper.are_close(DoubleHelper.nan_cast(e.OldValue), voff, DoubleUtil.MIN_SCROLL_DELTA)) { + scroll.InvokeOnLoaded(() => scroll.ScrollToVerticalOffset(voff), + () => scroll.GetValue(VerticalOffsetProperty) is double curr_voff && curr_voff == voff); + } + } + } + + public static double GetVerticalOffset(DependencyObject d) => (double)d.GetValue(VerticalOffsetProperty); + public static void SetVerticalOffset(DependencyObject d, double value) => d.SetValue(VerticalOffsetProperty, value); + + public static readonly DependencyProperty EnableTwoWayVerticalOffsetBindingProperty = + DependencyProperty.RegisterAttached("EnableTwoWayVerticalOffsetBinding", typeof(bool), typeof(ScrollViewerBehavior), new PropertyMetadata(false, OnEnableTwoWayVerticalOffsetBindingChanged)); + + static void OnEnableTwoWayVerticalOffsetBindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { + if (d is ScrollViewer scroll) { + if ((bool)e.NewValue) { + scroll.ScrollChanged += EnableTwoWayVerticalOffsetBindingUtils.on_scroll_changed; + } else { + scroll.ScrollChanged -= EnableTwoWayVerticalOffsetBindingUtils.on_scroll_changed; + } + } + } + + public static bool GetEnableTwoWayVerticalOffsetBinding(DependencyObject d) => (bool)d.GetValue(EnableTwoWayVerticalOffsetBindingProperty); + public static void SetEnableTwoWayVerticalOffsetBinding(DependencyObject d, bool value) => d.SetValue(EnableTwoWayVerticalOffsetBindingProperty, value); + + public static readonly DependencyProperty VerticalOffsetMirrorBusyProperty = + DependencyProperty.RegisterAttached("VerticalOffsetMirrorBusy", typeof(bool), typeof(ScrollViewerBehavior), new PropertyMetadata(false)); + + static class EnableTwoWayVerticalOffsetBindingUtils { + public static void on_scroll_changed(object sender, ScrollChangedEventArgs e) { + if (e.VerticalChange != 0 && sender is ScrollViewer scroll) { + if (!(bool)scroll.GetValue(VerticalOffsetMirrorBusyProperty)) { + scroll.SetValue(VerticalOffsetMirrorBusyProperty, true); + // scroll changes could get noisy while layouts are being processed; + // let everything settle down before updating the VerticalOffset binding sources/targets. + scroll.Dispatcher.InvokeAsync(() => { + scroll.SetCurrentValue(VerticalOffsetProperty, scroll.VerticalOffset); + scroll.SetValue(VerticalOffsetMirrorBusyProperty, false); + }, System.Windows.Threading.DispatcherPriority.ApplicationIdle); + } + } + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Extensions/UIElementExtensions.cs b/src/client/spark/view/Extensions/UIElementExtensions.cs index 0e5924fd2..3b48769a0 100644 --- a/src/client/spark/view/Extensions/UIElementExtensions.cs +++ b/src/client/spark/view/Extensions/UIElementExtensions.cs @@ -236,6 +236,14 @@ namespace mbts.ui.extensions { target.ScrollChanged += callback; return Disposable.Create(() => target.ScrollChanged -= callback); } + + public static bool TryScrollToVerticalOffset(this ScrollViewer scroll_viewer, double vertical_offset) { + if (vertical_offset >= 0 && scroll_viewer.IsLoaded && vertical_offset < scroll_viewer.ExtentHeight - scroll_viewer.ViewportHeight) { + scroll_viewer.ScrollToVerticalOffset(vertical_offset); + return true; + } + return false; + } } public static class TextBoxExtensions { From 75e98d353f58ed3396b45082c30a792d8de64b12 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 28 Oct 2019 15:41:39 -0500 Subject: [PATCH 4/4] adding left panel test steps --- testing/manual/settings/UserSettingsTest.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/testing/manual/settings/UserSettingsTest.txt b/testing/manual/settings/UserSettingsTest.txt index 573a14106..f1caf69c8 100644 --- a/testing/manual/settings/UserSettingsTest.txt +++ b/testing/manual/settings/UserSettingsTest.txt @@ -96,6 +96,16 @@ NOTES: 17) In dialog[A] select [DTab], then click "Copy from Custom" - [DTab] should now have the same settings as [CTab] +------------------------------------------------------------------------------- +Left Panel Scroll test +------------------------------------------------------------------------------- +The scroll position of the left-panel area is now remembered as part of the ladder settings + +Resize your ladder window to make it vertically small so that the left panel displays a scrollviewer. +Scroll the left panel down so that the qty box is the top-most visible control in the panel +Exit and relaunch Spark, loading your last saved workspace +Verify that the restored ladder window's left panel has been vertically scrolled so the the qty box is one again the top-most visble control + DONE!!!