From d0fd406816ebc565e12895e5459a06b59e169e81 Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 1 Nov 2026 08:55:58 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BF=9D=E5=AD=98=E3=81=AE=E5=8D=B0=E3=82=92YM?= =?UTF-8?q?M4=E3=81=AE=E4=BF=9D=E5=AD=98=E6=B8=88=E3=81=BF=E3=81=AE?= =?UTF-8?q?=E7=8A=B6=E6=85=8B=E3=81=AE=E5=A4=89=E5=8C=96=E3=81=A7=E4=BB=98?= =?UTF-8?q?=E3=81=91=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UndoTree.Tests/UndoTreeSaveWatcherTests.cs | 124 ++++++++++++++++++--- UndoTree/UndoTreeDispatch.cs | 8 ++ UndoTree/UndoTreeHistory.cs | 8 +- UndoTree/UndoTreeSaveWatcher.cs | 57 ++++++++-- 4 files changed, 171 insertions(+), 26 deletions(-) diff --git a/UndoTree.Tests/UndoTreeSaveWatcherTests.cs b/UndoTree.Tests/UndoTreeSaveWatcherTests.cs index 90de081..fdbde21 100644 --- a/UndoTree.Tests/UndoTreeSaveWatcherTests.cs +++ b/UndoTree.Tests/UndoTreeSaveWatcherTests.cs @@ -1,13 +1,33 @@ -using System.Collections.ObjectModel; +using System.IO; +using System.Reflection; +using YukkuriMovieMaker.Commons; using YukkuriMovieMaker.Plugin; using YukkuriMovieMaker.Project; -using YukkuriMovieMaker.Settings; using YukkuriMovieMaker.UndoRedo; namespace UndoTree.Tests; public sealed class UndoTreeSaveWatcherTests { + sealed class HostModel : Bindable + { + bool isProjectFileSaved = true; + + public HostModel(UndoRedoManager manager) + { + UndoRedoManager = manager; + manager.HistoryChanged += (_, _) => IsProjectFileSaved = false; + } + + public UndoRedoManager UndoRedoManager { get; } + + public bool IsProjectFileSaved + { + get => isProjectFileSaved; + set => Set(ref isProjectFileSaved, value, nameof(IsProjectFileSaved)); + } + } + static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable root) => new(manager, new AsyncAwaitStatus(), [root], 100, UndoRedoManagerReader.Create(manager)); @@ -58,39 +78,117 @@ public sealed class UndoTreeSaveWatcherTests } [Fact] - public void AddingARecentFileMeansTheProjectWasSavedButOtherChangesDoNot() + public void TheHostFlagTurningToSavedMarksTheNodeThatWasCurrent() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); + var model = new HostModel(manager); + using var watcher = new UndoTreeSaveWatcher(history, model); Histories.Edit(manager, () => document.X = 1); - var files = new ObservableCollection(); - using var watcher = new UndoTreeSaveWatcher(history, files); - - files.Add(new RecentFile(@"C:\work\a.ymmp")); - - Assert.True(history.Cursor.IsSaved); + var saved = history.Cursor; + Assert.False(model.IsProjectFileSaved); + Assert.False(saved.IsSaved); + model.IsProjectFileSaved = true; Histories.Edit(manager, () => document.Y = 2); - files.Clear(); + Assert.True(saved.IsSaved); Assert.False(history.Cursor.IsSaved); } + [Fact] + public void ClearingTheHistoryOnOpeningOrCreatingAProjectMarksNothing() + { + var (manager, document) = Histories.NewDocument(); + using var history = Track(manager, document); + var model = new HostModel(manager); + using var watcher = new UndoTreeSaveWatcher(history, model); + Histories.Edit(manager, () => document.X = 1); + + manager.Clear(); + manager.Subscribe(document); + model.IsProjectFileSaved = true; + + Assert.Equal(1, history.Count); + Assert.False(history.Root.IsSaved); + } + + [Fact] + public void EditingAloneMarksNothing() + { + var (manager, document) = Histories.NewDocument(); + using var history = Track(manager, document); + using var watcher = new UndoTreeSaveWatcher(history, new HostModel(manager)); + + Histories.Edit(manager, () => document.X = 1); + Histories.Edit(manager, () => document.Y = 2); + + Assert.All(history.Nodes, node => Assert.False(node.IsSaved)); + } + [Fact] public void ADisposedWatcherNoLongerReactsToSaving() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); + var model = new HostModel(manager); + var watcher = new UndoTreeSaveWatcher(history, model); Histories.Edit(manager, () => document.X = 1); - var files = new ObservableCollection(); - var watcher = new UndoTreeSaveWatcher(history, files); watcher.Dispose(); - files.Add(new RecentFile(@"C:\work\a.ymmp")); + model.IsProjectFileSaved = true; Assert.False(history.Cursor.IsSaved); } + [Fact] + public void AModelWithoutTheSavedFlagIsRefused() + { + var (manager, document) = Histories.NewDocument(); + using var history = Track(manager, document); + + Assert.Throws(() => new UndoTreeSaveWatcher(history, new Document())); + } + + [Fact] + public void OnlyTheModelOfTheSameManagerCountsAsItsHostModel() + { + var (manager, _) = Histories.NewDocument(); + var (other, _) = Histories.NewDocument(); + var model = new HostModel(manager); + + Assert.True(UndoTreeSaveWatcher.IsModelOf(model, manager)); + Assert.False(UndoTreeSaveWatcher.IsModelOf(model, other)); + Assert.False(UndoTreeSaveWatcher.IsModelOf(new Document(), manager)); + } + + [Fact] + public void WithoutAHostWindowThereIsNothingToWatch() + { + var (manager, document) = Histories.NewDocument(); + using var history = Track(manager, document); + + Assert.Null(UndoTreeSaveWatcher.Create(history, manager)); + } + + [Fact] + public void TheHostStillHasTheMainModelThatTheWatcherLooksFor() + { + if (!HostAssemblyResolver.IsConfigured) + return; + + var host = Assembly.LoadFrom(Path.Combine(HostAssemblyResolver.Directory, "YukkuriMovieMaker.dll")); + var viewModel = host.GetType("YukkuriMovieMaker.ViewModels.MainViewModel"); + var model = host.GetType("YukkuriMovieMaker.Project.MainModel"); + + Assert.NotNull(viewModel); + Assert.NotNull(model); + Assert.Contains(viewModel.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic), field => field.FieldType == model); + Assert.True(typeof(System.ComponentModel.INotifyPropertyChanged).IsAssignableFrom(model)); + Assert.NotNull(model.GetProperty("IsProjectFileSaved", typeof(bool))); + Assert.NotNull(model.GetProperty("UndoRedoManager", typeof(UndoRedoManager))); + } + [Fact] public void ARestoredNodeKeepsItsSavedMark() { diff --git a/UndoTree/UndoTreeDispatch.cs b/UndoTree/UndoTreeDispatch.cs index 8ea9fac..0d24f55 100644 --- a/UndoTree/UndoTreeDispatch.cs +++ b/UndoTree/UndoTreeDispatch.cs @@ -15,6 +15,14 @@ internal static class UndoTreeDispatch action(); } + public static void Defer(Action action) + { + if (Application.Current is { } application) + application.Dispatcher.InvokeAsync(action); + else + action(); + } + public static bool Confirm(string message) => Application.Current is null || MessageBox.Show(message, Texts.UndoTree, MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes; } diff --git a/UndoTree/UndoTreeHistory.cs b/UndoTree/UndoTreeHistory.cs index aecb34c..20bb351 100644 --- a/UndoTree/UndoTreeHistory.cs +++ b/UndoTree/UndoTreeHistory.cs @@ -2,7 +2,6 @@ using System.Reflection; using System.Runtime.CompilerServices; using YukkuriMovieMaker.Plugin; using YukkuriMovieMaker.Project; -using YukkuriMovieMaker.Settings; using YukkuriMovieMaker.UndoRedo; namespace UndoTree; @@ -73,7 +72,12 @@ internal class UndoTreeHistory : IDisposable { var created = new UndoTreeHistory(manager, info.AsyncAwaitStatus, RootsOf(info.Scenes), UndoTreeSettings.Default.MaxNodes, UndoRedoManagerReader.Create(manager), UndoTreeTelemetry.Report); created.recorder = new UndoTreeRecorder(created, UndoTreeRecordStore.Default, UndoTreeProject.PathOf); - created.saveWatcher = new UndoTreeSaveWatcher(created, SettingsBase.Default.Groups.Project.RecentFiles); + UndoTreeDispatch.Defer(() => + { + created.saveWatcher = UndoTreeSaveWatcher.Create(created, manager); + if (created.saveWatcher is null) + UndoTreeTelemetry.Report(new InvalidOperationException("The saved flag of the main model was not found.")); + }); return created; }); history.asyncAwaitStatus = info.AsyncAwaitStatus; diff --git a/UndoTree/UndoTreeSaveWatcher.cs b/UndoTree/UndoTreeSaveWatcher.cs index 3ee6dae..54ddf35 100644 --- a/UndoTree/UndoTreeSaveWatcher.cs +++ b/UndoTree/UndoTreeSaveWatcher.cs @@ -1,26 +1,61 @@ -using System.Collections.ObjectModel; -using System.Collections.Specialized; -using YukkuriMovieMaker.Settings; +using System.ComponentModel; +using System.Reflection; +using System.Windows; +using YukkuriMovieMaker.UndoRedo; +using YukkuriMovieMaker.ViewModels; namespace UndoTree; internal sealed class UndoTreeSaveWatcher : IDisposable { - readonly UndoTreeHistory history; - readonly ObservableCollection files; + const string SavedFlagName = "IsProjectFileSaved"; + const string ManagerName = "UndoRedoManager"; - public UndoTreeSaveWatcher(UndoTreeHistory history, ObservableCollection files) + readonly UndoTreeHistory history; + readonly INotifyPropertyChanged model; + readonly PropertyInfo savedFlag; + + public UndoTreeSaveWatcher(UndoTreeHistory history, INotifyPropertyChanged model) { this.history = history; - this.files = files; - files.CollectionChanged += Files_CollectionChanged; + this.model = model; + savedFlag = SavedFlagOf(model) ?? throw new ArgumentException($"The model has no {SavedFlagName} property.", nameof(model)); + model.PropertyChanged += Model_PropertyChanged; } - public void Dispose() => files.CollectionChanged -= Files_CollectionChanged; + public static UndoTreeSaveWatcher? Create(UndoTreeHistory history, UndoRedoManager manager) + => Find(manager) is { } model ? new UndoTreeSaveWatcher(history, model) : null; - void Files_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + public void Dispose() => model.PropertyChanged -= Model_PropertyChanged; + + static INotifyPropertyChanged? Find(UndoRedoManager manager) { - if (e.Action == NotifyCollectionChangedAction.Add) + if (Application.Current is not { } application) + return null; + + return application.Dispatcher.CheckAccess() ? Search(application, manager) : application.Dispatcher.Invoke(() => Search(application, manager)); + } + + static INotifyPropertyChanged? Search(Application application, UndoRedoManager manager) + => application.Windows.OfType() + .Select(window => window.DataContext) + .OfType() + .SelectMany(viewModel => viewModel.GetType() + .GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) + .Select(field => field.GetValue(viewModel))) + .OfType() + .FirstOrDefault(candidate => IsModelOf(candidate, manager)); + + internal static bool IsModelOf(object candidate, UndoRedoManager manager) + => SavedFlagOf(candidate) is not null + && candidate.GetType().GetProperty(ManagerName, typeof(UndoRedoManager)) is { } property + && ReferenceEquals(property.GetValue(candidate), manager); + + static PropertyInfo? SavedFlagOf(object model) => model.GetType().GetProperty(SavedFlagName, typeof(bool)); + + void Model_PropertyChanged(object? sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == SavedFlagName && savedFlag.GetValue(model) is true) UndoTreeDispatch.Post(history.MarkSaved); } }