feat(app): .titano project files that open their sequence with its settings

Setup registered .titano with Titano.exe "%1", but nothing wrote such a file
and Titano ignored the argument (BRIEF §18).

- Core ProjectBundle: versioned UTF-8 JSON ("titano-project", format 1) with
  the archive (id, name, path), the sequence (id, folder, title, frame
  count) and the project settings as project.json holds them; newer or
  foreign files are refused, frame positions apply only to the frames they
  were saved for, saving never replaces an earlier file.
- Data ProjectBundles: the file for a sequence of the index, and the sequence
  a file names (by id, else the same folder of the same archive).
- Editor: "Save project file" on the Export tab (sequences of this PC's
  archives); opening a file shows its sequence and applies the settings as
  one undo step with an Undo toast, or says in one sentence why not (not in
  the library, unreadable, a job runs).
- Titano.exe opens its file argument once the index is open and runs once
  per data folder: a later launch hands its file over a current-user named
  pipe, lets the running window come to the front and exits.

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-12 10:29:37 +00:00
1 parent 4f6bbfba5d
commit 2ffc0268a7
15 files changed
+877 -4

No files matched your search

@@ -0,0 +1,157 @@
using System.IO;
using System.Windows;
using Titano.App.DesignTime;
using Titano.App.Editor;
using Titano.App.Infrastructure;
using Titano.App.Shell;
using Titano.App.Tests.Windows;
using Titano.Core.Localization;
using Titano.Core.Project;
using Titano.Testing;
using Titano.Ui.Controls;
using Titano.Ui.Theming;
namespace Titano.App.Tests.Editor;
/// <summary>
/// <c>.titano</c> project files (BRIEF §18) in the real main window on the editor sample: the editor saves one, and
/// opening one shows its sequence with the file's settings as one undo step, or says in one sentence why not.
/// </summary>
[WindowsOnly]
[Serial]
[Timeout(300_000)]
public sealed class ProjectFilesTests(TestContext context)
{
[Test]
public Task AProjectFileSavesAndOpensItsSequenceWithItsSettings() => ScreenHost.RunAsync(async () =>
{
var folder = context.CreateTempDirectory("project-files");
var environment = AppEnvironment.ForCapture();
UiLanguage.Apply("en");
ThemeManager.Apply(AppTheme.Dark);
var services = new AppServices(environment, new AppSettings { Language = "en", FirstRunCompleted = true }, () => { }, new FolderDialogs(folder));
var sample = SampleData.Configure(services);
var shell = new ShellViewModel(services);
var toasts = new List<Toast>();
shell.PendingToasts.CollectionChanged += (_, e) => toasts.AddRange(e.NewItems?.OfType<Toast>() ?? []);
var window = new MainWindow(shell)
{
WindowStartupLocation = WindowStartupLocation.Manual,
Left = -20000,
Top = -20000,
Width = 1200,
Height = 788,
ShowActivated = false,
ShowInTaskbar = false,
};
window.Show();
try
{
await sample.OpenEmptyAsync();
await shell.StartAsync();
var id = await sample.FillEditorAsync();
var files = new ProjectFiles(shell);
// Saved under the sequence's title, never over an earlier file.
var editor = Assert.NotNull(await shell.Sequence.ShowAsync(id));
await files.SaveAsync(editor.Session);
await files.SaveAsync(editor.Session);
var path = Path.Combine(folder, "Harbour sunset.titano");
Assert.FileExists(path);
Assert.FileExists(Path.Combine(folder, "Harbour sunset (2).titano"));
Assert.Equal(Loc.L("Editor.ProjectFile.Saved"), toasts[^1].Title);
var bundle = ProjectBundle.Load(path);
Assert.Equal(id, bundle.Sequence.Id);
Assert.Equal(editor.FrameCount, bundle.Sequence.Frames);
Assert.Equal(editor.Session.Project.ToJson(), bundle.Project.ToJson());
// Opening it with other settings shows the sequence with them, as one step Undo takes back.
var before = editor.Session.Project;
bundle.ExposureOffset(0.7).Save(path);
await files.OpenAsync(path);
Assert.Same(editor, shell.Sequence.Editor);
Assert.Equal(0.7, editor.Session.Project.Exposure.ExposureOffsetEv);
var applied = toasts[^1];
Assert.Equal(Loc.L("Editor.ProjectFile.Applied"), applied.Title);
Assert.False(applied.IsError);
applied.ActionCommand!.Execute(null);
Assert.Equal(before.ToJson(), editor.Session.Project.ToJson());
// A sequence this library does not have, and a file that is not a project: one sentence each.
var elsewhere = bundle with
{
Archive = bundle.Archive with { Id = Guid.CreateVersion7() },
Sequence = bundle.Sequence with { Id = Guid.CreateVersion7(), Folder = "2026/elsewhere" },
};
var missing = Path.Combine(folder, "Elsewhere.titano");
elsewhere.Save(missing);
await files.OpenAsync(missing);
Assert.Equal(Loc.L("Editor.ProjectFile.NotFound"), toasts[^1].Title);
Assert.True(toasts[^1].IsError);
var text = Path.Combine(folder, "notes.titano");
await File.WriteAllTextAsync(text, "not a project");
await files.OpenAsync(text);
Assert.Equal(Loc.L("Editor.ProjectFile.Unreadable"), toasts[^1].Title);
Assert.True(toasts[^1].IsError);
Assert.Same(editor, shell.Sequence.Editor, "the open sequence stays");
await editor.Session.FlushAsync();
}
finally
{
window.Close();
await shell.Sequence.CloseAsync();
shell.Dispose();
await services.DisposeAsync();
environment.DeleteCaptureData();
}
});
[Test]
public void AHandOverMessageRoundTrips()
{
string[] args = [@"C:\Users\me\Desktop\Harbour sunset.titano"];
Assert.SequenceEqual(args, SingleInstance.Decode(SingleInstance.Encode(args))!);
Assert.Empty(SingleInstance.Decode(SingleInstance.Encode([]))!);
Assert.Null(SingleInstance.Decode("TITANO-UPLOADER/1\nE:\\"u8));
}
[Test]
public void ASecondLaunchOnTheSameDataFolderHandsItsFileToTheFirst()
{
var data = context.CreateTempDirectory("instance");
var first = Assert.NotNull(SingleInstance.TryAcquire(data));
try
{
var received = new TaskCompletionSource<IReadOnlyList<string>>(TaskCreationOptions.RunContinuationsAsynchronously);
first.Listen(args => received.TrySetResult(args));
Assert.Null(SingleInstance.TryAcquire(data), "one Titano per data folder");
using (var other = SingleInstance.TryAcquire(context.CreateTempDirectory("other-copy")))
Assert.NotNull(other, "a portable copy with its own data folder runs on its own");
Assert.True(SingleInstance.HandOver(data, [@"D:\Projects\Roof.titano"]));
Assert.True(received.Task.Wait(TimeSpan.FromSeconds(10)), "the running instance received the hand-over");
Assert.SequenceEqual([@"D:\Projects\Roof.titano"], received.Task.Result);
}
finally
{
first.Dispose();
}
}
/// <summary>Dialogs that pick one folder and cancel everything else.</summary>
private sealed class FolderDialogs(string folder) : IAppDialogs
{
public string? PickFolder(string title, string? initialFolder = null) => folder;
public string? PickJsonFile(string title) => null;
public bool Confirm(ConfirmRequest request) => false;
public bool SignIn(string server, string? username, Func<string, string, Task<string?>> signIn) => false;
}
}
internal static class ProjectBundleTestExtensions
{
/// <summary>The same file with another exposure offset.</summary>
public static ProjectBundle ExposureOffset(this ProjectBundle bundle, double ev) =>
bundle with { Project = bundle.Project with { Exposure = bundle.Project.Exposure with { ExposureOffsetEv = ev } } };
}
@@ -0,0 +1,104 @@
using Titano.Core.Project;
using Titano.Testing;
namespace Titano.Core.Tests.Project;
/// <summary>The <c>.titano</c> project file (BRIEF §18): a sequence reference plus the settings to open it with.</summary>
public sealed class ProjectBundleTests(TestContext context)
{
private static readonly Guid ArchiveId = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
private static readonly Guid SequenceId = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a02");
[Test]
public void ARoundTripKeepsTheReferenceAndTheSettings()
{
var bundle = Sample();
var path = Path.Combine(context.CreateTempDirectory("bundle"), ProjectBundle.FileNameFor("Harbour sunset"));
bundle.Save(path);
var back = ProjectBundle.Load(path);
Assert.Equal(bundle.Archive, back.Archive);
Assert.Equal(bundle.Sequence, back.Sequence);
Assert.Equal(bundle.Saved, back.Saved);
Assert.Equal(bundle.Project.ToJson(), back.Project.ToJson());
Assert.Equal(0.8, back.Project.Exposure.DeflickerStrength.Resolve(1.0));
Assert.SequenceEqual([3, 7], back.Project.ExcludedFrames);
var json = File.ReadAllText(path);
Assert.Contains("\"format\": \"titano-project\"", json);
Assert.Contains("\"formatVersion\": 1", json);
}
[Test]
public void OtherFilesAreRefusedAsNotProjectFiles()
{
Assert.Throws<InvalidDataException>(() => ProjectBundle.Parse("not json"));
Assert.Throws<InvalidDataException>(() => ProjectBundle.Parse("[1, 2]"));
// The archive's own meta files are JSON too, but not project files.
Assert.Throws<InvalidDataException>(() => ProjectBundle.Parse("""{ "schemaVersion": 1, "id": "0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01" }"""));
var json = Sample().ToJson();
Assert.Throws<InvalidDataException>(() => ProjectBundle.Parse(json.Replace("\"formatVersion\": 1", "\"formatVersion\": 2", StringComparison.Ordinal)));
Assert.Throws<InvalidDataException>(() => ProjectBundle.Parse(json.Replace(SequenceId.ToString(), "not-a-guid", StringComparison.Ordinal)));
Assert.Throws<InvalidDataException>(() => ProjectBundle.Parse(json.Replace("\"version\": 1", "\"version\": 99", StringComparison.Ordinal)));
Assert.Throws<InvalidDataException>(() => ProjectBundle.Parse(json.Replace("\"folder\"", "\"place\"", StringComparison.Ordinal)));
}
[Test]
public void AFileTooLargeToBeAProjectIsNotRead()
{
var path = Path.Combine(context.CreateTempDirectory("bundle"), "huge.titano");
using (var stream = File.Create(path)) stream.SetLength(ProjectBundle.MaxBytes + 1L);
Assert.Throws<InvalidDataException>(() => ProjectBundle.Load(path));
}
[Test]
public void FramePositionsApplyOnlyToTheFramesTheyWereSavedFor()
{
var bundle = Sample();
var current = new ProjectSettings
{
ExcludedFrames = [1],
HeroFrame = 2,
Camera = new CameraSettings { Keyframes = [new CameraKey(0, 0.5, 0.5, 1, 0)] },
};
Assert.Same(bundle.Project, bundle.ProjectFor(current, frameCount: 3600));
var changed = bundle.ProjectFor(current, frameCount: 3610);
Assert.SequenceEqual([1], changed.ExcludedFrames);
Assert.Equal(2, changed.HeroFrame);
Assert.Same(current.Camera.Keyframes, changed.Camera.Keyframes);
// Everything else comes from the file.
Assert.Equal(0.8, changed.Exposure.DeflickerStrength.Resolve(1.0));
Assert.Equal(bundle.Project.Camera.Crop, changed.Camera.Crop);
}
[Theory]
[Case("Harbour sunset", "Harbour sunset.titano")]
[Case("Roof: 12/03 \"storm\"?", "Roof_ 12_03 _storm__.titano")]
[Case("CON", "_CON.titano")]
[Case(" ", "Titano.titano")]
public void TheFileNameComesFromTheTitle(string title, string expected) => Assert.Equal(expected, ProjectBundle.FileNameFor(title));
[Test]
public void SavingNeverReplacesAnEarlierProjectFile()
{
var folder = context.CreateTempDirectory("bundle");
Assert.Equal(Path.Combine(folder, "Harbour sunset.titano"), ProjectBundle.FreePathIn(folder, "Harbour sunset"));
File.WriteAllText(Path.Combine(folder, "Harbour sunset.titano"), "{}");
Directory.CreateDirectory(Path.Combine(folder, "Harbour sunset (2).titano"));
Assert.Equal(Path.Combine(folder, "Harbour sunset (3).titano"), ProjectBundle.FreePathIn(folder, "Harbour sunset"));
}
private static ProjectBundle Sample() => new(
new BundleArchive(ArchiveId, "Main archive", @"D:\Timelapse"),
new BundleSequence(SequenceId, "2026/2026-09-28 Harbour sunset", "Harbour sunset", 3600),
new ProjectSettings
{
Exposure = new ExposureSettings { DeflickerStrength = new Auto<double>().WithManual(0.8) },
Camera = new CameraSettings { Crop = [0.1, 0.1, 0.9, 0.9], Keyframes = [new CameraKey(120, 0.4, 0.5, 1.2, 0)] },
ExcludedFrames = [3, 7],
HeroFrame = 1800,
},
new DateTimeOffset(2026, 10, 12, 9, 30, 0, TimeSpan.Zero));
}
@@ -0,0 +1,65 @@
using Titano.Core.Project;
using Titano.Data.Processing;
using Titano.Data.Tests.Infrastructure;
using Titano.Testing;
namespace Titano.Data.Tests.Processing;
/// <summary>A <c>.titano</c> project file names a sequence of this index, found again by id or by its folder (BRIEF §18).</summary>
public abstract class ProjectBundlesTests(TestContext context) : RepositoryTest(context)
{
[Test]
public async Task TheFileNamesTheSequenceAndItsArchive()
{
var db = await OpenTitanoAsync();
var archive = TestData.Archive("Main");
await db.Archives.UpsertAsync(archive, Ct);
var sequence = TestData.Sequence(archive.Id, title: "Harbour sunset");
await db.Sequences.UpsertAsync(sequence, Ct);
var project = new ProjectSettings { ExcludedFrames = [2] };
var bundle = await ProjectBundles.CreateAsync(db, sequence.Id, project, frames: 120, Ct);
Assert.Equal(new BundleArchive(archive.Id, "Main", archive.RootPath), bundle.Archive);
Assert.Equal(new BundleSequence(sequence.Id, sequence.Folder, "Harbour sunset", 120), bundle.Sequence);
Assert.Same(project, bundle.Project);
Assert.Equal(sequence.Id, (await ProjectBundles.ResolveAsync(db, ProjectBundle.Parse(bundle.ToJson()), Ct))!.Id);
await Assert.ThrowsAsync<InvalidOperationException>(() => ProjectBundles.CreateAsync(db, Guid.CreateVersion7(), project, 0, Ct));
}
[Test]
public async Task ASequenceWithANewIdIsFoundByItsFolderInTheSameArchive()
{
var db = await OpenTitanoAsync();
var archive = TestData.Archive("Main");
var other = TestData.Archive("Other");
await db.Archives.UpsertAsync(archive, Ct);
await db.Archives.UpsertAsync(other, Ct);
var adoptedAgain = TestData.Sequence(archive.Id) with { Folder = "2026/2026-09-28 Harbour" };
var elsewhere = TestData.Sequence(other.Id) with { Folder = "2026/2026-09-28 Roof" };
await db.Sequences.UpsertAsync(adoptedAgain, Ct);
await db.Sequences.UpsertAsync(elsewhere, Ct);
var bundle = Bundle(archive.Id, Guid.CreateVersion7(), "2026/2026-09-28 HARBOUR");
Assert.Equal(adoptedAgain.Id, (await ProjectBundles.ResolveAsync(db, bundle, Ct))!.Id);
// A folder of another archive, an unknown folder or an archive this index does not have: not found.
Assert.Null(await ProjectBundles.ResolveAsync(db, Bundle(archive.Id, Guid.CreateVersion7(), "2026/2026-09-28 Roof"), Ct));
Assert.Null(await ProjectBundles.ResolveAsync(db, Bundle(archive.Id, Guid.CreateVersion7(), "2025/missing"), Ct));
Assert.Null(await ProjectBundles.ResolveAsync(db, Bundle(Guid.CreateVersion7(), Guid.CreateVersion7(), "2026/2026-09-28 Harbour"), Ct));
}
private static ProjectBundle Bundle(Guid archiveId, Guid sequenceId, string folder) =>
new(new BundleArchive(archiveId, "Main", null), new BundleSequence(sequenceId, folder, "Harbour", 10), new ProjectSettings(), TestData.Now);
}
public sealed class SqliteProjectBundlesTests(TestContext context) : ProjectBundlesTests(context)
{
protected override Backend Backend => Backend.SqliteMemory;
}
[RequiresEnvironment(PgTestServer.Variable)]
public sealed class PostgresProjectBundlesTests(TestContext context) : ProjectBundlesTests(context)
{
protected override Backend Backend => Backend.Postgres;
}