From 9960bfabf17d4d32e7ebd3e70644bba7a4f82ce8 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Sun, 10 Mar 2024 14:49:55 +0800 Subject: [PATCH] feat: simplify key code. --- src/Ursa.Themes.Semi/Controls/NumPad.axaml | 4 ++ src/Ursa/Controls/NumPad/NumPad.cs | 56 +++++++--------------- 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/src/Ursa.Themes.Semi/Controls/NumPad.axaml b/src/Ursa.Themes.Semi/Controls/NumPad.axaml index 853d45d..bc6c8e6 100644 --- a/src/Ursa.Themes.Semi/Controls/NumPad.axaml +++ b/src/Ursa.Themes.Semi/Controls/NumPad.axaml @@ -46,18 +46,21 @@ Grid.Row="0" Grid.Column="1" FunctionContent="/" + FunctionKey="Divide" NumContent="/" NumKey="Divide" /> _keyInputMapping = new() + private static readonly Dictionary KeyInputMapping = new() { [Key.NumPad0] = "0", [Key.NumPad1] = "1", @@ -85,45 +85,25 @@ public class NumPad: TemplatedControl public void ProcessClick(object o) { - if (o is NumPadButton b) + if (Target is null || o is not NumPadButton b) return; + var key = (b.NumMode ? b.NumKey : b.FunctionKey)?? Key.None; + if (KeyInputMapping.TryGetValue(key, out string s)) { - if (b.NumKey is null) + Target.RaiseEvent(new TextInputEventArgs() { - Target?.RaiseEvent(new KeyEventArgs() - { - Source = this, - RoutedEvent = KeyDownEvent, - Key = b.FunctionKey ?? Key.None, - }); - } - 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, - }); - } + Source = this, + RoutedEvent = TextInputEvent, + Text = s, + }); + } + else + { + Target.RaiseEvent(new KeyEventArgs() + { + Source = this, + RoutedEvent = KeyDownEvent, + Key = key, + }); } - } } \ No newline at end of file