refactor(editor): the open sequence's jobs in their own type; restore undo as an object

WIP of the interrupted fix stage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-06 07:49:27 +00:00
1 parent 757695f037
commit 31c1a2d9c6
9 files changed
+356 -42

No files matched your search

+17 -3
View File
@@ -148,14 +148,16 @@ public sealed class EditorSession : IAsyncDisposable
: [RenderRequest(export)];
/// <summary>
/// Makes a "Save both" copy of this PC's settings the project, as one undoable step; false when it stores the same values.
/// Makes a "Save both" copy of this PC's settings the project, as one undoable step; null when it stores the same values.
/// </summary>
/// <returns>The restore, which its toast can take back while it is the last change.</returns>
/// <exception cref="System.IO.InvalidDataException">The copy cannot be read.</exception>
public async Task<bool> RestoreAsync(SavedCopy copy)
public async Task<ProjectRestore?> RestoreAsync(SavedCopy copy)
{
ArgumentNullException.ThrowIfNull(copy);
var restored = await Task.Run(() => SavedCopies.Read(copy));
return Edit(_ => restored);
var before = Project;
return Edit(_ => restored) ? new ProjectRestore(this, before, Project) : null;
}
/// <summary>The export of the project with preset <paramref name="presetId"/> chosen (own values, no batch).</summary>
@@ -235,3 +237,15 @@ public sealed class EditorSession : IAsyncDisposable
if (!ProjectMerge.SameValues(stored, project)) History.Replace(ProjectMerge.Merge(project, Project, stored));
}
}
/// <summary>
/// A "Save both" copy made the project (<see cref="EditorSession.RestoreAsync"/>). Its Undo brings back the project from
/// before as a new step, and only while the restore is still the last change: after another edit, an undo or a merged
/// save it would take that change back instead.
/// </summary>
public sealed class ProjectRestore(EditorSession session, ProjectSettings before, ProjectSettings restored)
{
public bool CanUndo => ReferenceEquals(session.Project, restored);
public bool Undo() => CanUndo && session.Edit(_ => before);
}
+11 -27
View File
@@ -40,7 +40,7 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
private readonly LatestRunner _corrections = new(Debounce);
private readonly LatestRunner _plans = new(Debounce);
private readonly DispatcherTimer _player;
private readonly Dictionary<Guid, Job> _jobs = [];
private readonly SequenceJobs _jobs;
private readonly AsyncRelayCommand _analyse;
private readonly AsyncRelayCommand _render;
private readonly AsyncRelayCommand _cancelServerJob;
@@ -83,6 +83,7 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
_encoders = encoders;
_host = host;
_dispatcher = Dispatcher.CurrentDispatcher;
_jobs = new SequenceJobs(session.Content.Sequence.Id);
_renderer = new PreviewRenderer(session.Content, session.Source);
Planner = new ExportPlanner(session.Content, encoders, host);
_thumbnails = new EditorThumbnails(session.Source, session.Content.Count);
@@ -181,10 +182,8 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
/// <summary>The Export "Sync" option: available, or why not.</summary>
public ExportSyncGate SyncGate { get => _syncGate; private set => Set(ref _syncGate, value); }
/// <summary>The server job the header shows: one running, else one paused, else the oldest waiting.</summary>
private Job? ServerJob => IsRemote
? _jobs.Values.OrderBy(j => j.State switch { JobState.Running => 0, JobState.Paused => 1, _ => 2 }).ThenBy(j => j.Started ?? j.Created).FirstOrDefault()
: null;
/// <summary>The server job the header shows (<see cref="SequenceJobs.Shown"/>).</summary>
private Job? ServerJob => IsRemote ? _jobs.Shown : null;
/// <summary>
/// "3,600 frames · Canon EOS R5 · 2 s interval · 2 h 00 m · 3 warnings"; on the Export tab what the video will be:
@@ -373,10 +372,10 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
public ExportPlan? Plan { get => _plan; private set => Set(ref _plan, value); }
/// <summary>An analysis or render of this sequence is running: settings wait, viewing tools stay live.</summary>
public bool IsBusy => _jobs.Values.Any(j => j.State == JobState.Running);
public bool IsBusy => _jobs.IsBusy;
/// <summary>A deep analysis of this sequence is queued, running or paused (Analyse would queue it twice).</summary>
public bool IsAnalysing => _jobs.Values.Any(j => j.Kind == JobKind.DeepAnalyse);
public bool IsAnalysing => _jobs.IsAnalysing;
/// <summary>
/// The project is shown, not changed: a server sequence for an account without the Edit right, or one whose frames
@@ -573,7 +572,7 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
{
await RefreshSyncGateAsync();
var jobs = await Session.Source.ListJobsAsync(CancellationToken.None);
foreach (var job in jobs) Track(job);
foreach (var job in jobs) _jobs.Track(job);
foreach (var job in jobs.OrderBy(j => j.Finished)) LearnAnalysisSpeed(job);
OnBusyChanged();
}
@@ -875,13 +874,13 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
private void OnJobChanged(Job job)
{
if (!Concerns(job)) return;
if (!_jobs.Concerns(job)) return;
_dispatcher.BeginInvoke(() =>
{
if (_disposed) return;
var wasActive = _jobs.ContainsKey(job.Id);
var wasActive = _jobs.IsActive(job.Id);
// Progress alone (several times a second on a server) only moves the server job's text and bar.
if (Track(job)) OnBusyChanged();
if (_jobs.Track(job)) OnBusyChanged();
else OnServerJobChanged();
LearnAnalysisSpeed(job);
if (!wasActive) return;
@@ -892,7 +891,7 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
/// <summary>
/// The frames of the server sequence changed while it is open (any thread): everything that changes the project is
/// disabled and saying why, and a toast offers to fetch the proxies again.
/// disabled and says why, and a toast offers to fetch the proxies again.
/// </summary>
private void OnReadOnlyChanged() => _dispatcher.BeginInvoke(() =>
{
@@ -951,21 +950,6 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable
}
}
/// <summary>An analysis or render of this sequence.</summary>
private bool Concerns(Job job) =>
job.TargetIds.Contains(Session.Content.Sequence.Id) && job.Kind is JobKind.DeepAnalyse or JobKind.LightAnalyse or JobKind.Render;
/// <summary>Keeps the jobs of this sequence that wait, run or are paused.</summary>
/// <returns>Whether a job joined or left them or changed state (progress alone does not count).</returns>
private bool Track(Job job)
{
if (!Concerns(job)) return false;
if (job.State is not (JobState.Queued or JobState.Running or JobState.Paused)) return _jobs.Remove(job.Id);
var changed = !_jobs.TryGetValue(job.Id, out var known) || known.State != job.State;
_jobs[job.Id] = job;
return changed;
}
/// <summary>Seconds per frame of the latest finished deep analysis (any sequence): the cost Analyse announces.</summary>
private void LearnAnalysisSpeed(Job job)
{
+9 -10
View File
@@ -563,31 +563,30 @@ public sealed class ExportTab : EditorTab
private async Task RestoreAsync()
{
if (SelectedCopy is not { } copy) return;
var session = Editor.Session;
var before = session.Project;
ProjectRestore? restore;
try
{
if (!await session.RestoreAsync(copy))
{
Editor.Shell.Notify(Loc.L("Editor.Copy.Same"), copy.Label);
return;
}
restore = await Editor.Session.RestoreAsync(copy);
}
catch (InvalidDataException ex)
{
Editor.Shell.ReportError(ex);
return;
}
var restored = session.Project;
if (restore is null)
{
Editor.Shell.Notify(Loc.L("Editor.Copy.Same"), copy.Label);
return;
}
var editor = Editor;
bool Undoable() => ReferenceEquals(editor.Shell.Sequence.Editor, editor) && ReferenceEquals(session.Project, restored);
editor.Shell.ShowToast(new Toast
{
Title = Loc.L("Editor.Copy.Restored"),
Message = copy.Label,
ActionText = Loc.L("Editor.Copy.Undo"),
ActionToolTip = Loc.L("Tooltips.Editor.Copy.Undo"),
ActionCommand = new RelayCommand(() => session.Edit(_ => before), Undoable),
// Only in the editor that made it: another sequence opened meanwhile has its own project.
ActionCommand = new RelayCommand(() => restore.Undo(), () => ReferenceEquals(editor.Shell.Sequence.Editor, editor) && restore.CanUndo),
});
}
+46
View File
@@ -0,0 +1,46 @@
using Titano.Core.Model;
namespace Titano.App.Editor;
/// <summary>
/// The analyses and renders of the open sequence that wait, run or are paused, as the source reports them (BRIEF §12.3
/// "while a job runs on this sequence, settings are disabled", §11.5): the editor's busy lock, whether Analyse would
/// queue a second analysis, and the server job the header of a server sequence shows. Call from one thread.
/// </summary>
public sealed class SequenceJobs(Guid sequenceId)
{
private readonly Dictionary<Guid, Job> _active = [];
/// <summary>An analysis or render runs: settings wait.</summary>
public bool IsBusy => _active.Values.Any(j => j.State == JobState.Running);
/// <summary>A deep analysis waits, runs or is paused.</summary>
public bool IsAnalysing => _active.Values.Any(j => j.Kind == JobKind.DeepAnalyse);
/// <summary>The job to show: one running, else one paused, else the oldest waiting; null when there is none.</summary>
public Job? Shown => _active.Values
.OrderBy(j => j.State switch { JobState.Running => 0, JobState.Paused => 1, _ => 2 })
.ThenBy(j => j.Started ?? j.Created)
.FirstOrDefault();
/// <summary>An analysis or render of this sequence.</summary>
public bool Concerns(Job job)
{
ArgumentNullException.ThrowIfNull(job);
return job.TargetIds.Contains(sequenceId) && job.Kind is JobKind.DeepAnalyse or JobKind.LightAnalyse or JobKind.Render;
}
/// <summary>The job is one of those that wait, run or are paused.</summary>
public bool IsActive(Guid jobId) => _active.ContainsKey(jobId);
/// <summary>Takes in a job's new state.</summary>
/// <returns>Whether a job joined or left the active ones or changed state; progress alone does not count.</returns>
public bool Track(Job job)
{
if (!Concerns(job)) return false;
if (job.State is not (JobState.Queued or JobState.Running or JobState.Paused)) return _active.Remove(job.Id);
var changed = !_active.TryGetValue(job.Id, out var known) || known.State != job.State;
_active[job.Id] = job;
return changed;
}
}