From 34da3489b5365921fc00296934cc3ff496546e50 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Sat, 25 Feb 2023 13:59:56 +0800 Subject: [PATCH] feat: add copy paste hotkey support. --- src/Ursa/Controls/IPv4Box.cs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Ursa/Controls/IPv4Box.cs b/src/Ursa/Controls/IPv4Box.cs index 536f5b2..a93e044 100644 --- a/src/Ursa/Controls/IPv4Box.cs +++ b/src/Ursa/Controls/IPv4Box.cs @@ -125,7 +125,8 @@ public class IPv4Box: TemplatedControl if (e.Key == Key.Enter) { ParseBytes(ShowLeadingZero); - SetIPAddress(); + SetIPAddressInternal(); + base.OnKeyDown(e); return; } if (Match(keymap.SelectAll)) @@ -134,6 +135,14 @@ public class IPv4Box: TemplatedControl _currentActivePresenter.SelectionEnd = _currentActivePresenter.Text?.Length ?? 0; return; } + else if (Match(keymap.Copy)) + { + OnCopy(); + } + else if (Match(keymap.Paste)) + { + OnPaste(); + } if (e.Key == Key.Tab) { _currentActivePresenter?.HideCaret(); @@ -268,7 +277,7 @@ public class IPv4Box: TemplatedControl } _currentActivePresenter = null; ParseBytes(ShowLeadingZero); - SetIPAddress(); + SetIPAddressInternal(); } protected override void OnGotFocus(GotFocusEventArgs e) @@ -372,7 +381,7 @@ public class IPv4Box: TemplatedControl IPAddress = null; } - private void SetIPAddress() + private void SetIPAddressInternal() { if (_firstByte is null && _secondByte is null && _thirdByte is null && _fourthByte is null) { @@ -477,6 +486,24 @@ public class IPv4Box: TemplatedControl _currentActivePresenter.CaretIndex--; } } + + public async void OnCopy() + { + string s = string.Join(".", _firstText?.Text, _secondText?.Text, _thirdText?.Text, _fourthText?.Text); + IClipboard? clipboard = AvaloniaLocator.Current.GetService(); + clipboard?.SetTextAsync(s); + } + + public async void OnPaste() + { + IClipboard? clipboard = AvaloniaLocator.Current.GetService(); + if (clipboard is null) return; + string? s = await clipboard.GetTextAsync(); + if (IPAddress.TryParse(s, out var address)) + { + IPAddress = address; + } + } } public static class TextPresenterHelper