diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownHelper.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownHelper.cs index 6cb68ec9c..3aa62bb46 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownHelper.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownHelper.cs @@ -1,4 +1,5 @@ -using System.Windows.Controls; +using System; +using System.Windows.Controls; using System.Windows.Data; namespace mbts.ui.controls { @@ -24,5 +25,66 @@ namespace mbts.ui.controls { UpdateSourceTrigger = update_source_trigger }); } + + // 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); + + // 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); + + // 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); + + // 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); + + // 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); + + // 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); + + // generic logic to avoid ticking across zero, in either direction. + static bool _tick(T value, T incremented, int expected_comparison, out T result) where T : struct, IComparable { + 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; + } + } + + static bool _no_op(out T result) { + result = default; + return false; + } } -} \ No newline at end of file +} diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownInt.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownInt.cs index 5b8c155af..35ca5eb59 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownInt.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownInt.cs @@ -121,10 +121,14 @@ namespace mbts.ui.controls { SetCurrentValue(ValueProperty, int.TryParse(_text_box.Text, out int i) ? i : 0); } - void _tick_up() => - SetCurrentValue(ValueProperty, Value + Increment); + void _tick_up() { + if (TickUpDownHelper.tick_up(Value, Increment, out int result)) + SetCurrentValue(ValueProperty, result); + } - void _tick_down() => - SetCurrentValue(ValueProperty, Value - Increment); + void _tick_down() { + if (TickUpDownHelper.tick_down(Value, Increment, out int result)) + SetCurrentValue(ValueProperty, result); + } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs index a15b86015..3e7026cb3 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs @@ -88,13 +88,13 @@ namespace mbts.ui.controls { } void _tick_up() { - int? v = Value; - SetCurrentValue(ValueProperty, v.HasValue ? v.Value + Increment : TickDefaultValue); + if (TickUpDownHelper.tick_up(Value, Increment, out int result)) + SetCurrentValue(ValueProperty, result); } void _tick_down() { - int? v = Value; - SetCurrentValue(ValueProperty, v.HasValue ? v.Value - Increment : TickDefaultValue); + if (TickUpDownHelper.tick_down(Value, Increment, out int result)) + SetCurrentValue(ValueProperty, result); } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs index 4e24d9c35..b22d1ef48 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs @@ -103,14 +103,13 @@ namespace mbts.ui.controls { } void _tick_up() { - uint? v = Value; - SetCurrentValue(ValueProperty, v.HasValue ? v.Value + Increment : TickDefaultValue); + if (TickUpDownHelper.tick_up(Value, Increment, out uint result)) + SetCurrentValue(ValueProperty, result); } void _tick_down() { - uint? v = Value; - uint inc = Increment; - SetCurrentValue(ValueProperty, v.HasValue && v.Value > inc ? v.Value - inc : TickDefaultValue); + if (TickUpDownHelper.tick_down(Value, Increment, out uint result)) + SetCurrentValue(ValueProperty, result); } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableULong.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableULong.cs index 021908100..9c89e572d 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableULong.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableULong.cs @@ -101,14 +101,13 @@ namespace mbts.ui.controls { } void _tick_up() { - ulong? v = Value; - SetCurrentValue(ValueProperty, v.HasValue ? v.Value + Increment : TickDefaultValue); + if (TickUpDownHelper.tick_up(Value, Increment, out ulong result)) + SetCurrentValue(ValueProperty, result); } void _tick_down() { - ulong? v = Value; - ulong inc = Increment; - SetCurrentValue(ValueProperty, v.HasValue && v.Value > inc ? v.Value - inc : TickDefaultValue); + if (TickUpDownHelper.tick_down(Value, Increment, out ulong result)) + SetCurrentValue(ValueProperty, result); } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUShort.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUShort.cs index 2ce45f9f4..6af50aac3 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUShort.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUShort.cs @@ -101,14 +101,13 @@ namespace mbts.ui.controls { } void _tick_up() { - ushort? v = Value; - SetCurrentValue(ValueProperty, v.HasValue ? v.Value + Increment : TickDefaultValue); + if (TickUpDownHelper.tick_up(Value, Increment, out ushort result)) + SetCurrentValue(ValueProperty, result); } void _tick_down() { - ushort? v = Value; - ushort inc = Increment; - SetCurrentValue(ValueProperty, v.HasValue && v.Value > inc ? v.Value - inc : TickDefaultValue); + if (TickUpDownHelper.tick_down(Value, Increment, out ushort result)) + SetCurrentValue(ValueProperty, result); } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownUInt.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownUInt.cs index 119b9862e..52c2d1224 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownUInt.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownUInt.cs @@ -91,10 +91,14 @@ namespace mbts.ui.controls { SetCurrentValue(ValueProperty, uint.TryParse(_text_box.Text, out uint u) ? u : 0u); } - void _tick_up() => - SetCurrentValue(ValueProperty, Value + Increment); + void _tick_up() { + if (TickUpDownHelper.tick_up(Value, Increment, out uint result)) + SetCurrentValue(ValueProperty, result); + } - void _tick_down() => - SetCurrentValue(ValueProperty, Value - Increment); + void _tick_down() { + if (TickUpDownHelper.tick_down(Value, Increment, out uint result)) + SetCurrentValue(ValueProperty, result); + } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownULong.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownULong.cs index 369b97f43..6afa9e192 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownULong.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownULong.cs @@ -95,10 +95,14 @@ namespace mbts.ui.controls { SetCurrentValue(ValueProperty, ulong.TryParse(_text_box.Text, out ulong u) ? u : 0u); } - void _tick_up() => - SetCurrentValue(ValueProperty, Value + Increment); + void _tick_up() { + if (TickUpDownHelper.tick_up(Value, Increment, out ulong result)) + SetCurrentValue(ValueProperty, result); + } - void _tick_down() => - SetCurrentValue(ValueProperty, Value - Increment); + void _tick_down() { + if (TickUpDownHelper.tick_down(Value, Increment, out ulong result)) + SetCurrentValue(ValueProperty, result); + } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownUShort.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownUShort.cs index 9f0ca1da1..ee3e608ef 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownUShort.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownUShort.cs @@ -95,10 +95,14 @@ namespace mbts.ui.controls { SetCurrentValue(ValueProperty, ushort.TryParse(_text_box.Text, out ushort u) ? u : 0u); } - void _tick_up() => - SetCurrentValue(ValueProperty, Value + Increment); + void _tick_up() { + if (TickUpDownHelper.tick_up(Value, Increment, out ushort result)) + SetCurrentValue(ValueProperty, result); + } - void _tick_down() => - SetCurrentValue(ValueProperty, Value - Increment); + void _tick_down() { + if (TickUpDownHelper.tick_down(Value, Increment, out ushort result)) + SetCurrentValue(ValueProperty, result); + } } } \ No newline at end of file