// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using System.Windows.Input;
namespace CommunityToolkit.Mvvm.Input.Internals;
///
/// A implementation wrapping to support cancellation.
///
internal sealed partial class CancelCommand : ICommand
{
///
/// The wrapped instance.
///
private readonly IAsyncRelayCommand command;
///
/// Creates a new instance.
///
/// The instance to wrap.
public CancelCommand(IAsyncRelayCommand command)
{
this.command = command;
this.command.PropertyChanged += OnPropertyChanged;
}
///
public event EventHandler? CanExecuteChanged;
///
public bool CanExecute(object? parameter)
{
return this.command.CanBeCanceled;
}
///
public void Execute(object? parameter)
{
this.command.Cancel();
}
///
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is null or nameof(IAsyncRelayCommand.CanBeCanceled))
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
}