fix #172 show not drag to add/cut while clamp

This commit is contained in:
heartacker
2024-03-21 15:19:30 +08:00
parent ac2c563cd2
commit c3ed31659b

View File

@@ -30,6 +30,12 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl
private Point? _point; private Point? _point;
protected internal bool _updateFromTextInput; protected internal bool _updateFromTextInput;
protected internal bool _canIncrease = true;
protected internal bool _canDecrease = true;
public static readonly StyledProperty<bool> AllowDragProperty = AvaloniaProperty.Register<NumericUpDown, bool>( public static readonly StyledProperty<bool> AllowDragProperty = AvaloniaProperty.Register<NumericUpDown, bool>(
nameof(AllowDrag), defaultValue: false); nameof(AllowDrag), defaultValue: false);
@@ -256,11 +262,13 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl
int d = GetDelta(delta.Value); int d = GetDelta(delta.Value);
if (d > 0) if (d > 0)
{ {
Increase(); if (_canIncrease)
Increase();
} }
else if (d < 0) else if (d < 0)
{ {
Decrease(); if (_canDecrease)
Decrease();
} }
_point = point; _point = point;
} }
@@ -332,7 +340,7 @@ public abstract class NumericUpDownBase<T> : NumericUpDown where T : struct, ICo
if (text.StartsWith("0X") || text.StartsWith("0x")) // support 0x hex while user input if (text.StartsWith("0X") || text.StartsWith("0x")) // support 0x hex while user input
{ {
text = text.Substring(2); text = text.Substring(2);
} }
else if (text.StartsWith("h") || text.StartsWith("H")) // support hex while user input else if (text.StartsWith("h") || text.StartsWith("H")) // support hex while user input
{ {
text = text.Substring(1); text = text.Substring(1);
@@ -511,6 +519,8 @@ public abstract class NumericUpDownBase<T> : NumericUpDown where T : struct, ICo
protected override void SetValidSpinDirection() protected override void SetValidSpinDirection()
{ {
var validDirection = ValidSpinDirections.None; var validDirection = ValidSpinDirections.None;
_canIncrease = false;
_canDecrease = false;
if (!IsReadOnly) if (!IsReadOnly)
{ {
if (Value is null) if (Value is null)
@@ -520,11 +530,13 @@ public abstract class NumericUpDownBase<T> : NumericUpDown where T : struct, ICo
if (Value.HasValue && Value.Value.CompareTo(Maximum) < 0) if (Value.HasValue && Value.Value.CompareTo(Maximum) < 0)
{ {
validDirection |= ValidSpinDirections.Increase; validDirection |= ValidSpinDirections.Increase;
_canIncrease = true;
} }
if (Value.HasValue && Value.Value.CompareTo(Minimum) > 0) if (Value.HasValue && Value.Value.CompareTo(Minimum) > 0)
{ {
validDirection |= ValidSpinDirections.Decrease; validDirection |= ValidSpinDirections.Decrease;
_canDecrease = true;
} }
} }
if (_spinner != null) if (_spinner != null)
@@ -553,28 +565,28 @@ public abstract class NumericUpDownBase<T> : NumericUpDown where T : struct, ICo
} }
if (!Equals(newValue, Value)) if (!Equals(newValue, Value))
{ {
SetCurrentValue(ValueProperty, newValue); SetCurrentValue(ValueProperty, newValue);
} }
} }
catch catch
{ {
parsedTextIsValid = false; parsedTextIsValid = false;
} }
} }
if (!_updateFromTextInput) if (!_updateFromTextInput)
{ {
if (forceTextUpdate) if (forceTextUpdate)
{ {
var newText = ConvertValueToText(Value); var newText = ConvertValueToText(Value);
if (_textBox != null && !Equals(_textBox.Text, newText)) if (_textBox != null && !Equals(_textBox.Text, newText))
{ {
_textBox.Text = newText; _textBox.Text = newText;
_textBox.CaretIndex = newText?.Length ?? 0; _textBox.CaretIndex = newText?.Length ?? 0;
} }
} }
} }
if (_updateFromTextInput && !parsedTextIsValid) if (_updateFromTextInput && !parsedTextIsValid)
{ {
if (_spinner is not null) if (_spinner is not null)