Files
Chronos/src/Titano.App/Editor/ProjectHistory.cs
T
Claude 25e85aee57 fix(app): editor review fixes for saving, undo, preview, commands and layout
- Undo/redo snapshots take outside changes (advice, merged edits) on Replace;
  a drag, click or key press ends a merged run; Reset to Auto is its own step.
- A failed save stays pending and is retried with backoff; closing or switching
  asks before discarding it; the local source throws unless the index took it.
- Opening a sequence survives any failure; the window title follows the editor;
  the job bar hides when idle in the editor.
- Render, Add to queue and Batch re-read their state; disabled commands say why;
  Analyse is disabled while queued and announces its cost and Ctrl+Shift+A.
- Preview: 1:1 and wheel zoom render the source's own pixels, the view keeps its
  place across resolutions, crop mask result mode, unreadable frames say so,
  labelled tools, keyboard focus on open, tools hidden on Export lose their effect.
- Preparation re-solves from measured series (no re-decode per slider step) with
  a half-precision frame cache.
- Exposure flicker and waveform charts, Time plan chart, Camera crop margins,
  sortable frame table, wrapped findings, responsive preset cards, render-done
  follow-up, Jobs history filtered on the sequence.

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

94 lines
3.4 KiB
C#

using Titano.Core.Project;
namespace Titano.App.Editor;
/// <summary>
/// Undo and redo of the editor's project (Ctrl+Z / Ctrl+Y). Projects are immutable, so each step is a snapshot.
/// Consecutive edits with the same <c>mergeKey</c> (one slider drag, typing in one field) form a single step until
/// <see cref="EndRun"/> (the drag or key press ended); any other edit, undo or redo also ends the run. The oldest steps
/// are dropped beyond <see cref="Capacity"/>.
/// </summary>
public sealed class ProjectHistory(ProjectSettings initial, int capacity = 200)
{
private readonly LinkedList<ProjectSettings> _undo = new();
private readonly LinkedList<ProjectSettings> _redo = new();
private string? _runKey;
/// <summary>The project, after any edit, undo, redo or <see cref="Replace"/>.</summary>
public event Action? Changed;
public ProjectSettings Current { get; private set; } = initial ?? throw new ArgumentNullException(nameof(initial));
public int Capacity { get; } = capacity > 0 ? capacity : throw new ArgumentOutOfRangeException(nameof(capacity));
public bool CanUndo => _undo.Count > 0;
public bool CanRedo => _redo.Count > 0;
/// <summary>Makes <paramref name="next"/> current as an undoable step; false (and nothing recorded) when it stores the same values.</summary>
public bool Apply(ProjectSettings next, string? mergeKey = null)
{
ArgumentNullException.ThrowIfNull(next);
if (ProjectMerge.SameValues(next, Current)) return false;
if (mergeKey is null || mergeKey != _runKey || _undo.Count == 0)
{
_undo.AddLast(Current);
if (_undo.Count > Capacity) _undo.RemoveFirst();
}
_runKey = mergeKey;
_redo.Clear();
Current = next;
Changed?.Invoke();
return true;
}
/// <summary>The gesture behind a run of merged edits ended: the next edit is a new step even with the same key.</summary>
public void EndRun() => _runKey = null;
public bool Undo()
{
if (_undo.Last is not { } previous) return false;
_undo.RemoveLast();
_redo.AddLast(Current);
return Move(previous.Value);
}
public bool Redo()
{
if (_redo.Last is not { } next) return false;
_redo.RemoveLast();
_undo.AddLast(Current);
return Move(next.Value);
}
/// <summary>
/// Adopts a project changed outside the editor (analysis advice, another client's edit merged on save) without an
/// undo step. Every undo and redo snapshot takes the same outside change, so undo reverts only the user's own steps
/// and never brings back a value that predates the adopted change.
/// </summary>
public void Replace(ProjectSettings current)
{
ArgumentNullException.ThrowIfNull(current);
if (ProjectMerge.SameValues(current, Current)) return;
var rebase = new ProjectMerge.Rebase(Current, current);
Rebase(_undo, rebase);
Rebase(_redo, rebase);
_runKey = null;
Current = current;
Changed?.Invoke();
}
private static void Rebase(LinkedList<ProjectSettings> steps, ProjectMerge.Rebase rebase)
{
for (var node = steps.First; node is not null; node = node.Next) node.Value = rebase.Apply(node.Value);
}
private bool Move(ProjectSettings target)
{
_runKey = null;
Current = target;
Changed?.Invoke();
return true;
}
}