feat: use numpad button.

This commit is contained in:
rabbitism
2024-03-10 01:50:20 +08:00
parent 93b55aae5e
commit dd31df874a
9 changed files with 214 additions and 83 deletions

View File

@@ -10,7 +10,6 @@ namespace Ursa.Controls;
public class NumPad: TemplatedControl
{
private Button? _sevenButton;
public static readonly StyledProperty<InputElement?> TargetProperty = AvaloniaProperty.Register<NumPad, InputElement?>(
nameof(Target));
@@ -20,6 +19,15 @@ public class NumPad: TemplatedControl
set => SetValue(TargetProperty, value);
}
public static readonly StyledProperty<bool> NumModeProperty = AvaloniaProperty.Register<NumPad, bool>(
nameof(NumMode), defaultValue: true);
public bool NumMode
{
get => GetValue(NumModeProperty);
set => SetValue(NumModeProperty, value);
}
public static readonly AttachedProperty<bool> AttachProperty =
AvaloniaProperty.RegisterAttached<NumPad, InputElement, bool>("Attach");
@@ -28,8 +36,7 @@ public class NumPad: TemplatedControl
static NumPad()
{
TargetProperty.Changed.AddClassHandler<NumPad, InputElement?>((n, args) => n.OnTargetChanged(args));
AttachProperty.Changed.AddClassHandler<InputElement, bool>((input, args)=> OnAttachNumPad(input, args));
AttachProperty.Changed.AddClassHandler<InputElement, bool>(OnAttachNumPad);
}
private static void OnAttachNumPad(InputElement input, AvaloniaPropertyChangedEventArgs<bool> args)
@@ -43,12 +50,6 @@ public class NumPad: TemplatedControl
GotFocusEvent.RemoveHandler(OnTargetGotFocus, input);
}
}
private void OnTargetChanged(AvaloniaPropertyChangedEventArgs<InputElement?> args)
{
//GotFocusEvent.RemoveHandler(OnTargetGotFocus, args.OldValue.Value);
//GotFocusEvent.AddHandler(OnTargetGotFocus, args.NewValue.Value);
}
private static void OnTargetGotFocus(object sender, GotFocusEventArgs e)
{
@@ -63,23 +64,54 @@ public class NumPad: TemplatedControl
OverlayDialog.Show(numPad, new object(), options: new OverlayDialogOptions() { Buttons = DialogButton.None });
}
private void OnSevenButtonClick(object sender, RoutedEventArgs e)
private Dictionary<Key, string> _keyInputMapping = new()
{
Target?.RaiseEvent(new TextInputEventArgs()
{
Source = this,
RoutedEvent = TextInputEvent,
Text = "7",
});
}
[Key.NumPad0] = "0",
[Key.NumPad1] = "1",
[Key.NumPad2] = "2",
[Key.NumPad3] = "3",
[Key.NumPad4] = "4",
[Key.NumPad5] = "5",
[Key.NumPad6] = "6",
[Key.NumPad7] = "7",
[Key.NumPad8] = "8",
[Key.NumPad9] = "9",
[Key.OemPlus] = "+",
[Key.OemMinus] = "-",
};
public void InputNumber(object o)
public void ProcessClick(object o)
{
Target?.RaiseEvent(new TextInputEventArgs()
if (o is NumPadButton b)
{
Source = this,
RoutedEvent = TextInputEvent,
Text = o.ToString(),
});
if (b is { NumMode: true, NumKey: not null })
{
Target?.RaiseEvent(new TextInputEventArgs()
{
Source = this,
RoutedEvent = TextInputEvent,
Text = _keyInputMapping.TryGetValue(b.NumKey.Value, out var text)? text:string.Empty,
});
}
else if (b is { NumMode: false, FunctionKey: null, NumKey: not null })
{
Target?.RaiseEvent(new TextInputEventArgs()
{
Source = this,
RoutedEvent = TextInputEvent,
Text = _keyInputMapping.TryGetValue(b.NumKey.Value, out var text)? text:string.Empty,
});
}
else
{
Target?.RaiseEvent(new KeyEventArgs()
{
Source = this,
RoutedEvent = KeyDownEvent,
Key = b.FunctionKey ?? Key.None,
});
}
}
}
}

View File

@@ -0,0 +1,54 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
namespace Ursa.Controls;
public class NumPadButton: RepeatButton
{
public static readonly StyledProperty<Key?> NumKeyProperty = AvaloniaProperty.Register<NumPadButton, Key?>(
nameof(NumKey));
public Key? NumKey
{
get => GetValue(NumKeyProperty);
set => SetValue(NumKeyProperty, value);
}
public static readonly StyledProperty<Key?> FunctionKeyProperty = AvaloniaProperty.Register<NumPadButton, Key?>(
nameof(FunctionKey));
public Key? FunctionKey
{
get => GetValue(FunctionKeyProperty);
set => SetValue(FunctionKeyProperty, value);
}
public static readonly StyledProperty<bool> NumModeProperty = AvaloniaProperty.Register<NumPadButton, bool>(
nameof(NumMode));
public bool NumMode
{
get => GetValue(NumModeProperty);
set => SetValue(NumModeProperty, value);
}
public static readonly StyledProperty<object?> NumContentProperty = AvaloniaProperty.Register<NumPadButton, object?>(
nameof(NumContent));
public object? NumContent
{
get => GetValue(NumContentProperty);
set => SetValue(NumContentProperty, value);
}
public static readonly StyledProperty<object?> FunctionContentProperty = AvaloniaProperty.Register<NumPadButton, object?>(
nameof(FunctionContent));
public object? FunctionContent
{
get => GetValue(FunctionContentProperty);
set => SetValue(FunctionContentProperty, value);
}
}