using System.Reactive.Disposables;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace Avalonia.Xaml.Interactions.Custom;
///
///
///
public class ExecuteCommandOnKeyUpBehavior : ExecuteCommandOnKeyBehaviorBase
{
///
///
///
///
protected override void OnAttachedToVisualTree(CompositeDisposable disposable)
{
var control = SourceControl ?? AssociatedObject;
var dispose = control?
.AddDisposableHandler(
InputElement.KeyUpEvent,
OnKeyUp,
EventRoutingStrategy);
if (dispose is not null)
{
disposable.Add(dispose);
}
}
private void OnKeyUp(object? sender, KeyEventArgs e)
{
var haveKey = Key is not null && e.Key == Key;
var haveGesture = Gesture is not null && Gesture.Matches(e);
if (!haveKey && !haveGesture)
{
return;
}
if (e.Handled)
{
return;
}
if (ExecuteCommand())
{
e.Handled = MarkAsHandled;
}
}
}