fix: extract template property.

This commit is contained in:
Zhang Dian
2023-08-05 00:40:30 +08:00
parent 5165933ffa
commit 20a0152b76
4 changed files with 22 additions and 26 deletions

View File

@@ -13,8 +13,8 @@
mc:Ignorable="d"> mc:Ignorable="d">
<StackPanel Margin="20"> <StackPanel Margin="20">
<TextBlock Text="Accept all keys" /> <TextBlock Text="Accept all keys" />
<u:KeyGestureInput HorizontalAlignment="Left" /> <u:KeyGestureInput HorizontalAlignment="Left" Width="500" />
<TextBlock Text="Accept only A,B and C" /> <TextBlock Text="Accept only A,B and C" />
<u:KeyGestureInput HorizontalAlignment="Left" AcceptableKeys="{Binding AcceptableKeys}" /> <u:KeyGestureInput HorizontalAlignment="Left" AcceptableKeys="{Binding AcceptableKeys}" Width="500" />
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

@@ -3,22 +3,25 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:Avalonia.Controls.Converters" xmlns:converters="using:Avalonia.Controls.Converters"
xmlns:u="https://irihi.tech/ursa"> xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here -->
<converters:PlatformKeyGestureConverter x:Key="KeyGestureConverter" /> <converters:PlatformKeyGestureConverter x:Key="KeyGestureConverter" />
<ControlTheme x:Key="{x:Type u:KeyGestureInput}" TargetType="u:KeyGestureInput"> <ControlTheme x:Key="{x:Type u:KeyGestureInput}" TargetType="u:KeyGestureInput">
<Setter Property="Width" Value="{DynamicResource KeyGestureInputWidth}" /> <Setter Property="Width" Value="{DynamicResource KeyGestureInputWidth}" />
<Setter Property="Height" Value="{DynamicResource KeyGestureInputHeight}" /> <Setter Property="Height" Value="{DynamicResource KeyGestureInputHeight}" />
<Setter Property="Background" Value="{DynamicResource KeyGestureInputBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource KeyGestureInputBorderBrush}" />
<Setter Property="BorderThickness" Value="{DynamicResource KeyGestureInputBorderThickness}" />
<Setter Property="CornerRadius" Value="{DynamicResource KeyGestureInputCornerRadius}" /> <Setter Property="CornerRadius" Value="{DynamicResource KeyGestureInputCornerRadius}" />
<Setter Property="Template"> <Setter Property="Template">
<ControlTemplate TargetType="u:KeyGestureInput"> <ControlTemplate TargetType="u:KeyGestureInput">
<Border <Border
Name="Background" Name="Background"
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" VerticalAlignment="Stretch"
Background="{DynamicResource KeyGestureInputBackground}" Background="{TemplateBinding Background}"
BorderBrush="Transparent" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1" BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"> CornerRadius="{TemplateBinding CornerRadius}">
<SelectableTextBlock <SelectableTextBlock
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"

View File

@@ -3,4 +3,5 @@
<x:Double x:Key="KeyGestureInputWidth">80</x:Double> <x:Double x:Key="KeyGestureInputWidth">80</x:Double>
<x:Double x:Key="KeyGestureInputHeight">32</x:Double> <x:Double x:Key="KeyGestureInputHeight">32</x:Double>
<CornerRadius x:Key="KeyGestureInputCornerRadius">3</CornerRadius> <CornerRadius x:Key="KeyGestureInputCornerRadius">3</CornerRadius>
<Thickness x:Key="KeyGestureInputBorderThickness">1</Thickness>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -72,32 +72,24 @@ public class KeyGestureInput: TemplatedControl
if (!ConsiderKeyModifiers) if (!ConsiderKeyModifiers)
{ {
if(e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl || e.Key == Key.LeftAlt || e.Key == Key.RightAlt || e.Key == Key.LeftShift || e.Key == Key.RightShift || e.Key == Key.LWin || e.Key == Key.RWin) if(e.Key is Key.LeftCtrl or Key.RightCtrl or Key.LeftAlt or Key.RightAlt or Key.LeftShift or Key.RightShift or Key.LWin or Key.RWin)
{ {
return; return;
} }
Gesture = new KeyGesture(e.Key); Gesture = new KeyGesture(e.Key);
} }
KeyGesture gesture; KeyGesture gesture;
if (e.KeyModifiers == KeyModifiers.Control && (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)) switch (e.KeyModifiers)
{ {
case KeyModifiers.Control when e.Key is Key.LeftCtrl or Key.RightCtrl:
case KeyModifiers.Alt when e.Key is Key.LeftAlt or Key.RightAlt:
case KeyModifiers.Shift when e.Key is Key.LeftShift or Key.RightShift:
case KeyModifiers.Meta when e.Key is Key.LWin or Key.RWin:
gesture = new KeyGesture(e.Key); gesture = new KeyGesture(e.Key);
} break;
else if (e.KeyModifiers == KeyModifiers.Alt && (e.Key == Key.LeftAlt || e.Key == Key.RightAlt)) default:
{
gesture = new KeyGesture(e.Key);
}
else if (e.KeyModifiers == KeyModifiers.Shift && (e.Key == Key.LeftShift || e.Key == Key.RightShift))
{
gesture = new KeyGesture(e.Key);
}
else if (e.KeyModifiers == KeyModifiers.Meta && (e.Key == Key.LWin || e.Key == Key.RWin))
{
gesture = new KeyGesture(e.Key);
}
else
{
gesture = new KeyGesture(e.Key, e.KeyModifiers); gesture = new KeyGesture(e.Key, e.KeyModifiers);
break;
} }
Gesture = gesture; Gesture = gesture;
e.Handled = true; e.Handled = true;