87 lines
2.8 KiB
C#
87 lines
2.8 KiB
C#
|
|
using System;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
using System.Windows.Input;
|
||
|
|
|
||
|
|
namespace TeamsISO.App.WinUI.ViewModels;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Synchronous command — same shape as the WPF host's RelayCommand.
|
||
|
|
/// System.Windows.Input.ICommand is the shared base type across both
|
||
|
|
/// hosts (lives in System.ObjectModel.dll on .NET 8), so no rewrite
|
||
|
|
/// is needed beyond the namespace.
|
||
|
|
/// </summary>
|
||
|
|
public sealed class RelayCommand : ICommand
|
||
|
|
{
|
||
|
|
private readonly Action _execute;
|
||
|
|
private readonly Func<bool>? _canExecute;
|
||
|
|
|
||
|
|
public RelayCommand(Action execute, Func<bool>? canExecute = null)
|
||
|
|
{
|
||
|
|
_execute = execute;
|
||
|
|
_canExecute = canExecute;
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool CanExecute(object? parameter) => _canExecute?.Invoke() ?? true;
|
||
|
|
public void Execute(object? parameter) => _execute();
|
||
|
|
public event EventHandler? CanExecuteChanged;
|
||
|
|
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Typed-parameter variant for hotkey-driven commands (NumPad 1-9).</summary>
|
||
|
|
public sealed class RelayCommand<T> : ICommand
|
||
|
|
{
|
||
|
|
private readonly Action<T> _execute;
|
||
|
|
private readonly Func<T, bool>? _canExecute;
|
||
|
|
|
||
|
|
public RelayCommand(Action<T> execute, Func<T, bool>? canExecute = null)
|
||
|
|
{
|
||
|
|
_execute = execute;
|
||
|
|
_canExecute = canExecute;
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool CanExecute(object? parameter) => _canExecute?.Invoke(Convert<T>(parameter)) ?? true;
|
||
|
|
public void Execute(object? parameter) => _execute(Convert<T>(parameter));
|
||
|
|
public event EventHandler? CanExecuteChanged;
|
||
|
|
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
||
|
|
|
||
|
|
private static TValue Convert<TValue>(object? value)
|
||
|
|
{
|
||
|
|
if (value is null) return default!;
|
||
|
|
if (value is TValue typed) return typed;
|
||
|
|
try { return (TValue)System.Convert.ChangeType(value, typeof(TValue)); }
|
||
|
|
catch { return default!; }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Async command that suppresses re-entrancy while in flight.</summary>
|
||
|
|
public sealed class AsyncRelayCommand : ICommand
|
||
|
|
{
|
||
|
|
private readonly Func<Task> _execute;
|
||
|
|
private readonly Func<bool>? _canExecute;
|
||
|
|
private bool _isRunning;
|
||
|
|
|
||
|
|
public AsyncRelayCommand(Func<Task> execute, Func<bool>? canExecute = null)
|
||
|
|
{
|
||
|
|
_execute = execute;
|
||
|
|
_canExecute = canExecute;
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool CanExecute(object? parameter) => !_isRunning && (_canExecute?.Invoke() ?? true);
|
||
|
|
|
||
|
|
public async void Execute(object? parameter)
|
||
|
|
{
|
||
|
|
if (_isRunning) return;
|
||
|
|
_isRunning = true;
|
||
|
|
RaiseCanExecuteChanged();
|
||
|
|
try { await _execute(); }
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
_isRunning = false;
|
||
|
|
RaiseCanExecuteChanged();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public event EventHandler? CanExecuteChanged;
|
||
|
|
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
||
|
|
}
|