diff --git a/src/Titano.App/DesignTime/SampleData.cs b/src/Titano.App/DesignTime/SampleData.cs index 1266176b..492533eb 100644 --- a/src/Titano.App/DesignTime/SampleData.cs +++ b/src/Titano.App/DesignTime/SampleData.cs @@ -15,6 +15,7 @@ using Titano.Core.Imaging; using Titano.Core.Imaging.Decoders; using Titano.Core.Metadata; using Titano.Core.Model; +using Titano.Core.Project; using Titano.Core.Video; using Titano.Data; using Titano.Data.Archiving; @@ -91,7 +92,8 @@ public sealed class SampleData /// /// The sequence editor's sample: a second archive holding a synthetic harbour sunset of real JPEG frames with Exif /// (flicker, two received exposure steps, white-balance drift, a horizon), indexed by a scan, analysed (light and - /// deep, saved as analysis.json), advised into project.json and given two past renders. + /// deep, saved as analysis.json), advised into project.json with one manual value and three camera + /// keyframes, and given two past renders. /// /// The sequence id to open in the editor. public async Task FillEditorAsync() @@ -119,7 +121,14 @@ public sealed class SampleData var sequence = content.Sequence; deep.Save(new ArchivePaths(root).Sequence(sequence.Folder).AnalysisPath); var advice = Advisor.Recommend(content.Light, deep, SequenceContext.FromFrames(content.Frames)); - await source.SaveProjectAsync(Advisor.Apply(content.Project, advice), content.Project, CancellationToken.None); + // As a user leaves it: the advice applied, one value taken over by hand (Sun track) and a slow push-in. + var advised = Advisor.Apply(content.Project, advice); + var project = advised with + { + Exposure = advised.Exposure with { DeflickerStrength = advised.Exposure.DeflickerStrength.WithManual(0.8) }, + Camera = advised.Camera with { Keyframes = [new CameraKey(0, 0.5, 0.5, 1, 0), new CameraKey(48, 0.6, 0.46, 1.4, 0), new CameraKey(95, 0.64, 0.4, 1.9, 0)] }, + }; + await source.SaveProjectAsync(project, content.Project, CancellationToken.None); await db.Sequences.RecordAnalysisAsync(id, SequenceStatus.AnalyzedLight | SequenceStatus.AnalyzedDeep | SequenceStatus.HasWarnings, SequenceStatus.None, advice.Findings.Count(f => f.Severity >= FindingSeverity.Warning), AnalysisSummary.From(deep).ToJson()); foreach (var (preset, codec, width, height, daysAgo, bytes) in new[] { ("4K archive", "hevc", 3840, 2160, 7, 2_210_000_000L), ("1080p share", "h264", 1920, 1080, 3, 412_000_000L) }) diff --git a/src/Titano.App/Editor/CameraTab.cs b/src/Titano.App/Editor/CameraTab.cs index 0d0f1841..b964b43a 100644 --- a/src/Titano.App/Editor/CameraTab.cs +++ b/src/Titano.App/Editor/CameraTab.cs @@ -12,15 +12,6 @@ using Titano.Ui.Visuals; namespace Titano.App.Editor; -/// Margins of the free crop, in the order of CameraTab.Margins (opposites differ in the lowest bit). -public enum CropSide -{ - Left, - Right, - Top, - Bottom, -} - /// /// Camera (BRIEF §12.3): the free crop of the frame (margins), the output aspect and its placement inside the crop, and /// virtual-camera keyframes edited as a zoom curve over the sequence (X = frame, Y = zoom) with the selected keyframe's @@ -31,11 +22,6 @@ public sealed class CameraTab : EditorTab /// Zoom at the top of the curve editor. public const double MaxZoom = 4; - /// Largest crop margin; opposite margins together leave at least of the frame. - public const double MaxCropMargin = 0.45; - - public const double MinCropSize = 0.1; - private int _selectedKey = -1; public CameraTab(EditorViewModel editor) @@ -169,25 +155,10 @@ public sealed class CameraTab : EditorTab OnPropertyChanged(nameof(RemoveKeyToolTip)); } - /// A margin of the free crop (, normalised x, y, width, height); all zero = the whole frame. + /// A margin of the free crop (); all zero = the whole frame. private static NumberRow CropRow(EditorSession session, CropSide side, string headerKey) => new(session, headerKey, "Tooltips.Editor.Camera.Crop", 0, - MaxCropMargin, 0.01, p => Margins(p.Camera.Crop)[(int)side], (p, v) => p with { Camera = p.Camera with { Crop = WithMargin(p.Camera.Crop, side, v) } }, - v => Formatting.Percent(v)); - - /// Left, right, top and bottom margins of a crop rectangle. - private static double[] Margins(double[]? crop) => crop is [var x, var y, var w, var h] && new[] { x, y, w, h }.All(double.IsFinite) - ? [double.Clamp(x, 0, 1), double.Clamp(1 - x - w, 0, 1), double.Clamp(y, 0, 1), double.Clamp(1 - y - h, 0, 1)] - : [0, 0, 0, 0]; - - private static double[]? WithMargin(double[]? crop, CropSide side, double value) - { - var margins = Margins(crop); - var opposite = (int)side ^ 1; - margins[(int)side] = double.Clamp(value, 0, double.Min(MaxCropMargin, 1 - MinCropSize - margins[opposite])); - if (margins.All(m => m == 0)) return null; - var (left, right, top, bottom) = (margins[0], margins[1], margins[2], margins[3]); - return [left, top, 1 - left - right, 1 - top - bottom]; - } + CropMargins.MaxMargin, 0.01, p => CropMargins.Of(p.Camera.Crop)[(int)side], + (p, v) => p with { Camera = p.Camera with { Crop = CropMargins.With(p.Camera.Crop, side, v) } }, v => Formatting.Percent(v)); private Point ToPoint(CameraKey key) => new(Editor.FrameCount > 1 ? (double)key.Frame / (Editor.FrameCount - 1) : 0, (key.Zoom - 1) / (MaxZoom - 1)); diff --git a/src/Titano.App/Editor/CropMargins.cs b/src/Titano.App/Editor/CropMargins.cs new file mode 100644 index 00000000..d82ab593 --- /dev/null +++ b/src/Titano.App/Editor/CropMargins.cs @@ -0,0 +1,37 @@ +namespace Titano.App.Editor; + +/// Margins of the free crop, in the order of (opposites differ in the lowest bit). +public enum CropSide +{ + Left, + Right, + Top, + Bottom, +} + +/// +/// The Camera tab's crop margins over CameraSettings.Crop (a normalised x, y, width, height rectangle, null for the +/// whole frame): each margin at most , opposite margins leaving at least . +/// +public static class CropMargins +{ + public const double MaxMargin = 0.45; + + public const double MinSize = 0.1; + + /// Left, right, top and bottom margins of (zeros for none or an invalid one, which the render ignores too). + public static double[] Of(double[]? crop) => crop is [var x, var y, var w, var h] && double.IsFinite(x + y + w + h) + ? [double.Clamp(x, 0, 1), double.Clamp(1 - x - w, 0, 1), double.Clamp(y, 0, 1), double.Clamp(1 - y - h, 0, 1)] + : [0, 0, 0, 0]; + + /// with one margin changed; null when no margin is left (the whole frame). + public static double[]? With(double[]? crop, CropSide side, double value) + { + var margins = Of(crop); + var opposite = (int)side ^ 1; + margins[(int)side] = double.Clamp(value, 0, double.Min(MaxMargin, 1 - MinSize - margins[opposite])); + if (margins.All(m => m == 0)) return null; + var (left, right, top, bottom) = (margins[0], margins[1], margins[2], margins[3]); + return [left, top, 1 - left - right, 1 - top - bottom]; + } +} diff --git a/src/Titano.App/Editor/EditorSession.cs b/src/Titano.App/Editor/EditorSession.cs index 7cad9de9..26b79100 100644 --- a/src/Titano.App/Editor/EditorSession.cs +++ b/src/Titano.App/Editor/EditorSession.cs @@ -156,11 +156,15 @@ public sealed class EditorSession : IAsyncDisposable /// Stores pending changes now; false when the save failed (the change stays pending and is retried). public Task FlushAsync() => _saver.FlushAsync(); - /// Stores what is pending one last time and releases the source; callers that must not lose an edit flush and ask first. + /// + /// Stores what is pending one last time, stops retrying and releases the source; callers that must not lose an edit + /// flush and ask first (SequenceViewModel.ConfirmCloseAsync). + /// public async ValueTask DisposeAsync() { History.Changed -= OnProjectChanged; if (!await _saver.FlushAsync()) Log.Warning("editor", $"The last change of {Content.Sequence.DisplayTitle} was not saved", _saver.LastError); + _saver.Stop(); await Source.DisposeAsync(); } diff --git a/src/Titano.App/Editor/ExposureTab.cs b/src/Titano.App/Editor/ExposureTab.cs index ea99bddc..0a47b21e 100644 --- a/src/Titano.App/Editor/ExposureTab.cs +++ b/src/Titano.App/Editor/ExposureTab.cs @@ -152,7 +152,7 @@ public sealed class ExposureTab : EditorTab if (content.Deep is { Regions: not null } withSky && withSky.Frames.SkyLogLuminance.Any(v => v is not null)) { series.Add(new ChartSeries(Loc.L("Editor.Chart.Sky"), withSky.Frames.SkyLogLuminance.Select(v => v ?? double.NaN).ToArray(), "Brush.Region") - { Dash = LineDash.Dashed }); + { Dash = LineDash.Dashed }); legend.Add(new ChartLegendItem(Loc.L("Editor.Chart.Sky"), "Brush.Region", LegendSwatch.DashedLine)); } } diff --git a/src/Titano.App/Editor/PreviewRenderer.cs b/src/Titano.App/Editor/PreviewRenderer.cs index 47317ec1..b31bfbfe 100644 --- a/src/Titano.App/Editor/PreviewRenderer.cs +++ b/src/Titano.App/Editor/PreviewRenderer.cs @@ -113,9 +113,12 @@ public sealed class PreviewRenderer(SequenceContent content, ISequenceSource sou if (Cached(project) is { } cached) return cached; var key = Key(project); var measured = await MeasureAsync(project, cancellationToken).ConfigureAwait(false); + // Nothing is measured again: white balance comes from the measuring preparation, and a stabilisation it could + // not solve (its warning is kept) is not tried again. var solveProject = project with { Exposure = project.Exposure with { WhiteBalanceSmoothing = project.Exposure.WhiteBalanceSmoothing.WithManual(false) }, + Motion = measured.Stabilization is null ? project.Motion with { Stabilize = project.Motion.Stabilize.WithManual(false) } : project.Motion, }; var solved = await RenderPreparation.PrepareAsync(Input(solveProject, Reuse(measured, project)), Options, null, cancellationToken).ConfigureAwait(false); var corrections = solved with diff --git a/src/Titano.App/Editor/ProjectSaver.cs b/src/Titano.App/Editor/ProjectSaver.cs index f00d8539..eb6acda2 100644 --- a/src/Titano.App/Editor/ProjectSaver.cs +++ b/src/Titano.App/Editor/ProjectSaver.cs @@ -18,6 +18,7 @@ public sealed class ProjectSaver(Func save, TimeSpan dela private ProjectSettings? _pending; private bool _running; private bool _flushRequested; + private bool _stopped; private int _failures; private CancellationTokenSource? _wait; @@ -42,6 +43,7 @@ public sealed class ProjectSaver(Func save, TimeSpan dela TaskCompletionSource start; lock (_gate) { + if (_stopped) return; _pending = project; if (_running) return; _running = true; @@ -67,6 +69,17 @@ public sealed class ProjectSaver(Func save, TimeSpan dela } } + /// Drops what is pending and stops retrying (the editor closed and the user chose to discard the change). + public void Stop() + { + lock (_gate) + { + _stopped = true; + _pending = null; + _wait?.Cancel(); + } + } + private async Task LoopAsync(Task started) { await started; @@ -118,13 +131,14 @@ public sealed class ProjectSaver(Func save, TimeSpan dela lock (_gate) { // A newer change contains this one (projects are whole snapshots); otherwise this one waits for the retry. - _pending ??= next; + if (!_stopped) _pending ??= next; failures = ++_failures; - Complete(false); } LastError = ex; wait = TimeSpan.FromSeconds(double.Min(MaxRetryDelay.TotalSeconds, double.Pow(2, failures))); Failed?.Invoke(ex, failures); + // Flushes learn the outcome after the failure was reported. + lock (_gate) Complete(false); } } } diff --git a/src/Titano.App/Shell/ScreenCapture.cs b/src/Titano.App/Shell/ScreenCapture.cs index 2646ac07..cdefe680 100644 --- a/src/Titano.App/Shell/ScreenCapture.cs +++ b/src/Titano.App/Shell/ScreenCapture.cs @@ -114,8 +114,12 @@ public static class ScreenCapture } } - /// The sequence editor on the synthetic sunset: every section in both themes (sequence-<tab>-<theme>.png). - private static async Task CaptureEditorAsync(Window window, ShellViewModel shell, SampleData sample, string directory) + /// + /// The sequence editor on the synthetic sunset: every section in both themes (sequence-<tab>-<theme>.png, + /// the Camera tab with a keyframe selected, the Export tab as Export.html with the crop mask and no compare), then + /// the preview overlays, the waveform and the "what changed" toast of Optimise all. + /// + private static async Task CaptureEditorAsync(MainWindow window, ShellViewModel shell, SampleData sample, string directory) { var written = 0; var id = await sample.FillEditorAsync(); @@ -126,22 +130,42 @@ public static class ScreenCapture Log.Warning("capture", "The editor sample did not open"); return 0; } + foreach (var camera in editor.Tabs.OfType()) camera.SelectedKey = 1; foreach (var theme in Themes) { ThemeManager.Apply(theme); foreach (var tab in editor.Tabs) { editor.SelectedTab = tab; + editor.IsCompare = tab is not ExportTab; editor.CurrentIndex = tab is ExportTab ? 40 : 45; - await Settle(window); - if (!await editor.WhenIdleAsync(TimeSpan.FromSeconds(30))) Log.Warning("capture", $"The {tab.Key} section was still busy"); - written += await SaveAsync(window, directory, "sequence-" + tab.Key.ToLowerInvariant(), theme); + written += await SaveEditorAsync(window, editor, directory, "sequence-" + tab.Key.ToLowerInvariant(), theme); } + + editor.SelectedTab = editor.Exposure; + editor.IsCompare = true; + editor.ShowVectors = editor.ShowRegions = true; + written += await SaveEditorAsync(window, editor, directory, "sequence-exposure-overlays", theme); + editor.ShowVectors = editor.ShowRegions = false; + editor.Exposure.ChartMode = ExposureChart.Waveform; + written += await SaveEditorAsync(window, editor, directory, "sequence-exposure-waveform", theme); + editor.Exposure.ChartMode = ExposureChart.Luminance; + + editor.OptimiseAllCommand.Execute(null); + written += await SaveEditorAsync(window, editor, directory, "sequence-optimise", theme); + editor.Session.Undo(); + foreach (var toast in window.Toasts.Toasts.ToList()) window.Toasts.Dismiss(toast); } - editor.SelectedTab = editor.Exposure; return written; } + private static async Task SaveEditorAsync(Window window, EditorViewModel editor, string directory, string screen, AppTheme theme) + { + await Settle(window); + if (!await editor.WhenIdleAsync(TimeSpan.FromSeconds(30))) Log.Warning("capture", $"{screen} was still busy"); + return await SaveAsync(window, directory, screen, theme); + } + private static async Task SaveAsync(Window window, string directory, string screen, AppTheme theme, ShellViewModel? waitForThumbnails = null) { await Settle(window); diff --git a/tests/Titano.App.Tests/Editor/EditorSessionTests.cs b/tests/Titano.App.Tests/Editor/EditorSessionTests.cs index d25b3b3c..7015300d 100644 --- a/tests/Titano.App.Tests/Editor/EditorSessionTests.cs +++ b/tests/Titano.App.Tests/Editor/EditorSessionTests.cs @@ -2,6 +2,7 @@ using System.IO; using System.Text.Json; using Titano.App.Editor; using Titano.Core.Advice; +using Titano.Core.Analysis; using Titano.Core.Model; using Titano.Core.Project; using Titano.Protocol; @@ -61,29 +62,62 @@ public sealed class EditorSessionTests { source.ExternalEdit = p => p with { Camera = p.Camera with { OffsetY = -0.2 } }; session.Edit(p => p with { HeroFrame = 5 }); - await session.FlushAsync(); + Assert.True(await session.FlushAsync()); Assert.Equal(5, session.Project.HeroFrame); Assert.Equal(-0.2, session.Project.Camera.OffsetY, "their change shows up in the editor"); Assert.True(session.Undo()); Assert.Null(session.Project.HeroFrame); + Assert.Equal(-0.2, session.Project.Camera.OffsetY, "undo keeps their change"); + Assert.True(await session.FlushAsync()); + Assert.Equal(-0.2, source.Stored.Camera.OffsetY, "and the undone project stored over it keeps it too"); + Assert.True(session.Redo()); + Assert.Equal(-0.2, session.Project.Camera.OffsetY); } } [Test] - public async Task AFailedSaveIsReportedAndRetriedByTheNextChange() + public async Task AdviceFromAReloadSurvivesUndo() + { + var (session, source) = Open(deep: false); + await using (session) + { + session.Edit(p => p with { HeroFrame = 2 }); + Assert.True(await session.FlushAsync()); + // The deep analysis finished: its advice is stored, the editor reads it back. + source.Content = EditorFixture.Content(EditorFixture.Sunset, deep: true) with { Sequence = session.Content.Sequence }; + var advice = Advisor.Recommend(source.Content.Light, source.Content.Deep, SequenceContext.FromFrames(source.Content.Frames)); + source.Stored = Advisor.Apply(source.Stored, advice); + await session.ReloadAsync(); + Assert.True(session.Project.Exposure.Deflicker.HasAdvice); + + Assert.True(session.Undo()); + Assert.Null(session.Project.HeroFrame); + Assert.True(session.Project.Exposure.Deflicker.HasAdvice, "undo does not wipe the advice"); + Assert.True(await session.FlushAsync()); + Assert.True(source.Stored.Exposure.Deflicker.HasAdvice, "nor does the save that follows"); + } + } + + [Test] + public async Task AFailedSaveStaysPendingUntilItIsStored() { var (session, source) = Open(); await using (session) { - Exception? reported = null; - session.SaveFailed += ex => reported = ex; + var reported = 0; + session.SaveFailed += _ => reported++; source.FailNextSave = new IOException("share offline"); session.Edit(p => p with { HeroFrame = 1 }); - await session.FlushAsync(); - Assert.NotNull(reported); - session.Edit(p => p with { HeroFrame = 2 }); - await session.FlushAsync(); - Assert.Equal(2, source.Stored.HeroFrame); + Assert.False(await session.FlushAsync(), "the flush reports the failure"); + Assert.Equal(1, reported); + Assert.True(session.HasPendingSave, "closing the window must not drop the edit"); + Assert.NotNull(session.LastSaveError); + Assert.Null(source.Stored.HeroFrame); + + Assert.True(await session.FlushAsync(), "the retry stores it without another edit"); + Assert.Equal(1, source.Stored.HeroFrame); + Assert.False(session.HasPendingSave); + Assert.Null(session.LastSaveError); } } diff --git a/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs b/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs index 77ab4244..1ff6d311 100644 --- a/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs +++ b/tests/Titano.App.Tests/Editor/EditorSmokeTests.cs @@ -60,6 +60,15 @@ public sealed class EditorSmokeTests Assert.NotNull(editor.Session.Content.Deep, "the sample is analysed"); Assert.True(editor.Session.Project.Exposure.Deflicker.HasAdvice, "the advice is in the project"); Assert.Equal("Harbour sunset", editor.Title); + Assert.Equal("Titano — Harbour sunset", shell.WindowTitle, "the window title follows the open sequence"); + Assert.False(shell.IsJobBarVisible, "the idle job bar gives its height to the editor"); + + // The Render split button can be clicked once the plan exists (its command re-reads its state). + Assert.True(await editor.WhenIdleAsync(TimeSpan.FromSeconds(60)), "the editor settled"); + await SettleAsync(window); + Assert.True(editor.CanRender, editor.RenderBlockedReason ?? "renderable"); + var render = Find(window).Single(b => b.IsVisible); + Assert.True(Find