diff --git a/src/client/spark/Images/Icons.cs b/src/client/spark/Images/Icons.cs index 256103869..94bf63070 100644 --- a/src/client/spark/Images/Icons.cs +++ b/src/client/spark/Images/Icons.cs @@ -10,6 +10,7 @@ namespace mbts.ui { public class Icons { public const string AddBoxesGeometryKey = nameof(AddBoxesGeometryKey), + CheckMarkGeometryKey = nameof(CheckMarkGeometryKey), DiskGeometryKey = nameof(DiskGeometryKey), DraggableGeometryKey = nameof(DraggableGeometryKey), GearGeometryKey = nameof(GearGeometryKey), @@ -17,8 +18,7 @@ namespace mbts.ui { PaymentGeometryKey = nameof(PaymentGeometryKey), PencilGeometryKey = nameof(PencilGeometryKey), PlayGeometryKey = nameof(PlayGeometryKey), - StarHollowGeometryKey = nameof(StarHollowGeometryKey), - StarSolidGeometryKey = nameof(StarSolidGeometryKey), + StarGeometryKey = nameof(StarGeometryKey), ToolsGeometryKey = nameof(ToolsGeometryKey), UndoCircularGeometryKey = nameof(UndoCircularGeometryKey); @@ -113,6 +113,7 @@ namespace mbts.ui { public static ImageSource CloneSynthetic { get { return load_resource_and_freeze("CloneSynthetic"); } } public static ImageSource ModifyOrder { get { return load_resource_and_freeze("ModifyOrder"); } } + public static ImageSource CheckMark => create_icon(CheckMarkGeometryKey); public static ImageSource Clock { get { return load_resource_and_freeze("Clock"); } } public static ImageSource Defrag { get { return load_resource_and_freeze("Defrag"); } } public static ImageSource Disk => create_icon(DiskGeometryKey); @@ -128,9 +129,7 @@ namespace mbts.ui { public static ImageSource Payment => create_icon(PaymentGeometryKey); public static ImageSource Pencil => create_icon(PencilGeometryKey); public static ImageSource Spawn { get { return load_resource_and_freeze("Spawn"); } } - public static ImageSource StarHollow => create_icon(StarHollowGeometryKey); - public static ImageSource StarSolid => create_icon(StarSolidGeometryKey); - public static ImageSource StarSolidGold => create_icon(StarSolidGeometryKey, Brushes.Gold); + public static ImageSource Star => create_icon(StarGeometryKey); public static ImageSource Target { get { return load_resource_and_freeze("Target"); } } public static ImageSource Tetris { get { return load_resource_and_freeze("Tetris"); } } public static ImageSource Tools => create_icon(ToolsGeometryKey); diff --git a/src/client/spark/Images/Vector/Icons.xaml b/src/client/spark/Images/Vector/Icons.xaml index e836315eb..05b80e49f 100644 --- a/src/client/spark/Images/Vector/Icons.xaml +++ b/src/client/spark/Images/Vector/Icons.xaml @@ -15,6 +15,9 @@ M105.5,393h374.8c5,0,9.1-4.1,9.1-9.1V9.1c0-5-4.1-9.1-9.1-9.1H105.5c-5,0-9.1,4.1-9.1,9.1v374.8 C96.4,388.9,100.5,393,105.5,393z M114.5,18.2h356.7v356.7H114.5V18.2z + + F1M9.97498,1.22334L4.6983,9.09834 4.52164,9.09834 0,5.19331 1.27664,3.52165 4.255,6.08833 8.33331,1.52588E-05 9.97498,1.22334z + M7.9999733,21.999969L7.9999733,29.999969 10.999944,29.999969 10.999944,23.999994 12.999944,23.999994 12.999944,29.999969 19.999973,29.999969 19.999973,21.999969z M7.9999437,2.9999943L7.9999437,14.999994 23.999945,14.999994 23.999945,2.9999943z M2.5,2C2.224,2,1.9999998,2.2250004,2,2.5L2,27.434 5.9999733,29.833797 5.9999733,19.999969 21.999973,19.999969 21.999973,30 29.5,30C29.775,30,30,29.775,30,29.5L30,2.5C30,2.2250004,29.775,2,29.5,2L25.999945,2 25.999945,16.999994 5.9999442,16.999994 5.9999442,2z M2.5,0L29.5,0C30.879,0,32,1.1210003,32,2.5L32,29.5C32,30.879,30.879,32,29.5,32L5.7219996,32 0,28.566 0,2.5C0,1.1210003,1.1209998,0,2.5,0z @@ -50,16 +53,8 @@ M0,0L25.194,16 0,32z - - M1540.22,2082.07L1546.95,2102.78 1568.73,2102.78 1551.11,2115.58 1557.84,2136.29 1540.22,2123.49 1522.6,2136.29 1529.33,2115.58 1511.71,2102.78 1533.49,2102.78 1540.22,2082.07 - - - - M15.719971,4.029998L13.156982,12.397001 3.9190063,12.312009 11.044983,17.740995 8.5620117,26.471996 15.749023,21.253993 22.960999,26.428004 20.138,17.617002 27.684998,12.669996 18.971008,12.587003z M15.586975,0L19.875977,11.288007 32,11.404004 21.685974,18.165006 25.411987,29.795999 15.754028,22.865001 6.2719727,29.751992 9.5440063,18.240995 0,10.969007 12.19397,11.081998z - - - - M16,0L19.8,12.200003 32,12.200003 22.1,19.700004 25.9,31.900007 16,24.400005 6.0999994,31.900007 9.9000001,19.700004 0,12.200003 12.2,12.200003z + + M1540.22,2082.07L1546.95,2102.78 1568.73,2102.78 1551.11,2115.58 1557.84,2136.29 1540.22,2123.49 1522.6,2136.29 1529.33,2115.58 1511.71,2102.78 1533.49,2102.78 1540.22,2082.07z diff --git a/src/client/spark/Modules/Trading/LadderWindowFactory.cs b/src/client/spark/Modules/Trading/LadderWindowFactory.cs index ff6a809de..3978af8c8 100644 --- a/src/client/spark/Modules/Trading/LadderWindowFactory.cs +++ b/src/client/spark/Modules/Trading/LadderWindowFactory.cs @@ -67,7 +67,7 @@ namespace bts.shell { var vm = new LadderVM(_bus, model, settings_manager); return new LadderView(vm); }); - string thread = Configuration.get().enabled ? $"Ladder{id:00}" : ""; + string thread = !Configuration.get().enabled ? $"Ladder{id:00}" : ""; return ShellWindow.create( module, md.security.ToString(), _window_type, view, (v,s) => save(_get_state(((LadderView)v).vm), s), diff --git a/src/client/spark/Orders/AlgoTemplateItemVM.cs b/src/client/spark/Orders/AlgoTemplateItemVM.cs index 9e5691950..7ac0e90d8 100644 --- a/src/client/spark/Orders/AlgoTemplateItemVM.cs +++ b/src/client/spark/Orders/AlgoTemplateItemVM.cs @@ -1,56 +1,91 @@ using bts.api.orderman; +using bts.spark; using bts.utils; using mbts.ui.shell; using System; using System.ComponentModel; +using System.Windows.Input; namespace mbts.spark { interface IAlgoTemplateItem : IEditableObject { string name { get; set; } + string abbreviation { get; set; } CustomOrderDefinitionImmutable definition { get; } + OrderMetadata meta { get; } bool is_edited { get; } + bool is_default { get; } + bool is_favorite { get; set; } } class AlgoTemplateItemVM : ViewModelBase, IAlgoTemplateItem, IEquatable { - readonly Property _name; - readonly Property _is_default, _is_favorite; - readonly DelegateProperty _is_edited; - readonly Property _definition; + readonly DelegateProperty _is_edited, _is_favorite, _is_default; readonly OrderPropertiesEditorVM _owner; + CustomOrderDefinitionImmutable _definition; + string _name, _abbreviation; Snapshot? _committed_state; - public static AlgoTemplateItemVM create_existing(OrderPropertiesEditorVM owner, AlgoTemplateImmutable template, bool is_default, bool is_favorite) => - new AlgoTemplateItemVM(owner, template.Name, template.Id, template.Definition, is_default, is_favorite); + public static AlgoTemplateItemVM create_existing(OrderPropertiesEditorVM owner, AlgoTemplateImmutable template) => + new AlgoTemplateItemVM(owner, template.Name, template.Id, template.Definition); - public static AlgoTemplateItemVM create_new(OrderPropertiesEditorVM owner, string name, bool is_default, bool is_favorite) => + public static AlgoTemplateItemVM create_new(OrderPropertiesEditorVM owner, string name) => new AlgoTemplateItemVM( owner, name, 0, owner.definition ?? new CustomOrderDefinitionImmutable( - new CustomOrderDefinition { Limit = new LimitOrderDefinition() }), - is_default, is_favorite); + new CustomOrderDefinition { Limit = new LimitOrderDefinition() })); - AlgoTemplateItemVM(OrderPropertiesEditorVM owner, string name, uint id, CustomOrderDefinitionImmutable definition, bool is_default, bool is_favorite) { + AlgoTemplateItemVM(OrderPropertiesEditorVM owner, string name, uint id, CustomOrderDefinitionImmutable definition) { _owner = owner; - _name = backing(nameof(name), name); + _name = name; this.id = id; - _definition = backing(nameof(definition), definition); - _is_default = backing(nameof(this.is_default), is_default); - _is_favorite = backing(nameof(this.is_favorite), is_favorite); + _definition = definition; + _is_default = backing(nameof(is_default), () => _owner.get_is_default(this.id)); + _is_favorite = backing(nameof(is_favorite), () => _owner.favorite_algo_templates.Contains(this)); _is_edited = backing(nameof(is_edited), () => { + if (_owner.selected_algo_template != this) return false; var odef = _owner.definition; - return odef.HasValue && !odef.Value.Equals(definition); + return odef.HasValue && !odef.Value.Equals(this.definition); }); } - public CustomOrderDefinitionImmutable definition { get => _definition.Value; set => _definition.set(value); } + public CustomOrderDefinitionImmutable definition { get => _definition; set => Set(ref _definition, value); } public string group_name => definition.CustomCase.ToString(); - public string name { get => _name.Value; set => _name.set(value); } - public bool is_default { get => _is_default.Value; set => _is_default.set(value); } - public bool is_favorite { get => _is_favorite.Value; set => _is_favorite.set(value); } + public string name { + get => _name; + set { + if (Set(ref _name, value)) + RaisePropertyChanged(nameof(abbreviation)); + } + } + public string abbreviation { get => _abbreviation ?? name; set => Set(ref _abbreviation, value); } + public bool is_default => _is_default.Value; + public bool is_favorite { + get => _is_favorite.Value; + set { + bool curr_is_fav = _owner.favorite_algo_templates.Contains(this); + if (value != curr_is_fav) { + if (value) { + _owner.favorite_algo_templates.Add(this); + } else _owner.favorite_algo_templates.Remove(this); + } + _is_favorite.set(value); + } + } public bool is_edited => _is_edited.Value; public uint id { get; set; } + public OrderMetadata meta => _owner.get_metadata(definition.CustomCase); + public ICommand reset_as_default_cmd { get; } public void refresh_is_edited() => _is_edited.refresh(); + public void refresh_is_default() => _is_default.refresh(); + public void refresh_is_favorite() => _is_favorite.refresh(); + public void load_from(AlgoTemplateImmutable value) { + name = value.Name; + definition = value.Definition; + id = value.Id; + if (_committed_state.HasValue) + _committed_state = new Snapshot(this); + refresh_is_edited(); + } public bool Equals(AlgoTemplateItemVM other) => other != null && id == other.id; public override bool Equals(object obj) => Equals(obj as AlgoTemplateItemVM); @@ -66,8 +101,6 @@ namespace mbts.spark { void IEditableObject.CancelEdit() { if (!_committed_state.HasValue) return; name = _committed_state.Value.name; - is_default = _committed_state.Value.is_default; - is_favorite = _committed_state.Value.is_favorite; definition = _committed_state.Value.definition; _committed_state = null; } diff --git a/src/client/spark/Orders/OrderPropertiesEditorVM.cs b/src/client/spark/Orders/OrderPropertiesEditorVM.cs index 6c7904f92..adce521a5 100644 --- a/src/client/spark/Orders/OrderPropertiesEditorVM.cs +++ b/src/client/spark/Orders/OrderPropertiesEditorVM.cs @@ -1,9 +1,13 @@ using bts; using bts.api; using bts.api.orderman; +using bts.spark; using bts.utils; +using mbts.collections; +using mbts.spark.utilities; using mbts.ui; using mbts.ui.shell; +using mbts.utils; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -12,6 +16,7 @@ using System.ComponentModel; using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; +using System.Windows.Data; using System.Windows.Input; namespace mbts.spark { @@ -97,17 +102,31 @@ namespace mbts.spark { class OrderPropertiesEditorVM : DisposableViewModelBase { readonly OrdersModel _model; readonly Property _security; - readonly Property _selected_algo_template; + readonly Property _selected_algo_template, _selected_favorite_algo_template; readonly SelectorProperty _accounts; readonly SelectorProperty _time_in_force; readonly SelectorProperty _order_type; - IReadOnlyDictionary _cached_templates = new Dictionary(); + readonly DeferrableActionWrapper _definition_refresh_model; + readonly ObservableKeyedCollection + _favorite_templates = new ObservableKeyedCollection(tvm => tvm.id), + _algo_templates = new ObservableKeyedCollection(tvm => tvm.id); + List _favorite_template_ids = new List(); + HashSet _default_template_ids = new HashSet(); IDisposable _order_type_param_errors_sub; public OrderPropertiesEditorVM(OrdersModel model, IObservable security_md_source, IObservable order_properties) { _model = model; var security_source = security_md_source.Select(smd => smd.security); - _security = backing(nameof(security), security_source.OnDispatcher(), SecurityDataHelper.Defaults.SecurityImmutable); + _security = backing(nameof(security), SecurityDataHelper.Defaults.SecurityImmutable); + var resources = new List { + security_source.OnDispatcher().Subscribe(s => { + using (_definition_refresh_model.defer_refresh()) { + _security.set(s); + foreach(var tvm in algo_templates) + tvm.definition = OrderDataHelper.with_security(tvm.definition, s); + } + }) + }; // guarentee that "this.security" will always be set before observers are notified // this is important bc some observers will reference "this.security" in their callbacks // - namely the [SelectorProperty]order_type's "get_default" function, @@ -144,12 +163,26 @@ namespace mbts.spark { tif => time_in_forces.Contains(tif)); _selected_algo_template = backing(nameof(selected_algo_template)); _selected_algo_template.Changed += on_selected_algo_changed; + _selected_favorite_algo_template = backing(nameof(selected_favorite_algo_template)); + // definition refresh model + _definition_refresh_model = new DeferrableActionWrapper(() => { + selected_algo_template?.refresh_is_edited(); + _selected_favorite_algo_template.set(favorite_algo_templates.Contains(selected_algo_template) ? selected_algo_template : null); + }); + // algo templates + _algo_templates.CollectionChanged += on_algo_templates_CollectionChanged; + visible_algo_templates = CollectionViewSource.GetDefaultView(_algo_templates); + visible_algo_templates.Filter = obj => selected_order_type != null && selected_order_type.order_type == ((IAlgoTemplateItem)obj).definition.CustomCase; + // favorite algo templates + visible_favorite_algo_templates = CollectionViewSource.GetDefaultView(_favorite_templates); + visible_favorite_algo_templates.Filter = obj => selected_order_type != null && selected_order_type.order_type == ((IAlgoTemplateItem)obj).definition.CustomCase; // save template save_template_cmd = new AsyncCommand(save_template); async Task save_template(AlgoTemplateItemVM tvm) { var result = await model.template_service.add(tvm.to_template()); if (result.success) tvm.id = result.value.id; + selected_algo_template = tvm; } // revert template changes revert_template_changes_cmd = new DelegateCommand(() => { @@ -158,16 +191,43 @@ namespace mbts.spark { }); // remove template remove_template_cmd = new AsyncCommand(tvm => model.template_service.delete(tvm.id)); - _dispose = Disposable.Create( - model.template_service.subscribe().OnDispatcher().Subscribe(d => { - _cached_templates = d; - sync_algo_templates(); - }), Disposable.Create(() => _order_type.Changed -= on_selected_order_type_changed)); - // local methods + resources.Add(model.template_service.subscribe().OnDispatcher().Subscribe(d => { + var old_selected_template = selected_algo_template; + var remove_keys = _algo_templates.Keys.Except(d.Keys).ToList(); + foreach (var k in remove_keys) _algo_templates.Remove(k); + foreach (var kv in d) { + var mutable = kv.Value.CloneToMutable(); + var def = mutable.Definition; + OrderDataHelper.assign_security(ref def, security); + mutable.Definition = def; + _algo_templates.AddOrUpdate(kv.Key, k => AlgoTemplateItemVM.create_existing(this, mutable), (k, curr) => curr.load_from(mutable)); + } + if (old_selected_template != null) { + foreach (var tvm in algo_templates) { + if (tvm.id == old_selected_template.id) { + selected_algo_template = tvm; + break; + } + } + } + })); + resources.Add(Disposable.Create(() => { + _order_type.Changed -= on_selected_order_type_changed; + _algo_templates.CollectionChanged -= on_algo_templates_CollectionChanged; + })); + _dispose = Disposable.Create(resources.ToArray()); + // add to favorites + add_to_favorites_cmd = new DelegateCommand(tvm => _favorite_templates.Add(tvm)); + // toggle favorite + toggle_favorite_cmd = new DelegateCommand(tvm => { + _toggle_favorite(tvm.id); + tvm.refresh_is_favorite(); + }, tvm => tvm != null); + // local members IOrderTypeVM create_order_type_vm(CustomOrderDefinitionImmutable cd, string id, string display_name) { IOrderTypeVM vm = null; - if(cd.CustomCase == CustomOrderDefinition.CustomOneofCase.Dynamic) - vm = OrderTypeVMFactory.create(cd.CustomCase, id, display_name, model, security_md_source, cd.Dynamic.DynamicOrderType); + if (cd.CustomCase == CustomOrderDefinition.CustomOneofCase.Dynamic) + vm = OrderTypeVMFactory.create(cd.CustomCase, id, display_name, model, security_md_source, cd.Dynamic.DynamicOrderType); else vm = OrderTypeVMFactory.create(cd.CustomCase, id, display_name, model, security_md_source); vm.load_from(cd); @@ -188,9 +248,8 @@ namespace mbts.spark { return order_types.FirstOrDefault(); } void on_selected_order_type_changed(IOrderTypeVM old_val, IOrderTypeVM new_val, SelectionReason reason) { - sync_algo_templates(); if (old_val != null) - old_val.definition_changed -= on_definition_changed; + old_val.definition_changed -= _definition_refresh_model.execute; if (new_val == null) { assert(false, nameof(selected_order_type)); } else { @@ -205,33 +264,26 @@ namespace mbts.spark { new_val.load_from(selected_algo_template.definition); if (new_val.has_editable_properties && reason == SelectionReason.UserSelectedNewValue) selected_order_type.editable_properties_open = true; - new_val.definition_changed += on_definition_changed; - } - } - void on_definition_changed() { - selected_algo_template?.refresh_is_edited(); - } - void sync_algo_templates() { - uint? prev_selected_algo = selected_algo_template?.id; - algo_templates.Clear(); - if (_order_type.selected_item != null) { - algo_templates.AddRange(_cached_templates.Values - .Where(t => t.Definition.CustomCase == selected_order_type.order_type) - .OrderBy(t => t.Name) - .Select(t => AlgoTemplateItemVM.create_existing(this, t, false, false))); - if (prev_selected_algo.HasValue) { - foreach (var t in algo_templates) { - if (t.id == prev_selected_algo.Value) { - selected_algo_template = t; - break; - } - } - } + new_val.definition_changed += _definition_refresh_model.execute; } + visible_algo_templates.Refresh(); + visible_favorite_algo_templates.Refresh(); } void on_selected_algo_changed(AlgoTemplateItemVM oldval, AlgoTemplateItemVM newval) { - if (newval != null) - selected_order_type.load_from(newval.definition); + if (newval != null) { + using (_definition_refresh_model.defer_refresh()) + selected_order_type.load_from(newval.definition); + } + if (oldval != null) + oldval.refresh_is_default(); + } + void on_algo_templates_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { + switch (e.Action) { + case NotifyCollectionChangedAction.Remove: + foreach (AlgoTemplateItemVM tvm in e.OldItems) + _favorite_templates.Remove(tvm); + break; + } } } @@ -243,12 +295,33 @@ namespace mbts.spark { public ReadOnlyObservableCollection order_types => _order_type.items; public IOrderTypeVM selected_order_type { get => _order_type.selected_item; set => _order_type.selected_item = value; } public CustomOrderDefinitionImmutable? definition => selected_order_type?.definition; - public ObservableCollection algo_templates { get; } = new ObservableCollection(); + public ObservableCollection algo_templates => _algo_templates; + public ObservableCollection favorite_algo_templates => _favorite_templates; public AlgoTemplateItemVM selected_algo_template { get => _selected_algo_template.Value; set => _selected_algo_template.Value = value; } + public AlgoTemplateItemVM selected_favorite_algo_template { + get => _selected_favorite_algo_template.Value; + set { + var curr = selected_favorite_algo_template; + if (value != curr) { + _selected_favorite_algo_template.set(value); + curr?.refresh_is_default(); + value?.refresh_is_default(); + if (value != null) + selected_algo_template = value; + } + } + } + public ICollectionView visible_favorite_algo_templates { get; } + public ICollectionView visible_algo_templates { get; } public Func algo_template_factory => _algo_template_factory; public ICommand save_template_cmd { get; } public ICommand revert_template_changes_cmd { get; } public ICommand remove_template_cmd { get; } + public ICommand add_to_favorites_cmd { get; } + public ICommand toggle_favorite_cmd { get; } + + public bool get_is_default(uint template_id) => _default_template_ids.Contains(template_id); + public bool get_is_favorite(uint template_id) => _favorite_template_ids.Contains(template_id); public IObservable observe_account(bool start_immediately = false) => _accounts.observe_current(start_immediately); @@ -269,12 +342,39 @@ namespace mbts.spark { return order; } + public OrderMetadata get_metadata(CustomOrderDefinition.CustomOneofCase order_type) => + _model.factory.get_metadata(order_type); + + public void on_template_settings(IEnumerable favorites, IEnumerable defaults) { + var curr_selected_favorite = selected_favorite_algo_template; + _favorite_template_ids = new List(favorites.empty_if_null()); + _default_template_ids = new HashSet(defaults.empty_if_null()); + _favorite_templates.Clear(); + foreach (var tvm in _algo_templates.Values) { + if (_favorite_template_ids.Contains(tvm.id)) + _favorite_templates.Add(tvm); + tvm.refresh_is_favorite(); + } + if (curr_selected_favorite != null && _favorite_templates.Contains(curr_selected_favorite)) + selected_favorite_algo_template = curr_selected_favorite; + } + protected override void Dispose(bool disposing) { base.Dispose(disposing); Disposable.dispose(ref _order_type_param_errors_sub); _order_type.items.OfType().DisposeAll(); } - IAlgoTemplateItem _algo_template_factory() => AlgoTemplateItemVM.create_new(this, selected_algo_template?.name ?? string.Empty, false, false); + IAlgoTemplateItem _algo_template_factory() => AlgoTemplateItemVM.create_new(this, selected_algo_template?.name ?? string.Empty); + + void _toggle_favorite(uint template_id) { + if (_favorite_template_ids.Remove(template_id)) { + _favorite_templates.Remove(template_id); + } else { + _favorite_template_ids.Add(template_id); + if (_algo_templates.TryGetValue(template_id, out AlgoTemplateItemVM tvm)) + _favorite_templates.Add(tvm); + } + } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml index 70e547ecc..0f93c1c67 100644 --- a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml +++ b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml @@ -29,7 +29,7 @@ - + @@ -121,6 +121,18 @@ + + + + + @@ -134,6 +146,10 @@ + + + + diff --git a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs index 5e729e3a1..ff006153e 100644 --- a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs +++ b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs @@ -1,4 +1,5 @@ using mbts.spark; +using mbts.ui.converters; using mbts.ui.extensions; using System; using System.Collections.Generic; @@ -7,6 +8,7 @@ using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Data; using System.Windows.Input; namespace mbts.ui.spark.algo { @@ -20,6 +22,7 @@ namespace mbts.ui.spark.algo { BeginSaveAsCommand = new RoutedUICommand("Save Template As", "Save As", typeof(AlgoTemplateToolBar)), BeginRevertCommand = new RoutedUICommand("Revert to Template", "Revert", typeof(AlgoTemplateToolBar)), BeginRemoveCommand = new RoutedUICommand("Delete Template", "Delete", typeof(AlgoTemplateToolBar)); + //ToggleFavoriteCommand = new RoutedUICommand("Toggle Favorite", "Favorite", typeof(AlgoTemplateToolBar)); public static readonly ExecutedRoutedEventHandler BeginAddNewExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._add_new()), @@ -27,6 +30,10 @@ namespace mbts.ui.spark.algo { BeginSaveAsExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._save_as()), BeginRevertExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._revert()), BeginRemoveExecuted = WPFHelper.create_cmd_executed((tb, t) => tb._remove(t)); + //ToggleFavoriteExecuted = WPFHelper.create_cmd_executed((tb, template) => template.is_favorite = !template.is_favorite); + + public static readonly CanExecuteRoutedEventHandler + ToggleFavoriteCanExecute = (s, e) => e.CanExecute = e.Parameter is IAlgoTemplateItem; ComboBox _templates_combo; IDisposable _templates_combo_hooks; @@ -93,6 +100,12 @@ namespace mbts.ui.spark.algo { public ICommand RemoveCommand { get => GetValue(RemoveCommandProperty) as ICommand; set => SetValue(RemoveCommandProperty, value); } #endregion + #region ToggleFavorite + public static readonly DependencyProperty ToggleFavoriteCommandProperty = + WPFHelper.create_dp(nameof(ToggleFavoriteCommand)); + public ICommand ToggleFavoriteCommand { get => GetValue(ToggleFavoriteCommandProperty) as ICommand; set => SetValue(ToggleFavoriteCommandProperty, value); } + #endregion + #region IsEdited /// /// This property indicates whether or not the order definition held by the is 'dirty' - different from the one that would be created by the OrderPropertiesEditor at this time. @@ -121,6 +134,12 @@ namespace mbts.ui.spark.algo { public bool IsEditing { get => (bool)GetValue(IsEditingProperty); set => SetValue(IsEditingProperty, value); } #endregion + #region IsAuxiliaryMenuExpanded + public static readonly DependencyProperty IsAuxiliaryMenuExpandedProperty = + WPFHelper.create_dp(nameof(IsAuxiliaryMenuExpanded)); + public bool IsAuxiliaryMenuExpanded { get => (bool)GetValue(IsAuxiliaryMenuExpandedProperty); set => SetValue(IsAuxiliaryMenuExpandedProperty, value); } + #endregion + public override void OnApplyTemplate() { Disposable.dispose(ref _templates_combo_hooks); _templates_combo = this.assert_template_child("PART_Templates_ComboBox"); @@ -142,6 +161,7 @@ namespace mbts.ui.spark.algo { } }), _templates_combo.SubscribeDropDownClosed((s, e) => _commit())); + //this.assert_template_child("PART_AuxiliaryExpander_MenuItem").Command = new DelegateCommand(() => SetCurrentValue(IsAuxiliaryMenuExpandedProperty, !IsAuxiliaryMenuExpanded)); } void _on_templates_combo_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e) { diff --git a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml index f1741f64f..52a2f726b 100644 --- a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml +++ b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml @@ -101,6 +101,7 @@ RemoveCommand="{Binding remove_template_cmd}" RevertCommand="{Binding revert_template_changes_cmd}" SaveCommand="{Binding save_template_cmd}" + ToggleFavoriteCommand="{Binding toggle_favorite_cmd}" SelectedItem="{Binding selected_algo_template, Mode=TwoWay}" ItemContainerStyle="{StaticResource AlgoTemplateItemContainerStyle}" > diff --git a/src/client/spark/view/Spark/Ladder/LadderSettings.cs b/src/client/spark/view/Spark/Ladder/LadderSettings.cs index 39a528650..1af5c41d0 100644 --- a/src/client/spark/view/Spark/Ladder/LadderSettings.cs +++ b/src/client/spark/view/Spark/Ladder/LadderSettings.cs @@ -178,7 +178,7 @@ namespace mbts.spark { // From the settings dialog, default settings will only be persisted when they are NOT EQUAL to the current default settings. // Settings are liable to change in the near future, so an internal IEqualityComparer simply won't do as it risks becoming out of date. [ProtoContract] - class LadderSettings : IEquatable { + class LadderSettings : IDefaultableSettings, IEquatable { public static LadderSettings Default { get { LadderColumnSettings @@ -277,6 +277,16 @@ namespace mbts.spark { public VolumeColumnSettings volume_column { get; set; } [ProtoMember(26)] public MarketSizeDisplay size_display { get; set; } + [ProtoMember(27)] + public bool use_defaults { get; set; } + + [ProtoMember(28)] + List _template_favorites; + public List template_favorites => _template_favorites ?? (_template_favorites = new List()); + + [ProtoMember(29)] + List _template_defaults; + public List template_defaults => _template_defaults ?? (_template_defaults = new List()); public bool Equals(LadderSettings other) => other != null && @@ -302,7 +312,10 @@ namespace mbts.spark { session_high_line.safe_equals(other.session_high_line) && session_range_fill.safe_equals(other.session_range_fill) && volume_column.safe_equals(other.volume_column) && - size_display.safe_equals(other.size_display); + size_display.safe_equals(other.size_display) && + use_defaults == use_defaults && + template_favorites.SequenceEqual(other.template_favorites) && + template_defaults.SequenceEqual(other.template_defaults); public override bool Equals(object obj) => Equals(obj as LadderSettings); @@ -330,7 +343,10 @@ namespace mbts.spark { EqualityComparer.safe_get_hash_code(session_high_line), EqualityComparer.safe_get_hash_code(session_range_fill), EqualityComparer.safe_get_hash_code(volume_column), - EqualityComparer.safe_get_hash_code(size_display)); + EqualityComparer.safe_get_hash_code(size_display), + use_defaults.GetHashCode(), + EqualityComparer.safe_aggregate_hash(template_favorites), + EqualityComparer.safe_aggregate_hash(template_defaults)); } static class LadderSettingsExtensions { diff --git a/src/client/spark/view/Spark/Ladder/LadderVM.cs b/src/client/spark/view/Spark/Ladder/LadderVM.cs index 905b5c072..4027f6481 100644 --- a/src/client/spark/view/Spark/Ladder/LadderVM.cs +++ b/src/client/spark/view/Spark/Ladder/LadderVM.cs @@ -89,6 +89,7 @@ namespace mbts.spark { order_properties_vm.observe_account(true).Select(a => a?.name ?? string.Empty).Subscribe(account_positions), account_positions, size_calculator = new CalculatorLogic(0, _settings_manager.subscribe_current().Select(s => s.calculator_buttons)), + settings.OnDispatcher().Subscribe(s => order_properties_vm.on_template_settings(s.template_favorites, s.template_defaults)), bus.subscribe(SparkTags.LogActivity).OnDispatcher().Subscribe(status_update), model.order_gateway.working_orders.OnDispatcher().Subscribe(working => { // Here we manage N orders to 1 price slot in the ladder @@ -234,18 +235,34 @@ namespace mbts.spark { public Brush ask_foreground => _ask_foreground.Value; public DialogCommand settings_command { get; } internal LadderModel model => _model; - internal IObservable settings => _settings_manager.subscribe_current(); + internal IObservable settings => _settings_manager.subscribe_current(t => { + var rv = SparkUtils.clone_proto(t.defaults); + rv.use_defaults = true; + if (t.instance != null) { + rv.template_favorites.SetItems(t.instance.template_favorites); + rv.template_defaults.SetItems(t.instance.template_defaults); + } + return rv; + }); public LadderSettings get_save_state() { // TODO: save an actual OrderDefinition to preserve order params. - if (_settings_manager.instance_settings == null) - return null; - var settings_clone = SparkUtils.clone_proto(_settings_manager.instance_settings); - settings_clone.default_order_type = order_properties_vm.selected_order_type?.order_type ?? settings_clone.default_order_type; - return settings_clone; + LadderSettings settings; + if (_settings_manager.instance_settings == null) { + settings = SparkUtils.clone_proto(_settings_manager.default_settings); + settings.use_defaults = true; + } else { + settings = SparkUtils.clone_proto(_settings_manager.instance_settings); + } + settings.default_order_type = order_properties_vm.selected_order_type?.order_type ?? settings.default_order_type; + settings.template_favorites.SetItems(order_properties_vm.favorite_algo_templates.Select(t => t.id)); + settings.template_defaults.SetItems(order_properties_vm.favorite_algo_templates.Where(t => t.is_default).Select(t => t.id)); + return settings; } public void key_down(KeyEventArgs e) { + // the ctrl modifier requirement helps avoid keyboard-input collisions w/ algo-templates popup + if (!KeyboardExtensions.IsCtrlDown()) return; var new_order_type = order_properties_vm.order_types.FirstOrDefault(o => o.key_match(e)); if (new_order_type != null) { order_properties_vm.selected_order_type = new_order_type; diff --git a/src/client/spark/view/Spark/Ladder/LadderView.xaml b/src/client/spark/view/Spark/Ladder/LadderView.xaml index 68c43b28f..e2329a305 100644 --- a/src/client/spark/view/Spark/Ladder/LadderView.xaml +++ b/src/client/spark/view/Spark/Ladder/LadderView.xaml @@ -404,6 +404,98 @@ + + + + + + + + + + + + + + + Auto Clear