From bc3f2db0f963afbdf7b9a1096a3af109e61da49a Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 19 Jun 2019 19:56:20 -0500 Subject: [PATCH] add OK/Cancel buttons to sec pickers in ladder/orderbook settings dialogs --- .../ManualFills/ManualFillRowEditor.xaml | 3 +- .../DIYGrid/Settings/GridSettingsView.xaml | 20 ++++-- .../view/Controls/Popup/PopupControl.xaml.cs | 5 ++ .../Shell/Settings/ShellSettingsDialog.xaml | 5 +- .../ContractPicker/ContractPickerModel.cs | 52 ++++++++++++--- .../ContractPickerPopup.xaml.cs | 63 +++++++++++++++---- 6 files changed, 120 insertions(+), 28 deletions(-) diff --git a/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml b/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml index c8673c321..d9d27d638 100644 --- a/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml +++ b/src/client/spark/Modules/Trading/ManualFills/ManualFillRowEditor.xaml @@ -63,9 +63,10 @@ IsEditMode="False" IsLazy="True" SecurityMap="{DynamicResource ResourceKey={x:Static usp:ManualFillRowEditor.SecuritiesKey}}" - SelectedSecurity="{Binding security}" + SelectedSecurity="{Binding security, UpdateSourceTrigger=Explicit}" ShowDialogSkirt="True" ToolTip="{Binding SelectedCriteria, RelativeSource={RelativeSource Self}, Converter={x:Static c:ContractPickerPopup.SecurityCriteriaTextConverter}, Mode=OneWay}" + OKClick="{x:Static c:ContractPickerPopup.FlushSelectedSecurityHandler}" /> diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml index 88fd35b7f..c0830d44d 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml @@ -227,11 +227,23 @@ + Header="{Binding value}" + ShowDialogSkirt="True" + ToolTip="{Binding value, Converter={x:Static c:ContractPickerPopup.SecurityCriteriaTextConverter}, Mode=OneWay}" + OKClick="{x:Static c:ContractPickerPopup.FlushSelectedCriteriaHandler}" + > + + + + + + diff --git a/src/client/spark/view/Controls/Popup/PopupControl.xaml.cs b/src/client/spark/view/Controls/Popup/PopupControl.xaml.cs index 244f29d9c..b8b07bddb 100644 --- a/src/client/spark/view/Controls/Popup/PopupControl.xaml.cs +++ b/src/client/spark/view/Controls/Popup/PopupControl.xaml.cs @@ -72,11 +72,13 @@ namespace mbts.ui.controls { static void OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var target = (PopupControl)d; if ((bool)e.NewValue) { + target.OnOpened(EventArgs.Empty); target.RaiseEvent(new RoutedEventArgs(OpenedEvent, target)); (FocusManager.GetFocusScope(target) as UIElement)?.Focus(); Keyboard.Focus(target); target?._popup?.Child?.Focus(); } else { + target.OnClosed(EventArgs.Empty); target.ReleaseMouseCapture(); target.RaiseEvent(new RoutedEventArgs(ClosedEvent, target)); if (target.IsKeyboardFocused || target.IsKeyboardFocusWithin) @@ -121,5 +123,8 @@ namespace mbts.ui.controls { _popup.PlacementTarget = PlacementTarget ?? _button; _popup_hook = PopupViewController.attach(_popup, _button); } + + protected virtual void OnOpened(EventArgs e) { } + protected virtual void OnClosed(EventArgs e) { } } } \ No newline at end of file diff --git a/src/client/spark/view/Shell/Settings/ShellSettingsDialog.xaml b/src/client/spark/view/Shell/Settings/ShellSettingsDialog.xaml index d39ac73c2..1741d74cf 100644 --- a/src/client/spark/view/Shell/Settings/ShellSettingsDialog.xaml +++ b/src/client/spark/view/Shell/Settings/ShellSettingsDialog.xaml @@ -346,13 +346,16 @@ diff --git a/src/client/spark/view/Spark/ContractPicker/ContractPickerModel.cs b/src/client/spark/view/Spark/ContractPicker/ContractPickerModel.cs index 98e2ff83e..456b2f9a5 100644 --- a/src/client/spark/view/Spark/ContractPicker/ContractPickerModel.cs +++ b/src/client/spark/view/Spark/ContractPicker/ContractPickerModel.cs @@ -13,10 +13,14 @@ using System.Windows.Input; using static mbts.spark.SparkTags; namespace mbts.spark { - internal class ContractPickerModel : ViewModelBase, IDisposable { + internal class ContractPickerModel : ViewModelBase, IEditableObject, IDisposable { private const string dot = "•"; - bool _is_edit_mode; + + readonly IRaiseCanExecuteChangedCommand _add_criteria_command, _remove_criteria_command, _ok_button_command; + + bool _allow_empty, _is_edit_mode; SecurityImmutable? _pending_sec; + SecurityCriteria _snapshot; public ContractPickerModel(IBus bus, bool edit_mode, SecurityCriteria existing_criteria, SecurityCriteria world_view_filter = null) : this(bus.subscribe(SecuritiesTag), edit_mode, existing_criteria, world_view_filter) { } @@ -31,7 +35,7 @@ namespace mbts.spark { (securities_view as ListCollectionView).CustomSort = new SecuritySorter(); _subs.Add(source.OnDispatcher().Subscribe(on_securities_changed)); - add_criteria_command = new DelegateCommand( + _add_criteria_command = new DelegateCommand( execute: _ => { if (is_edit_mode && selected_securities.Count > 1) { // make separate entries in case multiple securities are selected @@ -65,18 +69,20 @@ namespace mbts.spark { can_execute: _ => { return !String.IsNullOrEmpty(criteria_input_name); } ); - remove_criteria_command = new DelegateCommand( + _remove_criteria_command = new DelegateCommand( execute: _ => { security_criteria_clauses.removeRange(selected_security_criteria_clauses); ok_button_command.TryRaiseCanExecuteChanged(); - remove_criteria_command.TryRaiseCanExecuteChanged(); + _remove_criteria_command.TryRaiseCanExecuteChanged(); }, can_execute: _ => { return security_criteria_clauses.Count > 0; } ); - ok_button_command = new DelegateCommand( + _ok_button_command = new DelegateCommand( execute: _ => { ok_action?.Invoke(); }, can_execute: _ => { + if (_allow_empty) + return true; if (!is_edit_mode) { return (selected_securities.Count > 0); } else { @@ -236,9 +242,9 @@ namespace mbts.spark { public List selected_securities = new List(); public ObservableCollection securities { get; } = new ObservableCollection(); - public ICommand add_criteria_command { get; } - public ICommand remove_criteria_command { get; } - public ICommand ok_button_command { get; } + public ICommand add_criteria_command => _add_criteria_command; + public ICommand remove_criteria_command => _remove_criteria_command; + public ICommand ok_button_command => _ok_button_command; public ICommand cancel_button_command { get; } public ICommand add_symbol_command { get; } public ICommand add_security_type_command { get; } @@ -269,6 +275,15 @@ namespace mbts.spark { public event Action ok_action; public event Action cancel_action; + public bool allow_empty { + get => _allow_empty; + set { + if (_allow_empty != value) { + _allow_empty = value; + _ok_button_command.RaiseCanExecuteChanged(); + } + } + } public bool is_edit_mode { get => _is_edit_mode; set => Set(ref _is_edit_mode, value); } public event EventHandler security_criteria_clause_changed; @@ -319,6 +334,24 @@ namespace mbts.spark { } } + #region IEditableObject + public void BeginEdit() => _snapshot = result_criteria; + public void EndEdit() => _snapshot = null; + public void CancelEdit() { + if (_snapshot != null) { + if (security_criteria_clauses.Count > 0) + security_criteria_clauses.Clear(); + if (_snapshot.clauses != null) { + for (int i = 0; i < _snapshot.clauses.Length; i++) + security_criteria_clauses.Add(_snapshot.clauses[i]); + } + _snapshot = null; + _add_criteria_command.RaiseCanExecuteChanged(); + _remove_criteria_command.RaiseCanExecuteChanged(); + } + } + #endregion + private void _update_exchanges() { all_exchanges.sync_with(all_securities .Select(s => s.Value.security.Exchange) @@ -389,7 +422,6 @@ namespace mbts.spark { public void Dispose() { Disposable.dispose(_subs); } - } internal class SecurityCriteriaClauseChangedEventsArgs : EventArgs diff --git a/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml.cs b/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml.cs index 8576acdf5..49eb0599b 100644 --- a/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml.cs +++ b/src/client/spark/view/Spark/ContractPicker/ContractPickerPopup.xaml.cs @@ -14,9 +14,13 @@ namespace mbts.ui.controls { /// Interaction logic for ContractPickerView.xaml /// partial class ContractPickerPopup : PopupControl { + public static readonly EventHandler + FlushSelectedCriteriaHandler = (s, e) => (s as ContractPickerPopup)?.GetBindingExpression(SelectedCriteriaProperty)?.UpdateSource(), + FlushSelectedSecurityHandler = (s, e) => (s as ContractPickerPopup)?.GetBindingExpression(SelectedSecurityProperty)?.UpdateSource(); + IReadOnlyDictionary _cached_security_map = DictionaryHelper.empty(); ContractPickerPopupController _controller; - bool _is_lazy; + bool _allow_empty, _is_lazy; public ContractPickerPopup() { SetCurrentValue(SecurityMapProperty, new Dictionary(0)); @@ -57,12 +61,14 @@ namespace mbts.ui.controls { void on_data_context_disposed(bool obj) => Disposable.dispose(ref _controller); } + public event EventHandler OKClick, CancelClick; + event Action> _security_map_changed; event Action _selected_criteria_changed; - event Action _is_lazy_changed; + event Action _allow_empty_changed, _is_lazy_changed; public static IValueConverter SecurityCriteriaTextConverter = - ValueConverter.create(s => s == null ? "(None)" : string.Join(", ", s.clauses.empty_if_null())); + ValueConverter.create(s => s.is_defined() ? string.Join(", ", s.clauses.empty_if_null()) : "(None)"); #region ShowDialogSkirt public static readonly DependencyProperty ShowDialogSkirtProperty = @@ -135,8 +141,8 @@ namespace mbts.ui.controls { WPFHelper.create_dp( nameof(CloseOnOK), (o, oldval, newval) => { - if (oldval) o.Controller.model.ok_action -= o._close_on_ok; - if (newval) o.Controller.model.ok_action += o._close_on_ok; + if (oldval) o.Controller.model.ok_action -= o._close; + if (newval) o.Controller.model.ok_action += o._close; }); public bool CloseOnOK { @@ -167,6 +173,16 @@ namespace mbts.ui.controls { set => _controller.model.is_edit_mode = value; } + public bool AllowEmpty { + get => _allow_empty; + set { + if (_allow_empty != value) { + _allow_empty = value; + _allow_empty_changed?.Invoke(value); + } + } + } + public bool IsLazy { get => _is_lazy; set { @@ -177,21 +193,28 @@ namespace mbts.ui.controls { } } - void _close() => SetCurrentValue(IsOpenProperty, false); - - void _close_on_ok() { - this.set_current_if_changed(SelectedSecurityMDProperty, Controller.model.selected_security_md); - this.set_current_if_changed(SelectedSecurityProperty, Controller.model.selected_security); - this.set_current_if_changed(SelectedCriteriaProperty, Controller.model.result_criteria); - _close(); + protected override void OnClosed(EventArgs e) { + if (ShowDialogSkirt) + _controller.model.CancelEdit(); } + protected override void OnOpened(EventArgs e) { + if (ShowDialogSkirt) + _controller.model.BeginEdit(); + } + + void _close() => SetCurrentValue(IsOpenProperty, false); + public class ContractPickerPopupController : DisposableViewModelBase { readonly Property _selected_criteria; public ContractPickerPopupController(ContractPickerPopup owner, ContractPickerModel model) { _selected_criteria = backing(nameof(selected_criteria)); + model.allow_empty = owner._allow_empty; model.security_criteria_clauses.CollectionChanged += on_clauses_collection_changed; + model.ok_action += on_ok; + model.cancel_action += on_cancel; + owner._allow_empty_changed += on_allow_empty_changed; owner._selected_criteria_changed += on_selected_criteria_changed; _dispose = this.model = model; // local methods @@ -211,12 +234,28 @@ namespace mbts.ui.controls { _selected_criteria.set(criteria ?? new SecurityCriteria()); } + void on_allow_empty_changed(bool value) => model.allow_empty = value; void on_clauses_collection_changed(object sender, NotifyCollectionChangedEventArgs e) { owner._selected_criteria_changed -= on_selected_criteria_changed; _selected_criteria.set(model.result_criteria); WPFHelper.set_current_if_changed(owner, SelectedCriteriaProperty, selected_criteria); owner._selected_criteria_changed += on_selected_criteria_changed; } + void on_ok() { + model.EndEdit(); + propagate_to_owner(); + owner.OKClick?.Invoke(owner, EventArgs.Empty); + } + void on_cancel() { + model.CancelEdit(); + propagate_to_owner(); + owner.CancelClick?.Invoke(owner, EventArgs.Empty); + } + void propagate_to_owner() { + owner.set_current_if_changed(SelectedSecurityMDProperty, owner.Controller.model.selected_security_md); + owner.set_current_if_changed(SelectedSecurityProperty, owner.Controller.model.selected_security); + owner.set_current_if_changed(SelectedCriteriaProperty, owner.Controller.model.result_criteria); + } } public ContractPickerModel model { get; }