refactor: Consolidate TickUpDownNullable classes into generic impl

This commit is contained in:
John Colvin committed 2022-04-19 11:42:43 -04:00
1 parent 2fa9b2e0e5
commit fb3447a6b4
22 files changed
+534 -598

No files matched your search

@@ -385,15 +385,15 @@
and only necessary for the size param as of 6/8/2020 (slightly more uniform code doesn't warrant rocking the boat).-->
<u:TemplateSelectorKeyValue Key="{x:Static usp:AlgoParamTemplateKeys.Size}">
<DataTemplate>
<c:TickUpDownNullableInt Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.display_value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
<c:TickUpDownNullableInt.CommandBindings>
<c:TickUpDownNullableInt32 Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.display_value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
<c:TickUpDownNullableInt32.CommandBindings>
<CommandBinding Command="{x:Static u:NavigationHelper.MoveFocusCommand}" Executed="{x:Static u:NavigationHelper.KeyboardMoveFocusExecuted}"/>
</c:TickUpDownNullableInt.CommandBindings>
<c:TickUpDownNullableInt.InputBindings>
</c:TickUpDownNullableInt32.CommandBindings>
<c:TickUpDownNullableInt32.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>
</c:TickUpDownNullableInt32.InputBindings>
</c:TickUpDownNullableInt32>
</DataTemplate>
</u:TemplateSelectorKeyValue>
</usp:AlgoParamTemplateSelector.KeyedTemplates>
@@ -462,29 +462,29 @@
<!--Nullable Int-->
<usp:AlgoParamTemplateSelector.NullableIntTemplate>
<DataTemplate>
<c:TickUpDownNullableInt Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
<c:TickUpDownNullableInt.CommandBindings>
<c:TickUpDownNullableInt32 Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
<c:TickUpDownNullableInt32.CommandBindings>
<CommandBinding Command="{x:Static u:NavigationHelper.MoveFocusCommand}" Executed="{x:Static u:NavigationHelper.KeyboardMoveFocusExecuted}"/>
</c:TickUpDownNullableInt.CommandBindings>
<c:TickUpDownNullableInt.InputBindings>
</c:TickUpDownNullableInt32.CommandBindings>
<c:TickUpDownNullableInt32.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>
</c:TickUpDownNullableInt32.InputBindings>
</c:TickUpDownNullableInt32>
</DataTemplate>
</usp:AlgoParamTemplateSelector.NullableIntTemplate>
<!--Nullable UInt-->
<usp:AlgoParamTemplateSelector.NullableUIntTemplate>
<DataTemplate>
<c:TickUpDownNullableUInt Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
<c:TickUpDownNullableUInt.CommandBindings>
<c:TickUpDownNullableUInt32 Focusable="False" HorizontalAlignment="Stretch" Value="{Binding param.value, UpdateSourceTrigger=PropertyChanged}" ArrowKeyTickModifiers="Ctrl">
<c:TickUpDownNullableUInt32.CommandBindings>
<CommandBinding Command="{x:Static u:NavigationHelper.MoveFocusCommand}" Executed="{x:Static u:NavigationHelper.KeyboardMoveFocusExecuted}"/>
</c:TickUpDownNullableUInt.CommandBindings>
<c:TickUpDownNullableUInt.InputBindings>
</c:TickUpDownNullableUInt32.CommandBindings>
<c:TickUpDownNullableUInt32.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>
</c:TickUpDownNullableUInt32.InputBindings>
</c:TickUpDownNullableUInt32>
</DataTemplate>
</usp:AlgoParamTemplateSelector.NullableUIntTemplate>
<!--Order Def-->
@@ -178,7 +178,7 @@
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="size_label" Margin="8,0,0,0" Text="Size" Visibility="Collapsed"/>
<c:TickUpDownNullableUInt x:Name="size_tick_up_down" Value="{Binding Size, RelativeSource={RelativeSource TemplatedParent}}" ToolTip="Size"/>
<c:TickUpDownNullableUInt32 x:Name="size_tick_up_down" Value="{Binding Size, RelativeSource={RelativeSource TemplatedParent}}" ToolTip="Size"/>
<Button
x:Name="best_bid_size_button"
Grid.Column="2"
@@ -37,7 +37,7 @@
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="c:TickUpDownNullableUInt" BasedOn="{StaticResource {x:Type c:TickUpDownNullableUInt}}">
<Style TargetType="c:TickUpDownNullableUInt32" BasedOn="{StaticResource {x:Type c:TickUpDownNullableUInt32}}">
<Style.Triggers>
<Trigger Property="usp:OrderModificationControl.IsDirty" Value="True">
<Setter Property="BorderBrush" Value="{u:BTSColor UI.input_dirty_border}"/>
@@ -35,13 +35,13 @@
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center">Minimum</TextBlock>
<TextBlock Grid.Row="2" VerticalAlignment="Center">Maximum</TextBlock>
<c:TickUpDownNullableInt
<c:TickUpDownNullableInt32
x:Name="PART_Minimum_Tick"
Grid.Column="2"
Focusable="False"
Value="{Binding Minimum, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
/>
<c:TickUpDownNullableInt
<c:TickUpDownNullableInt32
x:Name="PART_Maximum_Tick"
Grid.Column="2"
Grid.Row="2"
@@ -42,8 +42,8 @@ namespace mbts.ui.spark {
public override void OnApplyTemplate() {
Disposable.dispose(ref _hooks);
var min_tick = this.assert_template_child<TickUpDownNullableInt>("PART_Minimum_Tick");
var max_tick = this.assert_template_child<TickUpDownNullableInt>("PART_Maximum_Tick");
var min_tick = this.assert_template_child<TickUpDownNullableInt32>("PART_Minimum_Tick");
var max_tick = this.assert_template_child<TickUpDownNullableInt32>("PART_Maximum_Tick");
set_max_target();
set_max_target();
_hooks = Disposable.Create(
@@ -35,13 +35,13 @@
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center">Minimum</TextBlock>
<TextBlock Grid.Row="2" VerticalAlignment="Center">Maximum</TextBlock>
<c:TickUpDownNullableUInt
<c:TickUpDownNullableUInt32
x:Name="PART_Minimum_Tick"
Grid.Column="2"
Focusable="False"
Value="{Binding Minimum, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
/>
<c:TickUpDownNullableUInt
<c:TickUpDownNullableUInt32
x:Name="PART_Maximum_Tick"
Grid.Column="2"
Grid.Row="2"
@@ -41,8 +41,8 @@ namespace mbts.ui.spark {
public override void OnApplyTemplate() {
Disposable.dispose(ref _hooks);
var min_tick = this.assert_template_child<TickUpDownNullableUInt>("PART_Minimum_Tick");
var max_tick = this.assert_template_child<TickUpDownNullableUInt>("PART_Maximum_Tick");
var min_tick = this.assert_template_child<TickUpDownNullableUInt32>("PART_Minimum_Tick");
var max_tick = this.assert_template_child<TickUpDownNullableUInt32>("PART_Maximum_Tick");
set_max_target();
set_max_target();
_hooks = Disposable.Create(
@@ -35,13 +35,13 @@
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center">Minimum</TextBlock>
<TextBlock Grid.Row="2" VerticalAlignment="Center">Maximum</TextBlock>
<c:TickUpDownNullableULong
<c:TickUpDownNullableUInt64
x:Name="PART_Minimum_Tick"
Grid.Column="2"
Focusable="False"
Value="{Binding Minimum, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
/>
<c:TickUpDownNullableULong
<c:TickUpDownNullableUInt64
x:Name="PART_Maximum_Tick"
Grid.Column="2"
Grid.Row="2"
@@ -42,8 +42,8 @@ namespace mbts.ui.spark {
public override void OnApplyTemplate() {
Disposable.dispose(ref _hooks);
var min_tick = this.assert_template_child<TickUpDownNullableULong>("PART_Minimum_Tick");
var max_tick = this.assert_template_child<TickUpDownNullableULong>("PART_Maximum_Tick");
var min_tick = this.assert_template_child<TickUpDownNullableUInt64>("PART_Minimum_Tick");
var max_tick = this.assert_template_child<TickUpDownNullableUInt64>("PART_Maximum_Tick");
set_max_target();
set_max_target();
_hooks = Disposable.Create(
@@ -35,13 +35,13 @@
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center">Minimum</TextBlock>
<TextBlock Grid.Row="2" VerticalAlignment="Center">Maximum</TextBlock>
<c:TickUpDownNullableUShort
<c:TickUpDownNullableUInt16
x:Name="PART_Minimum_Tick"
Grid.Column="2"
Focusable="False"
Value="{Binding Minimum, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
/>
<c:TickUpDownNullableUShort
<c:TickUpDownNullableUInt16
x:Name="PART_Maximum_Tick"
Grid.Column="2"
Grid.Row="2"
@@ -41,8 +41,8 @@ namespace mbts.ui.spark {
public override void OnApplyTemplate() {
Disposable.dispose(ref _hooks);
var min_tick = this.assert_template_child<TickUpDownNullableUShort>("PART_Minimum_Tick");
var max_tick = this.assert_template_child<TickUpDownNullableUShort>("PART_Maximum_Tick");
var min_tick = this.assert_template_child<TickUpDownNullableUInt16>("PART_Minimum_Tick");
var max_tick = this.assert_template_child<TickUpDownNullableUInt16>("PART_Maximum_Tick");
set_max_target();
set_max_target();
_hooks = Disposable.Create(
@@ -527,7 +527,7 @@
<VisualState Name="{StaticResource CannotEditValuesState}">
<VisualState.Storyboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="PART_HedgeTicks_TickUpDownNullableUInt" Storyboard.TargetProperty="(UIElement.IsEnabled)">
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="PART_HedgeTicks_TickUpDownNullableUInt32" Storyboard.TargetProperty="(UIElement.IsEnabled)">
<DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
@@ -551,7 +551,7 @@
<TextBlock VerticalAlignment="Center">Hedge Ticks</TextBlock>
<c:HelpButton Margin="{StaticResource Margin.LP1}" ToolTip="This is the number of ticks to offset the hedge."/>
</StackPanel>
<c:TickUpDownNullableUInt x:Name="PART_HedgeTicks_TickUpDownNullableUInt" Grid.Column="2" HorizontalAlignment="Stretch" Value="{Binding hedge_ticks}"/>
<c:TickUpDownNullableUInt32 x:Name="PART_HedgeTicks_TickUpDownNullableUInt32" Grid.Column="2" HorizontalAlignment="Stretch" Value="{Binding hedge_ticks}"/>
<uag:AlgoPickerNodeView
x:Name="PART_Hedge_AlgoPickerNodeView"
Grid.Row="2"
@@ -883,7 +883,7 @@
<VisualState Name="{StaticResource CannotEditValuesState}">
<VisualState.Storyboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="PART_MaxShowQuantity_TickUpDownNullableUInt" Storyboard.TargetProperty="(UIElement.IsEnabled)">
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="PART_MaxShowQuantity_TickUpDownNullableUInt32" Storyboard.TargetProperty="(UIElement.IsEnabled)">
<DiscreteBooleanKeyFrame KeyTime="0" Value="False"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
@@ -905,7 +905,7 @@
<TextBlock VerticalAlignment="Center">Max Show Quantity</TextBlock>
<c:HelpButton Margin="{StaticResource Margin.LP1}" ToolTip="Maximum size to show on GLBX, this is the exchange provided iceberg"/>
</StackPanel>
<c:TickUpDownNullableUInt x:Name="PART_MaxShowQuantity_TickUpDownNullableUInt" Grid.Column="2" HorizontalAlignment="Stretch" Value="{Binding max_show_quantity}"/>
<c:TickUpDownNullableUInt32 x:Name="PART_MaxShowQuantity_TickUpDownNullableUInt32" Grid.Column="2" HorizontalAlignment="Stretch" Value="{Binding max_show_quantity}"/>
</Grid>
</DataTemplate>
</usp:AlgoContentTemplateSelector.GLBXIcebergOrderTemplate>
@@ -357,18 +357,25 @@
Width="{TemplateBinding Width}"
MinWidth="{TemplateBinding MinWidth}"
MaxWidth="{TemplateBinding MaxWidth}"
TickUpCommand="{TemplateBinding TickUpCommand}"
TickUpCommandParameter="{TemplateBinding TickUpCommandParameter}"
TickDownCommand="{TemplateBinding TickDownCommand}"
TickDownCommandParameter="{TemplateBinding TickDownCommandParameter}"
>
<TextBox
Name="PART_TextBox"
u:TextBoxBehavior.CommitOnEnter="{TemplateBinding CommitOnEnter}"
u:TextBoxBehavior.EnableArrowKeyTraverse="True"
u:TextBoxBehavior.InputFilter="{u:RegexInputFilter Regex={x:Static Decimal, MemberType={u:Nested txt:TextUtils.RegexFilters}}}"
HorizontalContentAlignment="{TemplateBinding TextHorizontalAlignment}"
BorderThickness="0"
Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Converter={StaticResource NullableDoubleTextConverter}}"
/>
</c:TickUpDown>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type c:TickUpDownNullableInt}">
<Style TargetType="{x:Type c:TickUpDownNullableInt32}">
<Setter Property="Background" Value="{StaticResource TextBox.Static.Background.Brush}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border.Brush}"/>
<Setter Property="BorderThickness" Value="1"/>
@@ -377,7 +384,7 @@
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="c:TickUpDownNullableInt">
<ControlTemplate TargetType="c:TickUpDownNullableInt32">
<c:TickUpDown
x:Name="PART_tick_up_down"
Background="{TemplateBinding Background}"
@@ -402,7 +409,7 @@
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type c:TickUpDownNullableUInt}">
<Style TargetType="{x:Type c:TickUpDownNullableUInt32}">
<Setter Property="Background" Value="{StaticResource TextBox.Static.Background.Brush}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border.Brush}"/>
<Setter Property="BorderThickness" Value="1"/>
@@ -411,7 +418,7 @@
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="c:TickUpDownNullableUInt">
<ControlTemplate TargetType="c:TickUpDownNullableUInt32">
<c:TickUpDown
x:Name="PART_tick_up_down"
Background="{TemplateBinding Background}"
@@ -435,10 +442,10 @@
</Setter.Value>
</Setter>
</Style>
<Style x:Key="TickUpDownNullableUIntWithoutButtons" TargetType="{x:Type c:TickUpDownNullableUInt}" BasedOn="{StaticResource {x:Type c:TickUpDownNullableUInt}}">
<Style x:Key="TickUpDownNullableUInt32WithoutButtons" TargetType="{x:Type c:TickUpDownNullableUInt32}" BasedOn="{StaticResource {x:Type c:TickUpDownNullableUInt32}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="c:TickUpDownNullableUInt">
<ControlTemplate TargetType="c:TickUpDownNullableUInt32">
<c:TickUpDown
x:Name="PART_tick_up_down"
Background="{TemplateBinding Background}"
@@ -472,7 +479,7 @@
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type c:TickUpDownNullableUShort}">
<Style TargetType="{x:Type c:TickUpDownNullableUInt16}">
<Setter Property="Background" Value="{StaticResource TextBox.Static.Background.Brush}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border.Brush}"/>
<Setter Property="BorderThickness" Value="1"/>
@@ -481,7 +488,7 @@
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="c:TickUpDownNullableUShort">
<ControlTemplate TargetType="c:TickUpDownNullableUInt16">
<c:TickUpDown
x:Name="PART_tick_up_down"
Background="{TemplateBinding Background}"
@@ -506,7 +513,7 @@
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type c:TickUpDownNullableULong}">
<Style TargetType="{x:Type c:TickUpDownNullableUInt64}">
<Setter Property="Background" Value="{StaticResource TextBox.Static.Background.Brush}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border.Brush}"/>
<Setter Property="BorderThickness" Value="1"/>
@@ -515,7 +522,7 @@
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="c:TickUpDownNullableULong">
<ControlTemplate TargetType="c:TickUpDownNullableUInt64">
<c:TickUpDown
x:Name="PART_tick_up_down"
Background="{TemplateBinding Background}"
@@ -1,88 +1,216 @@
using System;
using System.Windows.Controls;
using System.Windows.Data;
#nullable enable
namespace mbts.ui.controls {
using bts.api.domain;
using bts.utils;
using System;
using System.Diagnostics.CodeAnalysis;
static class TickUpDownHelper {
public const string
target_prop_name = "Value",
text_box_name = "PART_TextBox";
public static void init_text_binding(Control owner, IValueConverter converter, bool flush_text_immediately, string target_prop_name = target_prop_name, string text_box_name = text_box_name) =>
init_text_binding(owner, converter, flush_text_immediately, target_prop_name, owner.get_template_child<TextBox>(text_box_name));
public static void init_text_binding(Control owner, IValueConverter converter, bool flush_text_immediately, string target_prop_name, TextBox text_box) =>
init_text_binding(owner, converter, flush_text_immediately ? UpdateSourceTrigger.PropertyChanged : UpdateSourceTrigger.Default, target_prop_name, text_box);
public static void init_text_binding(Control owner, IValueConverter converter, UpdateSourceTrigger update_source_trigger, string target_prop_name, TextBox text_box) {
if (text_box == null)
return;
BindingOperations.ClearBinding(text_box, TextBox.TextProperty);
text_box.SetBinding(TextBox.TextProperty, new Binding(target_prop_name) {
Converter = converter,
Mode = BindingMode.TwoWay,
Source = owner,
UpdateSourceTrigger = update_source_trigger
});
public static string? format<T>(
T? new_value, string curr_text, TickDisplaySettingsImmutable? settings = null)
where T : struct, IFloatingPoint<T> {
if (T.TryParse(curr_text, null, out var from_text)) {
if (!from_text.Equals(new_value)) {
return format(new_value, settings);
}
} else if (new_value is not null) {
return format(new_value, settings);
}
return null;
}
// int
public static bool tick_up(int value, int increment, out int result) => _tick(value, value + increment, 1, out result);
public static bool tick_down(int value, int increment, out int result) => _tick(value, value - increment, -1, out result);
// int?
public static bool tick_up(int? value, int increment, out int result) => value.HasValue ? _tick(value.Value, value.Value + increment, 1, out result) : _no_op(out result);
public static bool tick_down(int? value, int increment, out int result) => value.HasValue ? _tick(value.Value, value.Value - increment, -1, out result) : _no_op(out result);
public static string format<T>(T? value, TickDisplaySettingsImmutable? settings = null)
where T : struct, IFloatingPoint<T> {
if (!value.HasValue) {
return string.Empty;
}
if (settings.HasValue) {
return settings.Value.to_tick_display_settings().format(value.Value);
}
return value.Value.ToString() ?? string.Empty;
}
// uint
public static bool tick_up(uint value, uint increment, out uint result) => _tick(value, value + increment, 1, out result);
public static bool tick_down(uint value, uint increment, out uint result) => _tick(value, value - increment, -1, out result);
// uint?
public static bool tick_up(uint? value, uint increment, out uint result) => value.HasValue ? _tick(value.Value, value.Value + increment, 1, out result) : _no_op(out result);
public static bool tick_down(uint? value, uint increment, out uint result) => value.HasValue ? _tick(value.Value, value.Value - increment, -1, out result) : _no_op(out result);
public static bool try_parse<T>(
string? text,
[NotNullWhen(true)] out T? result,
TickDisplaySettingsImmutable? settings = null
) where T : IFloatingPoint<T> {
if (text is null) {
result = default;
return false;
}
var tds = settings?.to_tick_display_settings();
if (tds is not null) {
result = tds.parse<T>(text);
return true;
}
return T.TryParse(text, null, out result);
}
// short
public static bool tick_up(short value, short incremented, out short result) => _tick(value, (short)(value + incremented), 1, out result);
public static bool tick_down(short value, short incremented, out short result) => _tick(value, (short)(value - incremented), -1, out result);
// short?
public static bool tick_up(short? value, short increment, out short result) => value.HasValue ? _tick(value.Value, (short)(value.Value + increment), 1, out result) : _no_op(out result);
public static bool tick_down(short? value, short increment, out short result) => value.HasValue ? _tick(value.Value, (short)(value.Value - increment), -1, out result) : _no_op(out result);
public static bool can_nullable_tick_up<T>(
T? value, T increment, bool allow_nan_tick = true) where T : struct, INumber<T>, IMinMaxValue<T> =>
can_nullable_tick_up(value, increment, T.MaxValue, allow_nan_tick);
// ushort
public static bool tick_up(ushort value, ushort incremented, out ushort result) => _tick(value, (ushort)(value + incremented), 1, out result);
public static bool tick_down(ushort value, ushort incremented, out ushort result) => _tick(value, (ushort)(value - incremented), -1, out result);
// ushort?
public static bool tick_up(ushort? value, ushort increment, out ushort result) => value.HasValue ? _tick(value.Value, (ushort)(value.Value + increment), 1, out result) : _no_op(out result);
public static bool tick_down(ushort? value, ushort increment, out ushort result) => value.HasValue ? _tick(value.Value, (ushort)(value.Value - increment), -1, out result) : _no_op(out result);
public static bool can_nullable_tick_up<T>(
T? value, T increment, T maximum, bool allow_nan_tick = true) where T : struct, INumber<T> =>
value.HasValue && can_tick_up(value.Value, increment, maximum, allow_nan_tick);
// long
public static bool tick_up(long value, long incremented, out long result) => _tick(value, (long)(value + incremented), 1, out result);
public static bool tick_down(long value, long incremented, out long result) => _tick(value, (long)(value - incremented), -1, out result);
// long?
public static bool tick_up(long? value, long increment, out long result) => value.HasValue ? _tick(value.Value, (long)(value.Value + increment), 1, out result) : _no_op(out result);
public static bool tick_down(long? value, long increment, out long result) => value.HasValue ? _tick(value.Value, (long)(value.Value - increment), -1, out result) : _no_op(out result);
public static bool can_tick_up<T>(
T value, T increment, bool allow_nan_tick = true) where T : INumber<T>, IMinMaxValue<T> =>
can_tick_up(value, increment, T.MaxValue, allow_nan_tick);
// ulong
public static bool tick_up(ulong value, ulong incremented, out ulong result) => _tick(value, (ulong)(value + incremented), 1, out result);
public static bool tick_down(ulong value, ulong incremented, out ulong result) => _tick(value, (ulong)(value - incremented), -1, out result);
// ulong?
public static bool tick_up(ulong? value, ulong increment, out ulong result) => value.HasValue ? _tick(value.Value, (ulong)(value.Value + increment), 1, out result) : _no_op(out result);
public static bool tick_down(ulong? value, ulong increment, out ulong result) => value.HasValue ? _tick(value.Value, (ulong)(value.Value - increment), -1, out result) : _no_op(out result);
public static bool can_tick_up<T>(
T value, T increment, T maximum, bool allow_nan_tick = true) where T : INumber<T> {
if (increment < T.Zero) {
throw new ArgumentException("Must be greater or equal to zero", nameof(increment));
}
return !MathUtils.is_nan(increment) && (
(allow_nan_tick && MathUtils.is_nan(value)) ||
(maximum > value && (maximum - value) >= increment)
);
}
public static bool can_nullable_tick_down<T>(
T? value, T increment, bool allow_nan_tick = true) where T : struct, INumber<T>, IMinMaxValue<T> =>
can_nullable_tick_down(value, increment, T.MaxValue, allow_nan_tick);
public static bool can_nullable_tick_down<T>(
T? value, T increment, T maximum, bool allow_nan_tick = true) where T : struct, INumber<T> =>
value.HasValue && can_tick_down(value.Value, increment, maximum, allow_nan_tick);
public static bool can_tick_down<T>(
T value, T increment, bool allow_nan_tick = true) where T : INumber<T>, IMinMaxValue<T> =>
can_tick_down(value, increment, T.MaxValue, allow_nan_tick);
public static bool can_tick_down<T>(
T value, T increment, T minimum, bool allow_nan_tick = true) where T : INumber<T> {
if (increment < T.Zero) {
throw new ArgumentException("Must be greater or equal to zero", nameof(increment));
}
return !MathUtils.is_nan(increment) && (
(allow_nan_tick && MathUtils.is_nan(value)) ||
(minimum < value && (value - minimum) >= increment)
);
}
public static bool nullable_tick_up<T>(
T? value, T increment, out T? result, bool allow_nan_tick = true)
where T : struct, INumber<T>, IMinMaxValue<T> =>
nullable_tick_up(value, increment, out result, T.MaxValue, allow_nan_tick);
public static bool nullable_tick_up<T>(
T? value, T increment, out T? result, T maximum, bool allow_nan_tick = true)
where T : struct, INumber<T>, IMinMaxValue<T> {
if (!value.HasValue) {
result = null;
return false;
}
if (tick_up(value.Value, increment, out var res_val, maximum, allow_nan_tick)) {
result = res_val;
return true;
}
result = value;
return false;
}
public static bool tick_up<T>(
T value, T increment, out T result, bool allow_nan_tick = true)
where T : INumber<T>, IMinMaxValue<T> =>
tick_up(value, increment, out result, T.MaxValue, allow_nan_tick);
public static bool tick_up<T>(
T value, T increment, out T result, T maximum, bool allow_nan_tick = true)
where T : INumber<T> {
// if (!can_tick_up(value, increment, maximum, allow_nan_tick)) {
// result = value;
// return false;
// }
if (MathUtils.is_nan(value)) {
if (!allow_nan_tick) {
/// this is a redundant check, as <see cref="can_tick_up"/> should have returned
/// false if this was the case
throw new InvalidOperationException(
$"{nameof(can_tick_up)} returned true, but {nameof(value)} is NaN and " +
$"{nameof(allow_nan_tick)} is false");
}
result = T.Zero;
return true;
} else if (!MathUtils.is_nan(increment)) {
return _tick(value, value + increment, 1, out result);
}
result = value;
return false;
}
public static bool nullable_tick_down<T>(
T? value, T increment, out T? result, bool allow_nan_tick = true)
where T : struct, INumber<T>, IMinMaxValue<T> =>
nullable_tick_down(value, increment, out result, T.MinValue, allow_nan_tick);
public static bool nullable_tick_down<T>(
T? value, T increment, out T? result, T minimum, bool allow_nan_tick = true)
where T : struct, INumber<T>, IMinMaxValue<T> {
if (!value.HasValue) {
result = null;
return false;
}
if (tick_down(value.Value, increment, out var res_val, minimum, allow_nan_tick)) {
result = res_val;
return true;
}
result = value;
return false;
}
public static bool tick_down<T>(
T value, T increment, out T result, bool allow_nan_tick = true)
where T : INumber<T>, IMinMaxValue<T> =>
tick_down(value, increment, out result, T.MaxValue, allow_nan_tick);
public static bool tick_down<T>(
T value, T increment, out T result, T minimum, bool allow_nan_tick = true)
where T : INumber<T> {
// if (!can_tick_down(value, increment, minimum, allow_nan_tick)) {
// result = value;
// return false;
// }
if (MathUtils.is_nan(value)) {
if (!allow_nan_tick) {
/// this is a redundant check, as <see cref="can_tick_down"/> should have returned
/// false if this was the case
throw new InvalidOperationException(
$"{nameof(can_tick_down)} returned true, but {nameof(value)} is NaN and " +
$"{nameof(allow_nan_tick)} is false");
}
result = T.Zero;
return true;
} else if (!MathUtils.is_nan(increment)) {
return _tick(value, value - increment, -1, out result);
}
result = value;
return false;
}
// generic logic to avoid ticking across zero, in either direction.
static bool _tick<T>(T value, T incremented, int expected_comparison, out T result) where T : struct, IComparable<T> {
static bool _tick<T>(T value, T incremented, int expected_comparison, out T result)
where T : IComparable<T> {
if (expected_comparison == 0) {
result = value;
return incremented.CompareTo(value) == 0;
} else if ((incremented.CompareTo(value) > 0) == (expected_comparison > 0)) {
result = incremented;
return true;
} else {
result = value;
return false;
}
result = value;
return false;
}
static bool _no_op<T>(out T result) {
static bool _no_op<T>(out T? result) {
result = default;
return false;
}
@@ -1,39 +0,0 @@
using System.Windows;
using System.Windows.Controls;
namespace mbts.ui.controls {
public class TickUpDownNullableDouble : Control {
static TickUpDownNullableDouble() =>
DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownNullableDouble), new FrameworkPropertyMetadata(typeof(TickUpDownNullableDouble)));
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double?), typeof(TickUpDownNullableDouble),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public double? Value { get => GetValue(ValueProperty) as double?; set => SetValue(ValueProperty, value); }
public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(double), typeof(TickUpDownNullableDouble),
new PropertyMetadata(1d));
public double Increment { get { return (double)GetValue(IncrementProperty); } set { SetValue(IncrementProperty, value); } }
public static readonly DependencyProperty TickDefaultValueProperty = DependencyProperty.Register(nameof(TickDefaultValue), typeof(double), typeof(TickUpDownNullableDouble),
new PropertyMetadata(0d));
public double TickDefaultValue { get => (double)GetValue(TickDefaultValueProperty); set => SetValue(TickDefaultValueProperty, value); }
public override void OnApplyTemplate() {
var 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);
}
void _tick_up() {
double? v = Value;
Value = v.HasValue && !DoubleHelper.IsNaNOrInfinity(v.Value) ? v.Value + Increment : TickDefaultValue;
}
void _tick_down() {
double? v = Value;
if (v.HasValue && !DoubleHelper.IsNaNOrInfinity(v.Value)) {
Value = v.Value - Increment;
} else Value = TickDefaultValue;
}
}
}
@@ -1,101 +0,0 @@
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>(
nameof(Value),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, v) => {
if (o._text_box != null) {
if (int.TryParse(o._text_box.Text, out int from_text)) {
if (from_text != v)
o._text_box.Text = v.ToString();
} else if (v.HasValue) {
o._text_box.Text = v.ToString();
}
}
});
public int? Value {
get => GetValue(ValueProperty) as int?;
set => SetValue(ValueProperty, value);
}
#endregion
public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(int), typeof(TickUpDownNullableInt),
new PropertyMetadata(1));
public int Increment { get { return (int)GetValue(IncrementProperty); } set { SetValue(IncrementProperty, value); } }
public static readonly DependencyProperty TickDefaultValueProperty = DependencyProperty.Register(nameof(TickDefaultValue), typeof(int), typeof(TickUpDownNullableInt),
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);
// 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();
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() {
if (int.TryParse(_text_box.Text, out int i)) {
SetCurrentValue(ValueProperty, i);
} else {
SetCurrentValue(ValueProperty, null);
}
}
}
void _tick_up() {
if (TickUpDownHelper.tick_up(Value, Increment, out int result))
SetCurrentValue(ValueProperty, result);
}
void _tick_down() {
if (TickUpDownHelper.tick_down(Value, Increment, out int result))
SetCurrentValue(ValueProperty, result);
}
}
}
@@ -0,0 +1,263 @@
#nullable enable
namespace mbts.ui.controls {
using bts.api.domain;
using mbts.ui.extensions;
using mbts.ui.utilities;
using Spark.Core;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
class TickUpDownNullableNumeric<T> : Control where T : struct, INumber<T> {
readonly LazyPropagator<TickUpDown, TickUpDownNullableNumeric<T>> _explicit_props = new(
(t, v) => {
t._enable_arrow_key_tick.target = v;
t._arrow_key_tick_modifiers.target = v;
t._arrow_key_tick_alt_modifiers.target = v;
});
readonly LazyPropagator<bool, TickUpDown> _enable_arrow_key_tick = new(
(t, v) => t.EnableArrowKeyTick = v, true);
readonly LazyPropagator<ModifierKeys, TickUpDown> _arrow_key_tick_modifiers = new(
(t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None);
readonly LazyPropagator<ModifierKeys, TickUpDown> _arrow_key_tick_alt_modifiers = new(
(t, v) => t.ArrowKeyTickAltModifiers = v, ModifierKeys.Alt);
IDisposable? _text_hooks = null;
TextBox? _text_box;
static TickUpDownNullableNumeric() =>
DefaultStyleKeyProperty.OverrideMetadata(
typeof(TickUpDownNullableNumeric<T>),
new FrameworkPropertyMetadata(typeof(TickUpDownNullableNumeric<T>)));
public TickUpDownNullableNumeric() {
_explicit_props.target = this;
SetCurrentValue(TickUpCommandProperty, new Command(_tick_up, _can_tick_up));
SetCurrentValue(TickDownCommandProperty, new Command(_tick_down, _can_tick_down));
}
#region Value
public static readonly DependencyProperty ValueProperty =
WPFHelper.create_dp<double?, TickUpDownNullableNumeric<T>>(
nameof(Value),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, v) => {
using var _ = DeferredAction.create(o._refresh_can_tick);
if (o._text_box is null) {
return;
}
var text = TickUpDownHelper.format(v, o._text_box.Text, o.TickDisplaySettings);
if (text is not null) {
o._text_box.Text = text;
}
});
public double? Value {
get => GetValue(ValueProperty) as double?;
set => SetValue(ValueProperty, value);
}
#endregion
#region Minimum
public static readonly DependencyProperty MinimumProperty =
WPFHelper.create_dp<double, TickUpDownNullableNumeric<T>>(
nameof(Minimum), (o, v) => o._refresh_can_tick(), double.MinValue);
public double Minimum {
get => (double)GetValue(MinimumProperty);
set => SetValue(MinimumProperty, value);
}
#endregion
#region Maximum
public static readonly DependencyProperty MaximumProperty =
WPFHelper.create_dp<double, TickUpDownNullableNumeric<T>>(
nameof(Maximum), (o, v) => o._refresh_can_tick(), double.MaxValue);
public double Maximum {
get => (double)GetValue(MaximumProperty);
set => SetValue(MaximumProperty, value);
}
#endregion
#region Increment
public static readonly DependencyProperty IncrementProperty =
WPFHelper.create_dp<double, TickUpDownNullableNumeric<T>>(nameof(Increment), 1);
public double Increment {
get => (double)GetValue(IncrementProperty);
set => SetValue(IncrementProperty, value);
}
#endregion
#region TickDisplaySettings
public static readonly DependencyProperty TickDisplaySettingsProperty =
WPFHelper.create_dp<TickDisplaySettingsImmutable?, TickUpDownNullableNumeric<T>>(
nameof(TickDisplaySettings),
(o, v) => {
if (o._text_box is not null) {
o._text_box.Text = TickUpDownHelper.format(o.Value, o.TickDisplaySettings);
}
});
public TickDisplaySettingsImmutable? TickDisplaySettings {
get => GetValue(TickDisplaySettingsProperty) as TickDisplaySettingsImmutable?;
set => SetValue(TickDisplaySettingsProperty, value);
}
#endregion
#region TickDefaultValue
public static readonly DependencyProperty TickDefaultValueProperty =
WPFHelper.create_dp<double, TickUpDownNullableNumeric<T>>(nameof(TickDefaultValue));
public double TickDefaultValue {
get => (double)GetValue(TickDefaultValueProperty);
set => SetValue(TickDefaultValueProperty, value);
}
#endregion
#region TextHorizontalAlignment
public static readonly DependencyProperty TextHorizontalAlignmentProperty =
WPFHelper.create_dp<HorizontalAlignment, TickUpDownNullableNumeric<T>>(
nameof(TextHorizontalAlignment), HorizontalAlignment.Left);
public HorizontalAlignment TextHorizontalAlignment {
get => (HorizontalAlignment)GetValue(TextHorizontalAlignmentProperty);
set => SetValue(TextHorizontalAlignmentProperty, value);
}
#endregion
#region TickUpCommand
public static readonly DependencyProperty TickUpCommandProperty =
TickUpDown.TickUpCommandProperty.AddOwner(typeof(TickUpDownNullableNumeric<T>));
public ICommand? TickUpCommand {
get => GetValue(TickUpCommandProperty) as ICommand;
set => SetValue(TickUpCommandProperty, value);
}
#endregion
#region TickUpCommandParameter
public static readonly DependencyProperty TickUpCommandParameterProperty =
TickUpDown.TickUpCommandParameterProperty.AddOwner(typeof(TickUpDownNullableNumeric<T>));
public object? TickUpCommandParameter {
get => GetValue(TickUpCommandParameterProperty);
set => SetValue(TickUpCommandParameterProperty, value);
}
#endregion
#region TickDownCommand
public static readonly DependencyProperty TickDownCommandProperty =
TickUpDown.TickDownCommandProperty.AddOwner(typeof(TickUpDownNullableNumeric<T>));
public ICommand? TickDownCommand {
get => GetValue(TickDownCommandProperty) as ICommand;
set => SetValue(TickDownCommandProperty, value);
}
#endregion
#region TickDownCommandParameter
public static readonly DependencyProperty TickDownCommandParameterProperty =
TickUpDown.TickDownCommandParameterProperty.AddOwner(typeof(TickUpDownNullableNumeric<T>));
public object? TickDownCommandParameter {
get => GetValue(TickDownCommandParameterProperty);
set => SetValue(TickDownCommandParameterProperty, value);
}
#endregion
#region CommitOnEnter
public static readonly DependencyProperty CommitOnEnterProperty =
WPFHelper.create_dp<bool, TickUpDownNullableNumeric<T>>(nameof(CommitOnEnter));
public bool CommitOnEnter {
get => (bool)GetValue(CommitOnEnterProperty);
set => SetValue(CommitOnEnterProperty, value);
}
#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);
_tick_up_down = this.assert_template_child<TickUpDown>("PART_tick_up_down");
_text_box = this.assert_template_child<TextBox>("PART_TextBox");
_text_box.Text = Value.ToString();
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() {
double? parsed = TickUpDownHelper.try_parse(
_text_box.Text, out double res, TickDisplaySettings) ? res : null;
this.set_current_if_changed(ValueProperty, parsed);
}
}
bool _can_tick_up() =>
TickUpDownHelper.can_nullable_tick_up(Value, Increment, Maximum, true);
void _tick_up() {
if (TickUpDownHelper.nullable_tick_up(Value, Increment, out var result)) {
SetCurrentValue(ValueProperty, result);
CoerceValue(ValueProperty);
}
}
bool _can_tick_down() =>
TickUpDownHelper.can_nullable_tick_down(Value, Increment, Minimum);
void _tick_down() {
if (TickUpDownHelper.nullable_tick_down(Value, Increment, out var result, Minimum)) {
SetCurrentValue(ValueProperty, result);
CoerceValue(ValueProperty);
}
}
void _refresh_can_tick() {
TickUpCommand?.TryRaiseCanExecuteChanged();
TickDownCommand?.TryRaiseCanExecuteChanged();
}
}
// Non-generic derived classes are needed for wpf (generic types cannot be instantiated in xaml)
class TickUpDownNullableInt16 : TickUpDownNullableNumeric<short> { }
class TickUpDownNullableUInt16 : TickUpDownNullableNumeric<ushort> { }
class TickUpDownNullableInt32 : TickUpDownNullableNumeric<int> { }
class TickUpDownNullableUInt32 : TickUpDownNullableNumeric<uint> { }
class TickUpDownNullableInt64 : TickUpDownNullableNumeric<long> { }
class TickUpDownNullableUInt64 : TickUpDownNullableNumeric<ulong> { }
class TickUpDownNullableSingle : TickUpDownNullableNumeric<float> { }
class TickUpDownNullableDouble : TickUpDownNullableNumeric<double> { }
}
@@ -1,116 +0,0 @@
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 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>(
nameof(Value),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, v) => {
if (o._text_box != null) {
if (uint.TryParse(o._text_box.Text, out uint from_text)) {
if (from_text != v)
o._text_box.Text = v.ToString();
} else if (v.HasValue) {
o._text_box.Text = v.ToString();
}
}
});
public uint? Value {
get => GetValue(ValueProperty) as uint?;
set => SetValue(ValueProperty, value);
}
#endregion
public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(uint), typeof(TickUpDownNullableUInt),
new PropertyMetadata(1u));
public uint Increment { get { return (uint)GetValue(IncrementProperty); } set { SetValue(IncrementProperty, value); } }
public static readonly DependencyProperty TickDefaultValueProperty = DependencyProperty.Register(nameof(TickDefaultValue), typeof(uint), typeof(TickUpDownNullableUInt),
new PropertyMetadata(0u));
public uint TickDefaultValue { get => (uint)GetValue(TickDefaultValueProperty); set => SetValue(TickDefaultValueProperty, value); }
public static readonly DependencyProperty CommitOnEnterProperty =
WPFHelper.create_dp<bool, TickUpDownNullableUInt>(
nameof(CommitOnEnter),
(o, v) => {
Disposable.dispose(ref o._commit_on_enter_hooks);
if (v) {
o._commit_on_enter_hooks = o.SubscribePreviewKeyDown(Key.Enter, on_enter);
void on_enter() =>
o.GetBindingExpression(ValueProperty)?.UpdateSource();
}
});
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);
// 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();
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() {
if (uint.TryParse(_text_box.Text, out uint i)) {
SetCurrentValue(ValueProperty, i);
} else {
SetCurrentValue(ValueProperty, null);
}
}
}
void _tick_up() {
if (TickUpDownHelper.tick_up(Value, Increment, out uint result))
SetCurrentValue(ValueProperty, result);
}
void _tick_down() {
if (TickUpDownHelper.tick_down(Value, Increment, out uint result))
SetCurrentValue(ValueProperty, result);
}
}
}
@@ -20,8 +20,8 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<c:TickUpDownNullableUInt x:Name="qty_ticker" Value="{Binding SourceValue, ElementName=_me}"
Style="{StaticResource TickUpDownNullableUIntWithoutButtons}"
<c:TickUpDownNullableUInt32 x:Name="qty_ticker" Value="{Binding SourceValue, ElementName=_me}"
Style="{StaticResource TickUpDownNullableUInt32WithoutButtons}"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
/>
@@ -1,114 +0,0 @@
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 TickUpDownNullableULong : Control {
IDisposable _text_hooks = null;
TextBox _text_box;
// TickUpDown
LazyPropagator<TickUpDown, TickUpDownNullableULong> _explicit_props = new LazyPropagator<TickUpDown, TickUpDownNullableULong>((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 TickUpDownNullableULong() =>
DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownNullableULong), new FrameworkPropertyMetadata(typeof(TickUpDownNullableULong)));
public TickUpDownNullableULong() =>
_explicit_props.target = this;
#region Value
public static readonly DependencyProperty ValueProperty =
WPFHelper.create_dp<ulong?, TickUpDownNullableULong>(
nameof(Value),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, v) => {
if (o._text_box != null) {
if (ulong.TryParse(o._text_box.Text, out ulong from_text)) {
if (from_text != v)
o._text_box.Text = v.ToString();
} else if (v.HasValue) {
o._text_box.Text = v.ToString();
}
}
});
public ulong? Value {
get => GetValue(ValueProperty) as ulong?;
set => SetValue(ValueProperty, value);
}
#endregion
#region Increment
public static readonly DependencyProperty IncrementProperty =
WPFHelper.create_dp<ulong, TickUpDownNullableULong>(nameof(Increment), 1);
public ulong Increment {
get => (ulong)GetValue(IncrementProperty);
set => SetValue(IncrementProperty, value);
}
#endregion
#region TickDefaultValue
public static readonly DependencyProperty TickDefaultValueProperty =
WPFHelper.create_dp<ulong, TickUpDownNullableULong>(nameof(TickDefaultValue));
public ulong TickDefaultValue {
get => (ulong)GetValue(TickDefaultValueProperty);
set => SetValue(TickDefaultValueProperty, 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() {
Disposable.dispose(ref _text_hooks);
// 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();
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() {
if (ulong.TryParse(_text_box.Text, out ulong i)) {
SetCurrentValue(ValueProperty, i);
} else {
SetCurrentValue(ValueProperty, null);
}
}
}
void _tick_up() {
if (TickUpDownHelper.tick_up(Value, Increment, out ulong result))
SetCurrentValue(ValueProperty, result);
}
void _tick_down() {
if (TickUpDownHelper.tick_down(Value, Increment, out ulong result))
SetCurrentValue(ValueProperty, result);
}
}
}
@@ -1,114 +0,0 @@
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 TickUpDownNullableUShort : Control {
IDisposable _text_hooks = null;
TextBox _text_box;
// TickUpDown
LazyPropagator<TickUpDown, TickUpDownNullableUShort> _explicit_props = new LazyPropagator<TickUpDown, TickUpDownNullableUShort>((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 TickUpDownNullableUShort() =>
DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownNullableUShort), new FrameworkPropertyMetadata(typeof(TickUpDownNullableUShort)));
public TickUpDownNullableUShort() =>
_explicit_props.target = this;
#region Value
public static readonly DependencyProperty ValueProperty =
WPFHelper.create_dp<ushort?, TickUpDownNullableUShort>(
nameof(Value),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, v) => {
if (o._text_box != null) {
if (ushort.TryParse(o._text_box.Text, out ushort from_text)) {
if (from_text != v)
o._text_box.Text = v.ToString();
} else if (v.HasValue) {
o._text_box.Text = v.ToString();
}
}
});
public ushort? Value {
get => GetValue(ValueProperty) as ushort?;
set => SetValue(ValueProperty, value);
}
#endregion
#region Increment
public static readonly DependencyProperty IncrementProperty =
WPFHelper.create_dp<ushort, TickUpDownNullableUShort>(nameof(Increment), 1);
public ushort Increment {
get => (ushort)GetValue(IncrementProperty);
set => SetValue(IncrementProperty, value);
}
#endregion
#region TickDefaultValue
public static readonly DependencyProperty TickDefaultValueProperty =
WPFHelper.create_dp<ushort, TickUpDownNullableUShort>(nameof(TickDefaultValue));
public ushort TickDefaultValue {
get => (ushort)GetValue(TickDefaultValueProperty);
set => SetValue(TickDefaultValueProperty, 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() {
Disposable.dispose(ref _text_hooks);
// 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();
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() {
if (ushort.TryParse(_text_box.Text, out ushort i)) {
SetCurrentValue(ValueProperty, i);
} else {
SetCurrentValue(ValueProperty, null);
}
}
}
void _tick_up() {
if (TickUpDownHelper.tick_up(Value, Increment, out ushort result))
SetCurrentValue(ValueProperty, result);
}
void _tick_down() {
if (TickUpDownHelper.tick_down(Value, Increment, out ushort result))
SetCurrentValue(ValueProperty, result);
}
}
}
@@ -0,0 +1,22 @@
#nullable enable
namespace Spark.Core {
using System;
using System.Threading;
/// <summary>
/// An idempotent, thread-safe implemention of <see cref="IDisposable"/> that invokes
/// a particular action when disposed
/// </summary>
public sealed class DeferredAction : IDisposable {
Action? _action;
public DeferredAction(Action action) =>
_action = action;
public static DeferredAction create(Action action) =>
new(action);
public void Dispose() =>
Interlocked.Exchange(ref _action, null)?.Invoke();
}
}