feat: add menu flyout.

This commit is contained in:
rabbitism
2023-02-25 14:25:11 +08:00
parent 34da3489b5
commit a324399134
2 changed files with 45 additions and 5 deletions

View File

@@ -8,6 +8,27 @@
<TextBlock Text="Hello World" /> <TextBlock Text="Hello World" />
</StackPanel> </StackPanel>
</Design.PreviewWith> </Design.PreviewWith>
<MenuFlyout x:Key="IPv4BoxMenuFlyout">
<MenuItem
x:Name="TextBoxContextFlyoutCutItem"
Command="{Binding $parent[u:IPv4Box].Cut}"
Header="Cut"
InputGesture="{x:Static u:IPv4Box.CutKeyGesture}" />
<MenuItem
x:Name="TextBoxContextFlyoutCopyItem"
Command="{Binding $parent[u:IPv4Box].Copy}"
Header="Copy"
InputGesture="{x:Static u:IPv4Box.CopyKeyGesture}" />
<MenuItem
x:Name="TextBoxContextFlyoutPasteItem"
Command="{Binding $parent[u:IPv4Box].Paste}"
Header="Paste"
InputGesture="{x:Static u:IPv4Box.PasteKeyGesture}" />
<MenuItem
x:Name="TextBoxContextFlyoutClearItem"
Command="{Binding $parent[u:IPv4Box].Clear}"
Header="Clear" />
</MenuFlyout>
<ControlTheme x:Key="{x:Type u:IPv4Box}" TargetType="{x:Type u:IPv4Box}"> <ControlTheme x:Key="{x:Type u:IPv4Box}" TargetType="{x:Type u:IPv4Box}">
<Setter Property="u:IPv4Box.Focusable" Value="True" /> <Setter Property="u:IPv4Box.Focusable" Value="True" />
<Setter Property="u:IPv4Box.ShowLeadingZero" Value="True" /> <Setter Property="u:IPv4Box.ShowLeadingZero" Value="True" />
@@ -20,6 +41,7 @@
<Setter Property="u:IPv4Box.SelectionBrush" Value="{DynamicResource IPv4BoxSelectionBrush}" /> <Setter Property="u:IPv4Box.SelectionBrush" Value="{DynamicResource IPv4BoxSelectionBrush}" />
<Setter Property="u:IPv4Box.SelectionForegroundBrush" Value="{DynamicResource IPv4BoxSelectionForeground}" /> <Setter Property="u:IPv4Box.SelectionForegroundBrush" Value="{DynamicResource IPv4BoxSelectionForeground}" />
<Setter Property="u:IPv4Box.CaretBrush" Value="{DynamicResource IPv4BoxCaretBrush}" /> <Setter Property="u:IPv4Box.CaretBrush" Value="{DynamicResource IPv4BoxCaretBrush}" />
<Setter Property="u:IPv4Box.ContextFlyout" Value="{DynamicResource IPv4BoxMenuFlyout}" />
<Setter Property="u:IPv4Box.Template"> <Setter Property="u:IPv4Box.Template">
<ControlTemplate TargetType="u:IPv4Box"> <ControlTemplate TargetType="u:IPv4Box">
<Border <Border

View File

@@ -137,11 +137,11 @@ public class IPv4Box: TemplatedControl
} }
else if (Match(keymap.Copy)) else if (Match(keymap.Copy))
{ {
OnCopy(); Copy();
} }
else if (Match(keymap.Paste)) else if (Match(keymap.Paste))
{ {
OnPaste(); Paste();
} }
if (e.Key == Key.Tab) if (e.Key == Key.Tab)
{ {
@@ -378,6 +378,11 @@ public class IPv4Box: TemplatedControl
{ {
if (presenter != null) presenter.Text = null; if (presenter != null) presenter.Text = null;
} }
_firstByte = null;
_secondByte = null;
_thirdByte = null;
_fourthByte = null;
IPAddress = null; IPAddress = null;
} }
@@ -487,23 +492,36 @@ public class IPv4Box: TemplatedControl
} }
} }
public async void OnCopy() public async void Copy()
{ {
string s = string.Join(".", _firstText?.Text, _secondText?.Text, _thirdText?.Text, _fourthText?.Text); string s = string.Join(".", _firstText?.Text, _secondText?.Text, _thirdText?.Text, _fourthText?.Text);
IClipboard? clipboard = AvaloniaLocator.Current.GetService<IClipboard>(); IClipboard? clipboard = AvaloniaLocator.Current.GetService<IClipboard>();
clipboard?.SetTextAsync(s); clipboard?.SetTextAsync(s);
} }
public static KeyGesture? CopyKeyGesture { get; } = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>()?.Copy.FirstOrDefault();
public static KeyGesture? PasteKeyGesture { get; } = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>()?.Paste.FirstOrDefault();
public static KeyGesture? CutKeyGesture { get; } = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>()?.Cut.FirstOrDefault();
public async void OnPaste() public async void Paste()
{ {
IClipboard? clipboard = AvaloniaLocator.Current.GetService<IClipboard>(); IClipboard? clipboard = AvaloniaLocator.Current.GetService<IClipboard>();
if (clipboard is null) return; if (clipboard is null) return;
string? s = await clipboard.GetTextAsync(); string s = await clipboard.GetTextAsync();
if (IPAddress.TryParse(s, out var address)) if (IPAddress.TryParse(s, out var address))
{ {
IPAddress = address; IPAddress = address;
} }
} }
public async void Cut()
{
IClipboard? clipboard = AvaloniaLocator.Current.GetService<IClipboard>();
if(clipboard is null) return;
string s = string.Join(".", _firstText?.Text, _secondText?.Text, _thirdText?.Text, _fourthText?.Text);
await clipboard.SetTextAsync(s);
Clear();
}
} }
public static class TextPresenterHelper public static class TextPresenterHelper