teamsiso/src/TeamsISO.App/ViewModels/RelayCommand.cs
Zac Gaetano 9db0875f9e
Some checks failed
CI / build-and-test (push) Failing after 31s
Numpad 1-9 hotkeys toggle Nth participant's ISO
Fast keyboard-driven ISO routing for operators with one hand on the keyboard during a show. Both NumPad1..9 and top-row 1..9 bind to ToggleByIndexCommand which resolves against the filtered+sorted ParticipantsView — index matches what's on screen, not the underlying storage order.

Press a digit again to toggle off. Plays nice with sort modes: LoudestFirst means '1' is always whoever's loudest right now; Alphabetical lets you build muscle memory for recurring guests.

Implementation:

- New generic RelayCommand<T> in RelayCommand.cs so XAML CommandParameter strings convert to the action's T (int / string / etc.).

- ToggleByIndexCommand on MainViewModel iterates ParticipantsView, finds the Nth ParticipantViewModel, fires its ToggleIsoCommand if CanExecute.

- 18 KeyBindings (9 NumPad + 9 D1-D9) in MainWindow.xaml's Window.InputBindings.

- F1 help cheat sheet updated to mention the new range.
2026-05-10 21:26:37 -04:00

90 lines
2.8 KiB
C#

using System.Windows.Input;
namespace TeamsISO.App.ViewModels;
/// <summary>
/// Synchronous command that delegates execution to an <see cref="Action"/>.
/// </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>
/// Synchronous command that accepts a typed parameter. Used by hotkeys
/// that need to pass an index (e.g. NumPad1..NumPad9 → 1..9). The
/// parameter is converted from object via Convert.ChangeType so XAML
/// CommandParameter="1" works for int T.
/// </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 running.
/// </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);
}