From b3c7c97dd62a4a3cc24abeda474fbd9bdef4eca6 Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 1 Nov 2026 13:06:35 +0900 Subject: [PATCH] =?UTF-8?q?=E5=90=8D=E5=89=8D=E3=81=AE=E3=81=AA=E3=81=84?= =?UTF-8?q?=E5=AF=BE=E8=B1=A1=E3=82=92=E6=8C=81=E3=81=A1=E4=B8=BB=E3=81=A8?= =?UTF-8?q?=E4=BF=9D=E6=8C=81=E3=81=99=E3=82=8B=E9=A0=85=E7=9B=AE=E3=81=AE?= =?UTF-8?q?=E5=90=8D=E5=89=8D=E3=81=A7=E5=91=BC=E3=81=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- UndoTree.Tests/Documents.cs | 26 ++++++ UndoTree.Tests/UndoTreeTargetNamesTests.cs | 101 +++++++++++++++++++++ UndoTree/UndoTreeHistory.cs | 20 +++- UndoTree/UndoTreeOwners.cs | 21 +++-- UndoTree/UndoTreeTargetNames.cs | 31 +++++++ UndoTree/UndoTreeViewModel.cs | 6 +- 7 files changed, 193 insertions(+), 14 deletions(-) create mode 100644 UndoTree.Tests/UndoTreeTargetNamesTests.cs create mode 100644 UndoTree/UndoTreeTargetNames.cs diff --git a/README.md b/README.md index 954350d..807bb16 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ YMM4の取り消しとやり直しの履歴は1本の列で、取り消したあ 現在の位置は太字と青い丸、現在の位置までの経路は黒い丸、ほかは白抜きの丸で示します。やり直せるノードは少し薄く、ほかのブランチはさらに薄く表示します。右端には、ツールがノードを作った時刻を表示します。 -ノードの名前は、変更の対象の名前と項目名を「/」でつなぎます。対象の名前はアイテムやエフェクトの名前で、ないときは型の名前です。項目名はYMM4の画面に出る名前で、取れないときはプロパティの名前です。YMM4が持つ型と項目の名前は、このプラグインが画面の言語に訳します。ほかのプラグインのアイテムやエフェクトが、自分の名前や項目の表示名を持っているときは、そのプラグインが用意した言語の名前のままです。図形、波形、模様、場面切り替え、音声、立ち絵のパラメータが名前を持たないときは、同じDLLにあるプラグインの名前に、パラメータの種類を示す語を付けた名前になります。たとえばプラグインの名前が「ローソク足」なら、「ローソク足の図形パラメータ」です。同じDLLに同じ種類のプラグインが複数あるときは、型の名前の先頭が4文字以上一致し、一致が最も長いプラグインを選びます。同じ長さで並んだときは選びません。選べないときや、これらのいずれにも当たらないときに出るのは、型やプロパティの名前そのものです。1つのノードに複数の変更があるときは、先頭の変更のあとに「(ほかN件)」を付けます。リストの追加と削除は、追加だけなら「項目をn件追加」、削除だけなら「項目をm件削除」、両方なら「項目をn件追加、m件削除」と書きます。 +ノードの名前は、変更の対象の名前と項目名を「/」でつなぎます。対象の名前はアイテムやエフェクトの名前です。名前を持たない対象は、名前を持つ持ち主の名前に、持ち主がその対象を保持している項目の名前を続けます。たとえばアイテムのXのアニメーションの値を変えると、「アイテムの名前 / X / 値」になります。持ち主が見つからないときの名前は、型の名前です。項目名はYMM4の画面に出る名前で、取れないときはプロパティの名前です。YMM4が持つ型と項目の名前は、このプラグインが画面の言語に訳します。ほかのプラグインのアイテムやエフェクトが、自分の名前や項目の表示名を持っているときは、そのプラグインが用意した言語の名前のままです。図形、波形、模様、場面切り替え、音声、立ち絵のパラメータが名前を持たないときは、同じDLLにあるプラグインの名前に、パラメータの種類を示す語を付けた名前になります。たとえばプラグインの名前が「ローソク足」なら、「ローソク足の図形パラメータ」です。同じDLLに同じ種類のプラグインが複数あるときは、型の名前の先頭が4文字以上一致し、一致が最も長いプラグインを選びます。同じ長さで並んだときは選びません。選べないときや、これらのいずれにも当たらないときに出るのは、型やプロパティの名前そのものです。1つのノードに複数の変更があるときは、先頭の変更のあとに「(ほかN件)」を付けます。リストの追加と削除は、追加だけなら「項目をn件追加」、削除だけなら「項目をm件削除」、両方なら「項目をn件追加、m件削除」と書きます。 履歴が変わるたびに、現在の位置のノードを選びます。この動作はメニューの「現在の位置に追従する」で切り替えられます。「現在の位置を表示」ボタンは、現在の位置を隠している折りたたまれたブランチを開いて、そのノードを選びます。「すべてのブランチを展開」と「すべてのブランチを折りたたむ」ボタンは、ブランチを一度に開閉します。 diff --git a/UndoTree.Tests/Documents.cs b/UndoTree.Tests/Documents.cs index e4f87af..e4618bf 100644 --- a/UndoTree.Tests/Documents.cs +++ b/UndoTree.Tests/Documents.cs @@ -69,6 +69,32 @@ public sealed class Sheet : UndoRedoable public string Snapshot() => $"X={X} Parts=[{string.Join(",", Parts.Select(part => part.Label))}]"; } +public sealed class Slot : UndoRedoable +{ + int level; + + public int Level + { + get => level; + set => Set(ref level, value); + } +} + +public sealed class Panel : UndoRedoable +{ + public Panel() + { + SubscribeChildUndoRedoable(Gain); + SubscribeObservableCollectionChangedAndChild(Slots); + } + + public string Label => "panel"; + + public Slot Gain { get; } = new(); + + public ObservableCollection Slots { get; } = []; +} + public sealed class Rig : UndoRedoable { public Part Knob { get; } = new("knob"); diff --git a/UndoTree.Tests/UndoTreeTargetNamesTests.cs b/UndoTree.Tests/UndoTreeTargetNamesTests.cs new file mode 100644 index 0000000..e4c0ac1 --- /dev/null +++ b/UndoTree.Tests/UndoTreeTargetNamesTests.cs @@ -0,0 +1,101 @@ +using YukkuriMovieMaker.Project; +using YukkuriMovieMaker.UndoRedo; + +namespace UndoTree.Tests; + +public sealed class UndoTreeTargetNamesTests +{ + static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable root) + => new(manager, new AsyncAwaitStatus(), [root], 100, UndoRedoManagerReader.Create(manager)); + + [Fact] + public void AnObjectWithItsOwnLabelKeepsIt() + { + var panel = new Panel(); + + var name = UndoTreeTargetNames.Of(panel, new UndoTreeOwners([panel])); + + Assert.Null(name.Owner); + Assert.Equal("panel", name.Own); + Assert.Equal("panel", name.Full); + } + + [Fact] + public void AnObjectWithoutALabelIsNamedByTheLabeledOwnerAndThePropertyThatHoldsIt() + { + var panel = new Panel(); + + var name = UndoTreeTargetNames.Of(panel.Gain, new UndoTreeOwners([panel])); + + Assert.Equal("panel", name.Owner); + Assert.Equal("Gain", name.Own); + Assert.Equal("panel / Gain", name.Full); + } + + [Fact] + public void AnElementOfAListIsNamedByTheOwnerAndItsTypeBecauseThePositionIsNoName() + { + var panel = new Panel(); + var slot = new Slot(); + panel.Slots.Add(slot); + + var name = UndoTreeTargetNames.Of(slot, new UndoTreeOwners([panel])); + + Assert.Equal("panel / Slot", name.Full); + } + + [Fact] + public void AListItselfIsNamedByTheOwnerAndTheProperty() + { + var panel = new Panel(); + + var name = UndoTreeTargetNames.Of(panel.Slots, new UndoTreeOwners([panel])); + + Assert.Equal("panel / Slots", name.Full); + } + + [Fact] + public void AnObjectOutsideTheOwnersOrWithoutOwnersKeepsItsTypeName() + { + var panel = new Panel(); + var outsider = new Slot(); + + Assert.Equal(new UndoTreeTargetName(null, "Slot"), UndoTreeTargetNames.Of(outsider, new UndoTreeOwners([panel]))); + Assert.Equal(new UndoTreeTargetName(null, "Slot"), UndoTreeTargetNames.Of(panel.Gain, null)); + } + + [Fact] + public void ANodeIsNamedWithTheOwnerAndThePropertyOfWhatChanged() + { + var panel = new Panel(); + var manager = new UndoRedoManager(); + manager.Subscribe(panel); + using var history = Track(manager, panel); + + Histories.Edit(manager, () => panel.Gain.Level = 5); + var value = history.Cursor; + Histories.Edit(manager, () => panel.Slots.Add(new Slot())); + var list = history.Cursor; + + Assert.Equal("panel / Gain / Level", value.Label); + Assert.Equal("panel / Gain", value.Commands[0].TargetLabel); + Assert.Equal("panel / Slots / " + string.Format(Texts.ListAdded, 1), list.Label); + } + + [Fact] + public void ObjectsOfTheOwnersAreLinkedWithTheirPropertyAndWhetherTheyAreElements() + { + var panel = new Panel(); + var slot = new Slot(); + panel.Slots.Add(slot); + + var owners = new UndoTreeOwners([panel]); + + Assert.Equal("Gain", owners.LinkOf(panel.Gain)!.Value.Property.Name); + Assert.False(owners.LinkOf(panel.Gain)!.Value.IsElement); + Assert.Equal("Slots", owners.LinkOf(slot)!.Value.Property.Name); + Assert.True(owners.LinkOf(slot)!.Value.IsElement); + Assert.Same(panel, owners.LinkOf(panel.Slots)!.Value.Owner); + Assert.Null(owners.LinkOf(panel)); + } +} diff --git a/UndoTree/UndoTreeHistory.cs b/UndoTree/UndoTreeHistory.cs index 3c446f4..6561108 100644 --- a/UndoTree/UndoTreeHistory.cs +++ b/UndoTree/UndoTreeHistory.cs @@ -244,7 +244,7 @@ internal class UndoTreeHistory : IDisposable foreach (var command in commands.Where(command => !command.IsAction)) { var view = UndoRedoCommandView.Read(command); - var info = view.Describe(); + var info = InContext(view.Describe()); var verdict = Judge(view, touched, inverse); if (verdict is Verdict.DeadTarget) conflicts.Add(new UndoTreeConflict(ConflictKind.DeadTarget, info.TargetLabel, info.Member, false)); @@ -428,6 +428,20 @@ internal class UndoTreeHistory : IDisposable } } + UndoTreeCommandInfo InContext(UndoTreeCommandInfo info) + { + if (info.Target is not { } target) + return info; + + var name = UndoTreeTargetNames.Of(target, Owners); + return info.Kind switch + { + CommandKind.Property or CommandKind.Indexer => info with { TargetLabel = name.Full }, + CommandKind.ListItems when name.Owner is not null => info with { TargetLabel = name.Full }, + _ => info, + }; + } + static string LabelOf(IReadOnlyList commands) { if (commands.Count == 0) @@ -581,7 +595,7 @@ internal class UndoTreeHistory : IDisposable string label; try { - commands = source!.CommandsOf(handle).Select(command => UndoRedoCommandView.Read(command).Describe()).ToArray(); + commands = source!.CommandsOf(handle).Select(command => InContext(UndoRedoCommandView.Read(command).Describe())).ToArray(); label = LabelOf(commands); } catch (Exception exception) when (exception is not OperationCanceledException) @@ -775,7 +789,7 @@ internal class UndoTreeHistory : IDisposable } var view = UndoRedoCommandView.Read(command); - var info = view.Describe(); + var info = InContext(view.Describe()); var verdict = Judge(view, touched, inverse); var apply = policy == ConflictPolicy.Apply; switch (verdict) diff --git a/UndoTree/UndoTreeOwners.cs b/UndoTree/UndoTreeOwners.cs index 0abd9de..ecaf132 100644 --- a/UndoTree/UndoTreeOwners.cs +++ b/UndoTree/UndoTreeOwners.cs @@ -6,13 +6,15 @@ using YukkuriMovieMaker.UndoRedo; namespace UndoTree; +internal readonly record struct UndoTreeOwnerLink(object Owner, PropertyInfo Property, bool IsElement); + internal class UndoTreeOwners { const int MaxDepth = 32; static readonly ConcurrentDictionary Properties = new(); - readonly Dictionary parents = new(ReferenceEqualityComparer.Instance); + readonly Dictionary parents = new(ReferenceEqualityComparer.Instance); public UndoTreeOwners(IEnumerable roots) { @@ -32,11 +34,13 @@ internal class UndoTreeOwners } } + public UndoTreeOwnerLink? LinkOf(object target) => parents.TryGetValue(target, out var link) ? link : null; + public IReadOnlyList OwnersOf(object target) { var owners = new List(); - for (var current = target; parents.TryGetValue(current, out var parent); current = parent) - owners.Add(parent); + for (var current = target; parents.TryGetValue(current, out var link); current = link.Owner) + owners.Add(link.Owner); owners.Reverse(); return owners; @@ -58,24 +62,27 @@ internal class UndoTreeOwners if (value is IUndoRedoable child) { - Link(child, owner, childDepth, rootSet, pending); + Link(child, new UndoTreeOwnerLink(owner, property, false), childDepth, rootSet, pending); continue; } if (value is not IEnumerable items || value is string) continue; + if (value is INotifyCollectionChanged) + parents.TryAdd(value, new UndoTreeOwnerLink(owner, property, false)); + foreach (var item in items) { if (item is IUndoRedoable member) - Link(member, owner, childDepth, rootSet, pending); + Link(member, new UndoTreeOwnerLink(owner, property, true), childDepth, rootSet, pending); } } } - void Link(object child, object owner, int childDepth, HashSet rootSet, Queue<(object Owner, int Depth)> pending) + void Link(object child, UndoTreeOwnerLink link, int childDepth, HashSet rootSet, Queue<(object Owner, int Depth)> pending) { - if (rootSet.Contains(child) || !parents.TryAdd(child, owner)) + if (rootSet.Contains(child) || !parents.TryAdd(child, link)) return; pending.Enqueue((child, childDepth)); diff --git a/UndoTree/UndoTreeTargetNames.cs b/UndoTree/UndoTreeTargetNames.cs new file mode 100644 index 0000000..c430a17 --- /dev/null +++ b/UndoTree/UndoTreeTargetNames.cs @@ -0,0 +1,31 @@ +namespace UndoTree; + +internal static class UndoTreeTargetNames +{ + public static UndoTreeTargetName Of(object target, UndoTreeOwners? owners) + { + if (UndoRedoCommandView.TryGetLabel(target, out var label)) + return new UndoTreeTargetName(null, label); + + var fallback = UndoTreeNames.OfType(target.GetType()); + var names = new List(); + for (var current = target; owners?.LinkOf(current) is { } link; current = link.Owner) + { + if (!link.IsElement) + names.Add(UndoRedoCommandView.MemberLabel(link.Owner, link.Property.Name)); + + if (UndoRedoCommandView.TryGetLabel(link.Owner, out var ownerLabel)) + { + names.Reverse(); + return new UndoTreeTargetName(ownerLabel, names.Count == 0 ? fallback : string.Join(" / ", names)); + } + } + + return new UndoTreeTargetName(null, fallback); + } +} + +internal readonly record struct UndoTreeTargetName(string? Owner, string Own) +{ + public string Full => Owner is null ? Own : UndoTreeText.Join(Owner, Own); +} diff --git a/UndoTree/UndoTreeViewModel.cs b/UndoTree/UndoTreeViewModel.cs index 6f6c2c2..ce3bc49 100644 --- a/UndoTree/UndoTreeViewModel.cs +++ b/UndoTree/UndoTreeViewModel.cs @@ -830,7 +830,7 @@ internal class UndoTreeViewModel : Bindable, IToolViewModel, ITimelineToolViewMo } var owners = history.Owners; - var group = new DetailGroup(null, string.Empty); + var group = new DetailGroup(null, string.Empty, owner => UndoTreeTargetNames.Of(owner, owners).Own); foreach (var command in commands) { var current = group; @@ -860,7 +860,7 @@ internal class UndoTreeViewModel : Bindable, IToolViewModel, ITimelineToolViewMo internal sealed record TimelineLink(Func Contains, Action> Select); - sealed class DetailGroup(object? key, string text) + sealed class DetailGroup(object? key, string text, Func nameOf) { public object? Key { get; } = key; @@ -876,7 +876,7 @@ internal class UndoTreeViewModel : Bindable, IToolViewModel, ITimelineToolViewMo if (existing is not null) return existing; - var created = new DetailGroup(owner, UndoRedoCommandView.LabelOf(owner)); + var created = new DetailGroup(owner, nameOf(owner), nameOf); Children.Add(created); return created; }