diff --git a/src/Titano.App/Editor/EditorViewModel.cs b/src/Titano.App/Editor/EditorViewModel.cs index 6bd9affa..2e4ca35a 100644 --- a/src/Titano.App/Editor/EditorViewModel.cs +++ b/src/Titano.App/Editor/EditorViewModel.cs @@ -930,7 +930,13 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable if (!_jobs.Concerns(job)) return; _dispatcher.BeginInvoke(() => { - if (_disposed) return; + if (_disposed) + { + // Closed after the Jobs screen left this render's end to it (FollowsRender): said without a read. + if (job is { Kind: JobKind.Render, State: JobState.Done } && _jobs.IsActive(job.Id) && Shell.Jobs.ClaimRenderToast(job.Id)) + AnnounceRender(job, read: false); + return; + } 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 (_jobs.Track(job)) OnBusyChanged(); @@ -1037,6 +1043,12 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable } } + /// + /// This editor follows the render (UI thread): it saw it wait or run and has not seen it end yet, so it will take its + /// toast () when it does. + /// + internal bool FollowsRender(Guid jobId) => !_disposed && _jobs.IsActive(jobId); + /// "Render finished" with the output file and Show in folder; : the sequence was read again after the render. private void AnnounceRender(Job render, bool read) { diff --git a/src/Titano.App/Sections/JobsViewModel.cs b/src/Titano.App/Sections/JobsViewModel.cs index f0de54fa..1aa9de27 100644 --- a/src/Titano.App/Sections/JobsViewModel.cs +++ b/src/Titano.App/Sections/JobsViewModel.cs @@ -3,6 +3,7 @@ using System.IO; using System.Windows.Input; using Titano.App.Infrastructure; using Titano.App.Jobs; +using Titano.App.Library; using Titano.App.Settings; using Titano.App.Shell; using Titano.Core.Diagnostics; @@ -254,14 +255,17 @@ public sealed class JobsViewModel : SectionViewModel if (job.State != JobState.Failed) _unseenFailures.Remove(job.Id); else if (before != JobState.Failed && !IsSelected) _unseenFailures.Add(job.Id); var notice = JobNotices.For(before, job); - if (notice == JobNotice.Done && job.Kind == JobKind.Render && !ClaimRenderToast(job.Id)) return; + // A flush can take the end before the editor that follows the render has seen it: the editor says it, with its output. + if (notice == JobNotice.Done && job.Kind == JobKind.Render + && (Shell.Sequence.Editor?.FollowsRender(job.Id) == true || !ClaimRenderToast(job.Id))) return; _endings.Add(notice, job); } /// /// Takes the toast of a finished render, which is said once (UI thread): true for the first caller only, which then /// raises it. The editor of the render's sequence takes it when it sees the render end, and says it with the output - /// file even when the sequence cannot be read again; a render the editor did not follow is said here. + /// file even when the sequence cannot be read again or the editor closed meanwhile; a render the editor did not + /// follow is said here. /// internal bool ClaimRenderToast(Guid jobId) => _renderToasts.Add(jobId); @@ -411,14 +415,23 @@ public sealed class JobsViewModel : SectionViewModel /// /// Opens what a job worked on inside Titano: its sequence in the editor; several sequences (a deep analysis of a - /// Library selection) in the Library, every one of them selected; else the Archive screen. + /// Library selection) in the Library, every one of them selected; else the Archive screen. A job over more sequences + /// than a Library page holds (a maintenance run over a whole archive) opens the Library as it is: looking each one + /// up, then reading page after page to select them all, would hold the screen. /// internal async Task OpenTargetAsync(Job job) { var db = Shell.Services.Database; if (db is null || job.TargetIds.Count == 0) return; - var sequences = new List(); - foreach (var id in job.TargetIds.Distinct()) + var targets = job.TargetIds.Distinct().ToList(); + if (targets.Count > LibraryPager.DefaultPageSize) + { + Shell.Navigate(SectionKey.Library); + return; + } + // Each one is looked up: an id no longer in the index would have the Library read every page in search of it. + var sequences = new List(targets.Count); + foreach (var id in targets) { if (await db.Sequences.GetAsync(id) is { } sequence) sequences.Add(sequence.Id); } diff --git a/src/Titano.App/Sections/LibraryViewModel.cs b/src/Titano.App/Sections/LibraryViewModel.cs index deaf0b88..1a65e73e 100644 --- a/src/Titano.App/Sections/LibraryViewModel.cs +++ b/src/Titano.App/Sections/LibraryViewModel.cs @@ -71,6 +71,10 @@ public sealed class LibraryViewModel : SectionViewModel private bool _isLoading; private bool _hasLoaded; private bool _suppressReload; + + /// Many cards change selection at once (): the selection is counted once after them. + private bool _bulkSelect; + private int _layoutIndex; private SequenceItem? _selected; private int _selectionCount; @@ -601,15 +605,30 @@ public sealed class LibraryViewModel : SectionViewModel if (Items.Count == listed) break; } } - SequenceItem? first = null; - foreach (var item in Items) + SelectOnly(item => wanted.Contains(item.Sequence.Id)); + if (Items.FirstOrDefault(i => i.IsSelected) is { } first) ScrollRequested?.Invoke(first); + } + + /// + /// Selects exactly the cards picks. The selection, its summary and the mass actions' + /// tooltips are worked out once at the end: once per card, each over every card, a whole archive would hold the screen. + /// + private void SelectOnly(Func chosen) + { + _bulkSelect = true; + try { - var chosen = wanted.Contains(item.Sequence.Id); - if (item.IsSelected != chosen) item.IsSelected = chosen; - if (chosen) first ??= item; + foreach (var item in Items) + { + var selected = chosen(item); + if (item.IsSelected != selected) item.IsSelected = selected; + } + } + finally + { + _bulkSelect = false; } OnSelectionChanged(); - if (first is not null) ScrollRequested?.Invoke(first); } /// Opens Tags on the selection (capture, tests). @@ -960,15 +979,11 @@ public sealed class LibraryViewModel : SectionViewModel await LoadViewsAsync(); } - private void ClearSelection() - { - foreach (var item in Items) item.IsSelected = false; - OnSelectionChanged(); - } + private void ClearSelection() => SelectOnly(_ => false); private void OnItemChanged(object? sender, PropertyChangedEventArgs e) { - if (e.PropertyName == nameof(SequenceItem.IsSelected)) OnSelectionChanged(); + if (e.PropertyName == nameof(SequenceItem.IsSelected) && !_bulkSelect) OnSelectionChanged(); } private void RaiseState() diff --git a/src/Titano.App/Sections/SequenceViewModel.cs b/src/Titano.App/Sections/SequenceViewModel.cs index 8e7f1393..9f52f821 100644 --- a/src/Titano.App/Sections/SequenceViewModel.cs +++ b/src/Titano.App/Sections/SequenceViewModel.cs @@ -142,6 +142,13 @@ public sealed class SequenceViewModel : SectionViewModel await editor.DisposeAsync(); } + /// Shows , which the caller opened over a source of its own (tests), as the open sequence. + internal void Attach(EditorViewModel editor) + { + _openId = editor.Session.Content.Sequence.Id; + Editor = editor; + } + public override void Dispose() { Shell.PropertyChanged -= OnShellChanged; diff --git a/tests/Titano.App.Tests/Windows/ShellNotificationTests.cs b/tests/Titano.App.Tests/Windows/ShellNotificationTests.cs index e784f0fb..e5bcc36e 100644 --- a/tests/Titano.App.Tests/Windows/ShellNotificationTests.cs +++ b/tests/Titano.App.Tests/Windows/ShellNotificationTests.cs @@ -5,6 +5,7 @@ using System.Windows.Input; using System.Windows.Shell; using Titano.App.Editor; using Titano.App.Infrastructure; +using Titano.App.Library; using Titano.App.Sections; using Titano.App.Settings; using Titano.App.Shell; @@ -398,9 +399,58 @@ public sealed class ShellNotificationTests Assert.Equal(shell.Archive.Health.Count(h => !h.IsOk), shell.Archive.Badge); }); + /// + /// The Jobs screen's flush can take a render's end before the editor that followed the render has seen it: it leaves + /// the toast to the editor, which says it once with its output, and once without it when it closed in between. + /// + [Test] + public Task TheEditorSaysTheRenderItFollowedAlsoWhenTheJobsScreenSeesItsEndFirst() => ScreenHost.RunAsync(async () => + { + await using var app = await ScreenSmokeTests.SampleApp.StartAsync(); + var shell = app.Shell; + shell.CompleteWelcome(); + await JobsLoadedAsync(shell); + var jobs = shell.Jobs; + var raised = new List(); + shell.PendingToasts.CollectionChanged += (_, e) => raised.AddRange(e.NewItems?.Cast() ?? []); + var content = EditorFixture.Content(EditorFixture.Sunset); + var source = new FakeSequenceSource(EditorFixture.Sunset, content); + var editor = new EditorViewModel(shell, new EditorSession(source, content, []), [], EncoderHost.Client); + shell.Sequence.Attach(editor); + + foreach (var closes in new[] { false, true }) + { + raised.Clear(); + var render = Job(JobKind.Render, JobState.Running, content.Sequence.Id); + source.Report(render); + jobs.OnJobChanged(render); + await UntilAsync(() => editor.FollowsRender(render.Id)); + jobs.Flush(); + + // The end reaches the Jobs screen first; the editor's own callback runs after its flush. + jobs.OnJobChanged(render with { State = JobState.Done }); + jobs.Flush(); + Assert.False(raised.Any(t => t.Title == Loc.L("Shell.Job.Done")), "the Jobs screen leaves the end of a followed render to the editor"); + source.Report(render with { State = JobState.Done }); + if (closes) + { + await shell.Sequence.CloseAsync(); + Assert.Null(shell.Sequence.Editor); + } + await UntilAsync(() => raised.Any(t => t.Title == Loc.L("Editor.Render.Done"))); + jobs.Flush(); + var finished = Assert.Contains(raised, t => t.Title == Loc.L("Editor.Render.Done")); + Assert.Equal(1, raised.Count(t => t.Title == Loc.L("Editor.Render.Done")), "said once"); + Assert.False(raised.Any(t => t.Title == Loc.L("Shell.Job.Done")), "and never by the Jobs screen as well"); + if (closes) Assert.Null(finished.ActionCommand, "a closed editor does not read the sequence again for the output"); + } + }); + /// /// Open on a job that worked on several sequences (a deep analysis of a Library selection) shows every one of them - /// selected in the Library, also those a search hid; a job on an archive opens the Archive screen. + /// selected in the Library: also those a search hides, whether it is still pending or applied, and those beyond the + /// pages read so far. A job over more sequences than a page leaves the Library as it is; a job on an archive opens + /// the Archive screen. /// [Test] public Task OpenOnAJobOverSeveralSequencesSelectsThemAllInTheLibrary() => ScreenHost.RunAsync(async () => @@ -414,15 +464,60 @@ public sealed class ShellNotificationTests await library.RefreshAsync(); var ids = (await db.Sequences.ListAsync(new SequenceQuery { Sort = SequenceSort.Oldest, Limit = 3 })).Items.Select(s => s.Id).ToList(); Assert.Count(3, ids); - library.SearchText = "no such sequence"; + bool Listed(Guid id) => library.Items.Any(i => i.Sequence.Id == id); + Job Analysis(IReadOnlyList targets) => + Job(JobKind.DeepAnalyse, JobState.Done, targets[0]) with { TargetIds = targets, TargetLabel = Loc.Plural("Library.Selection.Count", targets.Count) }; - var analysis = Job(JobKind.DeepAnalyse, JobState.Done, ids[0]) with { TargetIds = ids, TargetLabel = Loc.Plural("Library.Selection.Count", ids.Count) }; - await shell.Jobs.OpenTargetAsync(analysis); + // A search still waiting to apply. + library.SearchText = "no such sequence"; + await shell.Jobs.OpenTargetAsync(Analysis(ids)); Assert.Equal(SectionKey.Library, shell.Current.Key); Assert.Equal(string.Empty, library.SearchText, "the search that hid them is cleared"); Assert.Equal(ids.Count, library.SelectionCount, "every sequence the job worked on, not only the first"); Assert.SequenceEqual(ids.Order(), library.SelectedItems().Select(i => i.Sequence.Id).Order()); + // A search applied: the list no longer holds them. + library.SearchText = "no such sequence"; + await UntilAsync(() => !ids.Any(Listed)); + Assert.False(ids.Any(Listed), "the search hides them"); + await shell.Jobs.OpenTargetAsync(Analysis(ids)); + Assert.Equal(string.Empty, library.SearchText); + Assert.SequenceEqual(ids.Order(), library.SelectedItems().Select(i => i.Sequence.Id).Order()); + + // More sequences than a page: those beyond the first page are read until each is listed. + var archiveId = Assert.NotNull(await db.Sequences.GetAsync(ids[0])).ArchiveId; + var bulk = new List(); + for (var i = 0; i < LibraryPager.DefaultPageSize + 10; i++) + { + var sequence = new Sequence + { + Id = Guid.CreateVersion7(), + ArchiveId = archiveId, + Folder = $"2021/2021-03-01 Bulk {i:000}", + Title = $"Bulk {i:000}", + FirstCapture = new DateTime(2021, 3, 1).AddHours(i), + LastCapture = new DateTime(2021, 3, 1).AddHours(i + 1), + FrameCount = 10, + UpdatedAt = DateTimeOffset.UtcNow, + }; + await db.Sequences.UpsertAsync(sequence); + bulk.Add(sequence.Id); + } + await library.RefreshAsync(); + var beyond = bulk.Where(id => !Listed(id)).Take(3).ToList(); + Assert.Count(3, beyond); + await shell.Jobs.OpenTargetAsync(Analysis(beyond)); + Assert.Greater(library.Items.Count, LibraryPager.DefaultPageSize, "the next pages were read"); + Assert.SequenceEqual(beyond.Order(), library.SelectedItems().Select(i => i.Sequence.Id).Order()); + + // A job over more sequences than a page (a maintenance run over the archive) opens the Library as it is. + shell.Navigate(SectionKey.Jobs); + library.SearchText = "Bulk"; + await shell.Jobs.OpenTargetAsync(Analysis(bulk)); + Assert.Equal(SectionKey.Library, shell.Current.Key); + Assert.Equal("Bulk", library.SearchText, "its search is not cleared"); + Assert.SequenceEqual(beyond.Order(), library.SelectedItems().Select(i => i.Sequence.Id).Order(), "nor its selection replaced"); + await shell.Jobs.OpenTargetAsync(Job(JobKind.Verify, JobState.Done, Guid.CreateVersion7())); Assert.Equal(SectionKey.Archive, shell.Current.Key); // The Archive screen reads its figures when it opens: let that end before the app closes.