Files
Chronos/src/Chronos.App/Editor/ExportGroups.cs
T
Alby96andClaude Opus 5.5 503b5f766a feat(P6): grouped Export panel with preset table, encoding and playback rows
The Export step's cards and long form gave no room for the advanced
encoding settings and the new playback contract. The Output panel is now
four collapsible groups (Output, Encoding, Playback, File) with a muted
summary while closed and a fixed footer (result line, Render now, Add to
queue, Batch (n), Videos (n), Reveal, Send again). Presets become a 28 px
table grouped by preset group.

- Encoding: rate control, bit rate or quality, keyframes in seconds kept
  across a frame rate change, B-frames and profile, each gated per codec
  and depth with the reason in the tooltip.
- Resolution: Preset, Source, the standard boxes fitted to the chosen
  proportions (4K at 9:16 renders 2160 x 3840) and Custom.
- Playback: direction (Boomerang gated with star trails), loops, fades
  and hold; a non-default playback writes settings format 3.
- Group open state is kept by the app through EditorSettings.Groups
  (bound to the app settings later), not in the project.
- ExportPlan carries the output program for duration and frame count.
- The tab resolves the export at once instead of from a stale plan.

Refs #21

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 02:26:04 +01:00

217 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Globalization;
using System.Windows.Input;
using Chronos.App.Infrastructure;
using Chronos.Core.Export;
using Chronos.Core.Localization;
using Chronos.Core.Project;
using Chronos.Core.Video;
using Chronos.Ui.Mvvm;
namespace Chronos.App.Editor;
/// <summary>
/// A collapsible group of the Export step's Output panel (owner review 1, R3; DESIGN §4): its name with a chevron and,
/// while collapsed, a muted one-line summary of its values. Open or closed is this user's choice, kept by the app
/// (<see cref="EditorSettings.Groups"/>), not in the project.
/// </summary>
public sealed class SettingGroup : ObservableObject
{
private readonly string _nameKey;
private readonly string _toolTipKey;
private readonly bool _expandedByDefault;
private string? _summary;
/// <param name="key">The group's key in <see cref="EditorSettings.Groups"/> ("export.encoding").</param>
public SettingGroup(string key, string nameKey, string toolTipKey, bool expandedByDefault)
{
Key = key;
(_nameKey, _toolTipKey, _expandedByDefault) = (nameKey, toolTipKey, expandedByDefault);
ToggleCommand = new RelayCommand(() => IsExpanded = !IsExpanded);
}
public string Key { get; }
/// <summary>"Encoding".</summary>
public string Header => Loc.L(_nameKey);
/// <summary>What opening the group shows.</summary>
public string ToolTip => Loc.L(_toolTipKey);
public bool IsExpanded
{
get => EditorSettings.Groups.IsExpanded(Key, _expandedByDefault);
set
{
if (value == IsExpanded) return;
EditorSettings.Groups.Set(Key, value);
OnPropertyChanged();
}
}
/// <summary>"Variable · 40 Mb/s · GOP 1 s · B 2 · High": the values, shown while the group is collapsed.</summary>
public string? Summary { get => _summary; set => Set(ref _summary, value); }
public ICommand ToggleCommand { get; }
/// <summary>The UI language changed, or another editor toggled the group: everything is read again.</summary>
public void Refresh() => OnAllPropertiesChanged();
}
/// <summary>
/// What the Export step's advanced encoding rows offer for a codec and colour depth (owner review 1, R8; decision 6), and
/// the keyframe interval in seconds as the project stores it in frames. Every value is an <see cref="ExportOverrides"/>
/// member nodes of phase 4 already read, so none of them raises the settings format.
/// </summary>
public static class ExportRules
{
/// <summary>Keyframe intervals offered, in seconds (0.5 to 10, step 0.5).</summary>
public static IReadOnlyList<double> KeyframeSeconds { get; } = [.. Enumerable.Range(1, 20).Select(i => i * 0.5)];
/// <summary>B-frame counts offered besides Auto.</summary>
public static IReadOnlyList<int> BFrameCounts { get; } = [0, 1, 2, 3, 4];
/// <summary>
/// The standard resolutions of the Resolution choice (decision 7): the landscape box written into
/// <see cref="ExportOverrides.Width"/>/<see cref="ExportOverrides.Height"/>, which the chosen proportions are fitted in.
/// </summary>
public static IReadOnlyList<(string Key, string LabelKey, int Width, int Height)> StandardResolutions { get; } =
[.. ExportPresets.Resolutions.Select(r => (r.Key, "Export.Resolution." + ResolutionLabel(r.Key), r.Width, r.Height))];
/// <summary>Only the hardware codecs have a rate control, a keyframe interval and B-frames to choose.</summary>
public static bool HasRateControl(VideoCodec codec) => !VideoCodecInfo.IsManaged(codec);
/// <summary>Why <paramref name="codec"/> has no rate control or keyframe interval to choose; null when it has.</summary>
public static string? RateControlBlocked(VideoCodec codec) =>
HasRateControl(codec) ? null : Loc.L("Tooltips.Export.RateControlNA", Loc.L("Export.Codec." + codec));
/// <summary>
/// Why <paramref name="codec"/> with <paramref name="profile"/> takes no B-frames (Baseline, MJPEG, GIF, APNG and the
/// image sequences); null when it does.
/// </summary>
public static string? BFramesBlocked(VideoCodec codec, CodecProfile profile) => codec switch
{
VideoCodec.H264 when profile == CodecProfile.H264Baseline => Loc.L("Tooltips.Export.BFramesNA", ProfileLabel(profile)),
_ when VideoCodecInfo.IsManaged(codec) => Loc.L("Tooltips.Export.BFramesNA", Loc.L("Export.Codec." + codec)),
_ => null,
};
/// <summary>
/// The profiles <paramref name="codec"/> offers at <paramref name="bitDepth"/>: H.264 Baseline, Main and High; HEVC Main
/// at 8 bits or Main 10 above; AV1 Main; none for the managed codecs.
/// </summary>
public static IReadOnlyList<CodecProfile> Profiles(VideoCodec codec, int bitDepth) => codec switch
{
VideoCodec.H264 => [CodecProfile.H264Baseline, CodecProfile.H264Main, CodecProfile.H264High],
VideoCodec.Hevc => [bitDepth > 8 ? CodecProfile.HevcMain10 : CodecProfile.HevcMain],
VideoCodec.Av1 => [CodecProfile.Av1Main],
_ => [],
};
/// <summary>Why the profile cannot be chosen (the codec has one, or none); null when it can.</summary>
public static string? ProfileBlocked(VideoCodec codec, int bitDepth) =>
Profiles(codec, bitDepth).Count > 1 ? null : Loc.L("Tooltips.Export.ProfileNA", Loc.L("Export.Codec." + codec));
/// <summary>"Baseline", "Main", "High", "Main 10".</summary>
public static string ProfileLabel(CodecProfile profile) => Loc.L(profile switch
{
CodecProfile.H264Baseline => "Export.Profile.Baseline",
CodecProfile.H264High => "Export.Profile.High",
CodecProfile.HevcMain10 => "Export.Profile.Main10",
_ => "Export.Profile.Main",
});
/// <summary>The interval of <paramref name="seconds"/> as stored: whole frames at <paramref name="frameRate"/>, at least one.</summary>
public static int GopFrames(double seconds, Rational frameRate) => int.Max(1, (int)double.Round(seconds * frameRate.Value));
/// <summary>The interval of <paramref name="gopFrames"/> at <paramref name="frameRate"/>, in seconds.</summary>
public static double GopSeconds(int gopFrames, Rational frameRate) => frameRate.Value > 0 ? gopFrames / frameRate.Value : 0;
/// <summary>The offered interval nearest <paramref name="gopFrames"/> at <paramref name="frameRate"/> (what the choice shows).</summary>
public static double NearestKeyframeSeconds(int gopFrames, Rational frameRate) =>
double.Clamp(double.Round(GopSeconds(gopFrames, frameRate) * 2) / 2, KeyframeSeconds[0], KeyframeSeconds[^1]);
/// <summary>
/// A keyframe interval chosen by hand keeps its seconds when the frame rate changes: <paramref name="gopFrames"/> at
/// <paramref name="from"/> as frames at <paramref name="to"/>.
/// </summary>
public static int RescaleGop(int gopFrames, Rational from, Rational to) => GopFrames(GopSeconds(gopFrames, from), to);
/// <summary>"1.5 s".</summary>
public static string Seconds(double seconds) => Loc.L("Export.Seconds", seconds.ToString("0.##", Loc.Culture));
private static string ResolutionLabel(string key) => key switch
{
"4k" => "Uhd4k",
"dci4k" => "Dci4k",
_ => key,
};
}
/// <summary>The one-line summaries of the Export step (owner review 1, R3): a collapsed group's values and the footer's line.</summary>
public static class ExportSummaries
{
private static string Separator => Loc.Separator;
/// <summary>
/// "Variable · 40 Mb/s · GOP 1 s · B 2 · High" for a video codec; the quality ("Quality 95") or n/a for the managed
/// codecs, which have no rate control.
/// </summary>
public static string Encoding(ResolvedExport export, long bitRate)
{
ArgumentNullException.ThrowIfNull(export);
if (!ExportRules.HasRateControl(export.Codec))
{
var managed = EditorText.Encoding(export, 0, null);
return managed.Length > 0 ? managed : Loc.L("Common.NotApplicable");
}
var parts = new List<string> { RateControlLabel(export.RateControl) };
parts.Add(export.RateControl == RateControl.Quality ? Loc.L("Editor.Export.QualityValue", export.Quality) : EditorText.BitRate(bitRate));
parts.Add(Loc.L("Export.Summary.Keyframes", ExportRules.Seconds(ExportRules.GopSeconds(export.GopFrames, export.FrameRate))));
if (ExportRules.BFramesBlocked(export.Codec, export.Profile) is null) parts.Add(Loc.L("Export.Summary.BFrames", export.BFrames));
if (export.Profile != CodecProfile.Auto) parts.Add(ExportRules.ProfileLabel(export.Profile));
return string.Join(Separator, parts);
}
/// <summary>"Forward · no fades", "Boomerang · ×2 · fades 1 s / 1.5 s · hold 2 s".</summary>
public static string Playback(PlaybackSettings? playback)
{
playback ??= new PlaybackSettings();
var parts = new List<string> { DirectionLabel(playback.EffectiveDirection) };
if (playback.EffectiveLoops > 1) parts.Add(Loc.L("Export.Summary.Loops", playback.EffectiveLoops));
parts.Add(playback.EffectiveFadeIn > 0 || playback.EffectiveFadeOut > 0
? Loc.L("Export.Summary.Fades", ExportRules.Seconds(playback.EffectiveFadeIn), ExportRules.Seconds(playback.EffectiveFadeOut))
: Loc.L("Export.Summary.NoFades"));
if (playback.EffectiveHoldEnd > 0) parts.Add(Loc.L("Export.Summary.Hold", ExportRules.Seconds(playback.EffectiveHoldEnd)));
return string.Join(Separator, parts);
}
/// <summary>"2026-09-28 Harbour sunset - 4K archive.mp4 · Sync": the result's name, then Sync when it is on.</summary>
public static string File(string? resultName, string folder, bool sync)
{
var parts = new List<string> { resultName is { Length: > 0 } name ? name : folder };
if (sync) parts.Add(Loc.L("Editor.Export.Sync"));
return string.Join(Separator, parts);
}
/// <summary>
/// The footer's line (decision 5): "2026-09-28 Harbour sunset - 4K….mp4 · 1.2 GB · about 4 min · 42 s · 1,260 frames"; the
/// size and time are left out when there is no estimate (no encoder here).
/// </summary>
public static string Footer(string resultName, ExportEstimate? estimate, double durationSeconds, int frames)
{
ArgumentNullException.ThrowIfNull(resultName);
var duration = Formatting.Duration(TimeSpan.FromSeconds(double.Max(0, durationSeconds)));
var count = Loc.Plural("Export.Footer.Frames", frames);
return estimate is null
? string.Join(Separator, resultName, duration, count)
: Loc.L("Export.Footer.Summary", resultName, Formatting.Bytes(estimate.Bytes), Loc.L("Editor.Export.About", Formatting.Duration(estimate.Time)), duration, count);
}
/// <summary>"Variable", "Constant", "Quality".</summary>
public static string RateControlLabel(RateControl rateControl) => Loc.L("Export.RateControl." + rateControl);
/// <summary>"Forward", "Reverse", "Boomerang" (<see cref="PlaybackDirection"/>).</summary>
public static string DirectionLabel(string direction) =>
Loc.L("Export.Direction." + CultureInfo.InvariantCulture.TextInfo.ToTitleCase(PlaybackDirection.IsKnown(direction) ? direction.ToLowerInvariant() : PlaybackDirection.Forward));
}