using Chronos.Core.Localization;
using Chronos.Core.Packaging;
using Chronos.Setup.Engine;
using Chronos.Ui.Mvvm;
namespace Chronos.Setup.Views;
///
/// A component checkbox: localized name and tooltip (falling back to the manifest), its state (size, "Installed ·
/// size", or "No longer offered" for a retained one) and selection. Unticking an installed component removes it;
/// unticking a retained one (Chronos Server 1.0 for Windows) removes it with its service. A component that installs for
/// all users only says so in its tooltip, and is unavailable when an installation that is not an all-users one inside
/// Program Files is upgraded.
///
internal sealed class ComponentOption : LocalizedViewModel
{
private readonly PayloadComponent? _component;
private readonly string? _unavailableToolTip;
private readonly Action _changed;
private bool _selected;
/// The version installed now; null when it is not installed.
/// The tooltip key saying why it cannot be added here; null when it can.
public ComponentOption(PayloadComponent component, bool selected, string? installedVersion, string? unavailableToolTip, Action changed)
{
_component = component;
_selected = selected;
InstalledVersion = installedVersion;
_unavailableToolTip = unavailableToolTip;
_changed = changed;
Id = component.Id;
}
/// A retained component: ticked keeps it as it is, unticked removes it with its service.
public ComponentOption(RetainedComponent retained, bool selected, Action changed)
{
Retained = retained;
_selected = selected;
InstalledVersion = retained.Version;
_changed = changed;
Id = retained.Id;
}
public string Id { get; }
/// The retained component this row keeps or removes; null for a component of the payload.
public RetainedComponent? Retained { get; }
public bool IsRetained => Retained is not null;
/// The version installed now; null when it is not installed.
public string? InstalledVersion { get; }
public bool IsInstalled => InstalledVersion is not null;
/// The component's name; a retained one carries its version ("Chronos Server 1.0.0").
public string Name => Retained is { } retained ? Loc.L("Setup.Component.Versioned", BaseName, retained.Version) : BaseName;
private string BaseName => Loc.Get("Setup.Component." + Id, Loc.Culture) ?? _component?.DisplayName ?? Id;
/// Installs for all users only (a program a LocalSystem service runs from Program Files).
public bool AllUsersOnly => _component?.AllUsersOnly == true;
/// False when it cannot be installed here: it needs all users in Program Files and this installation is not.
public bool IsAvailable => _unavailableToolTip is null;
public string ToolTip => _unavailableToolTip is { } reason ? Loc.L(reason)
: Retained is { } retained ? Loc.L("Tooltips.Setup.Component.Retained", retained.Version)
: AllUsersOnly ? Loc.L("Tooltips.Setup.AllUsersOnly", Description)
: InstalledVersion is { } version ? Loc.L("Tooltips.Setup.Component.InstalledVersion", Description, version)
: Description;
private string Description => Loc.Get("Tooltips.Setup.Component." + Id, Loc.Culture) ?? Loc.L("Tooltips.Setup.Component");
/// The right-hand text of the row: the size, "Installed · size", or "No longer offered".
public string StateText => IsRetained ? Loc.L("Setup.Component.NoLongerOffered")
: IsInstalled ? Loc.L("Setup.Component.Installed", SizeText)
: SizeText;
public string SizeText => ByteSize.Format(_component?.EstimatedSize ?? 0);
public bool IsSelected
{
get => _selected;
set
{
if (Set(ref _selected, value)) _changed();
}
}
}
/// Language offered in the picker.
internal sealed record LanguageOption(string Code, string Name);
/// Decimal byte sizes as Explorer shows them for drives ("1.5 MB"), units in the UI language ("1,5 Mo").
internal static class ByteSize
{
private static readonly string[] Formats = ["Common.Size.B", "Common.Size.KB", "Common.Size.MB", "Common.Size.GB", "Common.Size.TB"];
public static string Format(long bytes)
{
double value = bytes;
var unit = 0;
while (value >= 1000 && unit < Formats.Length - 1)
{
value /= 1000;
unit++;
}
return Loc.L(Formats[unit], value.ToString(unit == 0 || value >= 100 ? "0" : "0.#", Loc.Culture));
}
}