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