mirror of
https://github.com/SourMesen/Mesen2.git
synced 2025-04-02 10:21:44 -04:00
74 lines
1.8 KiB
C#
74 lines
1.8 KiB
C#
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Input;
|
|
using Avalonia.Markup.Xaml;
|
|
using Avalonia.Styling;
|
|
using Mesen.Config.Shortcuts;
|
|
using Mesen.Utilities;
|
|
using Mesen.Windows;
|
|
using System;
|
|
|
|
namespace Mesen.Controls
|
|
{
|
|
public class MultiKeyBindingButton : Button, IStyleable
|
|
{
|
|
Type IStyleable.StyleKey => typeof(Button);
|
|
|
|
public static readonly StyledProperty<KeyCombination> KeyBindingProperty = AvaloniaProperty.Register<KeyBindingButton, KeyCombination>(nameof(KeyBinding), new KeyCombination(), false, Avalonia.Data.BindingMode.TwoWay);
|
|
|
|
public KeyCombination KeyBinding
|
|
{
|
|
get { return GetValue(KeyBindingProperty); }
|
|
set { SetValue(KeyBindingProperty, value); }
|
|
}
|
|
|
|
static MultiKeyBindingButton()
|
|
{
|
|
KeyBindingProperty.Changed.AddClassHandler<MultiKeyBindingButton>((x, e) => {
|
|
x.SetKeyName();
|
|
});
|
|
}
|
|
|
|
public MultiKeyBindingButton()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void InitializeComponent()
|
|
{
|
|
AvaloniaXamlLoader.Load(this);
|
|
}
|
|
|
|
protected override void OnDataContextChanged(EventArgs e)
|
|
{
|
|
base.OnDataContextChanged(e);
|
|
SetKeyName();
|
|
}
|
|
|
|
protected override async void OnClick()
|
|
{
|
|
GetKeyWindow wnd = new GetKeyWindow(true);
|
|
wnd.SingleKeyMode = false;
|
|
wnd.WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
|
await wnd.ShowCenteredDialog(VisualRoot);
|
|
this.KeyBinding = wnd.ShortcutKey;
|
|
}
|
|
|
|
protected override void OnPointerReleased(PointerReleasedEventArgs e)
|
|
{
|
|
base.OnPointerReleased(e);
|
|
|
|
//Allow using right mouse button to clear bindings
|
|
if(e.InitialPressMouseButton == MouseButton.Right) {
|
|
this.KeyBinding = new KeyCombination();
|
|
}
|
|
}
|
|
|
|
private void SetKeyName()
|
|
{
|
|
string keyname = KeyBinding.ToString();
|
|
this.Content = keyname;
|
|
ToolTip.SetTip(this, string.IsNullOrWhiteSpace(keyname) ? null : keyname);
|
|
}
|
|
}
|
|
}
|