fix(app): store the editor's last edit before the main window closes

The project is saved a moment after each change; closing the window during
that moment now waits for the save (once per window, also when another
section is showing). EditorSession.HasPendingSave reports it.

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-02 17:39:26 +00:00
1 parent b2b36d5c57
commit 596b748c5c
5 files changed
+47 -1

No files matched your search

+3
View File
@@ -64,6 +64,9 @@ public sealed class EditorSession : IAsyncDisposable
public ProjectSettings Project => History.Current;
/// <summary>A change is waiting for its save or being saved.</summary>
public bool HasPendingSave => _saver.HasPendingChanges;
/// <summary>Applies <paramref name="change"/> as an undoable step; false when it changes nothing.</summary>
/// <param name="mergeKey">Consecutive edits with the same key are one undo step (a slider drag).</param>
public bool Edit(Func<ProjectSettings, ProjectSettings> change, string? mergeKey = null)
+1 -1
View File
@@ -4,7 +4,7 @@
xmlns:ui="clr-namespace:Titano.Ui.Controls;assembly=Titano.Ui"
xmlns:v="clr-namespace:Titano.Ui.Visuals;assembly=Titano.Ui"
xmlns:l="clr-namespace:Titano.Ui.Localization;assembly=Titano.Ui"
KeyDown="OnKeyDown">
KeyDown="OnKeyDown" Loaded="OnLoaded">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
@@ -1,18 +1,55 @@
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using Titano.App.Editor;
using Titano.App.Shell;
using Titano.Core.Diagnostics;
namespace Titano.App.Sections;
public partial class SequenceView : UserControl
{
/// <summary>Set on a window once its closing waits for the editor's last save.</summary>
private static readonly DependencyProperty SavesBeforeClosingProperty =
DependencyProperty.RegisterAttached("SavesBeforeClosing", typeof(bool), typeof(SequenceView), new PropertyMetadata(false));
public SequenceView() => InitializeComponent();
private EditorViewModel? Editor => (DataContext as SequenceViewModel)?.Editor;
/// <summary>
/// The project is saved a moment after each edit; closing the window first stores the last edit (once per window,
/// so the editor's save is kept even when another section is showing).
/// </summary>
private void OnLoaded(object sender, RoutedEventArgs e)
{
if (Window.GetWindow(this) is not { } window || (bool)window.GetValue(SavesBeforeClosingProperty)) return;
window.SetValue(SavesBeforeClosingProperty, true);
var (saved, closed) = (false, false);
window.Closing += OnClosing;
window.Closed += (_, _) => closed = true;
async void OnClosing(object? s, CancelEventArgs args)
{
if (saved || args.Cancel || window.DataContext is not ShellViewModel { Sequence: { HasPendingSave: true } section }) return;
saved = true;
args.Cancel = true;
try
{
await section.CloseAsync();
}
catch (Exception ex)
{
Log.Warning("editor", "The last edit was not saved before closing", ex);
}
// An application shutdown closes the window without waiting.
if (!closed) window.Close();
}
}
/// <summary>Space plays or pauses, arrows step a frame (BRIEF §12.3) — unless a text field or a control that uses the key has it.</summary>
private void OnKeyDown(object sender, KeyEventArgs e)
{
@@ -51,6 +51,9 @@ public sealed class SequenceViewModel : SectionViewModel
public bool IsEmpty => _editor is null && !_isLoading;
/// <summary>The open sequence has an edit not stored yet (the save follows each change after a moment).</summary>
public bool HasPendingSave => _editor?.Session.HasPendingSave == true;
/// <summary>"Titano — Harbour sunset" while a sequence is open.</summary>
public override string Title => _editor?.Title ?? Label;
@@ -43,8 +43,11 @@ public sealed class EditorSessionTests
var (session, source) = Open();
await using (session)
{
Assert.False(session.HasPendingSave);
for (var i = 1; i <= 20; i++) session.Edit(p => p with { Exposure = p.Exposure with { ExposureOffsetEv = i / 20.0 } }, "offset");
Assert.True(session.HasPendingSave, "closing the window waits for this save");
await session.FlushAsync();
Assert.False(session.HasPendingSave);
Assert.Equal(1, source.Saves);
Assert.Equal(1.0, source.Stored.Exposure.ExposureOffsetEv);
}