TestCommand.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Windows.Input;
  3. namespace Avalonia.Controls.UnitTests.Utils;
  4. internal class TestCommand : ICommand
  5. {
  6. private readonly Func<object, bool> _canExecute;
  7. private readonly Action<object> _execute;
  8. private EventHandler _canExecuteChanged;
  9. private bool _enabled = true;
  10. public TestCommand(bool enabled = true)
  11. {
  12. _enabled = enabled;
  13. _canExecute = _ => _enabled;
  14. _execute = _ => { };
  15. }
  16. public TestCommand(Func<object, bool> canExecute, Action<object> execute = null)
  17. {
  18. _canExecute = canExecute;
  19. _execute = execute ?? (_ => { });
  20. }
  21. public bool IsEnabled
  22. {
  23. get { return _enabled; }
  24. set
  25. {
  26. if (_enabled != value)
  27. {
  28. _enabled = value;
  29. _canExecuteChanged?.Invoke(this, EventArgs.Empty);
  30. }
  31. }
  32. }
  33. public int SubscriptionCount { get; private set; }
  34. public event EventHandler CanExecuteChanged
  35. {
  36. add { _canExecuteChanged += value; ++SubscriptionCount; }
  37. remove { _canExecuteChanged -= value; --SubscriptionCount; }
  38. }
  39. public bool CanExecute(object parameter) => _canExecute(parameter);
  40. public void Execute(object parameter) => _execute(parameter);
  41. public void RaiseCanExecuteChanged() => _canExecuteChanged?.Invoke(this, EventArgs.Empty);
  42. }