Files
Chronos/src/Titano.Ui/Mvvm/RelayCommand.cs
T
Claude c40b6a7cd3 feat: Phase 0 shells, design system, server skeleton, Docker and CI
- Titano.Ui: DESIGN.md tokens (dark/light), generated 16-px icon set,
  styles for all standard controls, custom chrome window with snap
  layouts, rail, job bar, Auto/manual slider, chips, toasts.
- Titano desktop shell with sections, Settings (language, theme),
  About and a --capture screenshot mode.
- Titano Server skeleton: env/server.json configuration, self-signed
  TLS with fingerprint, /health, /server/info, i18n endpoint and the
  embedded admin panel; in-process server tests.
- Docker image (multi-arch, PUID/PGID, health check), compose file,
  Unraid template; CI, release and nightly workflows; changelog tool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:35:03 +00:00

71 lines
2.2 KiB
C#

using System.Windows.Input;
namespace Titano.Ui.Mvvm;
/// <summary>Synchronous command with optional can-execute predicate.</summary>
public sealed class RelayCommand(Action<object?> execute, Func<object?, bool>? canExecute = null) : ICommand
{
public RelayCommand(Action execute, Func<bool>? canExecute = null)
: this(_ => execute(), canExecute is null ? null : _ => canExecute())
{
}
public event EventHandler? CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public bool CanExecute(object? parameter) => canExecute?.Invoke(parameter) ?? true;
public void Execute(object? parameter) => execute(parameter);
}
/// <summary>Asynchronous command that disables itself while running and reports failures through <see cref="Failed"/>.</summary>
public sealed class AsyncRelayCommand(Func<object?, CancellationToken, Task> execute, Func<object?, bool>? canExecute = null) : ICommand
{
private CancellationTokenSource? _cts;
public AsyncRelayCommand(Func<Task> execute, Func<bool>? canExecute = null)
: this((_, _) => execute(), canExecute is null ? null : _ => canExecute())
{
}
public event EventHandler? CanExecuteChanged;
/// <summary>Raised on the UI thread when the command throws (other than cancellation).</summary>
public static event Action<Exception>? Failed;
public bool IsRunning => _cts is not null;
public bool CanExecute(object? parameter) => !IsRunning && (canExecute?.Invoke(parameter) ?? true);
public async void Execute(object? parameter)
{
if (!CanExecute(parameter)) return;
_cts = new CancellationTokenSource();
RaiseCanExecuteChanged();
try
{
await execute(parameter, _cts.Token);
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
Failed?.Invoke(ex);
}
finally
{
_cts.Dispose();
_cts = null;
RaiseCanExecuteChanged();
}
}
public void Cancel() => _cts?.Cancel();
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}