HotKeyControl.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using System;
  5. using System.Windows.Input;
  6. namespace Ryujinx.Ava.UI.Helpers
  7. {
  8. public class HotKeyControl : ContentControl, ICommandSource
  9. {
  10. public static readonly StyledProperty<object> CommandParameterProperty =
  11. AvaloniaProperty.Register<HotKeyControl, object>(nameof(CommandParameter));
  12. public static readonly DirectProperty<HotKeyControl, ICommand> CommandProperty =
  13. AvaloniaProperty.RegisterDirect<HotKeyControl, ICommand>(nameof(Command),
  14. control => control.Command, (control, command) => control.Command = command, enableDataValidation: true);
  15. public static readonly StyledProperty<KeyGesture> HotKeyProperty = HotKeyManager.HotKeyProperty.AddOwner<Button>();
  16. private ICommand _command;
  17. private bool _commandCanExecute;
  18. public ICommand Command
  19. {
  20. get { return _command; }
  21. set { SetAndRaise(CommandProperty, ref _command, value); }
  22. }
  23. public KeyGesture HotKey
  24. {
  25. get { return GetValue(HotKeyProperty); }
  26. set { SetValue(HotKeyProperty, value); }
  27. }
  28. public object CommandParameter
  29. {
  30. get { return GetValue(CommandParameterProperty); }
  31. set { SetValue(CommandParameterProperty, value); }
  32. }
  33. public void CanExecuteChanged(object sender, EventArgs e)
  34. {
  35. var canExecute = Command == null || Command.CanExecute(CommandParameter);
  36. if (canExecute != _commandCanExecute)
  37. {
  38. _commandCanExecute = canExecute;
  39. UpdateIsEffectivelyEnabled();
  40. }
  41. }
  42. }
  43. }