feat: initialize control.
This commit is contained in:
26
src/Ursa.Themes.Semi/Controls/KeyGestureInput.axaml
Normal file
26
src/Ursa.Themes.Semi/Controls/KeyGestureInput.axaml
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<ResourceDictionary
|
||||||
|
xmlns="https://github.com/avaloniaui"
|
||||||
|
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="Template">
|
||||||
|
<ControlTemplate TargetType="u:KeyGestureInput">
|
||||||
|
<Border
|
||||||
|
Name="Background"
|
||||||
|
Width="100"
|
||||||
|
MinHeight="32"
|
||||||
|
Background="LightBlue"
|
||||||
|
BorderThickness="1">
|
||||||
|
<TextBlock Text="{TemplateBinding Gesture, Converter={StaticResource KeyGestureConverter}}" />
|
||||||
|
</Border>
|
||||||
|
</ControlTemplate>
|
||||||
|
</Setter>
|
||||||
|
<Style Selector="^:focus-within /template/ Border#Background">
|
||||||
|
<Setter Property="BorderBrush" Value="Blue" />
|
||||||
|
</Style>
|
||||||
|
</ControlTheme>
|
||||||
|
</ResourceDictionary>
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
<ResourceInclude Source="ButtonGroup.axaml" />
|
<ResourceInclude Source="ButtonGroup.axaml" />
|
||||||
<ResourceInclude Source="Divider.axaml" />
|
<ResourceInclude Source="Divider.axaml" />
|
||||||
<ResourceInclude Source="IPv4Box.axaml" />
|
<ResourceInclude Source="IPv4Box.axaml" />
|
||||||
|
<ResourceInclude Source="KeyGestureInput.axaml" />
|
||||||
<ResourceInclude Source="Loading.axaml" />
|
<ResourceInclude Source="Loading.axaml" />
|
||||||
<ResourceInclude Source="Navigation.axaml" />
|
<ResourceInclude Source="Navigation.axaml" />
|
||||||
<ResourceInclude Source="Pagination.axaml" />
|
<ResourceInclude Source="Pagination.axaml" />
|
||||||
|
|||||||
83
src/Ursa/Controls/KeyGestureInput.cs
Normal file
83
src/Ursa/Controls/KeyGestureInput.cs
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Controls.Converters;
|
||||||
|
using Avalonia.Controls.Primitives;
|
||||||
|
using Avalonia.Input;
|
||||||
|
|
||||||
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
|
public class KeyGestureInput: TemplatedControl
|
||||||
|
{
|
||||||
|
static KeyGestureInput()
|
||||||
|
{
|
||||||
|
InputElement.FocusableProperty.OverrideDefaultValue<KeyGestureInput>(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly StyledProperty<KeyGesture> GestureProperty = AvaloniaProperty.Register<KeyGestureInput, KeyGesture>(
|
||||||
|
nameof(Gesture));
|
||||||
|
|
||||||
|
public KeyGesture Gesture
|
||||||
|
{
|
||||||
|
get => GetValue(GestureProperty);
|
||||||
|
set => SetValue(GestureProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly StyledProperty<IList<Key>> AcceptableKeysProperty = AvaloniaProperty.Register<KeyGestureInput, IList<Key>>(
|
||||||
|
nameof(AcceptableKeys));
|
||||||
|
|
||||||
|
public IList<Key> AcceptableKeys
|
||||||
|
{
|
||||||
|
get => GetValue(AcceptableKeysProperty);
|
||||||
|
set => SetValue(AcceptableKeysProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly StyledProperty<bool> ConsiderKeyModifiersProperty = AvaloniaProperty.Register<KeyGestureInput, bool>(
|
||||||
|
nameof(ConsiderKeyModifiers));
|
||||||
|
|
||||||
|
public bool ConsiderKeyModifiers
|
||||||
|
{
|
||||||
|
get => GetValue(ConsiderKeyModifiersProperty);
|
||||||
|
set => SetValue(ConsiderKeyModifiersProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnKeyDown(KeyEventArgs e)
|
||||||
|
{
|
||||||
|
// base.OnKeyDown(e);
|
||||||
|
if (!AcceptableKeys.Contains(e.Key))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Gesture = new KeyGesture(e.Key);
|
||||||
|
}
|
||||||
|
KeyGesture gesture;
|
||||||
|
if (e.KeyModifiers == KeyModifiers.Control && (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl))
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
Gesture = gesture;
|
||||||
|
e.Handled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user