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">
<StackPanel Margin="20">
<TextBlock Text="Accept all keys" />
<u:KeyGestureInput HorizontalAlignment="Left" />
<u:KeyGestureInput HorizontalAlignment="Left" Width="500" />
<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>
</UserControl>
</UserControl>

View File

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

View File

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

View File

@@ -72,32 +72,24 @@ public class KeyGestureInput: TemplatedControl
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;
}
Gesture = new KeyGesture(e.Key);
}
KeyGesture gesture;
if (e.KeyModifiers == KeyModifiers.Control && (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl))
switch (e.KeyModifiers)
{
gesture = new KeyGesture(e.Key);
}
else if (e.KeyModifiers == KeyModifiers.Alt && (e.Key == Key.LeftAlt || e.Key == Key.RightAlt))
{
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);
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);
break;
default:
gesture = new KeyGesture(e.Key, e.KeyModifiers);
break;
}
Gesture = gesture;
e.Handled = true;