履歴の保存先と保存の状態をウィンドウのモデルから得る

このコミットが含まれているのは:
routersys committed 2026-11-01 09:10:43 +09:00
1 親 74330e4a8e
コミット cd919c8bb6
8ファイル変更
+194 -130

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

+2 -2
ファイルの表示
@@ -179,7 +179,7 @@ YMM4が取り消しの対象として別に登録した処理を含むノード
保存するのは、ノードの木、名前、時刻、対象と項目の名前、変更前と変更後の値の短い文字です。値は60文字で切ります。YMM4の取り消しの列そのものは保存できないため、保存した履歴から状態を戻すことはできません。
ファイルは、プロジェクトのファイルの場所ごとに1つ作ります。中には、今回の履歴と、1つ前の履歴が入ります。プロジェクトを開いて最初に保存するとき、前回の履歴を1つ前として持ち越します。履歴が変わってから2秒後に、裏で保存します。保存していないプロジェクトは、保存しません。名前を付けて保存したときは、新しい名前のファイルへ続けて保存します。
ファイルは、プロジェクトのファイルの場所ごとに1つ作ります。ウィンドウを複数開いているときは、各ウィンドウの履歴を、そのウィンドウのプロジェクトのファイルに保存します。中には、今回の履歴と、1つ前の履歴が入ります。プロジェクトを開いて最初に保存するとき、前回の履歴を1つ前として持ち越します。履歴が変わってから2秒後に、裏で保存します。保存していないプロジェクトは、保存しません。名前を付けて保存したときは、新しい名前のファイルへ続けて保存します。
メニューの「前回までの履歴を表示」で、1つ前の履歴を、読むだけの木として表示します。移動、チェリーピック、リバートはできません。絞り込み、ブランチの展開、詳細は使えます。プロジェクトが変わると、表示は自動で元に戻ります。
@@ -236,7 +236,7 @@ YMM4が取り消しの対象として別に登録した処理を含むノード
## 注意事項
- 読み取りの仕組み: YMM4の内部の仕組みを読み取って履歴を作ります。YMM4の更新で内部の仕組みが変わると、読み取り方法を切り替えるか、読み取れなくなります。そのときも、YMM4の取り消しとやり直しは影響を受けません。読み取りがYMM4の状態と合わなくなったときと、保存の印のためにYMM4の保存の状態を見つけられなかったときは、その内容を開発者へ報告します。保存の状態を見つけられないときは、印を付けないだけで、ほかの機能は影響を受けません。
- 読み取りの仕組み: YMM4の内部の仕組みを読み取って履歴を作ります。YMM4の更新で内部の仕組みが変わると、読み取り方法を切り替えるか、読み取れなくなります。そのときも、YMM4の取り消しとやり直しは影響を受けません。読み取りがYMM4の状態と合わなくなったときと、ウィンドウの保存の状態とプロジェクトの場所を見つけられなかったときは、その内容を開発者へ報告します。見つけられないときは、保存の印を付けず、履歴のファイルへの保存は、ウィンドウが1つのときだけ行います。ほかの機能は影響を受けません。
- 更新通知: 本プラグインを読み込んだときに、YMM4の実行中に一度だけ新しい正式版を確認します。更新情報を取得できない場合は通知せず、そのまま動作します。プレリリースは通知の対象に含めません。
- 不具合の自動送信: 想定していない例外が起きたときは、その内容を開発者へ自動で送信します。内容は「不具合の報告」に書いています。
- 履歴の保存: 変更前の値の文字には、消した文字などが含まれることがあります。共有しているPCでは、設定をOFFのままにするか、保存した履歴を消去してください。保存した履歴は、本プラグインの外へは送信しません。
+26
ファイルの表示
@@ -0,0 +1,26 @@
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.UndoRedo;
namespace UndoTree.Tests;
sealed class HostModel : Bindable
{
bool isProjectFileSaved = true;
public HostModel(UndoRedoManager manager, string? projectFilePath = null)
{
UndoRedoManager = manager;
ProjectFilePath = projectFilePath;
manager.HistoryChanged += (_, _) => IsProjectFileSaved = false;
}
public UndoRedoManager UndoRedoManager { get; }
public string? ProjectFilePath { get; }
public bool IsProjectFileSaved
{
get => isProjectFileSaved;
set => Set(ref isProjectFileSaved, value, nameof(IsProjectFileSaved));
}
}
+82
ファイルの表示
@@ -0,0 +1,82 @@
using System.IO;
using System.Reflection;
using YukkuriMovieMaker.Plugin;
using YukkuriMovieMaker.Project;
using YukkuriMovieMaker.UndoRedo;
namespace UndoTree.Tests;
public sealed class UndoTreeHostModelTests
{
[Fact]
public void ItReadsTheSavedFlagAndTheProjectPathOfTheModel()
{
var (manager, _) = Histories.NewDocument();
var model = new HostModel(manager, @"C:\work\a.ymmp");
var host = new UndoTreeHostModel(model);
Assert.True(host.IsSaved);
Assert.Equal(@"C:\work\a.ymmp", host.ProjectPath);
model.IsProjectFileSaved = false;
Assert.False(host.IsSaved);
}
[Fact]
public void AModelWithoutTheSavedFlagIsRefused()
{
Assert.Throws<ArgumentException>(() => new UndoTreeHostModel(new Document()));
}
[Fact]
public void OnlyTheModelOfTheSameManagerCountsAsItsHostModel()
{
var (manager, _) = Histories.NewDocument();
var (other, _) = Histories.NewDocument();
var model = new HostModel(manager);
Assert.True(UndoTreeHostModel.IsModelOf(model, manager));
Assert.False(UndoTreeHostModel.IsModelOf(model, other));
Assert.False(UndoTreeHostModel.IsModelOf(new Document(), manager));
}
[Fact]
public void WithoutAHostWindowThereIsNoModelToFind()
{
var (manager, _) = Histories.NewDocument();
Assert.Null(UndoTreeHostModel.Find(manager));
}
[Fact]
public void TheProjectPathOfAHistoryComesFromItsOwnHostModel()
{
var (manager, document) = Histories.NewDocument();
var history = UndoTreeHistory.For(new TimelineToolInfo(null!, new Scenes(true), manager, new AsyncAwaitStatus()));
Assert.Null(history.ProjectPath);
history.Host = new UndoTreeHostModel(new HostModel(manager, @"C:\work\a.ymmp"));
Assert.Equal(@"C:\work\a.ymmp", history.ProjectPath);
}
[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("ProjectFilePath", typeof(string)));
Assert.NotNull(model.GetProperty("UndoRedoManager", typeof(UndoRedoManager)));
}
}
+4 -74
ファイルの表示
@@ -1,6 +1,3 @@
using System.IO;
using System.Reflection;
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.Plugin;
using YukkuriMovieMaker.Project;
using YukkuriMovieMaker.UndoRedo;
@@ -9,25 +6,6 @@ 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));
@@ -83,7 +61,7 @@ public sealed class UndoTreeSaveWatcherTests
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
var model = new HostModel(manager);
using var watcher = new UndoTreeSaveWatcher(history, model);
using var watcher = new UndoTreeSaveWatcher(history, new UndoTreeHostModel(model));
Histories.Edit(manager, () => document.X = 1);
var saved = history.Cursor;
Assert.False(model.IsProjectFileSaved);
@@ -102,7 +80,7 @@ public sealed class UndoTreeSaveWatcherTests
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
var model = new HostModel(manager);
using var watcher = new UndoTreeSaveWatcher(history, model);
using var watcher = new UndoTreeSaveWatcher(history, new UndoTreeHostModel(model));
Histories.Edit(manager, () => document.X = 1);
manager.Clear();
@@ -118,7 +96,7 @@ public sealed class UndoTreeSaveWatcherTests
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
using var watcher = new UndoTreeSaveWatcher(history, new HostModel(manager));
using var watcher = new UndoTreeSaveWatcher(history, new UndoTreeHostModel(new HostModel(manager)));
Histories.Edit(manager, () => document.X = 1);
Histories.Edit(manager, () => document.Y = 2);
@@ -132,7 +110,7 @@ public sealed class UndoTreeSaveWatcherTests
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
var model = new HostModel(manager);
var watcher = new UndoTreeSaveWatcher(history, model);
var watcher = new UndoTreeSaveWatcher(history, new UndoTreeHostModel(model));
Histories.Edit(manager, () => document.X = 1);
watcher.Dispose();
@@ -141,54 +119,6 @@ public sealed class UndoTreeSaveWatcherTests
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()
{
+10 -4
ファイルの表示
@@ -64,6 +64,8 @@ internal class UndoTreeHistory : IDisposable
public IEnumerable<UndoTreeNodeViewModel> Nodes => byHandle.Values.Prepend(Root);
internal UndoTreeHostModel? Host { get; set; }
public event EventHandler? Changed;
public static UndoTreeHistory For(TimelineToolInfo info)
@@ -71,12 +73,14 @@ internal class UndoTreeHistory : IDisposable
var history = Instances.GetValue(info.UndoRedoManager, manager =>
{
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.recorder = new UndoTreeRecorder(created, UndoTreeRecordStore.Default, created.HostProjectPath);
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."));
created.Host = UndoTreeHostModel.Find(manager);
if (created.Host is null)
UndoTreeTelemetry.Report(new InvalidOperationException("The main model of the host was not found."));
else
created.saveWatcher = new UndoTreeSaveWatcher(created, created.Host);
});
return created;
});
@@ -315,6 +319,8 @@ internal class UndoTreeHistory : IDisposable
public string? ProjectPath => recorder?.CurrentPath();
string? HostProjectPath() => Host is { } host ? host.ProjectPath : UndoTreeProject.PathOfOnlyWindow();
public void MarkSaved()
{
if (Count <= 1 || Cursor.IsSaved)
+58
ファイルの表示
@@ -0,0 +1,58 @@
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using YukkuriMovieMaker.UndoRedo;
using YukkuriMovieMaker.ViewModels;
namespace UndoTree;
internal sealed class UndoTreeHostModel
{
internal const string SavedFlagName = "IsProjectFileSaved";
const string ProjectPathName = "ProjectFilePath";
const string ManagerName = "UndoRedoManager";
readonly PropertyInfo savedFlag;
readonly PropertyInfo? projectPath;
public UndoTreeHostModel(INotifyPropertyChanged model)
{
Model = model;
savedFlag = SavedFlagOf(model) ?? throw new ArgumentException($"The model has no {SavedFlagName} property.", nameof(model));
projectPath = model.GetType().GetProperty(ProjectPathName, typeof(string));
}
public INotifyPropertyChanged Model { get; }
public bool IsSaved => savedFlag.GetValue(Model) is true;
public string? ProjectPath => projectPath?.GetValue(Model) as string;
public static UndoTreeHostModel? Find(UndoRedoManager manager)
{
if (Application.Current is not { } application)
return null;
return application.Dispatcher.CheckAccess() ? Search(application, manager) : application.Dispatcher.Invoke(() => Search(application, 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 UndoTreeHostModel? 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>()
.Where(candidate => IsModelOf(candidate, manager))
.Select(candidate => new UndoTreeHostModel(candidate))
.FirstOrDefault();
static PropertyInfo? SavedFlagOf(object model) => model.GetType().GetProperty(SavedFlagName, typeof(bool));
}
+6 -7
ファイルの表示
@@ -1,5 +1,4 @@
using System.Windows;
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.Plugin;
using YukkuriMovieMaker.Settings;
using YukkuriMovieMaker.ViewModels;
@@ -8,7 +7,7 @@ namespace UndoTree;
internal static class UndoTreeProject
{
public static string? PathOf()
public static string? PathOfOnlyWindow()
{
if (Application.Current is not { } application)
return null;
@@ -18,13 +17,13 @@ internal static class UndoTreeProject
static string? Read(Application application)
{
var index = new ZOrderedWindowEnumerable(application.Windows.OfType<Window>())
.Select(window => window?.DataContext)
var indexes = application.Windows.OfType<Window>()
.Select(window => window.DataContext)
.OfType<IMainViewModel>()
.Select(model => model.Index)
.DefaultIfEmpty(0)
.First();
.Distinct()
.ToArray();
var states = SettingsBase<YMMSettings>.Default.MainWindowStates;
return index >= 0 && index < states.Count ? states[index].ProjectPath : null;
return indexes is [var index] && index >= 0 && index < states.Count ? states[index].ProjectPath : null;
}
}
+6 -43
ファイルの表示
@@ -1,61 +1,24 @@
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using YukkuriMovieMaker.UndoRedo;
using YukkuriMovieMaker.ViewModels;
namespace UndoTree;
internal sealed class UndoTreeSaveWatcher : IDisposable
{
const string SavedFlagName = "IsProjectFileSaved";
const string ManagerName = "UndoRedoManager";
readonly UndoTreeHistory history;
readonly INotifyPropertyChanged model;
readonly PropertyInfo savedFlag;
readonly UndoTreeHostModel host;
public UndoTreeSaveWatcher(UndoTreeHistory history, INotifyPropertyChanged model)
public UndoTreeSaveWatcher(UndoTreeHistory history, UndoTreeHostModel host)
{
this.history = history;
this.model = model;
savedFlag = SavedFlagOf(model) ?? throw new ArgumentException($"The model has no {SavedFlagName} property.", nameof(model));
model.PropertyChanged += Model_PropertyChanged;
this.host = host;
host.Model.PropertyChanged += Model_PropertyChanged;
}
public static UndoTreeSaveWatcher? Create(UndoTreeHistory history, UndoRedoManager manager)
=> Find(manager) is { } model ? new UndoTreeSaveWatcher(history, model) : null;
public void Dispose() => model.PropertyChanged -= Model_PropertyChanged;
static INotifyPropertyChanged? Find(UndoRedoManager manager)
{
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));
public void Dispose() => host.Model.PropertyChanged -= Model_PropertyChanged;
void Model_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == SavedFlagName && savedFlag.GetValue(model) is true)
if (e.PropertyName == UndoTreeHostModel.SavedFlagName && host.IsSaved)
UndoTreeDispatch.Post(history.MarkSaved);
}
}