From d60986c5f0628fc4e2bbb7cf19cc6115582614b5 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 15 Apr 2019 18:30:33 -0500 Subject: [PATCH] AlgoEditorGridView - ctrl+up/down arrow key increments/decrements value in TickUpDown cells --- .../AlgoEditorGridView.xaml | 71 +++++++++++++++++-- .../AlgoEditorGridView/DataGridEditHelper.cs | 25 ++++--- .../Order/QuantityMeasureControl.xaml.cs | 37 ++++++++++ .../view/Controls/PriceEntryControl.xaml | 1 + .../view/Controls/PriceEntryControl.xaml.cs | 23 ++++++ .../Controls/TickUpDown/TickUpDown.xaml.cs | 54 +++++++------- .../Controls/TickUpDown/TickUpDownDouble.cs | 47 ++++++++++-- .../view/Controls/TickUpDown/TickUpDownInt.cs | 31 ++++++-- .../TickUpDown/TickUpDownNullableInt.cs | 34 ++++++++- .../TickUpDown/TickUpDownNullableUInt.cs | 34 ++++++++- .../Controls/TickUpDown/TickUpDownUInt.cs | 35 ++++++++- .../spark/view/Utility/LazyPropagator.cs | 47 ++++++++++++ .../spark/view/Utility/NavigationHelper.cs | 21 ++++++ .../Extensions/EnumerableExtensions.cs | 7 ++ 14 files changed, 406 insertions(+), 61 deletions(-) create mode 100644 src/client/spark/view/Utility/LazyPropagator.cs diff --git a/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoEditorGridView.xaml b/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoEditorGridView.xaml index 1e6cebba5..c11086826 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoEditorGridView.xaml +++ b/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/AlgoEditorGridView.xaml @@ -308,7 +308,16 @@ ContractSpec="{Binding DataContext.spec, RelativeSource={RelativeSource AncestorType=usp:AlgoEditorGridView}}" Focusable="False" IsTabStop="False" - /> + ArrowKeyTickModifiers="Ctrl" + > + + + + + + + + @@ -321,7 +330,15 @@ - + + + + + + + + + @@ -338,7 +355,15 @@ - + + + + + + + + + @@ -361,13 +386,29 @@ - + + + + + + + + + - + + + + + + + + + @@ -379,7 +420,15 @@ - + + + + + + + + + @@ -416,7 +465,15 @@ - + + + + + + + + + diff --git a/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/DataGridEditHelper.cs b/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/DataGridEditHelper.cs index ee7f35ba4..837454231 100644 --- a/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/DataGridEditHelper.cs +++ b/src/client/spark/Modules/Trading/AlgoStudio/View/AlgoEditorGridView/DataGridEditHelper.cs @@ -17,22 +17,23 @@ namespace mbts.ui.spark { static class DataGridEditHelper { public static IDisposable set_hooks(DataGrid grid) { grid.PreviewKeyDown += on_preview_key_down; + grid.KeyDown += on_key_down; grid.SelectionChanged += on_selection_changed; grid.PreparingCellForEdit += on_preparing_cell_for_edit; grid.CellEditEnding += on_cell_edit_ending; grid.GotFocus += on_got_focus; return Disposable.Create(() => { - grid.PreviewKeyDown -= on_preview_key_down; + grid.PreviewKeyDown += on_preview_key_down; + grid.KeyDown -= on_key_down; grid.SelectionChanged -= on_selection_changed; grid.PreparingCellForEdit -= on_preparing_cell_for_edit; grid.CellEditEnding -= on_cell_edit_ending; grid.GotFocus -= on_got_focus; }); - // handlers + /// Handlers + // hooked up to PreviewKeyDown, instead of KeyDown, so the cells can't override this behavior void on_preview_key_down(object sender, KeyEventArgs e) { switch (e.Key) { - case Key.Up: move_focus(FocusNavigationDirection.Up); break; - case Key.Down: move_focus(FocusNavigationDirection.Down); break; case Key.Escape: { if (grid.CurrentCell != null && grid.TryGetCurrentRowView(out var row_view) && row_view.IsEditing) { using (grid.SuspendGotFocus(on_got_focus)) @@ -48,7 +49,7 @@ namespace mbts.ui.spark { using (grid.SuspendGotFocus(on_got_focus)) grid.CommitEdit(DataGridEditingUnit.Row, true); #if ENTER_BEHAVES_LIKE_TAB - move_focus(FocusNavigationDirection.Down); + NavigationHelper.keyboard_move_focus(FocusNavigationDirection.Down); #endif } } @@ -57,9 +58,17 @@ namespace mbts.ui.spark { default: return; } e.Handled = true; - // local methods - void move_focus(FocusNavigationDirection direction) => - (Keyboard.FocusedElement as UIElement)?.MoveFocus(new TraversalRequest(direction)); + } + + // hooked up to KeyDown, instead of PreviewKeyDown, so cells can implement custom Key/CommandBindings, or other key-based behavior without the DataGrid handling it in the PreviewKeyDown event. + // for ex: TickUpDown cells can do things like TickUp/Down when Ctrl+Up/Down arrow-key is pressed. + void on_key_down(object sender, KeyEventArgs e) { + switch (e.Key) { + case Key.Up: NavigationHelper.keyboard_move_focus(FocusNavigationDirection.Up); break; + case Key.Down: NavigationHelper.keyboard_move_focus(FocusNavigationDirection.Down); break; + default: return; + } + e.Handled = true; } void on_selection_changed(object sender, SelectionChangedEventArgs e) { diff --git a/src/client/spark/view/Controls/Order/QuantityMeasureControl.xaml.cs b/src/client/spark/view/Controls/Order/QuantityMeasureControl.xaml.cs index daa514300..df5489f3b 100644 --- a/src/client/spark/view/Controls/Order/QuantityMeasureControl.xaml.cs +++ b/src/client/spark/view/Controls/Order/QuantityMeasureControl.xaml.cs @@ -1,7 +1,11 @@ using bts.spark; +using mbts.ui.controls; +using mbts.ui.utilities; using mbts.ui.utility; +using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; namespace mbts.ui.spark.algo { /// @@ -14,16 +18,44 @@ namespace mbts.ui.spark.algo { static double _default_ratio => _default_quantity_measure.QuantityCase == bts.spark.QuantityMeasure.QuantityOneofCase.Ratio ? _default_quantity_measure.Ratio : default; readonly PropertyUpdateGate _update_gate = new PropertyUpdateGate(); + // TickUpDown + LazyPropagator<(TickUpDownUInt absolute, TickUpDownDouble ratio), QuantityMeasureControl> _explicit_props = new LazyPropagator<(TickUpDownUInt absolute, TickUpDownDouble ratio), QuantityMeasureControl>((t, v) => { + t._enable_arrow_key_tick.target = v; + t._arrow_key_tick_modifiers.target = v; + t._arrow_key_tick_alt_modifiers.target = v; + }); + LazyPropagator _enable_arrow_key_tick = new LazyPropagator((t, v) => { + if (t.absolute != null) t.absolute.EnableArrowKeyTick = v; + if (t.ratio != null) t.ratio.EnableArrowKeyTick = v; + }, true); + LazyPropagator + _arrow_key_tick_modifiers = new LazyPropagator((t, v) => { + if (t.absolute != null) t.absolute.ArrowKeyTickModifiers = v; + if (t.ratio != null) t.ratio.ArrowKeyTickModifiers = v; + }, ModifierKeys.None), + _arrow_key_tick_alt_modifiers = new LazyPropagator((t, v) => { + t.absolute.ArrowKeyTickAltModifiers = v; + t.ratio.ArrowKeyTickAltModifiers = v; + }, ModifierKeys.Alt); static QuantityMeasureControl() => DefaultStyleKeyProperty.OverrideMetadata(typeof(QuantityMeasureControl), new FrameworkPropertyMetadata(typeof(QuantityMeasureControl))); + public QuantityMeasureControl() => + _explicit_props.target = this; + public override void OnApplyTemplate() { string hcn = HeaderColName; if (hcn != null) { var root = GetTemplateChild("Root") as Grid; if (root != null) root.ColumnDefinitions[0].SharedSizeGroup = hcn; } + var absolute = GetTemplateChild("PART_Absolute_TickUpDown") as TickUpDownUInt; + var ratio = GetTemplateChild("PART_Ratio_TickUpDown") as TickUpDownDouble; + _explicit_props.value = (absolute, ratio); + // this is a workaround to get KeyBindings to work since the TextBox blocks them while it has keyboard focus. + if (absolute != null) absolute.InputBindings.AddRange(InputBindings.OfType()); + if (ratio != null) ratio.InputBindings.AddRange(InputBindings.OfType()); } public static readonly DependencyProperty HeaderColNameProperty = DependencyProperty.Register("HeaderColName", typeof(string), typeof(QuantityMeasureControl)); @@ -131,6 +163,11 @@ namespace mbts.ui.spark.algo { } #endregion + // explicit props + public bool EnableArrowKeyTick { get => _enable_arrow_key_tick.value; set => _enable_arrow_key_tick.value = value; } + public ModifierKeys ArrowKeyTickModifiers { get => _arrow_key_tick_modifiers.value; set => _arrow_key_tick_modifiers.value = value; } + public ModifierKeys ArrowKeyTickAltModifiers { get => _arrow_key_tick_alt_modifiers.value; set => _arrow_key_tick_alt_modifiers.value = value; } + static class VisualStateNames { public static class QuantityCase { public const string GroupName = nameof(QuantityCase); diff --git a/src/client/spark/view/Controls/PriceEntryControl.xaml b/src/client/spark/view/Controls/PriceEntryControl.xaml index 3a60e4fa9..766b79933 100644 --- a/src/client/spark/view/Controls/PriceEntryControl.xaml +++ b/src/client/spark/view/Controls/PriceEntryControl.xaml @@ -13,6 +13,7 @@ _explicit_props = new LazyPropagator((t, v) => { + t._enable_arrow_key_tick.target = v; + t._arrow_key_tick_modifiers.target = v; + t._arrow_key_tick_alt_modifiers.target = v; + }); + LazyPropagator _enable_arrow_key_tick = new LazyPropagator((t, v) => t.EnableArrowKeyTick = v, true); + LazyPropagator + _arrow_key_tick_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None), + _arrow_key_tick_alt_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickAltModifiers = v, ModifierKeys.Alt); static PriceEntryControl() => DefaultStyleKeyProperty.OverrideMetadata(typeof(PriceEntryControl), new FrameworkPropertyMetadata(typeof(PriceEntryControl))); public PriceEntryControl() { _spec = ContractSpec; + _explicit_props.target = this; TickUpCommand = new DelegateCommand(_tick_up); TickDownCommand = new DelegateCommand(_tick_down); } @@ -83,8 +97,17 @@ namespace mbts.ui.spark { public ContractSpecificationImmutable? ContractSpec { get => GetValue(ContractSpecProperty) as ContractSpecificationImmutable?; set => SetValue(ContractSpecProperty, value); } #endregion + // explicit props + public bool EnableArrowKeyTick { get => _enable_arrow_key_tick.value; set => _enable_arrow_key_tick.value = value; } + public ModifierKeys ArrowKeyTickModifiers { get => _arrow_key_tick_modifiers.value; set => _arrow_key_tick_modifiers.value = value; } + public ModifierKeys ArrowKeyTickAltModifiers { get => _arrow_key_tick_alt_modifiers.value; set => _arrow_key_tick_alt_modifiers.value = value; } + TickUpDown _tick_up_down { get => _explicit_props.value; set => _explicit_props.value = value; } + public override void OnApplyTemplate() { + _tick_up_down = this.assert_template_child("PART_TickUpDown"); _text = this.assert_template_child("_text"); + // this is a workaround to get KeyBindings to work since the TextBox blocks them while it has keyboard focus. + _text.InputBindings.AddRange(InputBindings.OfType()); Disposable.replace(ref _text_hooks, _text.SubscribeTextChanged((s, e) => _update_value())); _update_text(); } diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml.cs index a551f032c..555d99a55 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml.cs @@ -24,39 +24,37 @@ namespace mbts.ui.controls { protected override void OnPreviewKeyDown(KeyEventArgs e) { if (EnableArrowKeyTick) { - switch (Keyboard.Modifiers) { - case ModifierKeys.None: { - switch (e.Key) { - case Key.Up: - TickUpCommand.TryExecuteIfCan(TickUpParameter); - e.Handled = true; - break; - case Key.Down: - TickDownCommand.TryExecuteIfCan(TickDownParameter); - e.Handled = true; - break; - } - } - break; - case ModifierKeys.Alt: { - // see: https://stackoverflow.com/questions/3099472/previewkeydown-is-not-seeing-alt-modifiers - switch (e.SystemKey) { - case Key.Up: - TickUpCommand.TryExecuteIfCan(TickUpAltParameter); - e.Handled = true; - break; - case Key.Down: - TickDownCommand.TryExecuteIfCan(TickDownAltParameter); - e.Handled = true; - break; - } - } - break; + if (Keyboard.Modifiers == ArrowKeyTickModifiers) { + switch (e.Key) { + case Key.Up: + TickUpCommand.TryExecuteIfCan(TickUpParameter); + e.Handled = true; + break; + case Key.Down: + TickDownCommand.TryExecuteIfCan(TickDownParameter); + e.Handled = true; + break; + } + } else if (Keyboard.Modifiers == ArrowKeyTickAltModifiers) { + // see: https://stackoverflow.com/questions/3099472/previewkeydown-is-not-seeing-alt-modifiers + switch (e.SystemKey) { + case Key.Up: + TickUpCommand.TryExecuteIfCan(TickUpAltParameter); + e.Handled = true; + break; + case Key.Down: + TickDownCommand.TryExecuteIfCan(TickDownAltParameter); + e.Handled = true; + break; + } } + //(Keyboard.FocusedElement as UIElement)?.MoveFocus(); } base.OnPreviewKeyDown(e); } + public ModifierKeys ArrowKeyTickModifiers { get; set; } = ModifierKeys.None; + public ModifierKeys ArrowKeyTickAltModifiers { get; set; } = ModifierKeys.Alt; public bool EnableArrowKeyTick { get; set; } = true; #region TickCommand diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownDouble.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownDouble.cs index 9ea6437ac..ec18bd32f 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownDouble.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownDouble.cs @@ -1,17 +1,42 @@ using mbts.ui.extensions; +using mbts.ui.utilities; using System; using System.Globalization; +using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; namespace mbts.ui.controls { class TickUpDownDouble : Control { - IDisposable _text_hooks = null; + public static readonly RoutedCommand + TickUpCommand = new RoutedCommand(), + TickDownCommand = new RoutedCommand(); + + public static readonly ExecutedRoutedEventHandler + TickUpExecuted = (s, e) => (s as TickUpDownDouble)?._tick_up(), + TickDownExecuted = (s, e) => (s as TickUpDownDouble)?._tick_down(); + + // TextBox TextBox _text_box; + IDisposable _text_hooks = null; + // TickUpDown + LazyPropagator _explicit_props = new LazyPropagator((t, v) => { + t._enable_arrow_key_tick.target = v; + t._arrow_key_tick_modifiers.target = v; + t._arrow_key_tick_alt_modifiers.target = v; + }); + LazyPropagator _enable_arrow_key_tick = new LazyPropagator((t, v) => t.EnableArrowKeyTick = v, true); + LazyPropagator + _arrow_key_tick_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None), + _arrow_key_tick_alt_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickAltModifiers = v, ModifierKeys.Alt); static TickUpDownDouble() => DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownDouble), new FrameworkPropertyMetadata(typeof(TickUpDownDouble))); + public TickUpDownDouble() => + _explicit_props.target = this; + #region FlushTextImmediately public static readonly DependencyProperty FlushTextImmediatelyProperty = WPFHelper.create_dp(nameof(FlushTextImmediately)); @@ -45,14 +70,26 @@ namespace mbts.ui.controls { public double Multiplier { get { return (double)GetValue(MultiplierProperty); } set { SetValue(MultiplierProperty, value); } } public string Format { get { return GetValue(FormatProperty) as string; } set { SetValue(FormatProperty, value); } } public double TickDefaultValue { get => (double)GetValue(TickDefaultValueProperty); set => SetValue(TickDefaultValueProperty, value); } + // explicit props + public bool EnableArrowKeyTick { get => _enable_arrow_key_tick.value; set => _enable_arrow_key_tick.value = value; } + public ModifierKeys ArrowKeyTickModifiers { get => _arrow_key_tick_modifiers.value; set => _arrow_key_tick_modifiers.value = value; } + public ModifierKeys ArrowKeyTickAltModifiers { get => _arrow_key_tick_alt_modifiers.value; set => _arrow_key_tick_alt_modifiers.value = value; } + TickUpDown _tick_up_down { get => _explicit_props.value; set => _explicit_props.value = value; } public override void OnApplyTemplate() { Disposable.dispose(ref _text_hooks); - var tick_up_down = this.assert_template_child("PART_tick_up_down"); + // TickUpDown + _tick_up_down = this.assert_template_child("PART_tick_up_down"); + _tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); + _tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); + // TextBox _text_box = this.assert_template_child("PART_TextBox"); _text_box.Text = _format_value(Value, Format); - tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); - tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); + if (_text_box.InputBindings.Count == 0) { + // this is a workaround to get KeyBindings to work since the TextBox blocks them while it has keyboard focus. + // only do this if count == 0, to avoid interfering with keybindings explicitly set in a custom ControlTemplate. + _text_box.InputBindings.AddRange(InputBindings.OfType()); + } push_text_to_value(); _text_hooks = _text_box.SubscribeTextChanged((s, e) => push_text_to_value()); void push_text_to_value() { @@ -79,4 +116,4 @@ namespace mbts.ui.controls { static string _format_value(double value, string format) => format == null ? value.ToString() : value.ToString(format, CultureInfo.CurrentCulture); } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownInt.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownInt.cs index f392a12c7..5b8c155af 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownInt.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownInt.cs @@ -1,17 +1,33 @@ using bts.utils; using mbts.ui.extensions; +using mbts.ui.utilities; using System; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; namespace mbts.ui.controls { class TickUpDownInt : Control { - IDisposable _text_hooks = null; + // TextBox TextBox _text_box; + IDisposable _text_hooks = null; + // TickUpDown + LazyPropagator _explicit_props = new LazyPropagator((t, v) => { + t._enable_arrow_key_tick.target = v; + t._arrow_key_tick_modifiers.target = v; + t._arrow_key_tick_alt_modifiers.target = v; + }); + LazyPropagator _enable_arrow_key_tick = new LazyPropagator((t, v) => t.EnableArrowKeyTick = v, true); + LazyPropagator + _arrow_key_tick_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None), + _arrow_key_tick_alt_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickAltModifiers = v, ModifierKeys.Alt); static TickUpDownInt() => DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownInt), new FrameworkPropertyMetadata(typeof(TickUpDownInt))); + public TickUpDownInt() => + _explicit_props.target = this; + #region FlushTextImmediately public static readonly DependencyProperty FlushTextImmediatelyProperty = WPFHelper.create_dp( @@ -85,13 +101,20 @@ namespace mbts.ui.controls { } #endregion + public bool EnableArrowKeyTick { get => _enable_arrow_key_tick.value; set => _enable_arrow_key_tick.value = value; } + public ModifierKeys ArrowKeyTickModifiers { get => _arrow_key_tick_modifiers.value; set => _arrow_key_tick_modifiers.value = value; } + public ModifierKeys ArrowKeyTickAltModifiers { get => _arrow_key_tick_alt_modifiers.value; set => _arrow_key_tick_alt_modifiers.value = value; } + TickUpDown _tick_up_down { get => _explicit_props.value; set => _explicit_props.value = value; } + public override void OnApplyTemplate() { Disposable.dispose(ref _text_hooks); - var tick_up_down = this.assert_template_child("PART_tick_up_down"); + // TickUpDown + _tick_up_down = this.assert_template_child("PART_tick_up_down"); + _tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); + _tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); + // TextBox _text_box = this.assert_template_child("PART_TextBox"); _text_box.Text = Value.ToString(); - tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); - tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); push_text_to_value(); _text_hooks = _text_box.SubscribeTextChanged((s, e) => push_text_to_value()); void push_text_to_value() => diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs index e8294ebf4..a15b86015 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs @@ -1,16 +1,32 @@ using mbts.ui.extensions; +using mbts.ui.utilities; using System; +using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; namespace mbts.ui.controls { class TickUpDownNullableInt : Control { IDisposable _text_hooks = null; TextBox _text_box; + // TickUpDown + LazyPropagator _explicit_props = new LazyPropagator((t, v) => { + t._enable_arrow_key_tick.target = v; + t._arrow_key_tick_modifiers.target = v; + t._arrow_key_tick_alt_modifiers.target = v; + }); + LazyPropagator _enable_arrow_key_tick = new LazyPropagator((t, v) => t.EnableArrowKeyTick = v, true); + LazyPropagator + _arrow_key_tick_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None), + _arrow_key_tick_alt_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickAltModifiers = v, ModifierKeys.Alt); static TickUpDownNullableInt() => DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownNullableInt), new FrameworkPropertyMetadata(typeof(TickUpDownNullableInt))); + public TickUpDownNullableInt() => + _explicit_props.target = this; + #region Value public static readonly DependencyProperty ValueProperty = WPFHelper.create_dp( @@ -41,13 +57,25 @@ namespace mbts.ui.controls { new PropertyMetadata(0)); public int TickDefaultValue { get => (int)GetValue(TickDefaultValueProperty); set => SetValue(TickDefaultValueProperty, value); } + public bool EnableArrowKeyTick { get => _enable_arrow_key_tick.value; set => _enable_arrow_key_tick.value = value; } + public ModifierKeys ArrowKeyTickModifiers { get => _arrow_key_tick_modifiers.value; set => _arrow_key_tick_modifiers.value = value; } + public ModifierKeys ArrowKeyTickAltModifiers { get => _arrow_key_tick_alt_modifiers.value; set => _arrow_key_tick_alt_modifiers.value = value; } + TickUpDown _tick_up_down { get => _explicit_props.value; set => _explicit_props.value = value; } + public override void OnApplyTemplate() { Disposable.dispose(ref _text_hooks); - var tick_up_down = this.assert_template_child("PART_tick_up_down"); + // TickUpDown + _tick_up_down = this.assert_template_child("PART_tick_up_down"); + _tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); + _tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); + // TextBox _text_box = this.assert_template_child("PART_TextBox"); _text_box.Text = Value.ToString(); - tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); - tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); + if (_text_box.InputBindings.Count == 0) { + // this is a workaround to get KeyBindings to work since the TextBox blocks them while it has keyboard focus. + // only do this if count == 0, to avoid interfering with keybindings explicitly set in a custom ControlTemplate. + _text_box.InputBindings.AddRange(InputBindings.OfType()); + } push_text_to_value(); _text_hooks = _text_box.SubscribeTextChanged((s, e) => push_text_to_value()); void push_text_to_value() { diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs index 026c20f50..4e24d9c35 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs @@ -1,5 +1,7 @@ using mbts.ui.extensions; +using mbts.ui.utilities; using System; +using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -8,10 +10,23 @@ namespace mbts.ui.controls { class TickUpDownNullableUInt : Control { IDisposable _text_hooks = null, _commit_on_enter_hooks; TextBox _text_box; + // TickUpDown + LazyPropagator _explicit_props = new LazyPropagator((t, v) => { + t._enable_arrow_key_tick.target = v; + t._arrow_key_tick_modifiers.target = v; + t._arrow_key_tick_alt_modifiers.target = v; + }); + LazyPropagator _enable_arrow_key_tick = new LazyPropagator((t, v) => t.EnableArrowKeyTick = v, true); + LazyPropagator + _arrow_key_tick_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None), + _arrow_key_tick_alt_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickAltModifiers = v, ModifierKeys.Alt); static TickUpDownNullableUInt() => DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownNullableUInt), new FrameworkPropertyMetadata(typeof(TickUpDownNullableUInt))); + public TickUpDownNullableUInt() => + _explicit_props.target = this; + #region Value public static readonly DependencyProperty ValueProperty = WPFHelper.create_dp( @@ -56,13 +71,26 @@ namespace mbts.ui.controls { public bool CommitOnEnter { get => (bool)GetValue(CommitOnEnterProperty); set => SetValue(CommitOnEnterProperty, value); } + // explicit props + public bool EnableArrowKeyTick { get => _enable_arrow_key_tick.value; set => _enable_arrow_key_tick.value = value; } + public ModifierKeys ArrowKeyTickModifiers { get => _arrow_key_tick_modifiers.value; set => _arrow_key_tick_modifiers.value = value; } + public ModifierKeys ArrowKeyTickAltModifiers { get => _arrow_key_tick_alt_modifiers.value; set => _arrow_key_tick_alt_modifiers.value = value; } + TickUpDown _tick_up_down { get => _explicit_props.value; set => _explicit_props.value = value; } + public override void OnApplyTemplate() { Disposable.dispose(ref _text_hooks); - var tick_up_down = this.assert_template_child("PART_tick_up_down"); + // TickUpDown + _tick_up_down = this.assert_template_child("PART_tick_up_down"); + _tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); + _tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); + // TextBox _text_box = this.assert_template_child("PART_TextBox"); _text_box.Text = Value.ToString(); - tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); - tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); + if (_text_box.InputBindings.Count == 0) { + // this is a workaround to get KeyBindings to work since the TextBox blocks them while it has keyboard focus. + // only do this if count == 0, to avoid interfering with keybindings explicitly set in a custom ControlTemplate. + _text_box.InputBindings.AddRange(InputBindings.OfType()); + } push_text_to_value(); _text_hooks = _text_box.SubscribeTextChanged((s, e) => push_text_to_value()); void push_text_to_value() { diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownUInt.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownUInt.cs index a0f4bed50..119b9862e 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownUInt.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownUInt.cs @@ -1,16 +1,32 @@ using mbts.ui.extensions; +using mbts.ui.utilities; using System; +using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; namespace mbts.ui.controls { class TickUpDownUInt : Control { IDisposable _text_hooks = null; TextBox _text_box; + // TickUpDown + LazyPropagator _explicit_props = new LazyPropagator((t, v) => { + t._enable_arrow_key_tick.target = v; + t._arrow_key_tick_modifiers.target = v; + t._arrow_key_tick_alt_modifiers.target = v; + }); + LazyPropagator _enable_arrow_key_tick = new LazyPropagator((t, v) => t.EnableArrowKeyTick = v, true); + LazyPropagator + _arrow_key_tick_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None), + _arrow_key_tick_alt_modifiers = new LazyPropagator((t, v) => t.ArrowKeyTickAltModifiers = v, ModifierKeys.Alt); static TickUpDownUInt() => DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownUInt), new FrameworkPropertyMetadata(typeof(TickUpDownUInt))); + public TickUpDownUInt() => + _explicit_props.target = this; + #region FlushTextImmediately public static readonly DependencyProperty FlushTextImmediatelyProperty = WPFHelper.create_dp( @@ -49,13 +65,26 @@ namespace mbts.ui.controls { public uint Increment { get { return (uint)GetValue(IncrementProperty); } set { SetValue(IncrementProperty, value); } } + // explicit props + public bool EnableArrowKeyTick { get => _enable_arrow_key_tick.value; set => _enable_arrow_key_tick.value = value; } + public ModifierKeys ArrowKeyTickModifiers { get => _arrow_key_tick_modifiers.value; set => _arrow_key_tick_modifiers.value = value; } + public ModifierKeys ArrowKeyTickAltModifiers { get => _arrow_key_tick_alt_modifiers.value; set => _arrow_key_tick_alt_modifiers.value = value; } + TickUpDown _tick_up_down { get => _explicit_props.value; set => _explicit_props.value = value; } + public override void OnApplyTemplate() { Disposable.dispose(ref _text_hooks); - var tick_up_down = this.assert_template_child("PART_tick_up_down"); + // TickUpDown + _tick_up_down = this.assert_template_child("PART_tick_up_down"); + _tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); + _tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); + // TextBox _text_box = this.assert_template_child("PART_TextBox"); _text_box.Text = Value.ToString(); - tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); - tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); + if (_text_box.InputBindings.Count == 0) { + // this is a workaround to get KeyBindings to work since the TextBox blocks them while it has keyboard focus. + // only do this if count == 0, to avoid interfering with keybindings explicitly set in a custom ControlTemplate. + _text_box.InputBindings.AddRange(InputBindings.OfType()); + } push_text_to_value(); _text_hooks = _text_box.SubscribeTextChanged((s, e) => push_text_to_value()); void push_text_to_value() => diff --git a/src/client/spark/view/Utility/LazyPropagator.cs b/src/client/spark/view/Utility/LazyPropagator.cs new file mode 100644 index 000000000..f66686f98 --- /dev/null +++ b/src/client/spark/view/Utility/LazyPropagator.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; + +namespace mbts.ui.utilities { + class LazyPropagator { + readonly Action _assign; + readonly IEqualityComparer _comparer; + readonly IEqualityComparer _target_comparer; + Value _value; + Target _target; + + public LazyPropagator(Action assign) + : this(assign, default) { } + + public LazyPropagator(Action assign, Value init) + : this(assign, init, EqualityComparer.Default, EqualityComparer.Default) { } + + public LazyPropagator(Action assign, Value init, IEqualityComparer comparer, IEqualityComparer target_comparer) { + _assign = assign; + _value = init; + _comparer = comparer; + _target_comparer = target_comparer; + } + + public Target target { + get => _target; + set { + if (!_target_comparer.Equals(_target, value)) { + _target = value; + if (value != null) + _assign(value, _value); + } + } + } + + public Value value { + get => _value; + set { + if (!_comparer.Equals(value, _value)) { + _value = value; + if (_target != null) + _assign(_target, value); + } + } + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Utility/NavigationHelper.cs b/src/client/spark/view/Utility/NavigationHelper.cs index 6e5946b8b..e201502e7 100644 --- a/src/client/spark/view/Utility/NavigationHelper.cs +++ b/src/client/spark/view/Utility/NavigationHelper.cs @@ -5,6 +5,27 @@ using System.Windows.Input; namespace mbts.ui { static class NavigationHelper { + public static readonly RoutedCommand MoveFocusCommand = new RoutedCommand(); + public static readonly ExecutedRoutedEventHandler + KeyboardMoveFocusExecuted = (s, e) => { + if (e.Parameter is FocusNavigationDirection d) + (Keyboard.FocusedElement as UIElement)?.MoveFocus(new TraversalRequest(d)); + }; + + public static FocusNavigationDirection? get_direction_for_key(Key key) { + switch (key) { + case Key.Left: return FocusNavigationDirection.Left; + case Key.Up: return FocusNavigationDirection.Up; + case Key.Right: return FocusNavigationDirection.Right; + case Key.Down: return FocusNavigationDirection.Down; + case Key.Tab: return (Keyboard.Modifiers & ModifierKeys.Shift) != 0 ? FocusNavigationDirection.Previous : FocusNavigationDirection.Next; + default: return null; + } + } + + public static bool keyboard_move_focus(FocusNavigationDirection direction) => + (Keyboard.FocusedElement as UIElement)?.MoveFocus(new TraversalRequest(direction)) ?? false; + public static IDisposable connect_navigation(FrameworkElement element, FrameworkElement left, FrameworkElement up, FrameworkElement right, FrameworkElement down, ModifierKeys? modifier_keys = null) => connect_navigation(element, () => left, () => up, () => right, () => down, modifier_keys); diff --git a/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs b/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs index cea9ecd86..5b34cb0d0 100644 --- a/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/EnumerableExtensions.cs @@ -47,6 +47,13 @@ namespace System.Collections.Generic { return rv; } + public static IEnumerable ExceptNull(this IEnumerable source) where T : class { + foreach (var v in source) { + if (v != null) + yield return v; + } + } + /// /// Checks if implements or . If so, the resultant Count is returned, otherwise null (will not enumerate through any items). ///