Merge pull request #173 from irihitech/numeric_fix

Read-only and Draggable fix.
This commit is contained in:
Dong Bin
2024-03-21 20:48:43 +08:00
committed by GitHub
3 changed files with 71 additions and 63 deletions

View File

@@ -5,6 +5,9 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa" xmlns:u="https://irihi.tech/ursa"
xmlns:vm="using:Ursa.Demo.ViewModels"
x:DataType="vm:NumericUpDownDemoViewModel"
x:CompileBindings="True"
d:DesignHeight="450" d:DesignHeight="450"
d:DesignWidth="800" d:DesignWidth="800"
mc:Ignorable="d"> mc:Ignorable="d">

View File

@@ -52,7 +52,6 @@
FontSize="{TemplateBinding FontSize}" FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}" Foreground="{TemplateBinding Foreground}"
InnerLeftContent="{TemplateBinding InnerLeftContent}" InnerLeftContent="{TemplateBinding InnerLeftContent}"
IsReadOnly="{TemplateBinding IsReadOnly}"
TextWrapping="NoWrap" TextWrapping="NoWrap"
Theme="{DynamicResource NonErrorTextBox}" Theme="{DynamicResource NonErrorTextBox}"
Watermark="{TemplateBinding Watermark}" /> Watermark="{TemplateBinding Watermark}" />
@@ -61,8 +60,7 @@
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" VerticalAlignment="Stretch"
Background="Transparent" Background="Transparent"
Cursor="SizeAll" Cursor="SizeAll"/>
IsVisible="{TemplateBinding AllowDrag}" />
<Button <Button
Name="PART_ClearButton" Name="PART_ClearButton"
Margin="0,0,8,0" Margin="0,0,8,0"

View File

@@ -11,6 +11,7 @@ using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Layout; using Avalonia.Layout;
using Irihi.Avalonia.Shared.Contracts; using Irihi.Avalonia.Shared.Contracts;
using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls; namespace Ursa.Controls;
@@ -37,7 +38,7 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl
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), defaultBindingMode: BindingMode.TwoWay);
public bool AllowDrag public bool AllowDrag
{ {
@@ -46,7 +47,7 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl
} }
public static readonly StyledProperty<bool> IsReadOnlyProperty = AvaloniaProperty.Register<NumericUpDown, bool>( public static readonly StyledProperty<bool> IsReadOnlyProperty = AvaloniaProperty.Register<NumericUpDown, bool>(
nameof(IsReadOnly)); nameof(IsReadOnly), defaultBindingMode: BindingMode.TwoWay);
public bool IsReadOnly public bool IsReadOnly
{ {
@@ -140,11 +141,23 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl
{ {
NumberFormatProperty.Changed.AddClassHandler<NumericUpDown>((o, e) => o.OnFormatChange(e)); NumberFormatProperty.Changed.AddClassHandler<NumericUpDown>((o, e) => o.OnFormatChange(e));
FormatStringProperty.Changed.AddClassHandler<NumericUpDown>((o, e) => o.OnFormatChange(e)); FormatStringProperty.Changed.AddClassHandler<NumericUpDown>((o, e) => o.OnFormatChange(e));
IsReadOnlyProperty.Changed.AddClassHandler<NumericUpDown>((o, e) => o.ChangeToSetSpinDirection(e)); IsReadOnlyProperty.Changed.AddClassHandler<NumericUpDown, bool>((o, args) => o.OnIsReadOnlyChanged(args));
TextConverterProperty.Changed.AddClassHandler<NumericUpDown>((o, e) => o.OnFormatChange(e)); TextConverterProperty.Changed.AddClassHandler<NumericUpDown>((o, e) => o.OnFormatChange(e));
AllowDragProperty.Changed.AddClassHandler<NumericUpDown, bool>((o, e) => o.OnAllowDragChange(e));
} }
protected void ChangeToSetSpinDirection(AvaloniaPropertyChangedEventArgs avaloniaPropertyChangedEventArgs, bool afterInitialization = false) private void OnAllowDragChange(AvaloniaPropertyChangedEventArgs<bool> args)
{
IsVisibleProperty.SetValue(args.NewValue.Value, _dragPanel);
}
private void OnIsReadOnlyChanged(AvaloniaPropertyChangedEventArgs<bool> args)
{
ChangeToSetSpinDirection(args, false);
TextBox.IsReadOnlyProperty.SetValue(args.NewValue.Value, _textBox);
}
protected void ChangeToSetSpinDirection(AvaloniaPropertyChangedEventArgs e, bool afterInitialization = false)
{ {
if (afterInitialization) if (afterInitialization)
{ {
@@ -170,30 +183,19 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
if (_spinner is not null) Spinner.SpinEvent.RemoveHandler(OnSpin, _spinner);
{ PointerPressedEvent.RemoveHandler(OnDragPanelPointerPressed, _dragPanel);
_spinner.Spin -= OnSpin; PointerMovedEvent.RemoveHandler(OnDragPanelPointerMoved, _dragPanel);
} PointerReleasedEvent.RemoveHandler(OnDragPanelPointerReleased, _dragPanel);
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);
_dragPanel = e.NameScope.Find<Panel>(PART_DragPanel); _dragPanel = e.NameScope.Find<Panel>(PART_DragPanel);
if (_spinner is not null) IsVisibleProperty.SetValue(AllowDrag, _dragPanel);
{ TextBox.IsReadOnlyProperty.SetValue(IsReadOnly, _textBox);
_spinner.Spin += OnSpin; Spinner.SpinEvent.AddHandler(OnSpin, _spinner);
} PointerPressedEvent.AddHandler(OnDragPanelPointerPressed, _dragPanel);
if (_dragPanel is not null) PointerMovedEvent.AddHandler(OnDragPanelPointerMoved, _dragPanel);
{ PointerReleasedEvent.AddHandler(OnDragPanelPointerReleased, _dragPanel);
_dragPanel.PointerPressed += OnDragPanelPointerPressed;
_dragPanel.PointerMoved += OnDragPanelPointerMoved;
_dragPanel.PointerReleased += OnDragPanelPointerReleased;
}
} }
protected override void OnLostFocus(RoutedEventArgs e) protected override void OnLostFocus(RoutedEventArgs e)
@@ -219,7 +221,9 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl
if (AllowDrag && _dragPanel is not null) if (AllowDrag && _dragPanel is not null)
{ {
_dragPanel.IsVisible = true; _dragPanel.IsVisible = true;
_dragPanel.Focus(); // _dragPanel.Focus();
_textBox?.ClearSelection();
_spinner?.Focus();
} }
} }
} }
@@ -229,18 +233,20 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl
_point = e.GetPosition(this); _point = e.GetPosition(this);
if (e.ClickCount == 2 && _dragPanel is not null && AllowDrag) if (e.ClickCount == 2 && _dragPanel is not null && AllowDrag)
{ {
_dragPanel.IsVisible = false; IsVisibleProperty.SetValue(false, _dragPanel);
_textBox.IsReadOnly = false; _textBox?.Focus();
TextBox.IsReadOnlyProperty.SetValue(IsReadOnly, _textBox);
} }
else else
{ {
_textBox?.Focus(); _textBox?.Focus();
_textBox!.IsReadOnly = true; TextBox.IsReadOnlyProperty.SetValue(true, _textBox);
} }
} }
protected override void OnTextInput(TextInputEventArgs e) protected override void OnTextInput(TextInputEventArgs e)
{ {
if (IsReadOnly) return;
_textBox?.RaiseEvent(e); _textBox?.RaiseEvent(e);
} }
@@ -328,7 +334,8 @@ public abstract class NumericUpDownBase<T> : NumericUpDown where T : struct, ICo
{ {
protected static string TrimString(string? text, NumberStyles numberStyles) protected static string TrimString(string? text, NumberStyles numberStyles)
{ {
text = text!.Trim(); if (text is null) return string.Empty;
text = text.Trim();
if (text.Contains("_")) // support _ like 0x1024_1024(hex), 10_24 (normal) if (text.Contains("_")) // support _ like 0x1024_1024(hex), 10_24 (normal)
{ {
text = text.Replace("_", ""); text = text.Replace("_", "");