diff --git a/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml b/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml index 6a3cfe5..a828016 100644 --- a/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml +++ b/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml @@ -9,7 +9,13 @@ d:DesignWidth="800" mc:Ignorable="d"> - + + + + + + + diff --git a/src/Ursa.Themes.Semi/Controls/NumericUpDown.axaml b/src/Ursa.Themes.Semi/Controls/NumericUpDown.axaml index eff46a6..ad35f14 100644 --- a/src/Ursa.Themes.Semi/Controls/NumericUpDown.axaml +++ b/src/Ursa.Themes.Semi/Controls/NumericUpDown.axaml @@ -1,7 +1,8 @@ - - + + @@ -16,21 +17,30 @@ AllowSpin="{TemplateBinding AllowSpin}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" - BorderThickness="{TemplateBinding BorderThickness}" - > - + BorderThickness="{TemplateBinding BorderThickness}"> + + + + diff --git a/src/Ursa/Controls/NumericUpDown/IntUpDown.cs b/src/Ursa/Controls/NumericUpDown/IntUpDown.cs index 6aba69b..57b066c 100644 --- a/src/Ursa/Controls/NumericUpDown/IntUpDown.cs +++ b/src/Ursa/Controls/NumericUpDown/IntUpDown.cs @@ -29,4 +29,59 @@ public class IntUpDown : NumericUpDownBase protected override int? Add(int? a, int? b) => a + b; protected override int? Minus(int? a, int? b) => a - b; +} + +public class DoubleUpDown : NumericUpDownBase +{ + protected override Type StyleKeyOverride { get; } = typeof(NumericUpDown); + + static DoubleUpDown() + { + MaximumProperty.OverrideDefaultValue(double.MaxValue); + MinimumProperty.OverrideDefaultValue(double.MinValue); + StepProperty.OverrideDefaultValue(1); + } + + protected override bool ParseText(string? text, out double? number) + { + // Weird bug + var result = double.TryParse(text, out var value); + number = value; + return result; + } + + protected override string? ValueToString(double? value) => value?.ToString(FormatString, NumberFormat); + + protected override double Zero => 0; + + protected override double? Add(double? a, double? b) => a + b; + + protected override double? Minus(double? a, double? b) => a - b; +} + +public class ByteUpDown : NumericUpDownBase +{ + protected override Type StyleKeyOverride { get; } = typeof(NumericUpDown); + + static ByteUpDown() + { + MaximumProperty.OverrideDefaultValue(byte.MaxValue); + MinimumProperty.OverrideDefaultValue(byte.MinValue); + StepProperty.OverrideDefaultValue(1); + } + + protected override bool ParseText(string? text, out byte? number) + { + var result = byte.TryParse(text, ParsingNumberStyle, NumberFormat, out var value); + number = value; + return result; + } + + protected override string? ValueToString(byte? value) => value?.ToString(FormatString, NumberFormat); + + protected override byte Zero => 0; + + protected override byte? Add(byte? a, byte? b) => (byte?) (a + b); + + protected override byte? Minus(byte? a, byte? b) => (byte?) (a - b); } \ No newline at end of file diff --git a/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs b/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs index 1dc9a24..c60e205 100644 --- a/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs +++ b/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs @@ -1,4 +1,5 @@ -using System.Globalization; +using System.Diagnostics; +using System.Globalization; using System.Net.Mime; using Avalonia; using Avalonia.Controls; @@ -156,12 +157,6 @@ public abstract class NumericUpDown : TemplatedControl { _spinner.Spin -= OnSpin; } - if(_textBox is not null) - { - _textBox.TextChanged -= OnTextChange; - - } - if (_dragPanel is not null) { _dragPanel.PointerPressed -= OnDragPanelPointerPressed; @@ -170,16 +165,11 @@ public abstract class NumericUpDown : TemplatedControl } _spinner = e.NameScope.Find(PART_Spinner); _textBox = e.NameScope.Find(PART_TextBox); + _dragPanel = e.NameScope.Find(PART_DragPanel); if (_spinner is not null) { _spinner.Spin += OnSpin; } - - if (_textBox is not null) - { - _textBox.TextChanged += OnTextChange; - } - if (_dragPanel is not null) { _dragPanel.PointerPressed+= OnDragPanelPointerPressed; @@ -199,7 +189,7 @@ public abstract class NumericUpDown : TemplatedControl { if (e.Key == Key.Enter) { - var commitSuccess = CommitInput(); + var commitSuccess = CommitInput(true); e.Handled = !commitSuccess; } } @@ -216,22 +206,38 @@ public abstract class NumericUpDown : TemplatedControl private void OnDragPanelPointerMoved(object sender, PointerEventArgs e) { + if (TextEditable) return; + if(!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return; var point = e.GetPosition(this); var delta = point - _point; if (delta is null) { return; } + int d = GetDelta(delta.Value); + if(d > 0) + { + Increase(); + } + else if (d < 0) + { + Decrease(); + } + _point = point; } - [Obsolete] - private void OnTextChange(object sender, TextChangedEventArgs e) + private int GetDelta(Point point) { - _updateFromTextInput = true; - SyncTextAndValue(); - _updateFromTextInput = false; + bool horizontal = Math.Abs(point.X) > Math.Abs(point.Y); + var value = horizontal ? point.X : point.Y; + return value switch + { + > 0 => 1, + < 0 => -1, + _ => 0 + }; } - + private void OnSpin(object sender, SpinEventArgs e) { if (AllowSpin && !IsReadOnly) @@ -495,6 +501,7 @@ public abstract class NumericUpDownBase: NumericUpDown where T: struct, IComp if (_textBox!= null && !Equals(_textBox.Text, newText)) { _textBox.Text = newText; + _textBox.CaretIndex = newText?.Length??0; } } }