From 596b748c5c4dbef3d300a87be8d7ad0ce344879f Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 2 Oct 2026 17:39:26 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.App/Editor/EditorSession.cs | 3 ++ src/Titano.App/Sections/SequenceView.xaml | 2 +- src/Titano.App/Sections/SequenceView.xaml.cs | 37 +++++++++++++++++++ src/Titano.App/Sections/SequenceViewModel.cs | 3 ++ .../Editor/EditorSessionTests.cs | 3 ++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Titano.App/Editor/EditorSession.cs b/src/Titano.App/Editor/EditorSession.cs index 4a243957..8c0a2358 100644 --- a/src/Titano.App/Editor/EditorSession.cs +++ b/src/Titano.App/Editor/EditorSession.cs @@ -64,6 +64,9 @@ public sealed class EditorSession : IAsyncDisposable public ProjectSettings Project => History.Current; + /// A change is waiting for its save or being saved. + public bool HasPendingSave => _saver.HasPendingChanges; + /// Applies as an undoable step; false when it changes nothing. /// Consecutive edits with the same key are one undo step (a slider drag). public bool Edit(Func change, string? mergeKey = null) diff --git a/src/Titano.App/Sections/SequenceView.xaml b/src/Titano.App/Sections/SequenceView.xaml index 86c8b49d..28ba0da0 100644 --- a/src/Titano.App/Sections/SequenceView.xaml +++ b/src/Titano.App/Sections/SequenceView.xaml @@ -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"> diff --git a/src/Titano.App/Sections/SequenceView.xaml.cs b/src/Titano.App/Sections/SequenceView.xaml.cs index 15844610..44175d86 100644 --- a/src/Titano.App/Sections/SequenceView.xaml.cs +++ b/src/Titano.App/Sections/SequenceView.xaml.cs @@ -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 { + /// Set on a window once its closing waits for the editor's last save. + 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; + /// + /// 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). + /// + 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(); + } + } + /// Space plays or pauses, arrows step a frame (BRIEF §12.3) — unless a text field or a control that uses the key has it. private void OnKeyDown(object sender, KeyEventArgs e) { diff --git a/src/Titano.App/Sections/SequenceViewModel.cs b/src/Titano.App/Sections/SequenceViewModel.cs index a6624b1a..ce8da01f 100644 --- a/src/Titano.App/Sections/SequenceViewModel.cs +++ b/src/Titano.App/Sections/SequenceViewModel.cs @@ -51,6 +51,9 @@ public sealed class SequenceViewModel : SectionViewModel public bool IsEmpty => _editor is null && !_isLoading; + /// The open sequence has an edit not stored yet (the save follows each change after a moment). + public bool HasPendingSave => _editor?.Session.HasPendingSave == true; + /// "Titano — Harbour sunset" while a sequence is open. public override string Title => _editor?.Title ?? Label; diff --git a/tests/Titano.App.Tests/Editor/EditorSessionTests.cs b/tests/Titano.App.Tests/Editor/EditorSessionTests.cs index af26f11a..d25b3b3c 100644 --- a/tests/Titano.App.Tests/Editor/EditorSessionTests.cs +++ b/tests/Titano.App.Tests/Editor/EditorSessionTests.cs @@ -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); }