Files
Chronos/src/Titano.App/Server/SyncText.cs
T
Claude d80cb5f8a7 fix(app): Server, Jobs, Settings, About and Welcome after the accessibility and RTL review
- Keyboard: Tab follows the visual order (toolbars, section list, dialog
  buttons), reaches every Jobs row button, stays inside the Welcome dialog
  (which opens focused); the Settings section list shows a focus ring.
- Screen readers: sync and job rows read as their sequence or job with the
  state as item status; row buttons name their row; filter chips are radio
  buttons; the connection state, errors, sync retries, update checks and
  self-test results are announced; Auto values are text, not dead buttons.
- Right to left: paths, addresses, templates, fingerprints, the job log, sizes
  and the logo stay left to right; numbers stay right-aligned.
- Layout: Jobs and sync headers line up with their rows (shared padding,
  shared actions column, constant scroll-bar gutter); toolbars wrap before
  anything is cut; Settings labels share a column as wide as the longest one;
  path rows give way instead of wrapping; conflict rows use the warning tint.
- Behaviour: Settings reloads the archive folder and default server set
  elsewhere; forgetting a certificate in Settings asks first and disconnects
  the session on it; transfers count in the Local only and Remote chips; the
  Workers readout counts this PC's workers; badge, status strip and Running
  chip agree; the admin link shows for administrators only; Reset section is
  gone from Advanced; empty states carry one action; shorter labels; the
  encoders a render node offers and the presets each encoder serves.

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

297 lines
15 KiB
C#

using System.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;
using Titano.App.Infrastructure;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Sync;
using Titano.Ui.Controls;
namespace Titano.App.Server;
/// <summary>Filter chips of the sync table (Server.html).</summary>
public enum SyncFilter
{
All,
LocalOnly,
Remote,
Conflicts,
}
/// <summary>What a row of the sync table offers.</summary>
public enum SyncAction
{
Upload,
OpenWithProxies,
DownloadOriginals,
SyncSettings,
KeepMine,
TakeServer,
SaveBoth,
GetExports,
SendExports,
Pause,
Resume,
}
/// <summary>What the signed-in account may do (server permission bits).</summary>
/// <param name="CanUpload">Send originals, settings and renders (upload and edit rights).</param>
/// <param name="CanFetchOriginals">Download originals (editors and render nodes).</param>
/// <param name="CanEdit">Change the server's copy of a sequence's settings, title, tags, rating and notes.</param>
public readonly record struct SyncRights(bool CanUpload, bool CanFetchOriginals, bool CanEdit)
{
public static SyncRights Of(long permissions) => new(
(permissions & (long)Permissions.Upload) != 0 && (permissions & (long)Permissions.Edit) != 0,
(permissions & (long)(Permissions.Edit | Permissions.RenderNode)) != 0,
(permissions & (long)Permissions.Edit) != 0);
}
/// <summary>The words, glyphs and actions of the sync table (BRIEF §12.7): plain functions of a <see cref="SyncEntry"/>.</summary>
public static class SyncText
{
/// <summary>Resource keys of the settings and sequence fields a conflict can show, by JSON path (sections, then members).</summary>
private static readonly Dictionary<string, string> SettingKeys = new(StringComparer.OrdinalIgnoreCase)
{
["sequence.title"] = "Server.Setting.Title",
["sequence.tags"] = "Server.Setting.Tags",
["sequence.rating"] = "Server.Setting.Rating",
["sequence.notes"] = "Server.Setting.Notes",
["timing.mode"] = "Server.Setting.TimingMode",
["timing.frameRate"] = "Server.Setting.FrameRate",
["timing.fitToSeconds"] = "Server.Setting.FitToDuration",
["timing.remapCurve"] = "Server.Setting.TimeRemap",
["exposure.deflicker"] = "Server.Setting.Deflicker",
["exposure.deflickerWindow"] = "Server.Setting.DeflickerWindow",
["exposure.deflickerStrength"] = "Server.Setting.DeflickerStrength",
["exposure.regions"] = "Server.Setting.Regions",
["exposure.holyGrail"] = "Server.Setting.HolyGrail",
["exposure.transitionLength"] = "Server.Setting.Transitions",
["exposure.whiteBalanceSmoothing"] = "Server.Setting.WhiteBalance",
["exposure.exposureOffsetEv"] = "Server.Setting.ExposureOffset",
["exposure.highlightKnee"] = "Server.Setting.HighlightKnee",
["motion.motionBlur"] = "Server.Setting.MotionBlur",
["motion.targetShutterAngle"] = "Server.Setting.ShutterAngle",
["motion.flowQuality"] = "Server.Setting.FlowQuality",
["motion.stabilize"] = "Server.Setting.Stabilize",
["motion.stabilizeSmoothing"] = "Server.Setting.StabilizeSmoothing",
["camera.crop"] = "Server.Setting.Crop",
["camera.aspect"] = "Server.Setting.Aspect",
["camera.offsetX"] = "Server.Setting.CameraOffset",
["camera.offsetY"] = "Server.Setting.CameraOffset",
["camera.keyframes"] = "Server.Setting.Keyframes",
["stack.mode"] = "Server.Setting.Stacking",
["stack.length"] = "Server.Setting.TrailLength",
["export.preset"] = "Server.Setting.ExportPreset",
["export.overrides"] = "Server.Setting.ExportValues",
["export.nameTemplate"] = "Server.Setting.OutputName",
["export.folder"] = "Server.Setting.ExportFolder",
["export.batch"] = "Server.Setting.Batch",
["excludedFrames"] = "Server.Setting.ExcludedFrames",
["heroFrame"] = "Server.Setting.HeroFrame",
};
/// <summary>"originals · proxies", "proxies" (cached server proxies) or "—".</summary>
public static string Local(SyncEntry entry)
{
if (entry.HasLocalOriginals)
{
return (entry.Local!.Status & SequenceStatus.ProxiesBuilt) != 0
? Join(Loc.L("Server.Sync.Originals"), Loc.L("Server.Sync.Proxies"))
: Loc.L("Server.Sync.Originals");
}
return entry.Link is { ProxiesCached: > 0 } ? Loc.L("Server.Sync.Proxies") : Loc.L("Server.Sync.None");
}
/// <summary>"originals · proxies · 3 exports", "46 % received" while uploading, or "—".</summary>
public static string Server(SyncEntry entry)
{
if (entry.Transfer is { Kind: JobKind.UploadCommit, State: JobState.Running, ProgressTotal: > 0 } upload)
return Loc.L("Server.Sync.Received", Formatting.Percent((double)upload.ProgressDone / upload.ProgressTotal));
if (entry.Link is not { } link) return Loc.L("Server.Sync.None");
var parts = new List<string>(3) { Loc.L("Server.Sync.Originals") };
if (link.ServerHasProxies) parts.Add(Loc.L("Server.Sync.Proxies"));
if (link.RenderCount > 0) parts.Add(Loc.Plural("Server.Sync.Exports", link.RenderCount));
return Join([.. parts]);
}
/// <summary>The State column: glyph kind, word and tooltip (never colour alone).</summary>
public static (StatusKind Kind, string Text, string ToolTip) State(SyncEntry entry)
{
if (entry.Transfer is { } job)
{
var upload = job.Kind == JobKind.UploadCommit;
return job.State switch
{
JobState.Paused => (StatusKind.Paused, Loc.L("Server.Sync.State.Paused"), Loc.L("Tooltips.Server.Sync.State.Paused")),
JobState.Queued => (StatusKind.Queued, Loc.L("Server.Sync.State.Waiting"), Loc.L("Tooltips.Server.Sync.State.Waiting")),
_ => (StatusKind.Running, Loc.L(upload ? "Server.Sync.State.Uploading" : "Server.Sync.State.Downloading"),
Loc.L(upload ? "Tooltips.Server.Sync.State.Uploading" : "Tooltips.Server.Sync.State.Downloading")),
};
}
if (entry.Link?.Error is { } error) return (StatusKind.Warning, Loc.L("Server.Sync.State.Failed"), Loc.L("Tooltips.Server.Sync.State.Failed", error));
return entry.Status switch
{
SyncStatus.InSync => (StatusKind.Done, Loc.L("Server.Sync.State.InSync"), Loc.L("Tooltips.Server.Sync.State.InSync")),
SyncStatus.LocalOnly => (StatusKind.Offline, Loc.L("Server.Sync.State.LocalOnly"), Loc.L("Tooltips.Server.Sync.State.LocalOnly")),
SyncStatus.RemoteOnly => (StatusKind.Remote, Loc.L("Server.Sync.State.RemoteOnly"), Loc.L("Tooltips.Server.Sync.State.RemoteOnly")),
SyncStatus.ProxiesOnly => (StatusKind.Remote, Loc.L("Server.Sync.State.ProxiesOnly"), Loc.L("Tooltips.Server.Sync.State.ProxiesOnly")),
SyncStatus.LocalAhead => (StatusKind.Synced, Loc.L("Server.Sync.State.LocalAhead"), Loc.L("Tooltips.Server.Sync.State.LocalAhead")),
SyncStatus.ServerAhead => (StatusKind.Remote, Loc.L("Server.Sync.State.ServerAhead"), Loc.L("Tooltips.Server.Sync.State.ServerAhead")),
_ => (StatusKind.Warning, Loc.L("Server.Sync.State.Conflict"), ConflictSummary(entry)),
};
}
/// <summary>One line for the State tooltip: "Deflicker window 41 here, 33 on the server · +2".</summary>
public static string ConflictSummary(SyncEntry entry)
{
var lines = Conflict(entry, int.MaxValue);
return lines.Count switch
{
0 => Loc.L("Tooltips.Server.Sync.State.Conflict"),
1 => lines[0],
_ => Loc.L("Server.Conflict.More", lines[0], Formatting.Count(lines.Count - 1)),
};
}
/// <summary>
/// What differs, one line per setting or sequence field in project order ("Deflicker window 41 here, 33 on the
/// server", "Tags differ"), at most <paramref name="max"/>.
/// </summary>
public static IReadOnlyList<string> Conflict(SyncEntry entry, int max)
{
if (entry is not { Local: { } local, Link.ConflictRemote: { } theirs }) return [];
var lines = new List<string>();
var shown = new HashSet<string>(StringComparer.Ordinal);
foreach (var difference in SyncRules.Differences(SyncPayload.Of(local), theirs, 64))
{
var (key, field) = Setting(difference.Path);
if (!shown.Add(key)) continue;
var label = Loc.L(key);
var metadata = difference.Path.StartsWith(SyncRules.MetadataPath, StringComparison.Ordinal);
lines.Add((metadata ? Text(difference.Mine) : Value(difference.Mine, field)) is { } mine
&& (metadata ? Text(difference.Theirs) : Value(difference.Theirs, field)) is { } server
? Loc.L("Server.Conflict.Difference", label, mine, server)
: Loc.L("Server.Conflict.Changed", label));
if (lines.Count == max) break;
}
return lines;
}
/// <summary>The row's buttons, in the order the mockup shows them.</summary>
public static IReadOnlyList<SyncAction> Actions(SyncEntry entry, SyncRights rights)
{
if (entry.Transfer is { } job) return job.State == JobState.Paused ? [SyncAction.Resume] : [SyncAction.Pause];
var actions = new List<SyncAction>(3);
switch (entry.Status)
{
case SyncStatus.LocalOnly when rights.CanUpload:
actions.Add(SyncAction.Upload);
break;
case SyncStatus.RemoteOnly or SyncStatus.ProxiesOnly:
actions.Add(SyncAction.OpenWithProxies);
if (rights.CanFetchOriginals) actions.Add(SyncAction.DownloadOriginals);
break;
case SyncStatus.LocalAhead when entry.Link is { MissingRemote: > 0 }:
if (rights.CanUpload) actions.Add(SyncAction.Upload);
break;
case SyncStatus.ServerAhead when entry.Link is { MissingLocal: > 0 }:
if (rights.CanFetchOriginals) actions.Add(SyncAction.DownloadOriginals);
break;
case SyncStatus.LocalAhead when rights.CanEdit:
case SyncStatus.ServerAhead:
actions.Add(SyncAction.SyncSettings);
break;
case SyncStatus.Conflict:
if (rights.CanEdit) actions.Add(SyncAction.KeepMine);
actions.AddRange([SyncAction.TakeServer, SyncAction.SaveBoth]);
return actions;
}
// Renders travel on request once no original is waiting to go either way.
if (entry is { Local: not null, Link: { MissingRemote: 0, MissingLocal: 0 } link })
{
if (link.ExportsToGet > 0) actions.Add(SyncAction.GetExports);
if (link.ExportsToSend > 0 && rights.CanUpload) actions.Add(SyncAction.SendExports);
}
return actions;
}
/// <summary>The bytes an action moves (for the tooltips).</summary>
public static long Size(SyncEntry entry, SyncAction action) => action switch
{
SyncAction.Upload => entry.Local?.TotalBytes ?? 0,
SyncAction.DownloadOriginals => entry.Link?.TotalBytes ?? 0,
SyncAction.GetExports => entry.Link?.ExportsToGetBytes ?? 0,
SyncAction.SendExports => entry.Link?.ExportsToSendBytes ?? 0,
_ => 0,
};
/// <summary>The tooltip of a row button: what it does and, for transfers, what it moves.</summary>
public static string ToolTip(SyncEntry entry, SyncAction action) => action switch
{
SyncAction.Upload or SyncAction.DownloadOriginals => Loc.L("Tooltips.Server.Sync." + action, Formatting.Bytes(Size(entry, action))),
SyncAction.GetExports => Loc.Plural("Tooltips.Server.Sync.GetExports", entry.Link!.ExportsToGet, Formatting.Bytes(Size(entry, action))),
SyncAction.SendExports => Loc.Plural("Tooltips.Server.Sync.SendExports", entry.Link!.ExportsToSend, Formatting.Bytes(Size(entry, action))),
_ => Loc.L("Tooltips.Server.Sync." + action),
};
/// <summary>
/// The chips sort by where the originals are: a sequence being transferred still counts as only on this PC (no server
/// copy yet) or only on the server (no local originals yet), as the chip tooltips say.
/// </summary>
public static bool Matches(SyncEntry entry, SyncFilter filter, string search) =>
(filter switch
{
SyncFilter.LocalOnly => entry.Status == SyncStatus.LocalOnly || entry is { Status: SyncStatus.Transferring, Link: null },
SyncFilter.Remote => entry.Status is SyncStatus.RemoteOnly or SyncStatus.ProxiesOnly
|| entry is { Status: SyncStatus.Transferring, HasLocalOriginals: false, Link: not null },
SyncFilter.Conflicts => entry.Status == SyncStatus.Conflict,
_ => true,
}) && (search.Length == 0 || entry.Title.Contains(search, StringComparison.CurrentCultureIgnoreCase));
/// <summary>
/// The label key of a difference path and the member below the setting it reaches ("isAuto", "manual", … of an
/// Auto parameter; null for the setting itself): "exposure.deflickerWindow.manual" is the deflicker window's manual value.
/// </summary>
private static (string Key, string? Field) Setting(string path)
{
var segments = path.Split('.');
for (var length = segments.Length; length > 0; length--)
{
if (SettingKeys.TryGetValue(string.Join('.', segments[..length]), out var key)) return (key, length < segments.Length ? segments[length] : null);
}
return ("Server.Setting.Other", segments[^1]);
}
/// <summary>A title, tags, rating or notes value as shown ("—" when empty).</summary>
private static string Text(string? value) => string.IsNullOrWhiteSpace(value) ? Loc.L("Server.Sync.None") : Shorten(value);
/// <summary>A side's value as shown ("41", "on", "auto"); null when it is not a single value worth printing.</summary>
private static string? Value(string? json, string? field)
{
if (json is null) return field is null or "manual" ? Loc.L("Server.Sync.None") : null;
if (field is not (null or "manual" or "autoValue" or "isAuto")) return null;
JsonNode? node;
try
{
node = JsonNode.Parse(json);
}
catch (JsonException)
{
return null;
}
if (field == "isAuto") return node is JsonValue v && v.TryGetValue<bool>(out var auto) ? Loc.L(auto ? "Server.Setting.Auto" : "Server.Setting.Manual") : null;
return node switch
{
null => Loc.L("Server.Sync.None"),
JsonValue value when value.TryGetValue<bool>(out var flag) => Loc.L(flag ? "Server.Setting.On" : "Server.Setting.Off"),
JsonValue value when value.TryGetValue<double>(out var number) => number.ToString("0.###", Loc.Culture),
JsonValue value when value.TryGetValue<string>(out var text) => Shorten(text),
_ => null,
};
}
private static string Shorten(string text) => text.Length <= 40 ? text : string.Concat(text.AsSpan(0, 39), "…");
private static string Join(params string[] parts) => string.Join(" · ", parts);
}