| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="RelayCommand.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Reactive.Concurrency; |
| | | 9 | | using System.Windows.Input; |
| | | 10 | | using MyNet.UI.Threading; |
| | | 11 | | |
| | | 12 | | namespace MyNet.UI.Commands; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// A command that relays its functionality to other objects by invoking delegates. The default return value for the Can |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="execute">The action to execute.</param> |
| | | 18 | | /// <param name="canExecute">A function that determines whether the command can execute.</param> |
| | | 19 | | /// <param name="schedulerProvider">Optional scheduler provider used to raise CanExecuteChanged on the UI scheduler.</pa |
| | | 20 | | public class RelayCommand(Action execute, Func<bool>? canExecute = null, ISchedulerProvider? schedulerProvider = null) : |
| | | 21 | | { |
| | 786 | 22 | | private readonly Action _execute = execute ?? throw new ArgumentNullException(nameof(execute)); |
| | 786 | 23 | | private readonly IScheduler _uiScheduler = schedulerProvider?.Ui ?? CurrentThreadScheduler.Instance; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the function that determines whether the command can execute. If null, the command can always execute. |
| | | 27 | | /// </summary> |
| | | 28 | | public virtual event EventHandler? CanExecuteChanged; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Determines whether the command can execute in its current state. If the canExecute function is null, this method |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="parameter">The parameter to pass to the canExecute function.</param> |
| | | 34 | | /// <returns>True if the command can execute; otherwise, false.</returns> |
| | 72 | 35 | | public virtual bool CanExecute(object? parameter) => canExecute?.Invoke() ?? true; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Executes the command. If the command cannot execute, this method does nothing. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="parameter">The parameter to pass to the execute action.</param> |
| | | 41 | | public virtual void Execute(object? parameter) |
| | | 42 | | { |
| | 51 | 43 | | if (!CanExecute(parameter)) |
| | 3 | 44 | | return; |
| | | 45 | | |
| | 48 | 46 | | _execute(); |
| | 48 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Raises the CanExecuteChanged event to indicate that the return value of the CanExecute method has changed. This |
| | | 51 | | /// </summary> |
| | 522 | 52 | | public virtual void RaiseCanExecuteChanged() => _uiScheduler.Schedule(() => CanExecuteChanged?.Invoke(this, EventArg |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// A command that relays its functionality to other objects by invoking delegates. The default return value for the Can |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="execute">The action to execute.</param> |
| | | 59 | | /// <param name="canExecute">A function that determines whether the command can execute.</param> |
| | | 60 | | /// <param name="schedulerProvider">Optional scheduler provider used to raise CanExecuteChanged on the UI scheduler.</pa |
| | | 61 | | /// <typeparam name="T">The type of the parameter.</typeparam> |
| | | 62 | | public class RelayCommand<T>(Action<T?> execute, Func<T?, bool>? canExecute = null, ISchedulerProvider? schedulerProvide |
| | | 63 | | { |
| | | 64 | | private readonly Action<T?> _execute = execute ?? throw new ArgumentNullException(nameof(execute)); |
| | | 65 | | private readonly IScheduler _uiScheduler = schedulerProvider?.Ui ?? CurrentThreadScheduler.Instance; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the function that determines whether the command can execute. If null, the command can always execute. |
| | | 69 | | /// </summary> |
| | | 70 | | public event EventHandler? CanExecuteChanged; |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Determines whether the command can execute in its current state. If the canExecute function is null, this method |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="parameter">The parameter to pass to the canExecute function.</param> |
| | | 76 | | /// <returns>True if the command can execute; otherwise, false.</returns> |
| | | 77 | | public bool CanExecute(object? parameter) => parameter is T or null && (canExecute?.Invoke((T?)parameter) ?? true); |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Executes the command. If the command cannot execute, this method does nothing. The command can execute if the pa |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="parameter">The parameter to pass to the execute action.</param> |
| | | 83 | | public void Execute(object? parameter) |
| | | 84 | | { |
| | | 85 | | if (!CanExecute(parameter)) |
| | | 86 | | return; |
| | | 87 | | |
| | | 88 | | _execute((T?)parameter); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Raises the CanExecuteChanged event to indicate that the return value of the CanExecute method has changed. This |
| | | 93 | | /// </summary> |
| | | 94 | | public virtual void RaiseCanExecuteChanged() => _uiScheduler.Schedule(() => CanExecuteChanged?.Invoke(this, EventArg |
| | | 95 | | } |
| | | 96 | | |