diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 612a0adeb..983d4b509 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -156,7 +156,7 @@ namespace mbts.spark { vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); return dialog; })); - grid_settings_manager = new OrderBookGridSettingsManager(bus, notification_service, model, rows, core_columns); + grid_settings_manager = new OrderBookGridSettingsManager(bus, notification_service, rows, core_columns); grid_settings_manager.algo_tree_display_mode_changed += _on_filter_changed; IDisposable attach_vm_hooks = null; begin_attaching += on_begin_attaching; @@ -261,7 +261,8 @@ namespace mbts.spark { }), _columns); if (settings_manager != null) { - concat_disposable(settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager)); + concat_disposable( + settings_manager.subscribe_current().OnDispatcher().Subscribe(grid_settings_manager.load_from)); } IObservable> mk_fills_slave_predicate_src() => Observable.Create>(o => { diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs index b416e6937..77f922d0b 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs @@ -176,8 +176,10 @@ namespace mbts.spark { return _vm.grid_settings_manager.create_vm(s); } ); - gs_vm.SubscribeDisposed(literal_title_source.OnDispatcher().Subscribe(t => gs_vm.title = t)); var dialog = new ShellSettingsDialog { DataContext = gs_vm }; + gs_vm.SubscribeDisposed(Disposable.Create( + literal_title_source.OnDispatcher().Subscribe(t => gs_vm.title = t), + _bus.subscribe(SparkTags.SecuritiesTag).OnDispatcher().Subscribe(sec_map => dialog.Resources[ResourceKeys.Securities] = sec_map))); gs_vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); return dialog; })); diff --git a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettings.cs b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettings.cs index c2bd22935..82cc27090 100644 --- a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettings.cs +++ b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettings.cs @@ -7,6 +7,7 @@ using ProtoBuf; using System; using System.Collections.Generic; using System.ComponentModel; +using System.Linq; using GridSettings = mbts.ui.settings.GridSettings; namespace bts.shell.orderbook { @@ -42,8 +43,8 @@ namespace bts.shell.orderbook { 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 OrderBookGridSettings with(GridSettings general = null, IEnumerable predicate_animations = null) => + new OrderBookGridSettings(general ?? this.general, predicate_animations?.ToList() ?? this.predicate_animations) { algo_tree_display_mode = algo_tree_display_mode }; diff --git a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettingsManager.cs b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettingsManager.cs index 8500d5c5d..cffe1eb6d 100644 --- a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettingsManager.cs +++ b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettingsManager.cs @@ -1,23 +1,27 @@ using bts.api; -using bts.api.orderman; using mbts; using mbts.spark; using mbts.ui; using mbts.ui.controls; -using mbts.ui.settings; using mbts.ui.utility; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using Columns = mbts.spark.OrderBookGridView.ColumnKeys; +using CompositeDisposable = System.Reactive.Disposables.CompositeDisposable; namespace bts.shell.orderbook { - class OrderBookGridSettingsManager : IDisposable, IObserver { + class OrderBookGridSettingsManager : IDisposable { public static readonly IReadOnlyDictionary hard_column_flags; - readonly IObserver _core_man_as_observer; + readonly IEnumerable _rows; + readonly ObservableCollection _active_predicate_animations = + new ObservableCollection(); + + IReadOnlyList _predicate_animations; AlgoTreeDisplayMode _algo_tree_display_mode; + IDisposable _sound_hooks; static OrderBookGridSettingsManager() { // make font-weight immutable for all columns (the font-weight rule is hard-coded: Regular for root-orders, Light for all others) @@ -36,36 +40,22 @@ namespace bts.shell.orderbook { public OrderBookGridSettingsManager( IBus bus, INotificationService notification_service, - OrdersModel model, IEnumerable rows, ObservableCollection columns) { - core_manager = new GridSettingsManager( - bus, model, rows, columns, - () => new OrderRecordTuplePredicate(), - () => { - var pvm = PredicateVMWrapper.create( - OrderRecordTuplePredicateVMFactory.get_oneofs(bus, model), rp => rp.predicate_type_case); - pvm.selected_predicate_type = pvm.predicate_types.FirstOrDefault(); - return pvm; - }, - col_id => { - var pvm = PredicateVMWrapper.create( - 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; - }, - (gen, anims) => new OrderBookGridSettings(gen, anims.empty_if_null().Select(a => new OrderRecordTuplePredicateAnimations(a.predicate_data, a.cell_animations, a.row_animations, a.sound_notifications))) { algo_tree_display_mode = _algo_tree_display_mode }, - p => p.create_predicate(notification_service.data_cache), - p => p.create_notification_predicate(notification_service.data_cache), - notification_service == null ? new Func, SoundNotification, IDisposable>((p, sn) => Disposable.empty) : notification_service.link_to_order, - notification_service == null ? new Func, IDisposable>(t => Disposable.empty) : notification_service.link_to_order, - hard_column_flags); - _core_man_as_observer = core_manager; + _rows = rows; + core_manager = new GridSettingsManager(columns, hard_column_flags); + _sound_hooks = ObservableAutoWrapper.connect_item_hooks( + _active_predicate_animations, + a => { + var pred = a.predicate_data.create_predicate(notification_service.data_cache); + return new CompositeDisposable( + a.sound_notifications.Select(s => notification_service.link_to_order(pred, s))); + }); } public event Action algo_tree_display_mode_changed; - public GridSettingsManager core_manager { get; } + public GridSettingsManager core_manager { get; } public AlgoTreeDisplayMode algo_tree_display_mode { get => _algo_tree_display_mode; @@ -78,23 +68,29 @@ namespace bts.shell.orderbook { } public void load_from(OrderBookGridSettings gs) { - core_manager.load_from(gs); + core_manager.load_from(gs.general); algo_tree_display_mode = gs.algo_tree_display_mode; + _predicate_animations = gs.predicate_animations; + _active_predicate_animations.sync_with(_predicate_animations.Where(p => p.is_enabled)); + foreach (var r in _rows) + r.raise_invalidated(); } - public OrderBookGridSettings create(IEnumerable sorting_criterion) { - var gs = core_manager.create(); - gs.general = gs.general.with(sorting_criterion: sorting_criterion); - gs.algo_tree_display_mode = _algo_tree_display_mode; - return gs; - } - - public OrderBookSettingsVM create_vm(OrderBookGridSettings gs) => - new OrderBookSettingsVM(core_manager.create_vm(gs.general)) { - algo_tree_display_mode = gs.algo_tree_display_mode + public OrderBookGridSettings create(IEnumerable sorting_criterion) => + new OrderBookGridSettings( + core_manager.grid_settings, + _predicate_animations.ToList()) { + algo_tree_display_mode = _algo_tree_display_mode }; - public void Dispose() => core_manager.Dispose(); + public OrderBookSettingsVM create_vm(OrderBookGridSettings gs) { + var vm = new OrderBookSettingsVM(core_manager.create_vm()); + vm.load(gs); + return vm; + } + + public void Dispose() => + Disposable.dispose(ref _sound_hooks); static OrderRecordTuplePredicate.PredicateTypeOneofCase _corresponding_predicate_type(Columns col) { switch (col) { @@ -122,12 +118,5 @@ namespace bts.shell.orderbook { default: return default; } } - - void IObserver.OnNext(OrderBookGridSettings value) { - _core_man_as_observer.OnNext(value); - algo_tree_display_mode = value.algo_tree_display_mode; - } - void IObserver.OnError(Exception error) => _core_man_as_observer.OnError(error); - void IObserver.OnCompleted() => _core_man_as_observer.OnCompleted(); } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookSettingsVM.cs b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookSettingsVM.cs index 8b3dd2b44..63a6adb73 100644 --- a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookSettingsVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookSettingsVM.cs @@ -1,46 +1,143 @@ -using bts.api; -using bts.api.orderman; -using mbts; -using mbts.spark; -using mbts.ui; -using mbts.ui.settings; -using mbts.ui.shell; -using mbts.ui.spark; -using System; -using System.Collections.Generic; - -namespace bts.shell.orderbook { - class OrderBookSettingsVM : DisposableViewModelBase, IWindowSettingsVM, ITemplateSelectorKey { - AlgoTreeDisplayMode _algo_tree_display_mode; - - public OrderBookSettingsVM(GridSettingsVM core_settings_vm) => - this.core_settings_vm = core_settings_vm; - - public OrderBookSettingsVM( - IBus bus, - OrdersModel orders_model, - Func predicate_data_factory, - Func> notification_predicate_factory, - Func> create_predicate_vm, - Func> create_column_predicate_vm, - Func>, OrderBookGridSettings> create_settings_obj, - IReadOnlyDictionary column_flags = null) { - core_settings_vm = new GridSettingsVM( - bus, orders_model, predicate_data_factory, notification_predicate_factory, create_predicate_vm, create_column_predicate_vm, create_settings_obj, column_flags); - } - - public GridSettingsVM core_settings_vm { get; } - public AlgoTreeDisplayMode algo_tree_display_mode { get => _algo_tree_display_mode; set => Set(ref _algo_tree_display_mode, value); } - string ITemplateSelectorKey.key => GridSettingsTemplateKeys.OrderBook; - - public void load(OrderBookGridSettings grid_settings) { - core_settings_vm.load(grid_settings); - algo_tree_display_mode = grid_settings.algo_tree_display_mode; - } - public OrderBookGridSettings get_result() { - var gs = core_settings_vm.get_result(); - gs.algo_tree_display_mode = algo_tree_display_mode; - return gs; - } - } +using bts.api; +using bts.utils; +using mbts; +using mbts.spark; +using mbts.ui; +using mbts.ui.shell; +using mbts.ui.spark; +using mbts.ui.viewmodels; +using System.Collections; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Windows.Input; + +namespace bts.shell.orderbook { + class OrderBookSettingsVM : DisposableViewModelBase, IWindowSettingsVM, ITemplateSelectorKey { + readonly BooleanParameterVM _show_full_algo_tree = new BooleanParameterVM("Show Full Algo Tree"); + + public OrderBookSettingsVM(GridSettingsVM core_settings_vm) { + this.core_settings_vm = core_settings_vm; + this.echo(_show_full_algo_tree, nameof(algo_tree_display_mode)); + add_alert_cmd = new DelegateCommand(() => { + alerts.Add(new OrderAlertVM { + is_enabled = true, + predicate_type = OrderRecordTuplePredicate.PredicateTypeOneofCase.FilledEvent, + sound_type = SoundNotification.SystemSound.BellCounter + }); + }); + delete_alert_cmd = new DelegateCommand(vm => { + if (vm != null) + alerts.Remove(vm); + }); + } + + public GridSettingsVM core_settings_vm { get; } + public AlgoTreeDisplayMode algo_tree_display_mode { + get => _show_full_algo_tree.value ? AlgoTreeDisplayMode.FullTree : AlgoTreeDisplayMode.RootAndLeafNodes; + set => _show_full_algo_tree.value = value == AlgoTreeDisplayMode.FullTree; + } + public IEnumerable parameters { + get { + yield return _show_full_algo_tree; + } + } + public ICommand add_alert_cmd { get; } + public ICommand delete_alert_cmd { get; } + // TODO: make this generic (for now, all views orderbook settings can ignore it) + public ObservableCollection alerts { get; } = new ObservableCollection(); + string ITemplateSelectorKey.key => GridSettingsTemplateKeys.OrderBook; + + public void load(OrderBookGridSettings grid_settings) { + core_settings_vm.load(grid_settings.general); + algo_tree_display_mode = grid_settings.algo_tree_display_mode; + CollectionHelper.add_or_update_existing(alerts, + grid_settings.predicate_animations, + pa => { + var vm = new OrderAlertVM(); + vm.load_from(pa); + return vm; + }, + (vm, pa) => vm.load_from(pa)); + } + + public OrderBookGridSettings get_result() { + var gs = core_settings_vm.get_result(); + return new OrderBookGridSettings(gs, alerts.Select(vm => vm.create())) { + algo_tree_display_mode = algo_tree_display_mode + }; + } + } + + class OrderAlertVM : ViewModelBase, IValueFactory { + SecurityCriteria _security_criteria; + SoundNotification.SystemSound _sound_type; + OrderRecordTuplePredicate.PredicateTypeOneofCase _predicate_type; + bool _is_enabled; + + public bool is_enabled { get => _is_enabled; set => Set(ref _is_enabled, value); } + public SecurityCriteria security_criteria { get => _security_criteria; set => Set(ref _security_criteria, value); } + public SoundNotification.SystemSound sound_type { get => _sound_type; set => Set(ref _sound_type, value); } + public OrderRecordTuplePredicate.PredicateTypeOneofCase predicate_type { get => _predicate_type; set => Set(ref _predicate_type, value); } + public IEnumerable predicate_types { get; } = + new[] { + OrderRecordTuplePredicate.PredicateTypeOneofCase.FilledEvent, + OrderRecordTuplePredicate.PredicateTypeOneofCase.CompletedEvent, + OrderRecordTuplePredicate.PredicateTypeOneofCase.CanceledEvent + }; + + public OrderRecordTuplePredicateAnimations create() { + var pred = new OrderRecordTuplePredicate(); + switch (predicate_type) { + case OrderRecordTuplePredicate.PredicateTypeOneofCase.CanceledEvent: + pred.canceled_event = new OrderCanceledEventPredicate { + security = security_criteria.is_defined() ? + new SecurityPredicate { is_value = security_criteria } : null + }; + break; + case OrderRecordTuplePredicate.PredicateTypeOneofCase.FilledEvent: + pred.filled_event = new OrderFilledEventPredicate { + security = security_criteria.is_defined() ? + new SecurityPredicate { is_value = security_criteria } : null + }; + break; + case OrderRecordTuplePredicate.PredicateTypeOneofCase.CompletedEvent: + pred.completed_event = new OrderCompletedEventPredicate { + security = security_criteria.is_defined() ? + new SecurityPredicate { is_value = security_criteria } : null + }; + break; + } + return new OrderRecordTuplePredicateAnimations( + pred, null, null, + new[] { + new SoundNotification { system_sound = sound_type } + }, + is_enabled); + } + + public void load_from(OrderRecordTuplePredicateAnimations value) { + if (value.sound_notifications.Count == 0) + return; + is_enabled = value.is_enabled; + var animation = value.sound_notifications[0]; + var pred = value.predicate_data; + if (animation.sound_type_case == SoundNotification.SoundTypeOneofCase.System) + sound_type = animation.system_sound.Value; + if (pred != null) { + predicate_type = pred.predicate_type_case; + switch (pred.predicate_type_case) { + case OrderRecordTuplePredicate.PredicateTypeOneofCase.CanceledEvent: + security_criteria = pred.canceled_event.security?.is_value; + break; + case OrderRecordTuplePredicate.PredicateTypeOneofCase.CompletedEvent: + security_criteria = pred.completed_event.security?.is_value; + break; + case OrderRecordTuplePredicate.PredicateTypeOneofCase.FilledEvent: + security_criteria = pred.filled_event.security?.is_value; + break; + } + } + } + } } \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/OrderBook/View/OrderBookSettingsView.xaml b/src/client/spark/Modules/Trading/OrderBook/View/OrderBookSettingsView.xaml new file mode 100644 index 000000000..7c923b2d6 --- /dev/null +++ b/src/client/spark/Modules/Trading/OrderBook/View/OrderBookSettingsView.xaml @@ -0,0 +1,143 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/client/spark/Modules/Trading/OrderBook/View/OrderBookSettingsView.xaml.cs b/src/client/spark/Modules/Trading/OrderBook/View/OrderBookSettingsView.xaml.cs new file mode 100644 index 000000000..b09e69987 --- /dev/null +++ b/src/client/spark/Modules/Trading/OrderBook/View/OrderBookSettingsView.xaml.cs @@ -0,0 +1,35 @@ +using mbts.ui.converters; +using System.Windows.Controls; +using System.Windows.Data; + +namespace mbts.ui.spark { + /// + /// Interaction logic for OrderBookSettingsView.xaml + /// + partial class OrderBookSettingsView : UserControl { + public static readonly IValueConverter + DisplayNameVisualStateConverter = ValueConverter.create(can_override => can_override ? VisualStateNames.DisplayName.Active : VisualStateNames.DisplayName.Inactive), + ColumnVisibilityToggleVisualStateConverter = ValueConverter.create(can_override => can_override ? VisualStateNames.ColumnVisibilityToggle.Active : VisualStateNames.ColumnVisibilityToggle.Inactive); + + public OrderBookSettingsView() => + InitializeComponent(); + + public static class VisualStateNames { + public static class DisplayName { + public const string GroupName = nameof(DisplayName); + + public static readonly string + Active = $"{GroupName}{VisualStates.Active}", + Inactive = $"{GroupName}{VisualStates.Inactive}"; + } + + public static class ColumnVisibilityToggle { + public const string GroupName = nameof(ColumnVisibilityToggle); + + public static readonly string + Active = $"{GroupName}{VisualStates.Active}", + Inactive = $"{GroupName}{VisualStates.Inactive}"; + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/Predicates/Data/OrderRecordTuplePredicate.cs b/src/client/spark/Predicates/Data/OrderRecordTuplePredicate.cs index c137f7d0b..61b3b6d60 100644 --- a/src/client/spark/Predicates/Data/OrderRecordTuplePredicate.cs +++ b/src/client/spark/Predicates/Data/OrderRecordTuplePredicate.cs @@ -1,65 +1,15 @@ -using bts.api; -using bts.api.orderman; +using bts.api.orderman; using bts.shell; using bts.utils; using mbts.spark; using mbts.ui; -using mbts.ui.settings; -using mbts.ui.utility; -using mbts.utils; using ProtoBuf; using System; using System.Collections.Generic; -using System.Linq; -using System.Reactive.Linq; -using OrderType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase; +using System.ComponentModel; using PTOC = mbts.OrderRecordTuplePredicate.PredicateTypeOneofCase; namespace mbts { - static class OrderRecordTuplePredicateVMFactory { - // TODO: uncomment the remaining cases when ready - public static Dictionary> get_oneofs(IBus bus, OrdersModel orders_model) { - return new Dictionary> { - { PTOC.FilledEvent, wrap(rp => rp.filled_event, (rp, p) => rp.filled_event = p, p => p.predicate_type_case, OrderFilledEventPredicateVMFactory.get_oneofs(bus)) }, - { PTOC.CompletedEvent, wrap(rp => rp.completed_event, (rp, p) => rp.completed_event = p, p => p.predicate_type_case, OrderCompletedEventPredicateVMFactory.get_oneofs(bus)) }, - { PTOC.CanceledEvent, wrap(rp => rp.canceled_event, (rp, p) => rp.canceled_event = p, p => p.predicate_type_case, OrderCanceledEventPredicateVMFactory.get_oneofs(bus)) }, - //{ PTOC.Security, wrap(rp => rp.security, (rp, p) => rp.security = p, p => p.predicate_type_case, SecurityCriteriaVMFactory.get_oneofs(bus)) }, - //{ PTOC.Time, wrap(rp => rp.time_created, (rp, p) => rp.time_created = p, t => t.predicate_type_case, TimePredicateVMFactory.get_oneofs()) }, - //{ PTOC.LastChange, wrap(rp => rp.last_change, (rp, p) => rp.last_change = p, t => t.predicate_type_case, TimePredicateVMFactory.get_oneofs()) }, - //{ PTOC.User, wrap(rp => rp.user, (rp, p) => rp.user = p, p => p.predicate_type_case, DiscreteValuesPredicateVMFactory.get_oneofs( - // RXCollection.Create(orders_model.factory.subscribe_user_ids().Select(tups => tups.Select(convert_name_id))), id => id, id => id.Value, id => id, ids => ids, ids => ids.ToArray())) }, - //{ PTOC.Account, wrap(rp => rp.account, (rp, p) => rp.account = p, p => p.predicate_type_case, DiscreteValuesPredicateVMFactory.get_oneofs( - // RXCollection.Create(orders_model.factory.subscribe_account_ids().Select(tups => tups.Select(convert_name_id))), id => id, id => id.Value, id => id, ids => ids, ids => ids.ToArray())) }, - //{ PTOC.Exchange, wrap(rp => rp.exchange, (rp, p) => rp.exchange = p, p => p.predicate_type_case, DiscreteValuesPredicateVMFactory.get_oneofs_value_type(EnumHelper.get_values_except(Venue.Unset))) }, - //{ PTOC.TimeInForce, wrap(rp => rp.time_in_force, (rp, p) => rp.time_in_force = p, p => p.predicate_type_case, DiscreteValuesPredicateVMFactory.get_oneofs_value_type(EnumHelper.get_values_except(TimeInForce.Undefined))) }, - //{ PTOC.Side, wrap(rp => rp.side, (rp, p) => rp.side = p, p => p.predicate_type_case, SidePredicateVMFactory.get_oneofs()) }, - //{ PTOC.Price, wrap(rp => rp.price, (rp, p) => rp.price = p, p => p.predicate_type_case, DoublePredicateVMFactory.get_oneofs()) }, - //{ PTOC.Size, wrap(rp => rp.quantity, (rp, p) => rp.quantity = p, p => p.predicate_type_case, UIntPredicateVMFactory.get_oneofs()) }, - //{ PTOC.Filled, wrap(rp => rp.filled_quantity, (rp, p) => rp.filled_quantity = p, p => p.predicate_type_case, UIntPredicateVMFactory.get_oneofs()) }, - //{ PTOC.AvgFilled, wrap(rp => rp.avg_price, (rp, p) => rp.avg_price = p, p => p.predicate_type_case, DoublePredicateVMFactory.get_oneofs()) }, - //{ PTOC.LastPrice, wrap(rp => rp.last_price, (rp, p) => rp.last_price = p, p => p.predicate_type_case, DoublePredicateVMFactory.get_oneofs()) }, - //{ PTOC.OrderType, wrap(rp => rp.order_type, (rp, p) => rp.order_type = p, p => p.predicate_type_case, DiscreteValuesPredicateVMFactory.get_oneofs_value_type(EnumHelper.get_values_except(OrderType.None))) }, - //{ PTOC.SubType, wrap(rp => rp.order_sub_type, (rp, p) => rp.order_sub_type = p, p => p.predicate_type_case, DiscreteValuesPredicateVMFactory.get_oneofs_value_type(EnumHelper.get_values())) }, - //{ PTOC.Status, wrap(rp => rp.order_status, (rp, p) => rp.order_status = p, p => p.predicate_type_case, DiscreteValuesPredicateVMFactory.get_oneofs_value_type(EnumHelper.get_values())) }, - //{ PTOC.LastMessage, wrap(rp => rp.last_message, (rp, p) => rp.last_message = p, p => p.predicate_type_case, StringPredicateVMFactory.get_oneofs()) }, - //{ PTOC.SecurityKey, wrap(rp => rp.security_key, (rp, p) => rp.security_key = p, p => p.predicate_type_case, UIntPredicateVMFactory.get_oneofs()) }, - //{ PTOC.OrderKey, wrap(rp => rp.order_key, (rp, p) => rp.order_key = p, p => p.predicate_type_case, UIntPredicateVMFactory.get_oneofs()) }, - //{ PTOC.ParentKey, wrap(rp => rp.parent_key, (rp, p) => rp.parent_key = p, p => p.predicate_type_case, UIntPredicateVMFactory.get_oneofs()) }, - }; - - //IViewValue convert_name_id((string name, uint id) tup) => - // new ViewValueTuple(tup.id, tup.name); - - ITemplatedPropertyWrapperVM wrap( - Func get, - Action set, - Func key, - Dictionary> oneofs) where TChildPredicateOneof : struct where TChildPredicate : new() => - // - PredicateVMWrapper.wrap_child_oneof(get, set, key, oneofs); - } - } - [ProtoContract] class OrderRecordTuplePredicate : IEquatable { SecurityPredicate _security; @@ -120,11 +70,11 @@ namespace mbts { OrderKey, [PredicateCase(is_toastable:false)] ParentKey, - [PredicateCase("When order gets a fill", PredicateCategory.Event)] + [PredicateCase("Order Filled", PredicateCategory.Event)] FilledEvent, - [PredicateCase("When order is completed", PredicateCategory.Event)] + [PredicateCase("Order Completed", PredicateCategory.Event)] CompletedEvent, - [PredicateCase("When order is canceled", PredicateCategory.Event)] + [PredicateCase("Order Canceled", PredicateCategory.Event)] CanceledEvent, [PredicateCase(is_toastable:false)] AttachedTargetOrderKey @@ -363,6 +313,7 @@ namespace mbts { [ProtoContract] class OrderRecordTuplePredicateAnimations : IPredicateAnimationsData { + const bool _default_is_enabled = true; [ProtoMember(1)] OrderRecordTuplePredicate _predicate; [ProtoMember(2)] @@ -371,19 +322,31 @@ namespace mbts { List _row_animations; [ProtoMember(4)] List _sound_animations; + // is_enabled + [ProtoMember(5)] + [DefaultValue(_default_is_enabled)] + bool _is_enabled; - public OrderRecordTuplePredicateAnimations() { } + public OrderRecordTuplePredicateAnimations() => + _is_enabled = _default_is_enabled; - public OrderRecordTuplePredicateAnimations(OrderRecordTuplePredicate predicate, IEnumerable cell_animations, IEnumerable row_animations, IEnumerable sound_animations) { + public OrderRecordTuplePredicateAnimations( + OrderRecordTuplePredicate predicate, + IEnumerable cell_animations, + IEnumerable row_animations, + IEnumerable sound_animations, + bool is_enabled = true) { _predicate = predicate; - _cell_animations = new List(cell_animations); - _row_animations = new List ( row_animations ); - _sound_animations = new List ( sound_animations ); + _cell_animations = new List(cell_animations.empty_if_null()); + _row_animations = new List(row_animations.empty_if_null()); + _sound_animations = new List(sound_animations.empty_if_null()); + _is_enabled = is_enabled; } public OrderRecordTuplePredicate predicate_data => _predicate; public IReadOnlyList cell_animations => _cell_animations ?? (_cell_animations = new List()); public IReadOnlyList row_animations => _row_animations ?? (_row_animations = new List()); public IReadOnlyList sound_notifications => _sound_animations ?? (_sound_animations = new List()); + public bool is_enabled => _is_enabled; } } diff --git a/src/client/spark/core/Utility/ViewModelBase.cs b/src/client/spark/core/Utility/ViewModelBase.cs index 59fee1bfa..90f16328c 100644 --- a/src/client/spark/core/Utility/ViewModelBase.cs +++ b/src/client/spark/core/Utility/ViewModelBase.cs @@ -41,6 +41,13 @@ namespace mbts.ui.shell { public delegate void PropertyChange(T oldval, T newval); static class ValidationViewModelExtensions { + public static IDisposable echo(this IRaiseNotifyPropertyChanged vm, IProperty source_property, string target_property_name) { + source_property.Changed += on_changed; + return Disposable.Create(() => source_property.Changed -= on_changed); + void on_changed(T oldval, T newval) => + vm.raise_notify_property_changed(target_property_name); + } + public static void assert(this IValidationViewModel vm, bool condition, string property, string message) { if (condition) vm.clear_error(property); else vm.set_error(property, message); diff --git a/src/client/spark/core/data/Proto/ColorOpacityProto.cs b/src/client/spark/core/data/Proto/ColorOpacityProto.cs index b5d69b2d6..8d3399fa9 100644 --- a/src/client/spark/core/data/Proto/ColorOpacityProto.cs +++ b/src/client/spark/core/data/Proto/ColorOpacityProto.cs @@ -22,12 +22,20 @@ namespace bts.proto { } public Color color => _color.rgb_color(); - public Color argb_color => Color.FromArgb((byte)(MathUtils.within_range(0, 1, _opacity) * 255.0), color.R, color.G, color.B); + // TODO: get rid of this nonsense, all we need is a uint to store color & opacity + // (opacity is just the alpha bits of the color) + public Color argb_color => + opacity == 1 ? + _color.argb_color() : + Color.FromArgb((byte)(MathUtils.within_range(0, 1, _opacity) * 255.0), color.R, color.G, color.B); public double opacity => _opacity; public ColorOpacityProto with(uint? color = null, double? opacity = null) => new ColorOpacityProto(color ?? _color, opacity ?? _opacity); + public SolidColorBrush to_argb_brush() => + new SolidColorBrush(argb_color) { Opacity = _opacity }; + public SolidColorBrush to_brush() => new SolidColorBrush(_color.rgb_color()) { Opacity = _opacity }; diff --git a/src/client/spark/view/Colors.xaml b/src/client/spark/view/Colors.xaml index c70363e60..9d35ceee5 100644 --- a/src/client/spark/view/Colors.xaml +++ b/src/client/spark/view/Colors.xaml @@ -5,9 +5,19 @@ xmlns:ui="clr-namespace:mbts.ui" > + #E5E5E5 + #CFCFCF #B3B3B3 + #10000000 + #24000000 + #3A000000 + #FF0000 #FF999999 + + + + diff --git a/src/client/spark/view/Controls/Buttons/ExpanderToggleButton.xaml.cs b/src/client/spark/view/Controls/Buttons/ExpanderToggleButton.xaml.cs index d5ebc1f03..fa8389d53 100644 --- a/src/client/spark/view/Controls/Buttons/ExpanderToggleButton.xaml.cs +++ b/src/client/spark/view/Controls/Buttons/ExpanderToggleButton.xaml.cs @@ -1,11 +1,19 @@ using System.Windows; using System.Windows.Controls.Primitives; +using System.Windows.Input; namespace mbts.ui.controls { /// /// Interaction logic for ExpanderToggleButton.xaml /// class ExpanderToggleButton : ToggleButton { + public static readonly RoutedCommand ToggleCommand = new RoutedCommand(); + public static readonly ExecutedRoutedEventHandler ToggleExecuted = (s, e) => { + var bn = s as ToggleButton ?? e.Parameter as ToggleButton; + if (bn != null) + bn.SetCurrentValue(IsCheckedProperty, !bn.IsChecked.GetValueOrDefault()); + }; + static ExpanderToggleButton() => DefaultStyleKeyProperty.OverrideMetadata(typeof(ExpanderToggleButton), new FrameworkPropertyMetadata(typeof(ExpanderToggleButton))); diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs index ea34cf594..cea55a03c 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs @@ -1,18 +1,13 @@ using bts.collections; using bts.proto; -using mbts.collections; +using bts.utils; using mbts.ui; -using mbts.ui.controls; using mbts.ui.settings; using mbts.ui.shell; -using mbts.ui.utility; +using mbts.ui.viewmodels; using System; -using System.Collections.Generic; -using System.Linq; -using System.Reactive.Linq; +using System.Collections.ObjectModel; using System.Windows; -using System.Windows.Media; -using CellAnimation = mbts.ui.CellAnimation; namespace mbts.spark { /// @@ -28,172 +23,108 @@ namespace mbts.spark { ImmutableFontColor = 1, ImmutableFontWeight = 1 << 1, ImmutableHeader = 1 << 2, - ImmutableBackgroundColor = 1 << 3 + ImmutableBackgroundColor = 1 << 3, + ImmutableVisibility = 1 << 4 } - partial class GridSettingsVM { - public interface IColumnVM : ISimpleColumnVM, IValueFactory<(GridColumnSettings settings, ILookup animations)>, IDisposable { - ObservableEditableCollection predicate_animations { get; } - new (GridColumnSettings settings, ILookup animations) create(); - new void load_from((GridColumnSettings settings, ILookup animations) args); + class GridColumnSettingsVM : DisposableViewModelBase { + string _id = string.Empty; + GridColumnSettings _settings; + + public GridColumnSettingsVM() { + if (font_color.override_value.A == 0) + font_color.set_override(font_color.override_value.with(255)); + apply_flags(GridColumnSettingsFeatureFlags.None); } - public class ColumnVM : DisposableViewModelBase, IColumnVM { - public readonly GridSettingsVM owner; + public string id { get => _id; private set => Set(ref _id, value); } + public StringOverrideParameterVM header { get; } = new StringOverrideParameterVM("Header"); + public BooleanOverrideParameterVM show { get; } = new BooleanOverrideParameterVM("Show"); + public DoubleParameterVM width { get; } = new DoubleParameterVM("Width"); + public ColorOverrideParameterVM background { get; } = new ColorOverrideParameterVM("Background"); + public ColorOverrideParameterVM font_color { get; } = new ColorOverrideParameterVM("Font Color"); + public FontWeightOverrideParameterVM font_weight { get; } = new FontWeightOverrideParameterVM(); + public HorizontalAlignmentParameterVM horizontal_alignment { get; } = new HorizontalAlignmentParameterVM("Horizontal Alignment"); + // display name + public ObservableCollection parameters { get; } = new ObservableCollection(); - string - _id = string.Empty, - _display_name = string.Empty; - double _width = 70; - HorizontalAlignment _horizontal_alignment = HorizontalAlignment.Left; - bool - _is_visible, - _can_override_display_name = true, - _can_override_font_color = true, - _is_font_color_overridden, - _can_override_font_weight = true, - _is_font_weight_overridden, - _can_override_background_color = true, - _is_background_color_overridden; - FontWeight _font_weight_override; - GridColumnSettings _settings; - - public ColumnVM(GridSettingsVM owner) { - this.owner = owner; - // animations - predicate_animations = new ObservableEditableCollection(() => - new PredicateCellAnimationVM(id, owner._create_column_predicate_vm(id), owner._predicate_data_factory), true); - predicate_animations.CollectionChanged += WPFHelper.create_collection_changed_cb(on_items_added: (_, __) => owner.show_cell_predicate_animations = true); - _dispose = Disposable.Create(() => predicate_animations.OfType().DisposeAll()); - } - - public string id { get => _id; private set => Set(ref _id, value); } - public double width { get => _width; set => Set(ref _width, value); } - public HorizontalAlignment horizontal_alignment { get => _horizontal_alignment; set => Set(ref _horizontal_alignment, value); } - public bool is_visible { get => _is_visible; set => Set(ref _is_visible, value); } - // display name - public bool can_override_display_name { get => _can_override_display_name; private set => Set(ref _can_override_display_name, value); } - public string display_name { get => _display_name; set => Set(ref _display_name, value); } - // font color override - public bool can_override_font_color { get => _can_override_font_color; private set => Set(ref _can_override_font_color, value); } - public bool is_font_color_overridden { get => _is_font_color_overridden; set => Set(ref _is_font_color_overridden, value); } - public ColorPickerPopupVM font_color_override { get; } = new ColorPickerPopupVM(); - // font weight override - public bool can_override_font_weight { get => _can_override_font_weight; private set => Set(ref _can_override_font_weight, value); } - public bool is_font_weight_overridden { get => _is_font_weight_overridden; set => Set(ref _is_font_weight_overridden, value); } - public FontWeight font_weight_override { get => _font_weight_override; set => Set(ref _font_weight_override, value); } - // background color override - public bool can_override_background_color { get => _can_override_background_color; private set => Set(ref _can_override_background_color, value); } - public bool is_background_color_overridden { get => _is_background_color_overridden; set => Set(ref _is_background_color_overridden, value); } - public ColorPickerPopupVM background_color_override { get; } = new ColorPickerPopupVM() { bind_opacity_to_alpha = true }; - // animations - public ObservableEditableCollection predicate_animations { get; } - - public (GridColumnSettings settings, ILookup animations) create() { - var settings = _settings.with( - can_override_display_name ? display_name : null, - is_visible, - null, - is_font_weight_overridden ? new NullableFontWeightProto(font_weight_override) : new NullableFontWeightProto(null), - is_font_color_overridden ? new NullableColorProto(font_color_override.color) : new NullableColorProto(null), - width, - horizontal_alignment, - background_color_override: is_background_color_overridden ? - new NullableColorProto(background_color_override.color) : default); - var animations_lookup = predicate_animations.ToFlatLookup(pa => pa.predicate.create_assigned(owner._predicate_data_factory), pa => pa.animations.Select(a => a.create())); - return (settings, animations_lookup); - } - - public void load_from((GridColumnSettings settings, ILookup animations) value) { - _settings = value.settings; - id = _settings.id; - display_name = _settings.display_name; - width = _settings.width; - horizontal_alignment = _settings.horizontal_alignment; - is_visible = _settings.is_visible; - // font color - is_font_color_overridden = _settings.font_color_override.HasValue; - if (is_font_color_overridden) { - font_color_override.load_from(new SolidColorBrush(_settings.font_color_override.Value)); - } - // font weight - is_font_weight_overridden = _settings.font_weight_override.HasValue; - if (is_font_weight_overridden) { - _font_weight_override = _settings.font_weight_override.Value; - } - // background color - is_background_color_overridden = _settings.background_color_override.HasValue; - if (is_background_color_overridden) { - background_color_override.color = _settings.background_color_override.Value; - } - predicate_animations.SetItems(value.animations.empty_if_null().Select(g => { - var vm = new PredicateCellAnimationVM(id, owner._create_column_predicate_vm(id), owner._predicate_data_factory); - vm.load_from((g.Key, g)); - return vm; - })); - } - - public void apply_flags(GridColumnSettingsFeatureFlags flags) { - // display name - can_override_display_name = (flags & GridColumnSettingsFeatureFlags.ImmutableHeader) == 0; - if (!can_override_display_name) - display_name = id; - // font color - can_override_font_color = (flags & GridColumnSettingsFeatureFlags.ImmutableFontColor) == 0; - is_font_color_overridden &= can_override_font_color; - // font weight - can_override_font_weight = (flags & GridColumnSettingsFeatureFlags.ImmutableFontWeight) == 0; - is_font_weight_overridden &= can_override_font_weight; - // background color - can_override_background_color = (flags & GridColumnSettingsFeatureFlags.ImmutableBackgroundColor) == 0; - is_background_color_overridden &= can_override_background_color; - } - - public override string ToString() => $"{display_name} ({id})"; - - GridColumnSettings IValueFactory.create() => - create().settings; - - void IValueFactory.load_from(GridColumnSettings value) => - load_from((value, new Dictionary().ToLookup(kv => kv.Key, kv => kv.Value))); + public GridColumnSettings create() { + return _settings.with( + header.effective_is_overridden ? header.override_value : id, + show.effective_value, + null, + _get_optional_font_weight_proto(font_weight), + _get_optional_color_proto(font_color), + width.value, + horizontal_alignment.value, + null, + _get_optional_color_proto(background)); } - public class PredicateCellAnimationVM : DisposableViewModelBase, IValueFactory<(TPredicateData predicate, IEnumerable animations)> { - // - readonly string _col_id; - readonly Property _show_animations; - readonly Func _predicate_data_factory; - - public PredicateCellAnimationVM(string col_id, IPropertyWrapperVM predicate, Func predicate_data_factory) { - _col_id = col_id; - this.predicate = predicate; - _predicate_data_factory = predicate_data_factory; - _show_animations = backing(nameof(show_animations), true); - animations = new ObservableEditableCollection(() => { - var vm = new CellAnimationVM(col_id); - vm.selected_animation_type = vm.animation_types.FirstOrDefault(); - return vm; - }, true); - animations.CollectionChanged += WPFHelper.create_collection_changed_cb(on_items_added: (_, __) => _show_animations.set(true)); - _dispose = predicate as IDisposable; - } - - public IPropertyWrapperVM predicate { get; } - public ObservableEditableCollection animations { get; } - public IEnumerable animation_types { get; } = EnumHelper.get_values_except(CellAnimation.AnimationTypeOneofCase.None); - public bool show_animations { get => _show_animations.Value; set => _show_animations.set(value); } - - public (TPredicateData predicate, IEnumerable animations) create() => - (predicate.create_assigned(_predicate_data_factory), animations.Select(a => a.create())); - - public void load_from((TPredicateData predicate, IEnumerable animations) args) { - predicate.load_from(args.predicate); - animations.SetItems(args.animations.Select(a => { - var vm = new CellAnimationVM(_col_id); - vm.load_from(a); - return vm; - })); + public void load_from(GridColumnSettings value) { + _settings = value; + id = _settings.id; + header.default_value = _settings.id; + header.override_value = _settings.display_name; + width.value = _settings.width; + horizontal_alignment.value = _settings.horizontal_alignment; + show.override_value = value.is_visible; + // font color + font_color.is_overridden = _settings.font_color_override.HasValue; + if (font_color.can_override) { + if (font_color.is_overridden) + font_color.override_value = _settings.font_color_override.Value; } + // font weight + font_weight.is_overridden = _settings.font_weight_override.HasValue; + if (font_weight.is_overridden) + font_weight.override_value = _settings.font_weight_override.Value; + // background + background.is_overridden = _settings.background_color_override.HasValue; + if (background.is_overridden) + background.override_value = _settings.background_color_override.Value; } + + public void apply_flags(GridColumnSettingsFeatureFlags flags) { + header.can_override = (flags & GridColumnSettingsFeatureFlags.ImmutableHeader) == 0; + show.can_override = (flags & GridColumnSettingsFeatureFlags.ImmutableVisibility) == 0; + font_weight.can_override = (flags & GridColumnSettingsFeatureFlags.ImmutableFontWeight) == 0; + font_color.can_override = (flags & GridColumnSettingsFeatureFlags.ImmutableFontColor) == 0; + background.can_override = (flags & GridColumnSettingsFeatureFlags.ImmutableBackgroundColor) == 0; + sync_params(); + } + + public virtual void sync_params() { + var layout = new HeaderedParameterGroupVM("Column Layout"); + var fonts_and_colors = new HeaderedParameterGroupVM("Fonts/Colors"); + if (header.can_override) + layout.parameters.Add(header); + layout.parameters.Add(width); + layout.parameters.Add(horizontal_alignment); + if (font_weight.can_override) + fonts_and_colors.parameters.Add(font_weight); + if (font_color.can_override) + fonts_and_colors.parameters.Add(font_color); + if (background.can_override) + fonts_and_colors.parameters.Add(background); + parameters.SetItems(layout, fonts_and_colors); + } + + public override string ToString() => + header.ToString(); + + static NullableColorProto? _get_optional_color_proto(ColorOverrideParameterVM optional_color) => + optional_color.can_override ? + optional_color.is_overridden ? + new NullableColorProto(optional_color.override_value) : + new NullableColorProto() : + (NullableColorProto?)null; + + static NullableFontWeightProto? _get_optional_font_weight_proto(ParameterOverrideVM optional_font_weight) => + optional_font_weight.can_override ? + optional_font_weight.is_overridden ? + new NullableFontWeightProto(optional_font_weight.override_value) : + new NullableFontWeightProto() : + (NullableFontWeightProto?)null; } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs index a006995f0..822a66b83 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs @@ -1,12 +1,7 @@ -using bts; -using bts.api; -using bts.collections; -using bts.shell; using bts.utils; using mbts.spark; using mbts.ui.settings; using mbts.ui.shell; -using mbts.utils; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -16,73 +11,31 @@ using System.Windows.Media; using static mbts.ui.controls.DIYGrid; namespace mbts.ui { - class GridSettingsManager : - ViewModelBase, IValueFactory, IObserver, IDisposable - where TRow : IRowVM - where TSettingsData : class, IGridSettingsWithNotifications { + class GridSettingsManager : ViewModelBase { // - readonly IBus _bus; - readonly OrdersModel _orders_model; - readonly List _notification_subs = new List(); readonly ObservableCollection _columns; - readonly IEnumerable _rows; readonly Property _grid_settings; readonly DelegateProperty _font_family; readonly DelegateProperty _font_weight; readonly DelegateProperty _font_size; readonly DelegateProperty _font_color, _selection_highlight, _background_color; - readonly SetMap, AnimationDefinitionWrapper> _animation_defs = new SetMap, AnimationDefinitionWrapper>(); - readonly Func _predicate_data_factory; - readonly Func> _predicate_factory; - readonly Func> _notification_predicate_factory; - readonly Func> _create_predicate_vm; - readonly Func> _create_column_predicate_vm; - readonly Func>, TSettingsData> _create_settings_obj; - readonly Func, SoundNotification, IDisposable> _link_sound; - readonly Func, IDisposable> _link_toast_notification; readonly IReadOnlyDictionary _column_flags; - IEnumerable> _predicate_animations_data; public GridSettingsManager( - IBus bus, - OrdersModel orders_model, - IEnumerable rows, ObservableCollection columns, - Func predicate_data_factory, - Func> create_predicate_vm, - Func> create_column_predicate_vm, - Func>, TSettingsData> create_settings_obj, - Func> predicate_factory, - Func> notification_predicate_factory, - Func, SoundNotification, IDisposable> link_sound, - Func, IDisposable> link_toast_notification, IReadOnlyDictionary column_flags = null) { // - _bus = bus; - _orders_model = orders_model; - _rows = rows; _columns = columns; - _predicate_data_factory = predicate_data_factory; - _predicate_factory = predicate_factory; - _notification_predicate_factory = notification_predicate_factory; - _create_predicate_vm = create_predicate_vm; - _create_column_predicate_vm = create_column_predicate_vm; - _create_settings_obj = create_settings_obj; - _link_sound = link_sound; - _link_toast_notification = link_toast_notification; - _column_flags = column_flags.ToDictionary(kv => kv.Key, kv => ImmutableColumnSettingStore.create(kv.Value)); + _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)); _font_size = backing(nameof(font_size), () => grid_settings.font_size, nameof(font_size)); _font_color = backing(nameof(font_color), () => grid_settings.font_color.rgb_solid_brush().frozen(), nameof(grid_settings)); - _selection_highlight = backing(nameof(selection_highlight), () => grid_settings.selection_highlight.to_brush().frozen(), nameof(grid_settings)); + _selection_highlight = backing(nameof(selection_highlight), () => grid_settings.selection_highlight.to_argb_brush().frozen(), nameof(grid_settings)); _background_color = backing(nameof(background_color), () => grid_settings.background_color.argb_color().to_solid_brush().frozen(), nameof(grid_settings)); } - public void Dispose() => - Disposable.dispose(_notification_subs); - public GridSettings grid_settings => _grid_settings.Value; public FontFamily font_family => _font_family.Value; public FontWeight font_weight => _font_weight.Value; @@ -91,52 +44,16 @@ namespace mbts.ui { public SolidColorBrush selection_highlight => _selection_highlight.Value; public SolidColorBrush background_color => _background_color.Value; - public GridSettingsVM create_vm(GridSettings settings) { - var vm = new GridSettingsVM(_bus, _orders_model, _predicate_data_factory, _notification_predicate_factory, _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 GridSettingsVM create_vm() => + new GridSettingsVM(_column_flags?.ToDictionary(kv => kv.Key, kv => kv.Value.flags)); - public TSettingsData create() { - _grid_settings.set(grid_settings.with(_columns.Select(c => { - var col_settings = c.create(); - if (_column_flags.TryGetValue(c.id, out ImmutableColumnSettingStore store)) - col_settings = store.apply(col_settings); - return col_settings; - }))); - 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_immutables) { - Disposable.dispose(_notification_subs); - var grid = value.general ?? GridSettings.Default; + public void load_from(GridSettings grid) { _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, grid.background_color.argb_color()); - _predicate_animations_data = value.predicate_animations; - _animation_defs.clear(); - foreach (var pa in _predicate_animations_data) { - var predicate = _predicate_factory(pa.predicate_data); - if (predicate == null) - continue; - foreach (var s in pa.sound_notifications) { - _notification_subs.Add(_link_sound(predicate, s)); - } - var notification_predicate = _notification_predicate_factory(pa.predicate_data); - if (notification_predicate != null) - _notification_subs.Add(_link_toast_notification(notification_predicate)); - } // ** 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_immutables) { - 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) { @@ -150,31 +67,8 @@ namespace mbts.ui { col.load_from(cs); } // invalidate all the rows to draw them again to reflect the latest settings - foreach (var r in _rows) r.raise_invalidated(); - // string id_key(string id) => id.ToLower(); } } - - void IObserver.OnNext(TSettingsData sd) { - if (sd == null) return; - try { - 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); - } catch (Exception ex) { - Log.Ui.error($"Error loading GridSettings (as observer): {ex}"); - } - } - void IObserver.OnError(Exception error) { } - void IObserver.OnCompleted() { } } } diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsResources.xaml b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsResources.xaml new file mode 100644 index 000000000..29df822c6 --- /dev/null +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsResources.xaml @@ -0,0 +1,1085 @@ + + + 4 + 8 + 12 + 16 + 4 + 8 + 12 + 16 + 36 + 8 + 36 + 24 + 26 + 10,8 + + + + + + 0x4C + + + + + + + + Normal + + + + Italic + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The color may be difficult to see because the opacity is low. + + Hint: Click to open the color menu; use the slider on the right to adjust the opacity level. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + False + + + + + + + + + + + + + + + + + + + + + + + + + Use Grid Default + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Use Grid Default + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Family + + + + Weight + + + + + + + + Size + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + (Grid Default) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsResources.xaml.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsResources.xaml.cs new file mode 100644 index 000000000..f0b3c0bd6 --- /dev/null +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsResources.xaml.cs @@ -0,0 +1,16 @@ +using mbts.ui.converters; +using System.Windows; +using System.Windows.Data; + +namespace mbts.ui.spark { + /// + /// Interaction logic for GridSettingsView.xaml + /// + partial class GridSettingsResources : ResourceDictionary { + public GridSettingsResources() => + InitializeComponent(); + + public static IMultiValueConverter ColumnDisplayNameVisibilityConverter => + ValueConverter.create((id, header) => id == header ? Visibility.Collapsed : Visibility.Visible); + } +} \ 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 6df1bfd9f..e49fec94d 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs @@ -1,23 +1,22 @@ using bts.api; using bts.collections; using bts.proto; -using bts.shell; using bts.utils; -using mbts.collections; using mbts.ui; using mbts.ui.controls; using mbts.ui.settings; using mbts.ui.shell; using mbts.ui.utility; using mbts.ui.utility.text; +using mbts.ui.viewmodels; using System; +using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Linq; using System.Reactive.Linq; -using System.Windows; -using CellAnimation = mbts.ui.CellAnimation; -using FontFamily = System.Windows.Media.FontFamily; +using System.Windows.Media; namespace mbts.spark { interface IGridSettingsWithNotifications { @@ -45,70 +44,52 @@ namespace mbts.spark { EqualityComparer.safe_aggregate_hash(predicate_animations_data)); } - partial class GridSettingsVM : DisposableViewModelBase, IRequestViewClose, IWindowSettingsVM, ITemplateSelectorKey - where TSettingsData :class, IGridSettingsWithNotifications { + class GridSettingsVM : DisposableViewModelBase, IRequestViewClose, IWindowSettingsVM, ITemplateSelectorKey { // - protected readonly Func> _create_predicate_vm; - protected readonly Func> _create_column_predicate_vm; - readonly Func _predicate_data_factory; - readonly Func>, TSettingsData> _create_settings_obj; - readonly Property _current_column; - readonly Property _font_family; - readonly Property _font_weight; - readonly Property _font_display_name; - readonly Property _font_size; - readonly Property _show_predicate_animations, _show_cell_predicate_animations, _show_background_color; - readonly OrdersModel _orders_model; + readonly FontParameterVM _font_vm = new FontParameterVM("Cell Font"); + readonly ColorParameterVM + _font_color = new ColorParameterVM("Cell Font Color"), + _selection_highlight = new ColorParameterVM("Selection Highlight"), + _background = new ColorParameterVM("Background"); + readonly Property _current_column; readonly IReadOnlyDictionary _column_flags; + readonly ObservableCollection _parameters = new ObservableCollection(); + bool _background_enabled; - public GridSettingsVM( - IBus bus, - OrdersModel orders_model, - Func predicate_data_factory, - Func> notification_predicate_factory, - Func> create_predicate_vm, - Func> create_column_predicate_vm, - Func>, TSettingsData> create_settings_obj, - IReadOnlyDictionary column_flags = null) { + public GridSettingsVM(IReadOnlyDictionary column_flags = null) { // - this.bus = bus; - _orders_model = orders_model; - _predicate_data_factory = predicate_data_factory; - _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); - _font_display_name = backing(nameof(font_display_name), () => TextUtils.display_name(font_family, font_size), new[] { nameof(font_family), nameof(font_size) }); - _show_predicate_animations = backing(nameof(show_predicate_animations), true); - _show_cell_predicate_animations = backing(nameof(show_cell_predicate_animations), true); - _show_background_color = backing(nameof(show_background_color), false); - _current_column = backing(nameof(current_column)); - predicate_animations = new ObservableEditableCollection(() => - new PredicateRowAnimationVM(_create_predicate_vm(), _predicate_data_factory), true); - predicate_animations.CollectionChanged += WPFHelper.create_collection_changed_cb(on_items_added: (_, __) => _show_predicate_animations.set(true)); + _current_column = backing(nameof(current_column)); + _parameters.sync_with(_visible_parameters); + // font + _dispose = ObservableAutoWrapper.connect_item_hooks(columns, c => { + _font_vm.PropertyChanged += on_prop_changed; + _background.PropertyChanged += on_prop_changed; + _font_color.Changed += on_font_color_changed; + on_font_color_changed(default, _font_color.value); + on_prop_changed(null, null); + return Disposable.Create(() => { + _font_vm.PropertyChanged -= on_prop_changed; + _font_color.Changed -= on_font_color_changed; + }); + // set the whole state everytime, bc it's simpler and less error prone + // than coordinating between specific properties. + void on_prop_changed(object sender, PropertyChangedEventArgs e) { + c.background.default_value = _background.value; + c.font_weight.family = _font_vm.family; + c.font_weight.default_value = _font_vm.weight; + c.font_weight.size = _font_vm.size; + } + void on_font_color_changed(Color oldval, Color newval) => + c.font_color.default_value = newval; + }); } public event EventHandler RequestClose; public event Action committed; - public IBus bus { get; } - public ObservableCollection columns { get; } = new ObservableCollection(); - public IColumnVM current_column { get => _current_column.Value; set => _current_column.set(value); } - public string font_display_name => _font_display_name.Value; - public FontFamily font_family { get => _font_family.Value; set => _font_family.set(value); } - public FontWeight font_weight { get => _font_weight.Value; set => _font_weight.set(value); } - public ColorPickerPopupVM font_color { get; } = new ColorPickerPopupVM(); - public ColorPickerPopupVM selection_highlight { get; } = new ColorPickerPopupVM(); - public ColorPickerPopupVM background_color { get; } = new ColorPickerPopupVM(); - public bool show_background_color => _show_background_color.Value; - public double font_size { get => _font_size.Value; set => _font_size.set(value); } - public bool enable_sound_triggers => true; - public bool show_predicate_animations { get => _show_predicate_animations.Value; set => _show_predicate_animations.set(value); } - public bool show_cell_predicate_animations { get => _show_cell_predicate_animations.Value; set => _show_cell_predicate_animations.set(value); } - public ObservableEditableCollection predicate_animations { get; } + public ObservableCollection columns { get; } = new ObservableCollection(); + public GridColumnSettingsVM current_column { get => _current_column.Value; set => _current_column.set(value); } public IReadOnlyList dialog_skirt_items => new[] { new DialogSkirtItem("OK", new DelegateCommand(() => { committed?.Invoke(); @@ -120,60 +101,52 @@ namespace mbts.spark { public string key => ui.spark.GridSettingsTemplateKeys.SimpleGrid; - public TSettingsData get_result() { - var animation_map = predicate_animations - .Select(p => p.create()) - .GroupBy(d => d.predicate) - .ToDictionary(g => g.Key, g => animation_tuple( - g.SelectMany(v => v.animations).ToList(), - g.SelectMany(v => v.sounds).ToList())); - var col_settings = new List(); - foreach (var c in columns) { - var (settings, animations) = c.create(); - col_settings.Add(settings); - foreach (IGrouping g in animations) - animation_map.AddOrUpdate(g.Key, p => animation_tuple(c: g), (p, t) => t.cell_animations.AddRange(g)); + public IEnumerable parameters => _parameters; + + IEnumerable _visible_parameters { + get { + yield return _selection_highlight; + if (_background_enabled) + yield return _background; + yield return _font_vm; + yield return _font_color; } - return _create_settings_obj(new GridSettings(font_family.name(), font_size, font_weight, font_color.color.rgb_color(), selection_highlight.to_proto(), col_settings, - background: background_color.color.with((byte)(MathUtils.within_range(0, 1, background_color.opacity) * 255.0)).rgb_color()), - 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(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()); - _show_background_color.set(sd.general.enable_background_color); - if (sd.general.enable_background_color) { - // note that when the OB is moved to allow background colors - // we can just background_color.color instead of load_from - // so that we get all the alpha goodness similar to SimpleGridSettingsVM - // assuming bind_opacity_to_alpha on the picker. - background_color.load_from(sd.general.background_color.argb_color().to_solid_brush()); - } - _font_size.set(sd.general.font_size); - var col_animations = new Dictionary>>(); - predicate_animations.Clear(); - 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(), _predicate_data_factory); - vm.load_from((pa.predicate_data, pa.row_animations, pa.sound_notifications)); - predicate_animations.Add(vm); - } - foreach (var ca in pa.cell_animations) { - col_animations.AddOrUpdate(ca.column_id, - col => new Dictionary> { { pa.predicate_data, new List { ca } } }, - (col, d) => d.AddOrUpdate(pa.predicate_data, p => new List { ca }, (p, l) => l.Add(ca))); - } + public GridSettings get_result() { + var col_settings = new List(); + foreach (var c in columns) { + var settings = c.create(); + col_settings.Add(settings); } + return new GridSettings( + _font_vm.family.name(), + _font_vm.size, + _font_vm.weight, + _font_color.value.rgb_color(), + new ColorOpacityProto(_selection_highlight.value.rgb_color()), + col_settings, + background: _background_enabled ? _background.value.rgb_color() : 0, + enable_background_color: _background_enabled); + } + + public void load(GridSettings settings) { + _font_vm.family = new FontFamily(settings.font_family); + _font_vm.weight = settings.font_weight; + _font_vm.size = settings.font_size; + _font_color.value = settings.font_color.rgb_color(); + _selection_highlight.value = settings.selection_highlight.argb_color; + // note that when the OB is moved to allow background colors + // we can just background_color.color instead of load_from + // so that we get all the alpha goodness similar to SimpleGridSettingsVM + // assuming bind_opacity_to_alpha on the picker. + _background_enabled = settings.enable_background_color; + _background.value = settings.background; + var curr_col_id = current_column?.id; - 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))); + columns.SetItems(settings.columns.Select(col => { + var vm = new GridColumnSettingsVM(); + vm.load_from(col); // 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 @@ -181,55 +154,10 @@ namespace mbts.spark { vm.apply_flags(flags); return vm; })); - if (curr_col_id == null || !columns.TryGetFirst(c => c.id == curr_col_id, out IColumnVM curr_column)) + if (curr_col_id == null || !columns.TryGetFirst(c => c.id == curr_col_id, out var curr_column)) curr_column = columns.FirstOrDefault(); _current_column.set(curr_column); - } - - public class PredicateRowAnimationVM : DisposableViewModelBase, IValueFactory<(TPredicateData predicate, IEnumerable animations, IEnumerable sounds)> { - readonly Property _show_animations/*, _play_sound*/; - readonly Func _predicate_state_factory; - - public PredicateRowAnimationVM(IPropertyWrapperVM predicate, Func predicate_state_factory) { - this.predicate = predicate; - _predicate_state_factory = predicate_state_factory; - _show_animations = backing(nameof(show_animations), true); - //_play_sound = backing(nameof(play_sound), true); - animations = new ObservableEditableCollection(() => { - var vm = new RowAnimationVM(); - vm.selected_animation_type = vm.animation_types.FirstOrDefault(); - return vm; - }, true); - animations.CollectionChanged += WPFHelper.create_collection_changed_cb(on_items_added: (_, __) => _show_animations.set(true)); - _dispose = predicate as IDisposable; - } - - public IPropertyWrapperVM predicate { get; } - public ObservableEditableCollection animations { get; } - public IEnumerable animation_types { get; } = EnumHelper.get_values_except(CellAnimation.AnimationTypeOneofCase.None); - public bool show_animations { get => _show_animations.Value; set => _show_animations.set(value); } - //public bool play_sound { get => _play_sound.Value; set => _play_sound.set(value); } - public IPropertyWrapperVM sound { get; } = SoundAnimationVM.create_system_sound_vm(); - - public (TPredicateData predicate, IEnumerable animations, IEnumerable sounds) create() => - (predicate.create_assigned(_predicate_state_factory), - animations.Select(a => a.create_assigned()), - new[] { sound.create_assigned() }); - //EnumerableExtensions.return_if(play_sound, () => sound.create_assigned())); - - public void load_from((TPredicateData predicate, IEnumerable animations, IEnumerable sounds) args) { - predicate.load_from(args.predicate); - animations.SetItems(args.animations.Select(a => { - var vm = new RowAnimationVM(); - vm.load_from(a); - return vm; - })); - var en = args.sounds.GetEnumerator(); - if (en.MoveNext()) { - //_play_sound.set(true); - sound.load_from(en.Current); - } //else _play_sound.set(false); - } + _parameters.sync_with(_visible_parameters); } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml index bff94e8b6..3d1e9a522 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml @@ -5,6 +5,7 @@ xmlns:proto="clr-namespace:bts.proto" xmlns:m="clr-namespace:mbts" xmlns:sp="clr-namespace:mbts.spark" + xmlns:shell="clr-namespace:bts.shell" xmlns:pnl="clr-namespace:bts.shell.pnl" xmlns:u="clr-namespace:mbts.ui" xmlns:c="clr-namespace:mbts.ui.controls" @@ -40,6 +41,8 @@ 30 24 142 + 163 + 10,8 @@ -72,6 +75,16 @@ + + - - - - Grid - - - - - - - - - - - Sound/Notification Triggers - - - (click to add) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Columns - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (click to add) - - - + + + + + + + + - - + + + + + + + + + + - - + +