feat: add copy paste hotkey support.

This commit is contained in:
rabbitism
2023-02-25 13:59:56 +08:00
parent f2e3e03ee7
commit 34da3489b5

View File

@@ -125,7 +125,8 @@ public class IPv4Box: TemplatedControl
if (e.Key == Key.Enter) if (e.Key == Key.Enter)
{ {
ParseBytes(ShowLeadingZero); ParseBytes(ShowLeadingZero);
SetIPAddress(); SetIPAddressInternal();
base.OnKeyDown(e);
return; return;
} }
if (Match(keymap.SelectAll)) if (Match(keymap.SelectAll))
@@ -134,6 +135,14 @@ public class IPv4Box: TemplatedControl
_currentActivePresenter.SelectionEnd = _currentActivePresenter.Text?.Length ?? 0; _currentActivePresenter.SelectionEnd = _currentActivePresenter.Text?.Length ?? 0;
return; return;
} }
else if (Match(keymap.Copy))
{
OnCopy();
}
else if (Match(keymap.Paste))
{
OnPaste();
}
if (e.Key == Key.Tab) if (e.Key == Key.Tab)
{ {
_currentActivePresenter?.HideCaret(); _currentActivePresenter?.HideCaret();
@@ -268,7 +277,7 @@ public class IPv4Box: TemplatedControl
} }
_currentActivePresenter = null; _currentActivePresenter = null;
ParseBytes(ShowLeadingZero); ParseBytes(ShowLeadingZero);
SetIPAddress(); SetIPAddressInternal();
} }
protected override void OnGotFocus(GotFocusEventArgs e) protected override void OnGotFocus(GotFocusEventArgs e)
@@ -372,7 +381,7 @@ public class IPv4Box: TemplatedControl
IPAddress = null; IPAddress = null;
} }
private void SetIPAddress() private void SetIPAddressInternal()
{ {
if (_firstByte is null && _secondByte is null && _thirdByte is null && _fourthByte is null) if (_firstByte is null && _secondByte is null && _thirdByte is null && _fourthByte is null)
{ {
@@ -477,6 +486,24 @@ public class IPv4Box: TemplatedControl
_currentActivePresenter.CaretIndex--; _currentActivePresenter.CaretIndex--;
} }
} }
public async void OnCopy()
{
string s = string.Join(".", _firstText?.Text, _secondText?.Text, _thirdText?.Text, _fourthText?.Text);
IClipboard? clipboard = AvaloniaLocator.Current.GetService<IClipboard>();
clipboard?.SetTextAsync(s);
}
public async void OnPaste()
{
IClipboard? clipboard = AvaloniaLocator.Current.GetService<IClipboard>();
if (clipboard is null) return;
string? s = await clipboard.GetTextAsync();
if (IPAddress.TryParse(s, out var address))
{
IPAddress = address;
}
}
} }
public static class TextPresenterHelper public static class TextPresenterHelper