保存の印をYMM4の保存済みの状態の変化で付ける

このコミットが含まれているのは:
routersys committed 2026-11-01 08:55:58 +09:00
1 親 399b7c51dc
コミット d0fd406816
4ファイル変更
+171 -26

検索条件に一致するファイルはありません

+111 -13
ファイルの表示
@@ -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<RecentFile>();
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<RecentFile>();
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<ArgumentException>(() => 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()
{
+8
ファイルの表示
@@ -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;
}
+6 -2
ファイルの表示
@@ -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<FileSettings>.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;
+46 -11
ファイルの表示
@@ -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<RecentFile> files;
const string SavedFlagName = "IsProjectFileSaved";
const string ManagerName = "UndoRedoManager";
public UndoTreeSaveWatcher(UndoTreeHistory history, ObservableCollection<RecentFile> 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<Window>()
.Select(window => window.DataContext)
.OfType<IMainViewModel>()
.SelectMany(viewModel => viewModel.GetType()
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Select(field => field.GetValue(viewModel)))
.OfType<INotifyPropertyChanged>()
.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);
}
}