diff --git a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml index 816a0db49..f475fae4e 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml +++ b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml @@ -42,7 +42,7 @@ (Icons.Trash, v => $"Remove '{v?.name}'", v => v != null, value => { if (value is AlgoDefinitionTemplateVM t) { - t.owner.templates.Remove(t); - _model.algo_model.remove_template(t.id).ignore(); + _vm.remove_template(t).ignore(); } else if (value is AlgoDefinitionVM d) { if (d.templates.Count > 0) { var resp = BTSMessageBox.Show($"Deleting this definition will also delete it's templates ({d.templates.Count}).\nContinue?", "Algo Studio", BTSMessageBoxButtons.Yes | BTSMessageBoxButtons.No); diff --git a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs index 51217dfa8..b9d078560 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoDefinitionVM.cs @@ -44,13 +44,12 @@ namespace mbts.spark { : this(model, null) { } public AlgoDefinitionVM(OrdersModel model, AlgoDefinitionTemplates state) - : this(model, new AlgoDefinitionTemplates?(state)) { } + : this(model, new AlgoDefinitionTemplates?(state), state.record.Key) { } AlgoDefinitionVM(OrdersModel model, AlgoDefinitionTemplates? state, string id = null) { _model = model; algo_roots = new ObservableCollection(OrderDataHelper.exchange_order_types.Select(t => OrderTypeVMFactory.create(_model, t, _editing_permissions))); templates_view = (ListCollectionView)CollectionViewSource.GetDefaultView(templates); - template_factory = () => new AlgoDefinitionTemplateVM(this, model, algo_root.create(), "Untitled"); _selected_template = backing(nameof(selected_template)); _can_edit_structure = backing(nameof(can_edit_structure), templates.ObserveSelf(), _ => templates.Count == 0); if (state.HasValue) { @@ -132,8 +131,8 @@ namespace mbts.spark { public ObservableCollection templates => _templates; public IDictionary template_map => _templates; public ListCollectionView templates_view { get; } - public Func template_factory { get; } + public AlgoDefinitionTemplateVM template_factory() => new AlgoDefinitionTemplateVM(this, _model, algo_root.create(), "Untitled"); public IObservable subscribe_is_definition_dirty() => _is_dirty.observe_current(true); public IObservable subscribe_any_templates_dirty() => _any_templates_dirty_monitor; public IObservable subscribe_is_definition_or_any_templates_dirty() => diff --git a/src/client/spark/Orders/OrderPropertiesEditorVM.cs b/src/client/spark/Orders/OrderPropertiesEditorVM.cs index f6fe2e9ec..89265e07f 100644 --- a/src/client/spark/Orders/OrderPropertiesEditorVM.cs +++ b/src/client/spark/Orders/OrderPropertiesEditorVM.cs @@ -101,39 +101,6 @@ namespace mbts.spark { } } - //class SelectionHelper { - // readonly INotifyCollectionChanged _notify_collection_changed; - // readonly ICollection _collection; - // bool _has_desired_item; - // T _value, _desired_item; - // readonly IEqualityComparer _comparer; - - // public SelectionHelper(ICollection collection) { - // if (collection is INotifyCollectionChanged ncc) { - // ncc.CollectionChanged += _on_CollectionChanged; - // } - // } - - // public event SelectionChanged selection_changed; - - // public T value { - // get => _value; - // set { - // if (!_comparer.Equals(value, _value)) { - // _value = value; - // } - // } - // } - // public void try_select(T item) { - - // } - - // void _on_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { - // //switch (e.Action) { - - // //} - // } - //} class OrderPropertiesEditorVM : DisposableViewModelBase, IAlgoTemplateItemToolBarModelStore { readonly OrdersModel _model; readonly Property _security; @@ -142,6 +109,7 @@ namespace mbts.spark { readonly SelectorProperty _time_in_force; readonly SelectionModel _selected_algo_definition; readonly AlgoDefinitionCollection _algo_definitions; + readonly EditableCollectionModel _algo_template_collection_model; readonly ObservableKeyedCollection> _favorite_templates = new ObservableKeyedCollection>(tvm => tvm.vm_key); /// Each has its own algo-definition-templates . /// Because WPF ItemsControls do not handle changes to the underlying ItemsSource well (not CollectionChange events, but changing ItemsSource to a new Collection altogether), we must use a single ObservableCollection. @@ -202,14 +170,19 @@ namespace mbts.spark { toggle_favorite_cmd = new DelegateCommand(tvm => { _toggle_favorite(tvm.id); }, tvm => tvm != null); + _algo_template_collection_model = new EditableCollectionModel(d => selected_algo_definition == null ? default : selected_algo_definition.template_factory(), d => d != null); + redo_cmd = ObservantCommand.create( + t => t?.redo(), + t => t == null ? Observable.Return(false) : System.Reactive.Linq.ObservableExtensions.FromEvent(cb => t.can_redo_changed += cb, cb => t.can_redo_changed -= cb, () => t.can_redo), + out IObserver redo_observer); + undo_cmd = ObservantCommand.create( + t => t?.undo(), + t => t == null ? Observable.Return(false) : System.Reactive.Linq.ObservableExtensions.FromEvent(cb => t.can_undo_changed += cb, cb => t.can_undo_changed -= cb, () => t.can_undo), + out IObserver undo_observer); // - algo_template_factory = () => { - //var ar = new CustomOrderDefinitionImmutable((selected_algo_template == null ? selected_algo_definition.algo_root : selected_algo_template.algo_root).create()); - var dtvm = new AlgoDefinitionTemplateVM(selected_algo_definition, _model, new CustomOrderDefinitionImmutable((selected_algo_template == null ? selected_algo_definition.algo_root : selected_algo_template.algo_root).create()), "New Template"); - return dtvm; - }; _dispose = new CompositeDisposable( _selected_algo_definition.subscribe_changed(_selected_algo_definition_Changed), + ((IPropertyChangedDemuxer)this).subscribe(nameof(selected_algo_template)).Subscribe(undo_observer, redo_observer), security_source.Subscribe(s => { using (_selected_algo_definition.begin_update()) _algo_definitions.on_security(s); @@ -239,6 +212,7 @@ namespace mbts.spark { _selected_favorite_algo_template.Value = selected_algo_template is AlgoDefinitionTemplateVM t && t.is_persisted && _favorite_template_ids.Contains(t.id) ? t : null; if (old_value != null) _algo_templates.remove_collection(old_value.templates); + _algo_template_collection_model.OnNext(new_value); } public SecurityImmutable security => _security.Value; @@ -289,10 +263,12 @@ namespace mbts.spark { public ICollectionView visible_algo_templates { get; } public ICommand save_template_cmd { get; } public ICommand revert_template_changes_cmd { get; } + public ICommand undo_cmd { get; } + public ICommand redo_cmd { get; } public ICommand remove_template_cmd { get; } public ICommand add_to_favorites_cmd { get; } public ICommand toggle_favorite_cmd { get; } - public Func algo_template_factory { get; } + public IEditableCollectionModel algo_template_collection_model => _algo_template_collection_model; public bool get_is_default(string template_id) => _default_template_ids.Contains(template_id); public bool get_is_favorite(string template_id) => _favorite_template_ids.Contains(template_id); diff --git a/src/client/spark/core/Commands/ObservantCommand.cs b/src/client/spark/core/Commands/ObservantCommand.cs index d0880556f..783ebcacf 100644 --- a/src/client/spark/core/Commands/ObservantCommand.cs +++ b/src/client/spark/core/Commands/ObservantCommand.cs @@ -1,4 +1,5 @@ using System; +using System.Reactive; using System.Windows.Input; namespace mbts.ui { @@ -31,5 +32,35 @@ namespace mbts.ui { class ObservantCommand : ObservantCommand { public ObservantCommand(Action execute) : base(_ => execute()) { } public ObservantCommand(Action execute) : base(execute) { } + + public static ICommand create(Action execute, Func get_can_execute, out IObserver observer) { + T param = default; + bool can_execute = false; + var cmd = new DelegateCommand(() => execute(param), () => can_execute); + observer = new AnonymousObserver(p => { + param = p; + if (can_execute != get_can_execute(param)) + cmd.RaiseCanExecuteChanged(); + }); + return cmd; + } + + public static ICommand create(Action execute, Func> get_can_execute_source, out IObserver observer) { + T param = default; + bool can_execute = false; + var cmd = new DelegateCommand(() => execute(param), () => can_execute); + IDisposable can_execute_sub = null; + observer = new AnonymousObserver(p => { + param = p; + Disposable.dispose(ref can_execute_sub); + can_execute_sub = get_can_execute_source(p).Subscribe(ce => { + if (can_execute != ce) { + can_execute = ce; + cmd.RaiseCanExecuteChanged(); + } + }); + }, () => Disposable.dispose(ref can_execute_sub)); + return cmd; + } } } \ No newline at end of file diff --git a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs index 0798cba89..b288bb402 100644 --- a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs @@ -331,6 +331,14 @@ namespace System.Reactive.Linq }; } + public static IObservable FromEvent(Action add_handler, Action remove_handler, Func selector) => + Observable.Create(o => { + callback(); + add_handler(callback); + return Disposable.Create(() => remove_handler(callback)); + void callback() => o.OnNext(selector()); + }); + // This is placed here to intentionally make ObserveOnDispatcher ambiguous with the one provided by Rx public static IObservable ObserveOnDispatcher(this IObservable source) { throw new InvalidOperationException("This method can starve the UI of input messages. Please use OnDispatcher() instead"); diff --git a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml index 5a352ef4d..4fae84ca3 100644 --- a/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml +++ b/src/client/spark/view/Controls/Order/AlgoContentTemplateSelector.xaml @@ -29,7 +29,7 @@ - + diff --git a/src/client/spark/view/Controls/Order/AlgoDefinitionView.xaml b/src/client/spark/view/Controls/Order/AlgoDefinitionView.xaml index 8c39e46bb..4481833fd 100644 --- a/src/client/spark/view/Controls/Order/AlgoDefinitionView.xaml +++ b/src/client/spark/view/Controls/Order/AlgoDefinitionView.xaml @@ -53,7 +53,7 @@ x:Name="PART_AlgoTemplateToolBar" Margin="{StaticResource Margin.LP1}" IsReadOnly="{TemplateBinding IsAlgoTemplateToolBarReadOnly}" - NewItemFactory="{Binding algo_template_factory}" + EditableCollectionModel="{Binding algo_templates}" Padding="0,1,0,3" Header="{TemplateBinding Header}" HeaderTemplate="{StaticResource AlgoTemplateHeaderContentTemplate}" diff --git a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml index c2891a074..2c82574c2 100644 --- a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml +++ b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml @@ -91,10 +91,8 @@ @@ -105,6 +103,19 @@ + + + + + + + + - + + - diff --git a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs index c493bf521..9c6590642 100644 --- a/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs +++ b/src/client/spark/view/Controls/Order/AlgoTemplateToolBar.xaml.cs @@ -27,6 +27,8 @@ namespace mbts.ui.spark.algo { BeginSaveCommand = new RoutedUICommand("Save Template", "Save", typeof(AlgoTemplateToolBar)), BeginSaveAsCommand = new RoutedUICommand("Save Template As", "Save As", typeof(AlgoTemplateToolBar)), BeginRevertCommand = new RoutedUICommand("Revert to Template", "Revert", typeof(AlgoTemplateToolBar)), + BeginRedoCommand = new RoutedUICommand("Redo", "Redo", typeof(AlgoTemplateToolBar)), + BeginUndoCommand = new RoutedUICommand("Undo", "Undo", typeof(AlgoTemplateToolBar)), BeginRemoveCommand = new RoutedUICommand("Delete Template", "Delete", typeof(AlgoTemplateToolBar)); public static readonly ExecutedRoutedEventHandler @@ -34,6 +36,8 @@ namespace mbts.ui.spark.algo { BeginSaveExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._save()), BeginSaveAsExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._save_as()), BeginRevertExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._revert()), + BeginRedoExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._redo()), + BeginUndoExecuted = WPFHelper.create_cmd_executed((tb, _) => tb._undo()), BeginRemoveExecuted = WPFHelper.create_cmd_executed((tb, t) => tb._remove(t)); ComboBox _templates_combo; @@ -72,10 +76,10 @@ namespace mbts.ui.spark.algo { public AlgoDefinitionTemplateVM SelectedItem { get => GetValue(SelectedItemProperty) as AlgoDefinitionTemplateVM; set => SetValue(SelectedItemProperty, value); } #endregion - #region NewItemFactory - public static readonly DependencyProperty NewItemFactoryProperty = - WPFHelper.create_dp, AlgoTemplateToolBar>(nameof(NewItemFactory)); - public Func NewItemFactory { get => GetValue(NewItemFactoryProperty) as Func; set => SetValue(NewItemFactoryProperty, value); } + #region EditableCollectionModel + public static readonly DependencyProperty EditableCollectionModelProperty = + WPFHelper.create_dp, AlgoTemplateToolBar>(nameof(EditableCollectionModel)); + public IEditableCollectionModel EditableCollectionModel { get => GetValue(EditableCollectionModelProperty) as IEditableCollectionModel; set => SetValue(EditableCollectionModelProperty, value); } #endregion #region AddNew @@ -102,6 +106,18 @@ namespace mbts.ui.spark.algo { public ICommand RemoveCommand { get => GetValue(RemoveCommandProperty) as ICommand; set => SetValue(RemoveCommandProperty, value); } #endregion + #region Undo + public static readonly DependencyProperty UndoCommandProperty = + WPFHelper.create_dp(nameof(UndoCommand)); + public ICommand UndoCommand { get => GetValue(UndoCommandProperty) as ICommand; set => SetValue(UndoCommandProperty, value); } + #endregion + + #region Redo + public static readonly DependencyProperty RedoCommandProperty = + WPFHelper.create_dp(nameof(RedoCommand)); + public ICommand RedoCommand { get => GetValue(RedoCommandProperty) as ICommand; set => SetValue(RedoCommandProperty, value); } + #endregion + #region ToggleFavorite public static readonly DependencyProperty ToggleFavoriteCommandProperty = WPFHelper.create_dp(nameof(ToggleFavoriteCommand)); @@ -139,14 +155,7 @@ namespace mbts.ui.spark.algo { static readonly DependencyPropertyKey IsEditedPropertyKey = WPFHelper.create_ro_dp( nameof(IsEdited), - (o, v) => { - if (v == o._editable_items.IsEditingItem) { - // don't expect this will happen, but check it anyways to guard against future code changes that might break this contract. - return; - } - if (v) o._editable_items.EditItem(o.SelectedItem); - else o._editable_items.CancelEdit(); - }, + null, (o, v) => o.SelectedItem?.is_dirty ?? false); internal static readonly DependencyProperty IsEditedProperty = IsEditedPropertyKey.DependencyProperty; internal bool IsEdited { get => (bool)GetValue(IsEditedProperty); private set => SetValue(IsEditedPropertyKey, value); } @@ -173,6 +182,7 @@ namespace mbts.ui.spark.algo { switch (e.Key) { case Key.Enter: Keyboard.ClearFocus(); + _commit(); break; case Key.Escape: _cancel(); @@ -192,7 +202,7 @@ namespace mbts.ui.spark.algo { // 2) The user is renaming an existing template. // In both cases, committing the text is reasonable (and probably expected) behavior. // Note: This doesn't mean it is impossible to cancel such changes - the user may do so by pressing Esc. - _commit(); + //_commit(); } } @@ -204,37 +214,48 @@ namespace mbts.ui.spark.algo { #region Transactional void _add_new() { _prepare_edit(); - var factory = NewItemFactory; - if (factory != null) { - _editable_items.AddNewItem(factory()); - } else _editable_items.AddNew(); + var model = EditableCollectionModel; + if (model != null && model.can_add_new) + _editable_items.AddNewItem(model.new_item_factory()); } void _cancel() => _editable_items.CancelAny(); - void _commit() { + void _commit(bool execute_save = false) { if (_editable_items.IsEditingItem) { var editing_item = (AlgoDefinitionTemplateVM)_editable_items.CurrentEditItem; editing_item.name = _templates_combo.Text; _editable_items.CommitEdit(); SetCurrentValue(SelectedItemProperty, editing_item); - SaveCommand.TryExecuteIfCan(editing_item); + if (execute_save) + SaveCommand.TryExecuteIfCan(editing_item); SetCurrentValue(IsEditingProperty, false); } else if (_editable_items.IsAddingNew) { var new_item = (AlgoDefinitionTemplateVM)_editable_items.CurrentAddItem; new_item.name = _templates_combo.Text; _editable_items.CommitNew(); SetCurrentValue(SelectedItemProperty, new_item); - SaveCommand.TryExecuteIfCan(new_item); + if (execute_save) + SaveCommand.TryExecuteIfCan(new_item); SetCurrentValue(IsEditingProperty, false); } } void _revert() { - _cancel(); + //_cancel(); RevertCommand.TryExecuteIfCan(null); } + void _redo() { + if (SelectedItem is AlgoDefinitionTemplateVM t && t.can_redo) + t.redo(); + } + + void _undo() { + if (SelectedItem is AlgoDefinitionTemplateVM t && t.can_undo) + t.undo(); + } + void _remove(object template) { _editable_items?.Remove(template); RemoveCommand.TryExecuteIfCan(template); @@ -243,7 +264,7 @@ namespace mbts.ui.spark.algo { void _save() { if (SelectedItem == null) { _add_new(); - } else _commit(); + } else _commit(true); } void _save_as() { diff --git a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml index ca897ab17..8ae80cf21 100644 --- a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml +++ b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml @@ -94,13 +94,14 @@ x:Name="PART_AlgoTemplateToolBar" Margin="{StaticResource Margin.LP1}" IsReadOnly="{TemplateBinding IsAlgoTemplateToolBarReadOnly}" - NewItemFactory="{Binding algo_template_factory}" + EditableCollectionModel="{Binding algo_template_collection_model}" Padding="0,1,0,3" Header="{Binding SelectedItem.algo_root.order_type, RelativeSource={RelativeSource AncestorType=uag:OrderDetailsPopupControl}, Converter={StaticResource OrderTypeDisplayTextConverter}}" HeaderTemplate="{StaticResource AlgoTemplateHeaderContentTemplate}" ItemModelStore="{Binding}" RemoveCommand="{Binding remove_template_cmd}" - RevertCommand="{Binding revert_template_changes_cmd}" + UndoCommand="{Binding undo_cmd}" + RedoCommand="{Binding redo_cmd}" SaveCommand="{Binding save_template_cmd}" ToggleFavoriteCommand="{Binding toggle_favorite_cmd}" SelectedItem="{Binding selected_algo_template, Mode=TwoWay}" diff --git a/src/client/spark/view/Extensions/EditableCollectionViewEx.cs b/src/client/spark/view/Extensions/EditableCollectionViewEx.cs index 40aabedac..e977377dd 100644 --- a/src/client/spark/view/Extensions/EditableCollectionViewEx.cs +++ b/src/client/spark/view/Extensions/EditableCollectionViewEx.cs @@ -1,6 +1,7 @@ using bts.utils; using System; using System.ComponentModel; +using System.Reactive; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -36,6 +37,43 @@ namespace mbts.ui { public static bool operator !=(EditableItemKey a, EditableItemKey b) => !a.Equals(b); } + interface IEditableCollectionModel { + event Action can_add_new_changed; + object new_item_factory(); + bool can_add_new { get; } + } + + interface IEditableCollectionModel : IEditableCollectionModel { + new T new_item_factory(); + } + + class EditableCollectionModel : IEditableCollectionModel, IObserver { + TParameter _parameter; + readonly Func _get_can_add_new; + readonly Func _new_item_factory; + + public EditableCollectionModel(Func new_item_factory, Func can_add_new, TParameter init = default) { + _new_item_factory = new_item_factory; + _get_can_add_new = can_add_new; + _parameter = init; + this.can_add_new = _get_can_add_new(_parameter); + } + + public bool can_add_new { get; private set; } + public event Action can_add_new_changed; + public T new_item_factory() => _new_item_factory(_parameter); + public void OnCompleted() { } + public void OnError(Exception error) { } + public void OnNext(TParameter value) { + _parameter = value; + if (can_add_new != _get_can_add_new(value)) { + can_add_new = !can_add_new; + can_add_new_changed?.Invoke(); + } + } + object IEditableCollectionModel.new_item_factory() => new_item_factory(); + } + static class EditableCollectionViewEx { public static readonly RoutedUICommand AddNewItemCommand = new RoutedUICommand("Add New Item", "Add New", typeof(EditableCollectionViewEx)), @@ -47,8 +85,9 @@ namespace mbts.ui { AddNewItemExecuted = (s, e) => { var collection = _get_collection(s, e); if (collection != null) { - if (collection is IEditableCollectionViewAddNewItem cani && cani.CanAddNewItem && GetNewItemFactory(s as DependencyObject) is Func factory) { - cani.AddNewItem(factory()); + if (GetModel((DependencyObject)s) is IEditableCollectionModel model) { + if (model.can_add_new && collection is IEditableCollectionViewAddNewItem cani && cani.CanAddNewItem) + cani.AddNewItem(model.new_item_factory()); } else if (collection.CanAddNew) { collection.AddNew(); } @@ -74,14 +113,14 @@ namespace mbts.ui { public static readonly CanExecuteRoutedEventHandler AddNewItemCanExecute = (s, e) => e.CanExecute = true; - public static readonly DependencyProperty NewItemFactoryProperty = + public static readonly DependencyProperty ModelProperty = DependencyProperty.RegisterAttached( - "NewItemFactory", - typeof(Func), + "Model", + typeof(IEditableCollectionModel), typeof(EditableCollectionViewEx)); - public static Func GetNewItemFactory(DependencyObject d) => d.GetValue(NewItemFactoryProperty) as Func; - public static void SetNewItemFactory(DependencyObject d, Func value) => d.SetValue(NewItemFactoryProperty, value); + public static IEditableCollectionModel GetModel(DependencyObject d) => d.GetValue(ModelProperty) as IEditableCollectionModel; + public static void SetModel(DependencyObject d, IEditableCollectionModel value) => d.SetValue(ModelProperty, value); static IEditableCollectionView _assert_collection(ItemsControl items_control) => items_control.ItemsSource as IEditableCollectionView ??