feat: WIP
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
namespace Ursa.Controls;
|
using Avalonia.Utilities;
|
||||||
|
|
||||||
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
public class IntUpDown: NumericUpDownBase<int>
|
public class IntUpDown: NumericUpDownBase<int>
|
||||||
{
|
{
|
||||||
@@ -6,17 +8,41 @@ public class IntUpDown: NumericUpDownBase<int>
|
|||||||
|
|
||||||
static IntUpDown()
|
static IntUpDown()
|
||||||
{
|
{
|
||||||
MaximumProperty.OverrideDefaultValue<IntUpDown>(100);
|
MaximumProperty.OverrideDefaultValue<IntUpDown>(int.MaxValue);
|
||||||
|
StepProperty.OverrideDefaultValue<IntUpDown>(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Increase()
|
protected override void Increase()
|
||||||
{
|
{
|
||||||
//throw new NotImplementedException();
|
Value += Step;
|
||||||
Value += Maximum;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Decrease()
|
protected override void Decrease()
|
||||||
{
|
{
|
||||||
Value -= Maximum;
|
Value -= Step;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void UpdateTextToValue(string x)
|
||||||
|
{
|
||||||
|
if (int.TryParse(x, out var value))
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override bool CommitInput()
|
||||||
|
{
|
||||||
|
// throw new NotImplementedException();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void SyncTextAndValue()
|
||||||
|
{
|
||||||
|
// throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override int Clamp()
|
||||||
|
{
|
||||||
|
return MathUtilities.Clamp(Value, Maximum, Minimum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,19 +2,26 @@
|
|||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Controls.Metadata;
|
using Avalonia.Controls.Metadata;
|
||||||
using Avalonia.Controls.Primitives;
|
using Avalonia.Controls.Primitives;
|
||||||
|
using Avalonia.Input;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
|
||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
[TemplatePart(PART_Spinner, typeof(ButtonSpinner))]
|
[TemplatePart(PART_Spinner, typeof(ButtonSpinner))]
|
||||||
[TemplatePart(PART_TextBox, typeof(TextBox))]
|
[TemplatePart(PART_TextBox, typeof(TextBox))]
|
||||||
|
[TemplatePart(PART_DragPanel, typeof(Panel))]
|
||||||
public abstract class NumericUpDown : TemplatedControl
|
public abstract class NumericUpDown : TemplatedControl
|
||||||
{
|
{
|
||||||
public const string PART_Spinner = "PART_Spinner";
|
public const string PART_Spinner = "PART_Spinner";
|
||||||
public const string PART_TextBox = "PART_TextBox";
|
public const string PART_TextBox = "PART_TextBox";
|
||||||
|
public const string PART_DragPanel = "PART_DragPanel";
|
||||||
|
|
||||||
private Avalonia.Controls.NumericUpDown? _numericUpDown;
|
protected internal ButtonSpinner? _spinner;
|
||||||
private ButtonSpinner? _spinner;
|
protected internal TextBox? _textBox;
|
||||||
private TextBox? _textBox;
|
protected internal Panel? _dragPanel;
|
||||||
|
|
||||||
|
private Point? _point;
|
||||||
|
private bool _updateFromTextInput;
|
||||||
|
|
||||||
public static readonly StyledProperty<bool> TextEditableProperty = AvaloniaProperty.Register<NumericUpDown, bool>(
|
public static readonly StyledProperty<bool> TextEditableProperty = AvaloniaProperty.Register<NumericUpDown, bool>(
|
||||||
nameof(TextEditable), defaultValue: true);
|
nameof(TextEditable), defaultValue: true);
|
||||||
@@ -64,6 +71,13 @@ public abstract class NumericUpDown : TemplatedControl
|
|||||||
{
|
{
|
||||||
_textBox.TextChanged -= OnTextChange;
|
_textBox.TextChanged -= OnTextChange;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_dragPanel is not null)
|
||||||
|
{
|
||||||
|
_dragPanel.PointerPressed -= OnDragPanelPointerPressed;
|
||||||
|
_dragPanel.PointerMoved -= OnDragPanelPointerMoved;
|
||||||
|
_dragPanel.PointerReleased -= OnDragPanelPointerReleased;
|
||||||
|
}
|
||||||
_spinner = e.NameScope.Find<ButtonSpinner>(PART_Spinner);
|
_spinner = e.NameScope.Find<ButtonSpinner>(PART_Spinner);
|
||||||
_textBox = e.NameScope.Find<TextBox>(PART_TextBox);
|
_textBox = e.NameScope.Find<TextBox>(PART_TextBox);
|
||||||
if (_spinner is not null)
|
if (_spinner is not null)
|
||||||
@@ -76,12 +90,55 @@ public abstract class NumericUpDown : TemplatedControl
|
|||||||
_textBox.TextChanged += OnTextChange;
|
_textBox.TextChanged += OnTextChange;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_dragPanel is not null)
|
||||||
|
{
|
||||||
|
_dragPanel.PointerPressed+= OnDragPanelPointerPressed;
|
||||||
|
_dragPanel.PointerMoved += OnDragPanelPointerMoved;
|
||||||
|
_dragPanel.PointerReleased += OnDragPanelPointerReleased;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnLostFocus(RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
CommitInput();
|
||||||
|
base.OnLostFocus(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnKeyDown(KeyEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.Key == Key.Enter)
|
||||||
|
{
|
||||||
|
var commitSuccess = CommitInput();
|
||||||
|
e.Handled = !commitSuccess;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDragPanelPointerPressed(object sender, PointerPressedEventArgs e)
|
||||||
|
{
|
||||||
|
_point = e.GetPosition(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDragPanelPointerReleased(object sender, PointerReleasedEventArgs e)
|
||||||
|
{
|
||||||
|
_point = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDragPanelPointerMoved(object sender, PointerEventArgs e)
|
||||||
|
{
|
||||||
|
var point = e.GetPosition(this);
|
||||||
|
var delta = point - _point;
|
||||||
|
if (delta is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnTextChange(object sender, TextChangedEventArgs e)
|
private void OnTextChange(object sender, TextChangedEventArgs e)
|
||||||
{
|
{
|
||||||
|
_updateFromTextInput = true;
|
||||||
|
UpdateTextToValue(_textBox?.Text ?? string.Empty);
|
||||||
|
_updateFromTextInput = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnSpin(object sender, SpinEventArgs e)
|
private void OnSpin(object sender, SpinEventArgs e)
|
||||||
@@ -98,6 +155,9 @@ public abstract class NumericUpDown : TemplatedControl
|
|||||||
|
|
||||||
protected abstract void Increase();
|
protected abstract void Increase();
|
||||||
protected abstract void Decrease();
|
protected abstract void Decrease();
|
||||||
|
protected abstract void UpdateTextToValue(string x);
|
||||||
|
protected abstract bool CommitInput();
|
||||||
|
protected abstract void SyncTextAndValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class NumericUpDownBase<T>: NumericUpDown where T: struct, IComparable<T>
|
public abstract class NumericUpDownBase<T>: NumericUpDown where T: struct, IComparable<T>
|
||||||
@@ -129,5 +189,14 @@ public abstract class NumericUpDownBase<T>: NumericUpDown where T: struct, IComp
|
|||||||
set => SetValue(MinimumProperty, value);
|
set => SetValue(MinimumProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static readonly StyledProperty<T> StepProperty = AvaloniaProperty.Register<NumericUpDownBase<T>, T>(
|
||||||
|
nameof(Step));
|
||||||
|
|
||||||
|
public T Step
|
||||||
|
{
|
||||||
|
get => GetValue(StepProperty);
|
||||||
|
set => SetValue(StepProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract T Clamp();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user