A pull onto a newer Chronos's settings made the editor throw: ProjectHistory.Replace rebased every undo and redo snapshot through ProjectMerge, which refuses to merge a newer format when both sides changed, so any non-empty undo stack broke the reload after a sync that had succeeded. Replace now drops the steps instead when either side (or a step) is of a newer format: the project is shown read-only and undo never brings back values from before the newer settings. The media learnt the home's hashes and conversion aliases only inside a replay, so a status or Save version before that compared a SHA-named base with photo-named media and read a false Modified or saved a version that only re-keyed photos. Now: - ProjectContent.SameChoices also compares the texts as placed on the frames, hashed or not (Bound), so status and commit without a home agree with the base; - a status asked of the home teaches the media its names (written under the lease, or when the lease is free), and so does a checkout of a project already here; - WorkspaceStatus.MediaOutdated reports frames moved at the home, or names the media could not learn yet, and the project menu offers Update media for it (tooltip text updated in en and it). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
130 lines
5.3 KiB
C#
130 lines
5.3 KiB
C#
using Chronos.Core.Project;
|
|
using Chronos.Data.Projects;
|
|
|
|
namespace Chronos.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>
|
|
/// <param name="merge">
|
|
/// The three-way merge (basis, ours, theirs) that gives each step an outside change (<see cref="Replace"/>): the source's,
|
|
/// which merges a workspace project's frame positions photo by photo; <see cref="ProjectMerge.Merge"/> when null.
|
|
/// </param>
|
|
public sealed class ProjectHistory(ProjectSettings initial, int capacity = 200, Func<ProjectSettings, ProjectSettings, ProjectSettings, ProjectSettings>? merge = null)
|
|
{
|
|
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. Settings of a newer Chronos on either side (a pull
|
|
/// took them: the project is shown read-only) are never merged with this one's (<see cref="ProjectMerge"/>), so no step
|
|
/// can take that change: the undo and redo steps go instead, and undo never brings back values from before it.
|
|
/// </summary>
|
|
public void Replace(ProjectSettings current)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(current);
|
|
if (ProjectMerge.SameValues(current, Current)) return;
|
|
if (current.IsNewerFormat || Current.IsNewerFormat || _undo.Any(s => s.IsNewerFormat) || _redo.Any(s => s.IsNewerFormat))
|
|
{
|
|
_undo.Clear();
|
|
_redo.Clear();
|
|
_runKey = null;
|
|
Current = current;
|
|
Changed?.Invoke();
|
|
return;
|
|
}
|
|
var (basis, theirs) = (Current, current);
|
|
Func<ProjectSettings, ProjectSettings> rebase = merge is null ? new ProjectMerge.Rebase(basis, theirs).Apply : step => merge(basis, step, theirs);
|
|
Rebase(_undo, rebase);
|
|
Rebase(_redo, rebase);
|
|
_runKey = null;
|
|
Current = current;
|
|
Changed?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Rewrites the project and every undo and redo snapshot with <paramref name="remap"/>, without an undo step: a change
|
|
/// of what the values mean rather than of the values (frame positions following frames inserted under the editor), so
|
|
/// undo and redo keep naming the same frames.
|
|
/// </summary>
|
|
public void Remap(Func<ProjectSettings, ProjectSettings> remap)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(remap);
|
|
foreach (var steps in new[] { _undo, _redo })
|
|
{
|
|
for (var node = steps.First; node is not null; node = node.Next) node.Value = remap(node.Value);
|
|
}
|
|
var current = remap(Current);
|
|
if (ProjectMerge.SameValues(current, Current)) return;
|
|
_runKey = null;
|
|
Current = current;
|
|
Changed?.Invoke();
|
|
}
|
|
|
|
private static void Rebase(LinkedList<ProjectSettings> steps, Func<ProjectSettings, ProjectSettings> rebase)
|
|
{
|
|
for (var node = steps.First; node is not null; node = node.Next) node.Value = rebase(node.Value);
|
|
}
|
|
|
|
private bool Move(ProjectSettings target)
|
|
{
|
|
_runKey = null;
|
|
Current = target;
|
|
Changed?.Invoke();
|
|
return true;
|
|
}
|
|
}
|