94 lines
2.5 KiB
C#
94 lines
2.5 KiB
C#
using System.Windows.Input;
|
|
|
|
namespace AcDream.Launcher.ViewModels;
|
|
|
|
public sealed class RelayCommand : ICommand
|
|
{
|
|
private readonly Action<object?> _execute;
|
|
private readonly Func<object?, bool>? _canExecute;
|
|
|
|
public RelayCommand(Action execute, Func<bool>? canExecute = null)
|
|
: this(
|
|
_ => execute(),
|
|
canExecute is null ? null : _ => canExecute())
|
|
{
|
|
ArgumentNullException.ThrowIfNull(execute);
|
|
}
|
|
|
|
public RelayCommand(
|
|
Action<object?> execute,
|
|
Func<object?, bool>? canExecute = null)
|
|
{
|
|
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
|
_canExecute = canExecute;
|
|
}
|
|
|
|
public event EventHandler? CanExecuteChanged;
|
|
|
|
public bool CanExecute(object? parameter) => _canExecute?.Invoke(parameter) ?? true;
|
|
|
|
public void Execute(object? parameter)
|
|
{
|
|
if (CanExecute(parameter))
|
|
{
|
|
_execute(parameter);
|
|
}
|
|
}
|
|
|
|
public void NotifyCanExecuteChanged() =>
|
|
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
public sealed class AsyncRelayCommand : ICommand
|
|
{
|
|
private readonly Func<object?, Task> _execute;
|
|
private readonly Func<object?, bool>? _canExecute;
|
|
private bool _isExecuting;
|
|
|
|
public AsyncRelayCommand(Func<Task> execute, Func<bool>? canExecute = null)
|
|
: this(
|
|
_ => execute(),
|
|
canExecute is null ? null : _ => canExecute())
|
|
{
|
|
ArgumentNullException.ThrowIfNull(execute);
|
|
}
|
|
|
|
public AsyncRelayCommand(
|
|
Func<object?, Task> execute,
|
|
Func<object?, bool>? canExecute = null)
|
|
{
|
|
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
|
_canExecute = canExecute;
|
|
}
|
|
|
|
public event EventHandler? CanExecuteChanged;
|
|
|
|
public bool CanExecute(object? parameter) =>
|
|
!_isExecuting && (_canExecute?.Invoke(parameter) ?? true);
|
|
|
|
public async void Execute(object? parameter) =>
|
|
await ExecuteAsync(parameter).ConfigureAwait(true);
|
|
|
|
public async Task ExecuteAsync(object? parameter = null)
|
|
{
|
|
if (!CanExecute(parameter))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_isExecuting = true;
|
|
NotifyCanExecuteChanged();
|
|
try
|
|
{
|
|
await _execute(parameter).ConfigureAwait(true);
|
|
}
|
|
finally
|
|
{
|
|
_isExecuting = false;
|
|
NotifyCanExecuteChanged();
|
|
}
|
|
}
|
|
|
|
public void NotifyCanExecuteChanged() =>
|
|
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|