diff --git a/src/Titano.App/Editor/EditorViewModel.cs b/src/Titano.App/Editor/EditorViewModel.cs index 508c7b37..6bd9affa 100644 --- a/src/Titano.App/Editor/EditorViewModel.cs +++ b/src/Titano.App/Editor/EditorViewModel.cs @@ -937,7 +937,8 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable else OnServerJobChanged(); LearnAnalysisSpeed(job); if (!wasActive) return; - if (job.State == JobState.Done) _ = ReloadAsync(job.Kind == JobKind.Render ? job : null); + // A render this editor followed to its end is said here, with its output, unless the Jobs screen said it first. + if (job.State == JobState.Done) _ = ReloadAsync(job.Kind == JobKind.Render && Shell.Jobs.ClaimRenderToast(job.Id) ? job : null); else if (IsRemote && job.State is JobState.Failed or JobState.Cancelled) ReportServerJobEnded(job); }); } @@ -1010,23 +1011,37 @@ public sealed class EditorViewModel : LocalizedViewModel, IAsyncDisposable if (job is { Kind: JobKind.DeepAnalyse, State: JobState.Done, ItemsPerSecond: > 0 and var speed }) _analysisSecondsPerFrame = 1 / speed; } - /// Reads the sequence again after a job; a finished render offers its follow-ups (BRIEF §11.5). + /// + /// Reads the sequence again after a job. A finished render whose toast this editor took () + /// is said in any case, also when the editor closed meanwhile: with its output and follow-ups (BRIEF §11.5) once the + /// sequence is read again, without them when that failed. + /// private async Task ReloadAsync(Job? render) { + var reloaded = false; try { await Session.ReloadAsync(); + reloaded = true; } catch (Exception ex) when (ex is System.IO.IOException or System.IO.InvalidDataException or InvalidOperationException or UnauthorizedAccessException - or System.Net.Http.HttpRequestException or Protocol.Client.TitanoApiException or OperationCanceledException) + or System.Net.Http.HttpRequestException or Protocol.Client.TitanoApiException or OperationCanceledException + or Data.DatabaseException) { // A server that does not answer in time cancels the request (OperationCanceledException). Shell.ReportError(ex); - return; } - if (render is null || _disposed) return; - // A server's render stays on the server: nothing to show in Explorer. - var output = Session.Content.Renders.FirstOrDefault(r => render.Started is not { } started || r.Created >= started)?.OutputPath is { Length: > 0 } path + finally + { + if (render is not null) AnnounceRender(render, reloaded); + } + } + + /// "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) + { + // A server's render stays on the server: nothing to show in Explorer. Before a read the renders are the old ones. + var output = read && Session.Content.Renders.FirstOrDefault(r => render.Started is not { } started || r.Created >= started)?.OutputPath is { Length: > 0 } path ? path : null; Shell.ShowToast(new Toast diff --git a/src/Titano.App/Sections/JobsViewModel.cs b/src/Titano.App/Sections/JobsViewModel.cs index fff2a7fb..f0de54fa 100644 --- a/src/Titano.App/Sections/JobsViewModel.cs +++ b/src/Titano.App/Sections/JobsViewModel.cs @@ -63,6 +63,10 @@ public sealed class JobsViewModel : SectionViewModel private readonly Dictionary _rows = []; private readonly HashSet _unseenFailures = []; private readonly JobEndings _endings = new(); + + /// Finished renders whose toast was taken (). + private readonly HashSet _renderToasts = []; + private readonly JobActions _actions; private CoalescingThrottle? _throttle; private IJobQueue? _queue; @@ -250,10 +254,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 && EditorAnnounces(job)) return; + if (notice == JobNotice.Done && job.Kind == JobKind.Render && !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. + /// + internal bool ClaimRenderToast(Guid jobId) => _renderToasts.Add(jobId); + /// /// The toast of a job that ended, with one action (BRIEF §12.1, §0.2 rule 6). Several endings at once (a batch /// finished, or failed together) say how many and lead to this screen. Clicking the Windows notification of a @@ -311,11 +322,6 @@ public sealed class JobsViewModel : SectionViewModel }; } - /// A finished render of the sequence open in the editor: the editor says it, with its output file. - private bool EditorAnnounces(Job job) => - job is { Kind: JobKind.Render, State: JobState.Done } && Shell.Sequence.Editor is { IsRemote: false } editor - && job.TargetIds.Contains(editor.Session.Content.Sequence.Id); - /// A job changed state (any thread; tests feed it too): buffered until the next flush. internal void OnJobChanged(Job job) { @@ -403,13 +409,32 @@ public sealed class JobsViewModel : SectionViewModel SyncRows(); } - /// Opens what a job worked on inside Titano: its sequence in the editor, or the Archive screen. + /// + /// 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. + /// internal async Task OpenTargetAsync(Job job) { var db = Shell.Services.Database; if (db is null || job.TargetIds.Count == 0) return; - if (await db.Sequences.GetAsync(job.TargetIds[0]) is { } sequence) Shell.OpenSequence(sequence.Id); - else Shell.Navigate(SectionKey.Archive); + var sequences = new List(); + foreach (var id in job.TargetIds.Distinct()) + { + if (await db.Sequences.GetAsync(id) is { } sequence) sequences.Add(sequence.Id); + } + switch (sequences.Count) + { + case 0: + Shell.Navigate(SectionKey.Archive); + break; + case 1: + Shell.OpenSequence(sequences[0]); + break; + default: + Shell.Navigate(SectionKey.Library); + await Shell.Library.ShowSelectedAsync(sequences); + break; + } } /// Shows the folder of what a job worked on in Explorer. diff --git a/src/Titano.App/Sections/LibraryViewModel.cs b/src/Titano.App/Sections/LibraryViewModel.cs index 7fc70225..deaf0b88 100644 --- a/src/Titano.App/Sections/LibraryViewModel.cs +++ b/src/Titano.App/Sections/LibraryViewModel.cs @@ -581,6 +581,37 @@ public sealed class LibraryViewModel : SectionViewModel RaiseMass(); } + /// + /// Shows the sequences of selected, under the mass actions bar (Open on a job over several + /// sequences). Search and filters go when one of them would be hidden or is still being applied, pages are read until + /// each is listed, and the first comes into sight. Ids not in the index are left out. + /// + public async Task ShowSelectedAsync(IReadOnlyCollection ids) + { + if (_pager is null) return; + var wanted = ids.ToHashSet(); + if (_searchDelay.IsEnabled || _pager.IsLoading || !Lists(wanted)) + { + ResetFilters(); + await ReloadAsync(); + while (!Lists(wanted) && _pager is { HasMore: true }) + { + var listed = Items.Count; + await LoadMoreAsync(); + if (Items.Count == listed) break; + } + } + SequenceItem? first = null; + foreach (var item in Items) + { + var chosen = wanted.Contains(item.Sequence.Id); + if (item.IsSelected != chosen) item.IsSelected = chosen; + if (chosen) first ??= item; + } + OnSelectionChanged(); + if (first is not null) ScrollRequested?.Invoke(first); + } + /// Opens Tags on the selection (capture, tests). public void OpenRetag() => OpenRetag(Targets(MassAction.Tag)); @@ -850,6 +881,13 @@ public sealed class LibraryViewModel : SectionViewModel } private void ClearFilters() + { + ResetFilters(); + Reload(); + } + + /// Search and filters back to "any", without reading the list again. + private void ResetFilters() { _suppressReload = true; _searchDelay.Stop(); @@ -863,9 +901,11 @@ public sealed class LibraryViewModel : SectionViewModel _timelineMonth = null; _scrollToMonth = false; _suppressReload = false; - Reload(); } + /// Every sequence of is in the list on screen. + private bool Lists(HashSet ids) => ids.IsSubsetOf(Items.Select(i => i.Sequence.Id)); + /// Shows a saved view: its search, filters and sort, at once. private void Apply(SavedView view) { diff --git a/tests/Titano.App.Tests/Editor/EditorFixture.cs b/tests/Titano.App.Tests/Editor/EditorFixture.cs index 8fb39b85..2099ebd1 100644 --- a/tests/Titano.App.Tests/Editor/EditorFixture.cs +++ b/tests/Titano.App.Tests/Editor/EditorFixture.cs @@ -111,7 +111,15 @@ internal sealed class FakeSequenceSource(SyntheticSequence sequence, SequenceCon /// Awaited by every decode (tests of cancellation). public Task DecodeGate { get; set; } = Task.CompletedTask; - public Task LoadAsync(CancellationToken cancellationToken) => Task.FromResult(Content with { Project = Stored }); + /// Thrown by the next load (the sequence read again after a job). + public Exception? FailNextLoad { get; set; } + + public Task LoadAsync(CancellationToken cancellationToken) + { + if (FailNextLoad is not { } failure) return Task.FromResult(Content with { Project = Stored }); + FailNextLoad = null; + return Task.FromException(failure); + } public async ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) { diff --git a/tests/Titano.App.Tests/Windows/ShellNotificationTests.cs b/tests/Titano.App.Tests/Windows/ShellNotificationTests.cs index 11b19f09..e784f0fb 100644 --- a/tests/Titano.App.Tests/Windows/ShellNotificationTests.cs +++ b/tests/Titano.App.Tests/Windows/ShellNotificationTests.cs @@ -1,11 +1,15 @@ +using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Shell; +using Titano.App.Editor; using Titano.App.Infrastructure; using Titano.App.Sections; using Titano.App.Settings; using Titano.App.Shell; +using Titano.App.Tests.Editor; +using Titano.Core.Export; using Titano.Core.Localization; using Titano.Core.Model; using Titano.Data.Repositories; @@ -133,26 +137,40 @@ public sealed class ShellNotificationTests Assert.Equal(badge, jobs.Badge); Assert.NotEqual(TaskbarItemProgressState.Error, shell.TaskbarState); - // The editor announces the renders of its own sequence, with their output: the shell says nothing then. - var id = await app.Sample.FillEditorAsync(); - shell.OpenSequence(id); - await shell.Sequence.OpenAsync(id); - Assert.NotNull(shell.Sequence.Editor); + // The editor says the renders it followed to their end, with their output: the shell says nothing then. It says them + // also when the sequence cannot be read again (that error is said too), without Show in folder then. + var content = EditorFixture.Content(EditorFixture.Sunset); + var source = new FakeSequenceSource(EditorFixture.Sunset, content); + var editor = new EditorViewModel(shell, new EditorSession(source, content, []), [], EncoderHost.Client); + await using (editor) + { + foreach (var readable in new[] { true, false }) + { + raised.Clear(); + var render = Job(JobKind.Render, JobState.Running, content.Sequence.Id); + source.Report(render); + jobs.OnJobChanged(render); + await UntilAsync(() => editor.IsBusy); + jobs.Flush(); + if (!readable) source.FailNextLoad = new IOException("The specified network name is no longer available."); + source.Report(render with { State = JobState.Done }); + jobs.OnJobChanged(render with { State = JobState.Done }); + 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.False(raised.Any(t => t.Title == Loc.L("Shell.Job.Done")), "the editor's own render toast is not doubled"); + Assert.Equal(readable ? 0 : 1, raised.Count(t => t.IsError), "a failed read is said beside the render's end"); + if (!readable) Assert.Null(finished.ActionCommand, "without the sequence read again, there is no output to show"); + } + } + // A render the editor did not follow (of another sequence) is the shell's to announce. raised.Clear(); - var render = Job(JobKind.Render, JobState.Running, id); - jobs.OnJobChanged(render); - jobs.Flush(); - jobs.OnJobChanged(render with { State = JobState.Done }); - jobs.Flush(); - Assert.False(raised.Any(t => t.Title == Loc.L("Shell.Job.Done")), "the editor's own render toast is not doubled"); - // A render of another sequence is the shell's to announce. var other = Job(JobKind.Render, JobState.Running, Guid.CreateVersion7()); jobs.OnJobChanged(other); jobs.Flush(); jobs.OnJobChanged(other with { State = JobState.Done }); jobs.Flush(); Assert.Equal(1, raised.Count(t => t.Title == Loc.L("Shell.Job.Done"))); - await shell.Sequence.CloseAsync(); // Server › Upload all: the batch speaks once, when its last upload ends. raised.Clear(); @@ -380,6 +398,43 @@ public sealed class ShellNotificationTests Assert.Equal(shell.Archive.Health.Count(h => !h.IsOk), shell.Archive.Badge); }); + /// + /// 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. + /// + [Test] + public Task OpenOnAJobOverSeveralSequencesSelectsThemAllInTheLibrary() => ScreenHost.RunAsync(async () => + { + await using var app = await ScreenSmokeTests.SampleApp.StartAsync(); + var shell = app.Shell; + shell.CompleteWelcome(); + await app.Sample.FillAsync(); + var db = Assert.NotNull(shell.Services.Database); + var library = shell.Library; + 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"; + + 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); + 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()); + + 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. + await shell.Archive.LoadAsync(); + }); + + /// Lets the dispatcher run what was posted to it (the editor's job events and reload) until holds. + private static async Task UntilAsync(Func done) + { + for (var i = 0; i < 250 && !done(); i++) await Task.Delay(20); + } + /// The Jobs screen read its first page (it would set the job bar and the badge again). private static async Task JobsLoadedAsync(ShellViewModel shell) {