diff --git a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml.cs b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml.cs index f14a1798f..f142e44a4 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioView.xaml.cs @@ -1,31 +1,24 @@ using mbts.spark; +using mbts.ui.controls; using mbts.ui.converters; +using mbts.ui.extensions; using System.Windows.Controls; using System.Windows.Data; +using System.Windows.Input; namespace mbts.ui.spark { partial class AlgoStudioView : UserControl { public static readonly IValueConverter EditPaneVisualStateConverter = ValueConverter.create(obj => obj == null ? VisualStates.Inactive : VisualStates.Active); - IAlgoStudioViewController _controller; - public AlgoStudioView() { InitializeComponent(); - DataContextChanged += (s, e) => set_controller(); - set_controller(); - // local methods - void set_controller() => Disposable.replace(ref _controller, AlgoStudioViewControllerFactory.create(DataContext, name_text_box, custom_badge_color_picker_button, custom_badge_color_picker_popup)); + PopupViewController.attach(custom_badge_color_picker_popup, custom_badge_color_picker_button); + this.ConnectDataContext(vm => vm.request_view_focus_name += focus_name, vm => vm.request_view_focus_name -= focus_name); + void focus_name(AlgoDefinitionVM target) => + Dispatcher.InvokeAsync(() => Keyboard.Focus(name_text_box)); } - public new AlgoStudioVM DataContext { - get => base.DataContext as AlgoStudioVM; - set => base.DataContext = value; - } - - public void add_definition() => _controller.add_definition(); - public void add_definition_clone(AlgoDefinitionVM src_definition) => _controller.add_definition_clone(src_definition); - public static class VisualStateNames { public static class EditPane { public const string GroupName = nameof(EditPane); diff --git a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioViewController.cs b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioViewController.cs deleted file mode 100644 index 6fa462c60..000000000 --- a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioViewController.cs +++ /dev/null @@ -1,59 +0,0 @@ -using mbts.spark; -using mbts.ui.controls; -using System; -using System.Windows.Controls; -using System.Windows.Controls.Primitives; -using System.Windows.Input; - -namespace mbts.ui.spark { - interface IAlgoStudioViewController : IDisposable { - void add_definition(); - void add_definition_clone(AlgoDefinitionVM src_definition); - } - - class AlgoStudioViewControllerFactory { - static readonly IAlgoStudioViewController _empty = new EmptyController(); - - public static IAlgoStudioViewController create(AlgoStudioVM vm, TextBox name_text_box, Button color_picker_button, Popup color_picker_popup) => - vm == null || name_text_box == null || color_picker_button == null || color_picker_popup == null ? - _empty : new Controller(vm, name_text_box, color_picker_button, color_picker_popup); - - /// - /// This wrapper class is returned by when the vm or tree_view is null. - /// This further separates the view from the controller-logic; it doesn't even need to check for null when accessing methods. - /// - class EmptyController : IAlgoStudioViewController { - public void add_definition() { } - public void add_definition_clone(AlgoDefinitionVM src_definition) { } - public void Dispose() { } - } - - class Controller : IAlgoStudioViewController { - readonly AlgoStudioVM _vm; - readonly TextBox _name_text_box; - IDisposable _dispose; - - public Controller(AlgoStudioVM vm, TextBox name_text_box, Button color_picker_button, Popup color_picker_popup) { - _vm = vm; - _name_text_box = name_text_box; - _dispose = PopupViewController.attach(color_picker_popup, color_picker_button); - } - - public void add_definition() { - _vm.add_new_definition(); - _begin_edit(); - } - - public void add_definition_clone(AlgoDefinitionVM src_definition) { - _vm.add_new_definition_clone(src_definition); - _begin_edit(); - } - - public void Dispose() => - Disposable.dispose(ref _dispose); - - void _begin_edit() => - WPFHelper.push_frame_until(() => Keyboard.Focus(_name_text_box) == _name_text_box); - } - } -} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs index d0e80f526..5d2b975e4 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/AlgoStudioWindow.cs @@ -64,11 +64,12 @@ namespace mbts.spark { _view = new AlgoStudioView { DataContext = _vm }; _resources = new CompositeDisposable(); // add definition - _commands.Add(new SparkCommandCommand("Add Algo", Icons.AddBoxes, new DelegateCommand(_view.add_definition))); - // add definition clone - var add_definition_clone_spark_cmd = new ObservantSparkCommandCommand(Icons.DocumentCopy, d => $"Clone '{d?.name}'", d => d != null, _view.add_definition_clone); - _commands.Add(add_definition_clone_spark_cmd); - _resources.Add(_vm.subscribe_definition().OnDispatcher().Subscribe(add_definition_clone_spark_cmd)); + _commands.Add(new SparkCommandCommand("Add Algo", Icons.AddBoxes, _vm.add_definition_cmd)); + // clone definition + var clone_selected_definition_spark_cmd = new SparkCommandCommand(string.Empty, Icons.DocumentCopy, _vm.clone_selected_definition_cmd); + _commands.Add(clone_selected_definition_spark_cmd); + _resources.Add(_vm.subscribe_definition().OnDispatcher().Subscribe( + def => clone_selected_definition_spark_cmd.title = def == null ? $"Clone (None Selected)" : $"Clone '{def.name}'")); // save if (_ui_configuration.can_save_specific) { var content_is_dirty_src = _vm.subscribe_definition().SelectManyInSeries(def => def?.subscribe_is_definition_dirty() ?? Observable.Return(false)); diff --git a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoStudioVM.cs b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoStudioVM.cs index d27b6b0e8..d2ff6af6b 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoStudioVM.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/VM/AlgoStudioVM.cs @@ -58,6 +58,9 @@ namespace mbts.spark { CustomSort = AlgoCollectionHelper.algo_definition_vm_comparer.to_non_generic() }; definitions_view.Filter = d => !(d.key.mode == AlgoKey.Mode.Persisted && _persisted_meta.TryGetValue(d.key, out AlgoDefinitionMetadata meta) && meta.is_hidden); + // add/clone + add_definition_cmd = new DelegateCommand(add_definition); + clone_selected_definition_cmd = new DelegateCommand(clone_selected_definition); // save var save_cmd = new ObservantAsyncCommand(save_async); this.save_cmd = save_cmd; @@ -107,32 +110,42 @@ namespace mbts.spark { } return null; } + + void add_definition() => + add_definition_common(AlgoDefinitionVM.new_algo_def, AlgoDefinitionVM.new_algo_name); + + void clone_selected_definition() { + if (selected_definition == null || selected_definition.algo_root == null) { + add_definition(); + } else { + add_definition_common( + selected_definition.algo_root.definition, + string.IsNullOrEmpty(selected_definition.name) ? AlgoDefinitionVM.new_algo_name : $"{selected_definition.name} - Copy"); + } + } + + void add_definition_common(CustomOrderDefinitionImmutable def, string name) { + var new_def = AlgoDefinitionVM.create_placeholder(_model, def, name, _studio_formats); + _definitions.Add(new_def); + selected_definition = new_def; + request_view_focus_name?.Invoke(new_def); + } } + public event Action request_view_focus_name; + public ObservableCollection definitions => _definitions; public ObservableCollection content_visual_states { get; } = new ObservableCollection(); public AlgoDefinitionVM selected_definition { get => _selected_definition.Value; set => _selected_definition.set(value); } public IAlgoVM editable_content => _editable_content.Value; public ICollectionView definitions_view => _definitions_view; + public ICommand add_definition_cmd { get; } + public ICommand clone_selected_definition_cmd { get; } public ICommand save_cmd { get; } public ICommand revert_cmd { get; } public string save_cmd_tooltip => _save_cmd_tooltip.Value; public string revert_cmd_tooltip => _revert_cmd_tooltip.Value; - public void add_new_definition() => - _add_new_definition(AlgoDefinitionVM.new_algo_def, AlgoDefinitionVM.new_algo_name); - - public void add_new_definition_clone(AlgoDefinitionVM src_definition) { - if (src_definition == null || src_definition.algo_root == null) { - add_new_definition(); - } else { - _add_new_definition(src_definition.algo_root.definition, string.IsNullOrEmpty(src_definition.name) ? AlgoDefinitionVM.new_algo_name : $"{src_definition.name} - Copy"); - } - } - - void _add_new_definition(CustomOrderDefinitionImmutable def, string name) => - _definitions.Add(AlgoDefinitionVM.create_placeholder(_model, def, name, _studio_formats)); - public StartupData create() => new StartupData(); public void load_from(StartupData state) { } public IObservable subscribe_definition() => _selected_definition.observe_current(true); diff --git a/src/client/spark/view/Extensions/FrameworkElementExtensions.cs b/src/client/spark/view/Extensions/FrameworkElementExtensions.cs index 3e7670175..3af8efbcb 100644 --- a/src/client/spark/view/Extensions/FrameworkElementExtensions.cs +++ b/src/client/spark/view/Extensions/FrameworkElementExtensions.cs @@ -61,6 +61,26 @@ namespace mbts.ui.extensions { } } + public static IDisposable ConnectDataContext(this FrameworkElement element, Action connect, Action disconnect) => + ConnectDataContext(element, vm => { + connect(vm); + return Disposable.Create(() => disconnect(vm)); + }); + + public static IDisposable ConnectDataContext(this FrameworkElement element, Func connect) { + var connection = element.DataContext is T vm ? connect(vm) : null; + element.DataContextChanged += on_data_context_changed; + return Disposable.Create(dispose); + // + void on_data_context_changed(object sender, DependencyPropertyChangedEventArgs e) => + Disposable.replace(ref connection, e.NewValue is T new_vm ? connect(new_vm) : null); + + void dispose() { + Disposable.dispose(ref connection); + element.DataContextChanged -= on_data_context_changed; + } + } + public static IObservable ObserveIsChecked(this ToggleButton toggle_button) => Observable.Create(o => { toggle_button.Checked += on_checked;