mirror of
https://github.com/ApfelTeeSaft/NESDecompiler.git
synced 2026-08-26 19:33:30 +00:00
150 lines
5.0 KiB
C#
150 lines
5.0 KiB
C#
using System;
|
|
using System.Windows.Input;
|
|
|
|
namespace NESDecompiler.GUI.Commands
|
|
{
|
|
/// <summary>
|
|
/// A command whose sole purpose is to relay its functionality to other
|
|
/// objects by invoking delegates.
|
|
/// </summary>
|
|
public class RelayCommand : ICommand
|
|
{
|
|
private readonly Action execute;
|
|
private readonly Func<bool>? canExecute;
|
|
|
|
/// <summary>
|
|
/// Creates a new command that can always execute.
|
|
/// </summary>
|
|
/// <param name="execute">The execution logic.</param>
|
|
public RelayCommand(Action execute)
|
|
: this(execute, null)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new command.
|
|
/// </summary>
|
|
/// <param name="execute">The execution logic.</param>
|
|
/// <param name="canExecute">The execution status logic.</param>
|
|
public RelayCommand(Action execute, Func<bool>? canExecute)
|
|
{
|
|
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
|
this.canExecute = canExecute;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether this command can execute in its current state.
|
|
/// </summary>
|
|
/// <param name="parameter">
|
|
/// Data used by the command. If the command does not require data to be passed,
|
|
/// this object can be set to null.
|
|
/// </param>
|
|
/// <returns>true if this command can be executed; otherwise, false.</returns>
|
|
public bool CanExecute(object? parameter)
|
|
{
|
|
return canExecute == null || canExecute();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes the command on the current command target.
|
|
/// </summary>
|
|
/// <param name="parameter">
|
|
/// Data used by the command. If the command does not require data to be passed,
|
|
/// this object can be set to null.
|
|
/// </param>
|
|
public void Execute(object? parameter)
|
|
{
|
|
execute();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Occurs when changes occur that affect whether or not the command should execute.
|
|
/// </summary>
|
|
public event EventHandler? CanExecuteChanged
|
|
{
|
|
add { CommandManager.RequerySuggested += value; }
|
|
remove { CommandManager.RequerySuggested -= value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the CanExecuteChanged event.
|
|
/// </summary>
|
|
public void RaiseCanExecuteChanged()
|
|
{
|
|
CommandManager.InvalidateRequerySuggested();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A generic command whose sole purpose is to relay its functionality to other
|
|
/// objects by invoking delegates.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of parameter that this command expects.</typeparam>
|
|
public class RelayCommand<T> : ICommand
|
|
{
|
|
private readonly Action<T?> execute;
|
|
private readonly Predicate<T?>? canExecute;
|
|
|
|
/// <summary>
|
|
/// Creates a new command that can always execute.
|
|
/// </summary>
|
|
/// <param name="execute">The execution logic.</param>
|
|
public RelayCommand(Action<T?> execute)
|
|
: this(execute, null)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new command.
|
|
/// </summary>
|
|
/// <param name="execute">The execution logic.</param>
|
|
/// <param name="canExecute">The execution status logic.</param>
|
|
public RelayCommand(Action<T?> execute, Predicate<T?>? canExecute)
|
|
{
|
|
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
|
this.canExecute = canExecute;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether this command can execute in its current state.
|
|
/// </summary>
|
|
/// <param name="parameter">
|
|
/// Data used by the command. If the command does not require data to be passed,
|
|
/// this object can be set to null.
|
|
/// </param>
|
|
/// <returns>true if this command can be executed; otherwise, false.</returns>
|
|
public bool CanExecute(object? parameter)
|
|
{
|
|
return canExecute == null || canExecute((T?)parameter);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes the command on the current command target.
|
|
/// </summary>
|
|
/// <param name="parameter">
|
|
/// Data used by the command. If the command does not require data to be passed,
|
|
/// this object can be set to null.
|
|
/// </param>
|
|
public void Execute(object? parameter)
|
|
{
|
|
execute((T?)parameter);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Occurs when changes occur that affect whether or not the command should execute.
|
|
/// </summary>
|
|
public event EventHandler? CanExecuteChanged
|
|
{
|
|
add { CommandManager.RequerySuggested += value; }
|
|
remove { CommandManager.RequerySuggested -= value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raises the CanExecuteChanged event.
|
|
/// </summary>
|
|
public void RaiseCanExecuteChanged()
|
|
{
|
|
CommandManager.InvalidateRequerySuggested();
|
|
}
|
|
}
|
|
} |