using System.Windows.Input; using Titano.App.Infrastructure; using Titano.App.Sections; using Titano.Core.Localization; using Titano.Core.Model; using Titano.Ui.Controls; using Titano.Ui.Mvvm; namespace Titano.App.Jobs; /// A row action: label, tooltip and command (26-px row buttons, DESIGN §4 tables). public sealed record JobAction(string Text, string ToolTip, ICommand Command, bool IsDestructive = false); /// One row of the Jobs table (Jobs.html): kind, target, progress, speed, left, where, state, actions, log. public sealed class JobRowViewModel : ObservableObject { private readonly JobActions _actions; private JobEntry _entry; private int _queuePosition; private bool _isExpanded; private string? _logTail; internal JobRowViewModel(JobEntry entry, int queuePosition, JobActions actions) { _entry = entry; _queuePosition = queuePosition; _actions = actions; ToggleLogCommand = new RelayCommand(() => IsExpanded = !IsExpanded); } public JobEntry Entry => _entry; public Guid Id => _entry.Id; public Job Job => _entry.Job; public string Kind => JobText.Kind(_entry.Job.Kind); public string Target => _entry.Job.TargetLabel ?? string.Empty; /// Progress 0…1 (0 while unknown). public double Progress => _entry.State == JobState.Done ? 1 : _entry.Fraction ?? 0; /// "62 %", "—" while queued or unknown. public string PercentText => _entry.State == JobState.Done ? Formatting.Percent(1) : _entry.Fraction is { } f && _entry.State != JobState.Queued ? Formatting.Percent(f) : "—"; public string SpeedText => _entry.State is JobState.Running or JobState.Done ? Blank(JobText.Speed(_entry.Job.Kind, _entry.ItemsPerSecond)) : "—"; public string LeftText => Blank(JobText.Left(_entry, _queuePosition)); public string WhereText => _entry.State == JobState.Queued ? Loc.L("Jobs.Where.ThisPc") : Blank(JobText.Where(_entry.Job.NodeId, _actions.WorkerId)); public string StateText => JobText.State(_entry); /// Glyph and colour of the state (paired with the word, never colour alone). public StatusKind StateKind => _entry.State switch { JobState.Running => StatusKind.Running, JobState.Queued => StatusKind.Queued, JobState.Paused => StatusKind.Paused, JobState.Done => StatusKind.Done, JobState.Failed => StatusKind.Failed, _ => StatusKind.Offline, }; /// The error of a failed job, or the state explanation. public string StateToolTip => _entry.Job.Error is { Length: > 0 } error ? error : Loc.L("Tooltips.Jobs.State." + _entry.State); /// Muted text for rows that are not doing anything (queued, history), as in the mockup. public bool IsDimmed => _entry.State is JobState.Queued or JobState.Done or JobState.Cancelled; public bool IsFailed => _entry.State == JobState.Failed; public bool IsDone => _entry.State == JobState.Done; public IReadOnlyList Actions => _actions.For(_entry); public ICommand ToggleLogCommand { get; } public bool IsExpanded { get => _isExpanded; set { if (!Set(ref _isExpanded, value)) return; OnPropertyChanged(nameof(ToggleToolTip)); if (value) _ = _actions.LoadLogAsync(this); } } public string ToggleToolTip => Loc.L(_isExpanded ? "Tooltips.Jobs.HideLog" : "Tooltips.Jobs.ShowLog"); /// The last lines of the job log, plus the live message while it runs. public string LogText { get { var tail = _logTail ?? _entry.Job.LogTail ?? string.Empty; if (_entry.State == JobState.Running && _entry.Message is { Length: > 0 } message && !tail.EndsWith(message, StringComparison.Ordinal)) tail = tail.Length == 0 ? message : tail + "\n" + message; return tail.Length == 0 ? Loc.L("Jobs.Log.Empty") : tail; } } internal void SetLog(string? logTail) { _logTail = logTail; OnPropertyChanged(nameof(LogText)); } /// Refreshes the row from the model; returns true when it changed position-relevant data. internal void Update(JobEntry entry, int queuePosition) { var stateChanged = entry.State != _entry.State; _entry = entry; _queuePosition = queuePosition; if (stateChanged) { _logTail = null; OnAllPropertiesChanged(); if (_isExpanded) _ = _actions.LoadLogAsync(this); return; } OnPropertyChanged(nameof(Progress)); OnPropertyChanged(nameof(PercentText)); OnPropertyChanged(nameof(SpeedText)); OnPropertyChanged(nameof(LeftText)); OnPropertyChanged(nameof(WhereText)); OnPropertyChanged(nameof(StateText)); if (_isExpanded) OnPropertyChanged(nameof(LogText)); } internal void Refresh() => OnAllPropertiesChanged(); private static string Blank(string text) => text.Length == 0 ? "—" : text; }