AlgoEditorGridView - ctrl+up/down arrow key increments/decrements value in TickUpDown cells

This commit is contained in:
John Colvin committed 2019-04-15 20:04:54 -05:00
1 parent 19fca8304d
commit d60986c5f0
14 files changed
+406 -61

No files matched your search

@@ -308,7 +308,16 @@
ContractSpec="{Binding DataContext.spec, RelativeSource={RelativeSource AncestorType=usp:AlgoEditorGridView}}"
Focusable="False"
IsTabStop="False"
/>
ArrowKeyTickModifiers="Ctrl"
>
<usp:PriceEntryControl.CommandBindings>
<CommandBinding Command="{x:Static u:NavigationHelper.MoveFocusCommand}" Executed="{x:Static u:NavigationHelper.KeyboardMoveFocusExecuted}"/>
</usp:PriceEntryControl.CommandBindings>
<usp:PriceEntryControl.InputBindings>
<KeyBinding Key="Up" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Up}"/>
<KeyBinding Key="Down" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Down}"/>
</usp:PriceEntryControl.InputBindings>
</usp:PriceEntryControl>
</DataTemplate>
</u:TemplateSelectorKeyValue>
</usp:AlgoParamTemplateSelector.KeyedTemplates>
@@ -321,7 +330,15 @@
<!--Double-->
<usp:AlgoParamTemplateSelector.DoubleTemplate>
<DataTemplate>
<c:TickUpDownDouble Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}"/>
<c:TickUpDownDouble x:Name="tick_up_down" Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
<c:TickUpDownDouble.CommandBindings>
<CommandBinding Command="{x:Static u:NavigationHelper.MoveFocusCommand}" Executed="{x:Static u:NavigationHelper.KeyboardMoveFocusExecuted}"/>
</c:TickUpDownDouble.CommandBindings>
<c:TickUpDownDouble.InputBindings>
<KeyBinding Key="Up" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Up}"/>
<KeyBinding Key="Down" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Down}"/>
</c:TickUpDownDouble.InputBindings>
</c:TickUpDownDouble>
</DataTemplate>
</usp:AlgoParamTemplateSelector.DoubleTemplate>
<!--Enum-->
@@ -338,7 +355,15 @@
<!--Int-->
<usp:AlgoParamTemplateSelector.IntTemplate>
<DataTemplate>
<c:TickUpDownInt Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}"/>
<c:TickUpDownInt Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
<c:TickUpDownInt.CommandBindings>
<CommandBinding Command="{x:Static u:NavigationHelper.MoveFocusCommand}" Executed="{x:Static u:NavigationHelper.KeyboardMoveFocusExecuted}"/>
</c:TickUpDownInt.CommandBindings>
<c:TickUpDownInt.InputBindings>
<KeyBinding Key="Up" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Up}"/>
<KeyBinding Key="Down" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Down}"/>
</c:TickUpDownInt.InputBindings>
</c:TickUpDownInt>
</DataTemplate>
</usp:AlgoParamTemplateSelector.IntTemplate>
<!--Nullable Bool-->
@@ -361,13 +386,29 @@
<!--Nullable Int-->
<usp:AlgoParamTemplateSelector.NullableIntTemplate>
<DataTemplate>
<c:TickUpDownNullableInt Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}"/>
<c:TickUpDownNullableInt Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
<c:TickUpDownNullableInt.CommandBindings>
<CommandBinding Command="{x:Static u:NavigationHelper.MoveFocusCommand}" Executed="{x:Static u:NavigationHelper.KeyboardMoveFocusExecuted}"/>
</c:TickUpDownNullableInt.CommandBindings>
<c:TickUpDownNullableInt.InputBindings>
<KeyBinding Key="Up" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Up}"/>
<KeyBinding Key="Down" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Down}"/>
</c:TickUpDownNullableInt.InputBindings>
</c:TickUpDownNullableInt>
</DataTemplate>
</usp:AlgoParamTemplateSelector.NullableIntTemplate>
<!--Nullable UInt-->
<usp:AlgoParamTemplateSelector.NullableUIntTemplate>
<DataTemplate>
<c:TickUpDownNullableUInt Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}"/>
<c:TickUpDownNullableUInt Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
<c:TickUpDownNullableUInt.CommandBindings>
<CommandBinding Command="{x:Static u:NavigationHelper.MoveFocusCommand}" Executed="{x:Static u:NavigationHelper.KeyboardMoveFocusExecuted}"/>
</c:TickUpDownNullableUInt.CommandBindings>
<c:TickUpDownNullableUInt.InputBindings>
<KeyBinding Key="Up" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Up}"/>
<KeyBinding Key="Down" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Down}"/>
</c:TickUpDownNullableUInt.InputBindings>
</c:TickUpDownNullableUInt>
</DataTemplate>
</usp:AlgoParamTemplateSelector.NullableUIntTemplate>
<!--Order Def-->
@@ -379,7 +420,15 @@
<!--QuantityMeasure-->
<usp:AlgoParamTemplateSelector.QuantityMeasureTemplate>
<DataTemplate>
<uag:QuantityMeasureControl Focusable="False" HorizontalAlignment="Stretch" QuantityMeasure="{Binding param.value, UpdateSourceTrigger=PropertyChanged}"/>
<uag:QuantityMeasureControl Focusable="False" HorizontalAlignment="Stretch" QuantityMeasure="{Binding param.value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
<uag:QuantityMeasureControl.CommandBindings>
<CommandBinding Command="{x:Static u:NavigationHelper.MoveFocusCommand}" Executed="{x:Static u:NavigationHelper.KeyboardMoveFocusExecuted}"/>
</uag:QuantityMeasureControl.CommandBindings>
<uag:QuantityMeasureControl.InputBindings>
<KeyBinding Key="Up" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Up}"/>
<KeyBinding Key="Down" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Down}"/>
</uag:QuantityMeasureControl.InputBindings>
</uag:QuantityMeasureControl>
</DataTemplate>
</usp:AlgoParamTemplateSelector.QuantityMeasureTemplate>
<!--Timestamp-->
@@ -416,7 +465,15 @@
<!--UInt-->
<usp:AlgoParamTemplateSelector.UIntTemplate>
<DataTemplate>
<c:TickUpDownUInt Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}"/>
<c:TickUpDownUInt Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}">
<c:TickUpDownUInt.CommandBindings>
<CommandBinding Command="{x:Static u:NavigationHelper.MoveFocusCommand}" Executed="{x:Static u:NavigationHelper.KeyboardMoveFocusExecuted}"/>
</c:TickUpDownUInt.CommandBindings>
<c:TickUpDownUInt.InputBindings>
<KeyBinding Key="Up" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Up}"/>
<KeyBinding Key="Down" Command="{x:Static u:NavigationHelper.MoveFocusCommand}" CommandParameter="{x:Static FocusNavigationDirection.Down}"/>
</c:TickUpDownUInt.InputBindings>
</c:TickUpDownUInt>
</DataTemplate>
</usp:AlgoParamTemplateSelector.UIntTemplate>
</usp:AlgoParamTemplateSelector>
@@ -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) {
@@ -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 {
/// <summary>
@@ -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<bool, (TickUpDownUInt absolute, TickUpDownDouble ratio)> _enable_arrow_key_tick = new LazyPropagator<bool, (TickUpDownUInt absolute, TickUpDownDouble ratio)>((t, v) => {
if (t.absolute != null) t.absolute.EnableArrowKeyTick = v;
if (t.ratio != null) t.ratio.EnableArrowKeyTick = v;
}, true);
LazyPropagator<ModifierKeys, (TickUpDownUInt absolute, TickUpDownDouble ratio)>
_arrow_key_tick_modifiers = new LazyPropagator<ModifierKeys, (TickUpDownUInt absolute, TickUpDownDouble ratio)>((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<ModifierKeys, (TickUpDownUInt absolute, TickUpDownDouble ratio)>((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<KeyBinding>());
if (ratio != null) ratio.InputBindings.AddRange(InputBindings.OfType<KeyBinding>());
}
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);
@@ -13,6 +13,7 @@
<uspcv:TickDisplayTwoWayConverter x:Key="TickDisplayTwoWayConverter" x:Shared="False"/>
<ControlTemplate x:Key="PriceEntryTemplate" TargetType="usp:PriceEntryControl">
<c:TickUpDown
x:Name="PART_TickUpDown"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
@@ -1,10 +1,13 @@
using bts.api;
using bts.utils;
using mbts.spark;
using mbts.ui.controls;
using mbts.ui.extensions;
using mbts.ui.utilities;
using mbts.ui.utility;
using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
@@ -19,12 +22,23 @@ namespace mbts.ui.spark {
TextBox _text;
IDisposable _text_hooks;
double? _raw_value;
// TickUpDown
LazyPropagator<TickUpDown, PriceEntryControl> _explicit_props = new LazyPropagator<TickUpDown, PriceEntryControl>((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<bool, TickUpDown> _enable_arrow_key_tick = new LazyPropagator<bool, TickUpDown>((t, v) => t.EnableArrowKeyTick = v, true);
LazyPropagator<ModifierKeys, TickUpDown>
_arrow_key_tick_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None),
_arrow_key_tick_alt_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((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<TickUpDown>("PART_TickUpDown");
_text = this.assert_template_child<TextBox>("_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<KeyBinding>());
Disposable.replace(ref _text_hooks, _text.SubscribeTextChanged((s, e) => _update_value()));
_update_text();
}
@@ -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
@@ -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<TickUpDown, TickUpDownDouble> _explicit_props = new LazyPropagator<TickUpDown, TickUpDownDouble>((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<bool, TickUpDown> _enable_arrow_key_tick = new LazyPropagator<bool, TickUpDown>((t, v) => t.EnableArrowKeyTick = v, true);
LazyPropagator<ModifierKeys, TickUpDown>
_arrow_key_tick_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None),
_arrow_key_tick_alt_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((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<bool, TickUpDownDouble>(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<TickUpDown>("PART_tick_up_down");
// TickUpDown
_tick_up_down = this.assert_template_child<TickUpDown>("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<TextBox>("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<KeyBinding>());
}
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);
}
}
}
@@ -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<TickUpDown, TickUpDownInt> _explicit_props = new LazyPropagator<TickUpDown, TickUpDownInt>((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<bool, TickUpDown> _enable_arrow_key_tick = new LazyPropagator<bool, TickUpDown>((t, v) => t.EnableArrowKeyTick = v, true);
LazyPropagator<ModifierKeys, TickUpDown>
_arrow_key_tick_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None),
_arrow_key_tick_alt_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((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<bool, TickUpDownInt>(
@@ -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<TickUpDown>("PART_tick_up_down");
// TickUpDown
_tick_up_down = this.assert_template_child<TickUpDown>("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<TextBox>("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() =>
@@ -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<TickUpDown, TickUpDownNullableInt> _explicit_props = new LazyPropagator<TickUpDown, TickUpDownNullableInt>((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<bool, TickUpDown> _enable_arrow_key_tick = new LazyPropagator<bool, TickUpDown>((t, v) => t.EnableArrowKeyTick = v, true);
LazyPropagator<ModifierKeys, TickUpDown>
_arrow_key_tick_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None),
_arrow_key_tick_alt_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((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<int?, TickUpDownNullableInt>(
@@ -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<TickUpDown>("PART_tick_up_down");
// TickUpDown
_tick_up_down = this.assert_template_child<TickUpDown>("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<TextBox>("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<KeyBinding>());
}
push_text_to_value();
_text_hooks = _text_box.SubscribeTextChanged((s, e) => push_text_to_value());
void push_text_to_value() {
@@ -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<TickUpDown, TickUpDownNullableUInt> _explicit_props = new LazyPropagator<TickUpDown, TickUpDownNullableUInt>((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<bool, TickUpDown> _enable_arrow_key_tick = new LazyPropagator<bool, TickUpDown>((t, v) => t.EnableArrowKeyTick = v, true);
LazyPropagator<ModifierKeys, TickUpDown>
_arrow_key_tick_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None),
_arrow_key_tick_alt_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((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<uint?, TickUpDownNullableUInt>(
@@ -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<TickUpDown>("PART_tick_up_down");
// TickUpDown
_tick_up_down = this.assert_template_child<TickUpDown>("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<TextBox>("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<KeyBinding>());
}
push_text_to_value();
_text_hooks = _text_box.SubscribeTextChanged((s, e) => push_text_to_value());
void push_text_to_value() {
@@ -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<TickUpDown, TickUpDownUInt> _explicit_props = new LazyPropagator<TickUpDown, TickUpDownUInt>((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<bool, TickUpDown> _enable_arrow_key_tick = new LazyPropagator<bool, TickUpDown>((t, v) => t.EnableArrowKeyTick = v, true);
LazyPropagator<ModifierKeys, TickUpDown>
_arrow_key_tick_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None),
_arrow_key_tick_alt_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((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<bool, TickUpDownUInt>(
@@ -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<TickUpDown>("PART_tick_up_down");
// TickUpDown
_tick_up_down = this.assert_template_child<TickUpDown>("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<TextBox>("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<KeyBinding>());
}
push_text_to_value();
_text_hooks = _text_box.SubscribeTextChanged((s, e) => push_text_to_value());
void push_text_to_value() =>
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
namespace mbts.ui.utilities {
class LazyPropagator<Value, Target> {
readonly Action<Target, Value> _assign;
readonly IEqualityComparer<Value> _comparer;
readonly IEqualityComparer<Target> _target_comparer;
Value _value;
Target _target;
public LazyPropagator(Action<Target, Value> assign)
: this(assign, default) { }
public LazyPropagator(Action<Target, Value> assign, Value init)
: this(assign, init, EqualityComparer<Value>.Default, EqualityComparer<Target>.Default) { }
public LazyPropagator(Action<Target, Value> assign, Value init, IEqualityComparer<Value> comparer, IEqualityComparer<Target> 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);
}
}
}
}
}
@@ -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);
@@ -47,6 +47,13 @@ namespace System.Collections.Generic {
return rv;
}
public static IEnumerable<T> ExceptNull<T>(this IEnumerable<T> source) where T : class {
foreach (var v in source) {
if (v != null)
yield return v;
}
}
/// <summary>
/// Checks if <paramref name="source"/> implements <see cref="ICollection{T}"/> or <see cref="IReadOnlyCollection{T}"/>. If so, the resultant Count is returned, otherwise null (will not enumerate through any items).
/// </summary>