diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 93996d4fe..ff18e3298 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -3,6 +3,7 @@ using bts.api.core; using bts.api.orderman; using bts.shell; using mbts.collections; +using mbts.persist; using mbts.ui; using mbts.ui.controls; using mbts.ui.settings; @@ -22,6 +23,7 @@ using System.Windows.Data; using System.Windows.Input; using static bts.shell.OrderBookWindowFactory; using static mbts.spark.OrderBookView; +using _GridSettingsManager = mbts.ui.GridSettingsManager; namespace mbts.spark { partial class OrderBookVM : DisposableViewModelBase, IHandleRowSelection { @@ -38,7 +40,7 @@ namespace mbts.spark { SetMap _parent_to_children = new SetMap(); SetMap _security_orders = new SetMap(); - public OrderBookVM(IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model) { + public OrderBookVM(IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model, IWindowSettingsManager settings_manager) { columns = new OrderBookColumnCollection(bus, model, _order_securities_subject); rows_view = CollectionViewSource.GetDefaultView(_order_rows); rows_view.Filter = obj => obj is OrderBookRowVM vm && vm.is_visible && vm.passes_filter; @@ -72,6 +74,7 @@ namespace mbts.spark { _dispose = Disposable.Create( bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(on_securities), model.on_orders.OnDispatcher().AddRemoveOnly().Subscribe(on_orders), + settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager), grid_settings_manager, columns); @@ -219,7 +222,7 @@ namespace mbts.spark { public OrderRecordImmutable? current_order => current_row?.order; public IEnumerable selected_orders => _selected_rows.Select(r => r.order); - public GridSettingsManager grid_settings_manager { get; } + public _GridSettingsManager grid_settings_manager { get; } public ICommand collapse_all_rows_command { get; } public ICommand expand_all_rows_command { get; } @@ -229,21 +232,21 @@ namespace mbts.spark { } public OrderBookStartupData create() { - var (grid, animations) = grid_settings_manager.create(); - return new OrderBookStartupData(columns.create(), grid, animations.Select(a => new OrderRecordTuplePredicateAnimations(a.predicate_builder, a.cell_animations, a.row_animations, a.sound_notifications))); + var gs = grid_settings_manager.create(); + return new OrderBookStartupData(columns.create(), gs.general, gs.predicate_animations.Select(a => new OrderRecordTuplePredicateAnimations(a.predicate_builder, a.cell_animations, a.row_animations, a.sound_notifications))); } public void load_from(OrderBookStartupData state) { if (state == null) return; _state = state; - grid_settings_manager.load_from((state.grid, state.predicate_animations)); + grid_settings_manager.load_from(state.grid_settings_wrapper()); columns.load_from(state.filters); } internal void remove_branch_filter_root(uint key) => _branch_filter_roots.Remove(key); - static GridSettingsManager _create_grid_settings_manager( + static _GridSettingsManager _create_grid_settings_manager( IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model, ObservableCollection rows, ICollectionView rows_view, ObservableCollection columns) { // make font-weight immutable for all columns (the font-weight rule is hard-coded: Regular for root-orders, Light for all others) @@ -255,7 +258,7 @@ namespace mbts.spark { foreach (var c in new[] { Columns.Side, Columns.Size, Columns.Filled }) { col_settings_flags[c.ToString()] = GridColumnSettingsFeatureFlags.ImmutableFontColor | GridColumnSettingsFeatureFlags.ImmutableFontWeight; } - return new GridSettingsManager( + return new _GridSettingsManager( owner_title, bus, notification_service, model, rows, rows_view, columns, () => { var pvm = PredicateBuilderVMWrapper.create( @@ -268,7 +271,9 @@ namespace mbts.spark { OrderRecordTuplePredicateVMFactory.get_oneofs(bus, model), rp => rp.predicate_type_case); pvm.selected_predicate_type = _corresponding_predicate_type(EnumHelper.parse_or_default(col_id)); return pvm; - }, notification_service.data_cache, col_settings_flags); + }, + (gen, anims) => new OrderBookGridSettings(gen, anims.Select(a => new OrderRecordTuplePredicateAnimations(a.predicate_builder, a.cell_animations, a.row_animations, a.sound_notifications))), + notification_service.data_cache, col_settings_flags); } static OrderRecordTuplePredicate.PredicateTypeOneofCase _corresponding_predicate_type(Columns col) { diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs index cce32129f..ea4c8f1dc 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs @@ -2,18 +2,27 @@ using bts.api.orderman; using bts.shell; using bts.spark; +using mbts.persist; using mbts.ui; +using mbts.ui.converters; +using mbts.ui.spark; +using mbts.ui.utility; using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using System.Windows; +using System.Windows.Data; using System.Windows.Media; +//using IOrderBookWSM = mbts.persist.IWindowSettingsManager>; +using OrderBookGridSettings = bts.shell.OrderBookWindowFactory.OrderBookGridSettings; +using OrderBookGridSettingsVM = mbts.spark.GridSettingsVM; namespace mbts.spark { internal class OrderBookWindow : SparkWindow, ISharedWindow { - const string _window_type = "OrderBook", ORDERBOOK_THREAD_ID = "OrderBook"; + const string _window_type = "OrderBook", ORDERBOOK_THREAD_ID = "OrderBook", _default_title = "Order Book"; readonly INotificationService _notification_service; + readonly IWindowSettingsManager _settings_manager; OrderBookWindowFactory.OrderBookStartupData _state; OrdersModel _model; OrderBookVM _vm; @@ -32,12 +41,14 @@ namespace mbts.spark { , _toggle_expand_all_command , _show_grid_settings_command; - public OrderBookWindow(OrdersModel model, OrderBookWindowFactory factory, IBus bus, INotificationService notification_service, OrderBookWindowFactory.OrderBookStartupData state, ImageSource icon) - : base("Order Book", bus, factory, SparkCommands.ShowOrders.icon) { + public OrderBookWindow(OrdersModel model, OrderBookWindowFactory factory, IBus bus, INotificationService notification_service, + OrderBookWindowFactory.OrderBookStartupData state, ImageSource icon, IWindowSettingsManager settings_manager) + : base(_default_title, bus, factory, SparkCommands.ShowOrders.icon) { _model = model; _factory = factory; _notification_service = notification_service; _state = state; + _settings_manager = settings_manager; } public override OrderBookWindowFactory.OrderBookStartupData SaveState => _state = _vm.create(); @@ -55,7 +66,7 @@ namespace mbts.spark { } public void thread_init() { - _vm = new OrderBookVM(this.observe_title(), _bus, _notification_service, _model); + _vm = new OrderBookVM(this.observe_title(), _bus, _notification_service, _model, _settings_manager); _vm.load_from(_state); _view = new OrderBookView(_vm); @@ -106,9 +117,28 @@ namespace mbts.spark { Icons.DownArrow, _vm.expand_all_rows_command); - _show_grid_settings_command = SparkCommands.window_settings_command(_vm.grid_settings_manager.launch_settings_editor); + _show_grid_settings_command = SparkCommands.window_settings_command(new DialogCommand(() => { + var vm = new WindowSettingsDialogVM( + _settings_manager, + us => us.order_book_grid, + UserSettings.FileExtensions.GRID, + s => _vm.grid_settings_manager.create_vm( + // we need to get the most up to date GridSettings when launching b/c users may + // have changed some settings on the grid directly, like column widths. + s.general.with(_vm.columns.Select(c => c.create())), + this.observe_title() + ) + ); + // TODO: bind to main window's Title + var dialog = new SimpleGridSettingsDialog { DataContext = vm }; + dialog.SetBinding(Window.TitleProperty, new Binding($"{nameof(vm.instance_vm)}.{nameof(OrderBookGridSettingsVM.owner_title)}") { + Converter = ValueConverter.create(str => (string.IsNullOrEmpty(str) ? _default_title : str) + " Settings") + }); + vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); + return dialog; + })); - _commands = new SparkCommand[] { + _commands = new SparkCommand[] { _show_fills_command, _modify_order_command, _cancel_selected_orders_command, diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs index c3037472b..5fe8912a8 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindowFactory.cs @@ -1,8 +1,9 @@ using bts.api; using bts.api.orderman; using bts.spark; -using Google.Protobuf.Collections; +using bts.utils; using mbts; +using mbts.persist; using mbts.spark; using mbts.spark.filters; using mbts.ui; @@ -22,19 +23,22 @@ namespace bts.shell { readonly INotificationService _notification_service; readonly INotificationServiceDataCache _notification_service_data_cache; readonly OrdersModel _model; + readonly UserSettingsHub _settings_hub; - public OrderBookWindowFactory(IShellModule owner, IBus bus, INotificationService notification_service, INotificationServiceDataCache notification_service_data_cache, OrdersModel model) : base(owner, "SparkOrderBookWindow") { + public OrderBookWindowFactory(IShellModule owner, IBus bus, INotificationService notification_service, INotificationServiceDataCache notification_service_data_cache, OrdersModel model, UserSettingsHub settings_hub) : base(owner, "SparkOrderBookWindow") { _bus = bus; _notification_service = notification_service; _notification_service_data_cache = notification_service_data_cache; _model = model; - + _settings_hub = settings_hub; _dispose = bus.subscribe(SparkTags.ShowOrderBookTag).OnDispatcher().Subscribe(show); } public override Task load(OrderBookStartupData state) { - var w = new OrderBookWindow(_model, this, _bus, _notification_service, state, SparkCommands.ShowOrders.icon) as IShellWindow; - return Task.FromResult(w); + var settings_manager = new WindowSettingsManager(_settings_hub, new OrderBookGridSettings(state.grid, state.predicate_animations), + us => us.order_book_grid, (us, s) => us.with(order_book_grid: s)); + var w = new OrderBookWindow(_model, this, _bus, _notification_service, state, SparkCommands.ShowOrders.icon, settings_manager.as_non_shared()); + return Task.FromResult(w); } async void show(OrderBookStartupData startup_data) { @@ -64,6 +68,7 @@ namespace bts.shell { } public IReadOnlyList predicate_animations => _predicate_animations ?? (_predicate_animations = new List()); + public OrderBookGridSettings grid_settings_wrapper() => new OrderBookGridSettings(grid, predicate_animations); public static OrderBookStartupData Default => new OrderBookStartupData( @@ -171,5 +176,38 @@ namespace bts.shell { [ProtoMember(20)] public string[] symbols; } + + [ProtoContract] + public class OrderBookGridSettings : IGridSettingsWithNotifications, IEquatable { + [ProtoMember(1)] + public GridSettings general { get; set; } + [ProtoMember(2)] + List _predicate_animations; + + public OrderBookGridSettings() { } + public OrderBookGridSettings(GridSettings general = default, IEnumerable predicate_animations = null) { + this.general = general; + _predicate_animations = new List(); + if (predicate_animations != null) + _predicate_animations.AddRange(predicate_animations); + } + + public IReadOnlyList predicate_animations => _predicate_animations ?? (_predicate_animations = new List()); + IEnumerable> IGridSettingsWithNotifications.predicate_animations => predicate_animations; + + public OrderBookGridSettings with(GridSettings general) => + new OrderBookGridSettings(general ?? this.general, predicate_animations); + + public bool Equals(OrderBookGridSettings other) => + other != null && + EqualityComparer.null_check_equals(general, other.general) && + EqualityComparer.safe_sequence_equals(predicate_animations, other.predicate_animations); + + public override bool Equals(object obj) => Equals(obj as OrderBookGridSettings); + public override int GetHashCode() => + EqualityComparer.aggregate_hash( + EqualityComparer.safe_get_hash_code(general), + EqualityComparer.safe_aggregate_hash(predicate_animations)); + } } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/TradingShellModule.cs b/src/client/spark/Modules/Trading/TradingShellModule.cs index eede04c5f..22a6c2f14 100644 --- a/src/client/spark/Modules/Trading/TradingShellModule.cs +++ b/src/client/spark/Modules/Trading/TradingShellModule.cs @@ -23,7 +23,7 @@ namespace bts.shell { public TradingShellModule(IBus bus, IWindowManager wm, INotificationService notification_service, INotificationServiceDataCache notification_service_data_cache, UserSettingsHub settings_hub, OrdersModel orders_model, PAndLModel pandl_model, MarketDataAuxiliaryModel md_auxiliary_model) { _lwf = new LadderWindowFactory(this, bus, wm, settings_hub, orders_model, md_auxiliary_model); - _obwf = new OrderBookWindowFactory(this, bus, notification_service, notification_service_data_cache, orders_model); + _obwf = new OrderBookWindowFactory(this, bus, notification_service, notification_service_data_cache, orders_model, settings_hub); _fwf = new FillsWindowFactory(this, bus, settings_hub, orders_model); _plwf = new PAndLWindowFactory(this, bus, settings_hub, pandl_model); } diff --git a/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs b/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs index 7fbbfac3d..3f08f80d2 100644 --- a/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs +++ b/src/client/spark/core/ShellInterface/UserSettings/UserSettings.cs @@ -1,5 +1,4 @@ -using bts.proto; -using Google.Protobuf.WellKnownTypes; +using Google.Protobuf.WellKnownTypes; using mbts.ui.settings; using ProtoBuf; using System; @@ -17,7 +16,8 @@ namespace mbts.spark { _market_view_grid = bts.shell.MarketViewWindowFactory.StartupData.DefaultGridSettings, _fills_grid = bts.shell.FillsWindowFactory.StartupData.DefaultGridSettings, _time_and_sales_grid = bts.shell.TimeAndSalesWindowFactory.StartupData.DefaultGridSettings, - _synthetic_management_grid = bts.shell.SyntheticManagementWindowFactory.StartupData.DefaultGridSettings + _synthetic_management_grid = bts.shell.SyntheticManagementWindowFactory.StartupData.DefaultGridSettings, + _order_book_grid = bts.shell.OrderBookWindowFactory.OrderBookStartupData.Default.grid_settings_wrapper(), }; [ProtoMember(1)] @@ -36,6 +36,8 @@ namespace mbts.spark { GridSettings _synthetic_management_grid; [ProtoMember(9)] GridSettings _pnl_grid; + [ProtoMember(10)] + bts.shell.OrderBookWindowFactory.OrderBookGridSettings _order_book_grid; public UserSettings() { } @@ -46,6 +48,7 @@ namespace mbts.spark { public GridSettings fills_grid => _fills_grid ?? (_fills_grid = bts.shell.FillsWindowFactory.StartupData.DefaultGridSettings); public GridSettings synthetic_management_grid => _synthetic_management_grid ?? (_synthetic_management_grid = bts.shell.SyntheticManagementWindowFactory.StartupData.DefaultGridSettings); public GridSettings pnl_grid => _pnl_grid ?? (_pnl_grid = bts.shell.PAndLWindowFactory.PAndLStartupData.DefaultGridSettings); + public bts.shell.OrderBookWindowFactory.OrderBookGridSettings order_book_grid => _order_book_grid ?? (_order_book_grid = bts.shell.OrderBookWindowFactory.OrderBookStartupData.Default.grid_settings_wrapper()); public UserSettings with(LadderSettings ladder = null, NotificationSettings notification = null, @@ -53,7 +56,8 @@ namespace mbts.spark { GridSettings time_and_sales = null, GridSettings provided_fills_gridsettings = null, GridSettings provided_synthetic_management_gridsettings = null, - GridSettings provided_pnl_gridsettings = null) => + GridSettings provided_pnl_gridsettings = null, + bts.shell.OrderBookWindowFactory.OrderBookGridSettings order_book_grid = null) => new UserSettings { _ladder = ladder ?? _ladder, _notification = notification ?? _notification, @@ -62,6 +66,7 @@ namespace mbts.spark { _fills_grid = provided_fills_gridsettings ?? _fills_grid, _synthetic_management_grid = provided_synthetic_management_gridsettings ?? _synthetic_management_grid, _pnl_grid = provided_pnl_gridsettings ?? _pnl_grid, + _order_book_grid = order_book_grid ?? _order_book_grid }; public static class FileExtensions { diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs index 57917bf1c..99e12a66f 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs @@ -27,7 +27,7 @@ namespace mbts.spark { ImmutableFontWeight } - partial class GridSettingsVM { + partial class GridSettingsVM { public interface IColumnVM : ISimpleColumnVM, IValueFactory<(GridColumnSettings settings, ILookup animations)>, IDisposable { ObservableEditableCollection predicate_animations { get; } new (GridColumnSettings settings, ILookup animations) create(); @@ -35,7 +35,7 @@ namespace mbts.spark { } public class ColumnVM : DisposableViewModelBase, IColumnVM { - public readonly GridSettingsVM owner; + public readonly GridSettingsVM owner; readonly Property _id, _display_name; readonly Property _width; @@ -44,7 +44,7 @@ namespace mbts.spark { readonly Property _font_weight_override; GridColumnSettings _settings; - public ColumnVM(GridSettingsVM owner) { + public ColumnVM(GridSettingsVM owner) { this.owner = owner; _id = backing(nameof(id), string.Empty); _display_name = backing(nameof(display_name), string.Empty); diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs index 01cf24d39..f77686dbf 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs @@ -17,10 +17,10 @@ using System.Windows.Media; using static mbts.ui.controls.DIYGrid; namespace mbts.ui { - class GridSettingsManager : - ViewModelBase, IValueFactory<(GridSettings grid, IEnumerable> predicate_animations_data)>, IDisposable + class GridSettingsManager : ViewModelBase, IValueFactory, IObserver, IDisposable where TRow : IRowVM - where TPredicateBuilder : IPredicateBuilder, new() { + where TPredicateBuilder : IPredicateBuilder, new() + where TSettingsData : class, IGridSettingsWithNotifications { // public readonly ObservableCollection live_sorting_props; @@ -39,12 +39,18 @@ namespace mbts.ui { readonly DelegateProperty _font_color, _selection_highlight; readonly SetMap, AnimationDefinitionWrapper> _animation_defs = new SetMap, AnimationDefinitionWrapper>(); readonly TPredicateBuilderArg _arg; + readonly Func> _create_predicate_vm; + readonly Func> _create_column_predicate_vm; + readonly Func>, TSettingsData> _create_settings_obj; + readonly IReadOnlyDictionary _column_flags; IEnumerable> _predicate_animations_data; public GridSettingsManager( IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel orders_model, ObservableCollection rows, ICollectionView rows_view, ObservableCollection columns, Func> create_predicate_vm, - Func> create_column_predicate_vm, TPredicateBuilderArg arg, IReadOnlyDictionary column_flags = null) { + Func> create_column_predicate_vm, + Func>, TSettingsData> create_settings_obj, + TPredicateBuilderArg arg, IReadOnlyDictionary column_flags = null) { // _bus = bus; _notification_service = notification_service; @@ -52,7 +58,11 @@ namespace mbts.ui { _rows = rows; _rows_view = rows_view; _columns = columns; + _create_predicate_vm = create_predicate_vm; + _create_column_predicate_vm = create_column_predicate_vm; + _create_settings_obj = create_settings_obj; _arg = arg; + _column_flags = column_flags.ToDictionary(kv => kv.Key, kv => ImmutableColumnSettingStore.create(kv.Value)); _grid_settings = backing(nameof(grid_settings), GridSettings.Default); _font_family = backing(nameof(font_family), () => new FontFamily(grid_settings.font_family), nameof(grid_settings)); _font_weight = backing(nameof(font_weight), () => grid_settings.font_weight, nameof(grid_settings)); @@ -72,10 +82,10 @@ namespace mbts.ui { return pa; } Window create_settings_view() { - var vm = new GridSettingsVM(owner_title, _bus, _orders_model, create_predicate_vm, create_column_predicate_vm); + var vm = new GridSettingsVM(owner_title, _bus, _orders_model, create_predicate_vm, create_column_predicate_vm, _create_settings_obj); // need to refresh before launching the settings-view bc users can chanve some grid-column-settings properties directly from the grid (e.g. width, is_collapsed, etc.) _grid_settings.set(grid_settings.with(columns.Select(c => c.create()))); - vm.load_from((grid_settings, _predicate_animations_data)); + vm.load(_create_settings_obj(grid_settings, _predicate_animations_data)); if (column_flags != null) { foreach (var col in vm.columns) { if (column_flags.TryGetValue(col.id, out GridColumnSettingsFeatureFlags f)) @@ -84,10 +94,10 @@ namespace mbts.ui { } var window = new GridSettingsDialog { DataContext = vm }; vm.committed += () => { - var (grid, predicate_animations_data) = vm.create(); + TSettingsData sd = vm.get_result(); // IMPORTANT: don't overwrite current sorting - grid = grid.with(sorting_criterion: rows_view.get_sorting_criteria(columns)); - load_from((grid, predicate_animations_data)); + sd = _create_settings_obj(sd.general.with(sorting_criterion: rows_view.get_sorting_criteria(columns)), sd.predicate_animations); + load_from(sd); }; vm.RequestClose += (s, e) => WindowUtilities.force_close(window); return window; @@ -107,22 +117,30 @@ namespace mbts.ui { public SolidColorBrush selection_highlight => _selection_highlight.Value; public IDialogCommand launch_settings_editor { get; } - public (GridSettings grid, IEnumerable> predicate_animations_data) create() { - // IMPORTANT: don't overwrite current sorting - _grid_settings.set(grid_settings.with(_columns.Select(c => c.create()), _rows_view.get_sorting_criteria(_columns))); - return (grid_settings, _predicate_animations_data); + public GridSettingsVM create_vm(GridSettings settings, IObservable title) { + var vm = new GridSettingsVM(title, _bus, _orders_model, _create_predicate_vm, _create_column_predicate_vm, _create_settings_obj, _column_flags.ToDictionary(kv => kv.Key, kv => kv.Value.flags)); + vm.load(_create_settings_obj(settings, _predicate_animations_data)); + return vm; } - public void load_from((GridSettings grid, IEnumerable> predicate_animations_data) value) { + public TSettingsData create() { + // IMPORTANT: don't overwrite current sorting + _grid_settings.set(grid_settings.with(_columns.Select(c => c.create()), _rows_view.get_sorting_criteria(_columns))); + return _create_settings_obj(grid_settings, _predicate_animations_data); + } + + public void load_from(TSettingsData value) => _load_from_core(value, true); + + void _load_from_core(TSettingsData value, bool reset_immutable_flag_stores) { Disposable.dispose(_notification_subs); - var grid = value.grid ?? GridSettings.Default; + var grid = value.general ?? GridSettings.Default; _grid_settings.set(grid); foreach (ColumnBase col in _columns) col.set_fallback_font_params(new FontFamily(grid.font_family), grid.font_size, grid.font_color.rgb_color(), grid.font_weight); #pragma warning disable CS0612 // Type or member is obsolete _rows_view.set_sort_descriptions(grid.sorting_criterion, _columns); #pragma warning restore CS0612 // Type or member is obsolete - _predicate_animations_data = value.predicate_animations_data; + _predicate_animations_data = value.predicate_animations; _animation_defs.clear(); foreach (var pa in _predicate_animations_data) { var predicate = pa.predicate_builder.create_predicate(_arg); @@ -142,6 +160,12 @@ namespace mbts.ui { // ** in the unfortunate event that we botch backwards compatability, and the columns don't bear a 1:1 relationship with the column-settings // don't add insult to injury by crashing ** if (grid_settings.columns != null && _columns != null) { + if (reset_immutable_flag_stores) { + foreach (var kv in _column_flags) { + if (grid.columns.TryGetFirst(c => c.id == kv.Key, out GridColumnSettings cs)) + kv.Value.store(cs); + } + } var col_settings_id_map = grid_settings.columns.ToDictionary(cs => id_key(cs.id)); var columns_id_map = _columns.ToDictionary(c => id_key(c.id)); if (col_settings_id_map.Count == columns_id_map.Count && col_settings_id_map.Count == grid_settings.columns.Count) { @@ -161,5 +185,21 @@ namespace mbts.ui { IGridColumn lookup_column(string col_id) => _columns.OfType>().FirstOrDefault(c => string.Equals(col_id, c.id, StringComparison.CurrentCultureIgnoreCase)); } + + void IObserver.OnNext(TSettingsData sd) { + var grid = sd.general; + if (grid.columns != null) { + var cols_with_applied_flags = grid.columns.ToList(); + for (int i = 0; i < cols_with_applied_flags.Count; i++) { + if (_column_flags.TryGetValue(cols_with_applied_flags[i].id, out ImmutableColumnSettingStore store)) { + cols_with_applied_flags[i] = store.apply(cols_with_applied_flags[i]); + } + } + sd = _create_settings_obj(grid.with(cols_with_applied_flags), sd.predicate_animations); + } + _load_from_core(sd, false); + } + void IObserver.OnError(Exception error) { } + void IObserver.OnCompleted() { } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs index e48955331..4e280d9e6 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs @@ -1,5 +1,6 @@ using bts.api; using bts.shell; +using bts.utils; using mbts.collections; using mbts.collections.extensions; using mbts.ui; @@ -18,14 +19,38 @@ using CellAnimation = mbts.ui.CellAnimation; using FontFamily = System.Windows.Media.FontFamily; namespace mbts.spark { - interface IGridSettingsFactory : IValueFactory<(GridSettings grid, IEnumerable> predicate_animations_data)> - where TPredicateBuilder : IPredicateBuilder, new() { } + interface IGridSettingsWithNotifications where TPredicateBuilder : IPredicateBuilder { + GridSettings general { get; } + IEnumerable> predicate_animations { get; } + } - partial class GridSettingsVM : DisposableViewModelBase, IGridSettingsFactory, IRequestViewClose - where TPredicateBuilder : IPredicateBuilder, new() { + class GridSettingsWithNotifications : IEquatable> where TPredicateBuilder : IPredicateBuilder { + public readonly GridSettings grid; + public readonly IEnumerable> predicate_animations_data; + public GridSettingsWithNotifications(GridSettings grid, IEnumerable> predicate_animations_data) { + this.grid = grid; + this.predicate_animations_data = predicate_animations_data; + } + + public bool Equals(GridSettingsWithNotifications other) => + other != null && + EqualityComparer.null_check_equals(grid, other.grid) && + EqualityComparer.safe_sequence_equals(predicate_animations_data, other.predicate_animations_data); + + public override bool Equals(object obj) => Equals(obj as GridSettingsWithNotifications); + public override int GetHashCode() => + EqualityComparer.aggregate_hash( + EqualityComparer.safe_get_hash_code(grid), + EqualityComparer.safe_aggregate_hash(predicate_animations_data)); + } + + partial class GridSettingsVM : DisposableViewModelBase, IRequestViewClose, IWindowSettingsVM + where TPredicateBuilder : IPredicateBuilder, new() + where TSettingsData :class, IGridSettingsWithNotifications { // protected readonly Func> _create_predicate_vm; protected readonly Func> _create_column_predicate_vm; + readonly Func>, TSettingsData> _create_settings_obj; readonly Property _current_column; readonly Property _font_family; readonly Property _font_weight; @@ -33,16 +58,22 @@ namespace mbts.spark { readonly Property _font_size; readonly Property _show_predicate_animations, _show_cell_predicate_animations; readonly OrdersModel _orders_model; + readonly IReadOnlyDictionary _column_flags; public GridSettingsVM( IObservable owner_title, IBus bus, OrdersModel orders_model, - Func> create_predicate_vm, Func> create_column_predicate_vm) { + Func> create_predicate_vm, + Func> create_column_predicate_vm, + Func>, TSettingsData> create_settings_obj, + IReadOnlyDictionary column_flags = null) { // _owner_title = backing(nameof(owner_title), owner_title); this.bus = bus; _orders_model = orders_model; _create_predicate_vm = create_predicate_vm; _create_column_predicate_vm = create_column_predicate_vm; + _create_settings_obj = create_settings_obj; + _column_flags = column_flags ?? new Dictionary(); _font_family = backing(nameof(font_family), TextUtils.StandardFontFamily); _font_size = backing(nameof(font_size), FontSizes.Small); _font_weight = backing(nameof(font_weight), FontWeights.Normal); @@ -81,7 +112,7 @@ namespace mbts.spark { new DialogSkirtItem("Apply", new DelegateCommand(() => committed?.Invoke())) }; - public (GridSettings grid, IEnumerable> predicate_animations_data) create() { + public TSettingsData get_result() { var animation_map = predicate_animations .Select(p => p.create()) .GroupBy(d => d.predicate) @@ -95,22 +126,22 @@ namespace mbts.spark { foreach (IGrouping g in animations) animation_map.AddOrUpdate(g.Key, p => animation_tuple(c: g), (p, t) => t.cell_animations.AddRange(g)); } - return (new GridSettings(font_family.name(), font_size, font_weight, font_color.color.rgb_color(), selection_highlight.to_proto(), col_settings), + return _create_settings_obj(new GridSettings(font_family.name(), font_size, font_weight, font_color.color.rgb_color(), selection_highlight.to_proto(), col_settings), animation_map.Select(kv => PredicateAnimations.create_data(kv.Key, kv.Value.cell_animations, kv.Value.row_animations, kv.Value.sounds))); // (List row_animations, List sounds, List cell_animations) animation_tuple(IEnumerable r = null, IEnumerable s = null, IGrouping c = null) => (r?.ToList() ?? new List(), s?.ToList() ?? new List(), c?.ToList() ?? new List()); } - public void load_from((GridSettings grid, IEnumerable> predicate_animations_data) value) { - _font_family.set(new FontFamily(value.grid.font_family)); - _font_weight.set(value.grid.font_weight); - font_color.load_from(value.grid.font_color.rgb_solid_brush()); - selection_highlight.load_from(value.grid.selection_highlight.to_brush()); - _font_size.set(value.grid.font_size); + public void load(TSettingsData sd) { + _font_family.set(new FontFamily(sd.general.font_family)); + _font_weight.set(sd.general.font_weight); + font_color.load_from(sd.general.font_color.rgb_solid_brush()); + selection_highlight.load_from(sd.general.selection_highlight.to_brush()); + _font_size.set(sd.general.font_size); var col_animations = new Dictionary>>(); predicate_animations.Clear(); - foreach (var pa in value.predicate_animations_data) { + foreach (var pa in sd.predicate_animations) { if (pa.row_animations.Count > 0 || pa.sound_notifications.Count > 0) { var vm = new PredicateRowAnimationVM(_create_predicate_vm()); vm.load_from((pa.predicate_builder, pa.row_animations, pa.sound_notifications)); @@ -123,9 +154,14 @@ namespace mbts.spark { } } var curr_col_id = current_column?.id; - columns.SetItems(value.grid.columns.Select(col => { + columns.SetItems(sd.general.columns.Select(col => { var vm = new ColumnVM(this); vm.load_from((col, col_animations.GetValueOrDefault(col.id)?.ToFlatLookup(kv => kv.Key, kv => kv.Value))); + // reapply our favorite column flags + // this comes in handy when performing Load From Factory - the editor will contain + // a consistent view of what can be overridden and what cannot + if (_column_flags.TryGetValue(vm.id, out GridColumnSettingsFeatureFlags flags)) + vm.apply_flags(flags); return vm; })); if (curr_col_id == null || !columns.TryGetFirst(c => c.id == curr_col_id, out IColumnVM curr_column)) diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/ImmutableColumnSettingsStore.cs b/src/client/spark/view/Controls/DIYGrid/Settings/ImmutableColumnSettingsStore.cs new file mode 100644 index 000000000..6e7b4e4f6 --- /dev/null +++ b/src/client/spark/view/Controls/DIYGrid/Settings/ImmutableColumnSettingsStore.cs @@ -0,0 +1,72 @@ +using bts.proto; +using mbts.spark; +using mbts.ui.settings; +using System.Collections.Generic; + +namespace mbts.ui { + abstract class ImmutableColumnSettingStore { + public readonly GridColumnSettingsFeatureFlags flags; + + protected ImmutableColumnSettingStore(GridColumnSettingsFeatureFlags flags) => + this.flags = flags; + + public static ImmutableColumnSettingStore create(GridColumnSettingsFeatureFlags flags) { + var stores_per_flag = new List(); + if ((flags & GridColumnSettingsFeatureFlags.ImmutableFontColor) != 0) { + stores_per_flag.Add(new ImmutableFontColorOverrideStore()); + } + if ((flags & GridColumnSettingsFeatureFlags.ImmutableFontWeight) != 0) { + stores_per_flag.Add(new ImmutableFontWeightOverrideStore()); + } + return new CompositeImmutableSettingsStore(stores_per_flag, flags); + } + + public abstract GridColumnSettings apply(GridColumnSettings settings); + public abstract void store(GridColumnSettings settings); + + class ImmutableFontColorOverrideStore : ImmutableColumnSettingStore { + NullableColorProto _value; + + public ImmutableFontColorOverrideStore() + : base(GridColumnSettingsFeatureFlags.ImmutableFontColor) { } + + public override GridColumnSettings apply(GridColumnSettings settings) => + settings.with(font_color_override: _value); + + public override void store(GridColumnSettings settings) => + _value = new NullableColorProto(settings.font_color_override); + } + + class ImmutableFontWeightOverrideStore : ImmutableColumnSettingStore { + NullableFontWeightProto _value; + + public ImmutableFontWeightOverrideStore() + : base(GridColumnSettingsFeatureFlags.ImmutableFontWeight) { } + + public override GridColumnSettings apply(GridColumnSettings settings) => + settings.with(font_weight_override: _value); + + public override void store(GridColumnSettings settings) => + _value = new NullableFontWeightProto(settings.font_weight_override); + } + + class CompositeImmutableSettingsStore : ImmutableColumnSettingStore { + readonly IReadOnlyList _stores; + public CompositeImmutableSettingsStore(IReadOnlyList stores, GridColumnSettingsFeatureFlags flags) + : base(flags) => _stores = stores; + + public override GridColumnSettings apply(GridColumnSettings settings) { + foreach (var s in _stores) { + settings = s.apply(settings); + } + return settings; + } + + public override void store(GridColumnSettings settings) { + foreach (var s in _stores) { + s.store(settings); + } + } + } + } +} diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs index 4072621bd..e1b1d22bc 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs @@ -1,13 +1,10 @@ -using bts.proto; -using mbts.persist; -using mbts.spark; +using mbts.spark; using mbts.ui.settings; using mbts.ui.shell; using mbts.utils; using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.ComponentModel; using System.Linq; using System.Reactive.Linq; using System.Windows; @@ -120,70 +117,5 @@ namespace mbts.ui { string id_key(string id) => id.ToLower(); } } - - abstract class ImmutableColumnSettingStore { - public readonly GridColumnSettingsFeatureFlags flags; - - protected ImmutableColumnSettingStore(GridColumnSettingsFeatureFlags flags) => - this.flags = flags; - - public static ImmutableColumnSettingStore create(GridColumnSettingsFeatureFlags flags) { - var stores_per_flag = new List(); - if ((flags & GridColumnSettingsFeatureFlags.ImmutableFontColor) != 0) { - stores_per_flag.Add(new ImmutableFontColorOverrideStore()); - } if ((flags & GridColumnSettingsFeatureFlags.ImmutableFontWeight) != 0) { - stores_per_flag.Add(new ImmutableFontWeightOverrideStore()); - } - return new CompositeImmutableSettingsStore(stores_per_flag, flags); - } - - public abstract GridColumnSettings apply(GridColumnSettings settings); - public abstract void store(GridColumnSettings settings); - - class ImmutableFontColorOverrideStore : ImmutableColumnSettingStore { - NullableColorProto _value; - - public ImmutableFontColorOverrideStore() - : base(GridColumnSettingsFeatureFlags.ImmutableFontColor) { } - - public override GridColumnSettings apply(GridColumnSettings settings) => - settings.with(font_color_override: _value); - - public override void store(GridColumnSettings settings) => - _value = new NullableColorProto(settings.font_color_override); - } - - class ImmutableFontWeightOverrideStore : ImmutableColumnSettingStore { - NullableFontWeightProto _value; - - public ImmutableFontWeightOverrideStore() - : base(GridColumnSettingsFeatureFlags.ImmutableFontWeight) { } - - public override GridColumnSettings apply(GridColumnSettings settings) => - settings.with(font_weight_override: _value); - - public override void store(GridColumnSettings settings) => - _value = new NullableFontWeightProto(settings.font_weight_override); - } - - class CompositeImmutableSettingsStore : ImmutableColumnSettingStore { - readonly IReadOnlyList _stores; - public CompositeImmutableSettingsStore(IReadOnlyList stores, GridColumnSettingsFeatureFlags flags) - : base(flags) => _stores = stores; - - public override GridColumnSettings apply(GridColumnSettings settings) { - foreach (var s in _stores) { - settings = s.apply(settings); - } - return settings; - } - - public override void store(GridColumnSettings settings) { - foreach (var s in _stores) { - s.store(settings); - } - } - } - } } } diff --git a/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs b/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs index cf105a417..5c5867fb3 100644 --- a/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs +++ b/src/client/spark/view/Spark/Utilities/WindowSettingsDialogVM.cs @@ -15,6 +15,7 @@ namespace mbts.spark { readonly Property _use_default; readonly DialogSkirtItem _ok_item, _apply_item, _cancel_item, _apply_to_defaults_item; readonly DelegateProperty> _left_skirt_items; + readonly List _resources = new List(2); // initialize with 2 capacity bc the only potential disposables are the instance/default vms public WindowSettingsDialogVM(IWindowSettingsManager settings_manager, Func get_factory_settings, string settings_file_ext, Func vm_factory) { // by default, each new window will use their own instance settings @@ -51,7 +52,11 @@ namespace mbts.spark { apply(); close(); }), ui.spark.LadderSettingsDialog.ResetToFactoryKey); - + // maybe dispose vms + if (instance_vm is IDisposable dispose_ivm) + _resources.Add(dispose_ivm); + if (default_vm is IDisposable dispose_dvm) + _resources.Add(dispose_dvm); // local methods void apply() { if (!use_default) { @@ -90,5 +95,8 @@ namespace mbts.spark { get => _use_default.Value; set => _use_default.Value = value; } + + public void Dispose() => + Disposable.dispose(_resources); } } \ No newline at end of file