add OK/Cancel buttons to sec pickers in ladder/orderbook settings dialogs

This commit is contained in:
John Colvin committed 2019-06-19 19:56:20 -05:00
1 parent ed08cb11d7
commit bc3f2db0f9
6 files changed
+120 -28

No files matched your search

@@ -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}"
/>
<ComboBox Grid.Row="2" Grid.Column="2" ItemsSource="{DynamicResource ResourceKey={x:Static usp:ManualFillRowEditor.AccountsKey}}" SelectedItem="{Binding account}"/>
<ComboBox Grid.Row="2" Grid.Column="4" ItemsSource="{DynamicResource ResourceKey={x:Static usp:ManualFillRowEditor.UsersKey}}" SelectedItem="{Binding user}"/>
@@ -227,11 +227,23 @@
<u:TemplateSelectorKeyValue Key="{x:Static usp:PredicateTemplateKeys.SecurityCriteria}">
<DataTemplate>
<c:ContractPickerPopup
SelectedCriteria="{Binding value}"
AllowEmpty="True"
CloseOnOK="True"
CloseOnCancel="True"
MaxWidth="180"
SelectedCriteria="{Binding value, UpdateSourceTrigger=Explicit}"
SecurityMap="{Binding Path='[security_map]', UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
Header="{Binding SelectedCriteria, RelativeSource={RelativeSource Self}}"
ToolTip="{Binding SelectedCriteria, RelativeSource={RelativeSource Self}, Converter={x:Static c:ContractPickerPopup.SecurityCriteriaTextConverter}, Mode=OneWay}"
/>
Header="{Binding value}"
ShowDialogSkirt="True"
ToolTip="{Binding value, Converter={x:Static c:ContractPickerPopup.SecurityCriteriaTextConverter}, Mode=OneWay}"
OKClick="{x:Static c:ContractPickerPopup.FlushSelectedCriteriaHandler}"
>
<c:ContractPickerPopup.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={x:Static c:ContractPickerPopup.SecurityCriteriaTextConverter}}" TextTrimming="CharacterEllipsis"/>
</DataTemplate>
</c:ContractPickerPopup.HeaderTemplate>
</c:ContractPickerPopup>
</DataTemplate>
</u:TemplateSelectorKeyValue>
</u:KeyedTemplateSelector>
@@ -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) { }
}
}
@@ -346,13 +346,16 @@
</c:PopupControl>
<c:ContractPickerPopup
x:Name="security_picker"
AllowEmpty="True"
CloseOnCancel="True"
CloseOnOK="True"
IsEnabled="{Binding selected_profile.is_global}"
IsLazy="True"
SecurityMap="{DynamicResource {x:Static usp:ShellSettingsDialog.SecurityMapKey}}"
SelectedCriteria="{Binding selected_profile.security_criteria}"
SelectedCriteria="{Binding selected_profile.security_criteria, UpdateSourceTrigger=Explicit}"
ShowDialogSkirt="True"
Style="{StaticResource SparkToolBarItem}"
OKClick="{x:Static c:ContractPickerPopup.FlushSelectedCriteriaHandler}"
>
<c:ContractPickerPopup.Header>
<Image Source="{x:Static u:Icons.HandPointingOverDocument}"/>
@@ -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<uint> selected_securities = new List<uint>();
public ObservableCollection<SecurityImmutable> securities { get; } = new ObservableCollection<SecurityImmutable>();
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<SecurityCriteriaClauseChangedEventsArgs> 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
@@ -14,9 +14,13 @@ namespace mbts.ui.controls {
/// Interaction logic for ContractPickerView.xaml
/// </summary>
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<uint, SecurityMD> _cached_security_map = DictionaryHelper.empty<uint, SecurityMD>();
ContractPickerPopupController _controller;
bool _is_lazy;
bool _allow_empty, _is_lazy;
public ContractPickerPopup() {
SetCurrentValue(SecurityMapProperty, new Dictionary<uint, SecurityMD>(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<IReadOnlyDictionary<uint, SecurityMD>> _security_map_changed;
event Action<SecurityCriteria> _selected_criteria_changed;
event Action<bool> _is_lazy_changed;
event Action<bool> _allow_empty_changed, _is_lazy_changed;
public static IValueConverter SecurityCriteriaTextConverter =
ValueConverter.create<SecurityCriteria, string>(s => s == null ? "(None)" : string.Join(", ", s.clauses.empty_if_null()));
ValueConverter.create<SecurityCriteria, string>(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<bool, ContractPickerPopup>(
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<SecurityCriteria> _selected_criteria;
public ContractPickerPopupController(ContractPickerPopup owner, ContractPickerModel model) {
_selected_criteria = backing<SecurityCriteria>(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; }