using System.Windows.Input;
using Titano.App.Infrastructure;
using Titano.Core.Localization;
using Titano.Ui.Controls;
using Titano.Ui.Mvvm;
namespace Titano.App.Settings;
/// Sections of the Settings screen (BRIEF §12.8).
public enum SettingsPage
{
General,
Archive,
Rendering,
Storage,
Server,
Updates,
Advanced,
}
/// An entry of the section list: name, tooltip (what is inside) and the words the search matches.
public sealed class SettingsPageItem(SettingsPage page, string[] keywordKeys) : ObservableObject
{
public SettingsPage Page { get; } = page;
public string Label => Loc.L("Settings.Page." + Page);
public string ToolTip => Loc.L("Tooltips.Settings.Page." + Page);
/// True when appears in the page name, its tooltip or one of its labels.
public bool Matches(string text) =>
Label.Contains(text, StringComparison.CurrentCultureIgnoreCase)
|| ToolTip.Contains(text, StringComparison.CurrentCultureIgnoreCase)
|| keywordKeys.Any(k => Loc.L(k).Contains(text, StringComparison.CurrentCultureIgnoreCase));
internal void Refresh() => OnAllPropertiesChanged();
}
/// A choice of a settings dropdown.
public sealed record SettingsOption(T Value, string Label)
{
public override string ToString() => Label;
}
/// One row of the encoder table with its self-test state.
public sealed class EncoderRowViewModel(EncoderRow row) : ObservableObject
{
private SelfTestResult? _result;
private bool _isTesting;
public EncoderRow Row { get; } = row;
public string Codec => Row.Codec;
/// "NVIDIA NVENC (hardware)".
public string Name => Row.Source switch
{
EncoderSource.MediaFoundation => Loc.L(Row.Hardware ? "Settings.Rendering.Encoder.Hardware" : "Settings.Rendering.Encoder.Software", Row.DisplayName),
_ => Loc.L("Settings.Rendering.Encoder.BuiltIn", Row.DisplayName),
};
/// "Main10", "10-bit" or "—".
public string TenBitText => !Row.TenBit ? "—" : Row.VideoCodec == Core.Video.VideoCodec.Hevc ? "Main10" : Loc.L("Settings.Rendering.TenBit");
public bool HasTenBit => Row.TenBit;
/// "8192×8192" (width first in every language) or "no limit".
public string MaxText => Row.MaxWidth > 0 ? Formatting.LeftToRight($"{Row.MaxWidth}×{Row.MaxHeight}") : Loc.L("Settings.Rendering.NoLimit");
/// The built-in presets this encoder serves (Settings.html "Used for").
public string UsedForText => EncoderUse.Text(Row);
public bool IsTesting
{
get => _isTesting;
set
{
if (Set(ref _isTesting, value)) RaiseResult();
}
}
public SelfTestResult? Result
{
get => _result;
set
{
if (Set(ref _result, value)) RaiseResult();
}
}
public bool HasResult => _result is not null || _isTesting;
public StatusKind ResultKind => _isTesting ? StatusKind.Running : _result is { Passed: true } ? StatusKind.Done : StatusKind.Failed;
/// "142 fps · ok", "failed", "testing".
public string ResultText => _isTesting ? Loc.L("Settings.Rendering.Testing")
: _result is not { } r ? "—"
: r.Passed ? Loc.L("Settings.Rendering.Passed", Formatting.Rate(r.FramesPerSecond))
: Loc.L("Settings.Rendering.Failed");
public string ResultToolTip => _result is { Passed: false, Error: { } error } ? error : Loc.L("Tooltips.Settings.SelfTestResult");
/// What a screen reader announces when the self-test of this row moves on: "HEVC NVIDIA NVENC (hardware): 142 fps · ok".
public string ResultAnnouncement => Loc.L("Settings.Rendering.ResultFor", Codec, Name, ResultText);
internal void Refresh() => OnAllPropertiesChanged();
private void RaiseResult()
{
OnPropertyChanged(nameof(HasResult));
OnPropertyChanged(nameof(ResultKind));
OnPropertyChanged(nameof(ResultText));
OnPropertyChanged(nameof(ResultToolTip));
OnPropertyChanged(nameof(ResultAnnouncement));
}
}
/// A trusted server certificate in Settings › Server.
public sealed class TrustedCertificateRow(TrustedCertificate certificate, ICommand forget)
{
public TrustedCertificate Certificate { get; } = certificate;
public string Server => Certificate.Server ?? "—";
public string Fingerprint => Titano.App.Server.ServerSession.ShortFingerprint(Certificate.Fingerprint);
public string FullFingerprint => Certificate.Fingerprint;
public string TrustedText => Formatting.Date(Certificate.TrustedAt);
public ICommand ForgetCommand { get; } = forget;
/// "Forget: https://nas.local:8443": the button names its server for screen readers.
public string ForgetName => Loc.L("Settings.Server.ForgetName", Server);
}