ファイル
YMM4-UndoTree/UndoTree.Tests/Documents.cs
T

130 行
2.7 KiB
C#

using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using YukkuriMovieMaker.UndoRedo;
namespace UndoTree.Tests;
public sealed class Part : UndoRedoable
{
int value;
public Part(string label) => Label = label;
public string Label { get; }
public int Value
{
get => value;
set => Set(ref this.value, value);
}
public override string ToString() => $"{Label}:{Value}";
}
public sealed class Document : UndoRedoable
{
int x;
int y;
public Document() => SubscribeObservableCollectionChangedAndChild(Parts);
public int X
{
get => x;
set => Set(ref x, value);
}
public int Y
{
get => y;
set => Set(ref y, value);
}
public ObservableCollection<Part> Parts { get; } = [];
public string Snapshot() => $"X={X} Y={Y} Parts=[{string.Join(",", Parts)}]";
}
public sealed class Sheet : UndoRedoable
{
ImmutableList<Part> parts = [];
int x;
public int X
{
get => x;
set => Set(ref x, value);
}
public ImmutableList<Part> Parts
{
get => parts;
set => Set(ref parts, value);
}
public void Seed(ImmutableList<Part> seeded) => parts = seeded;
public string Snapshot() => $"X={X} Parts=[{string.Join(",", Parts.Select(part => part.Label))}]";
}
public sealed class Group : UndoRedoable
{
Document? child;
public Document? Child
{
get => child;
set => Set(ref child, value);
}
}
public sealed class Labelled : UndoRedoable
{
int amount;
int broken;
[Display(Name = "Visible name")]
public int Amount
{
get => amount;
set => Set(ref amount, value);
}
[Display(Name = "Missing", ResourceType = typeof(Texts))]
public int Broken
{
get => broken;
set => Set(ref broken, value);
}
}
public static class Histories
{
public static (UndoRedoManager Manager, Document Document) NewDocument(params string[] partLabels)
{
var document = new Document();
foreach (var label in partLabels)
document.Parts.Add(new Part(label));
var manager = new UndoRedoManager();
manager.Subscribe(document);
return (manager, document);
}
public static (UndoRedoManager Manager, Sheet Sheet) NewSheet(params string[] partLabels)
{
var sheet = new Sheet();
sheet.Seed([.. partLabels.Select(label => new Part(label))]);
var manager = new UndoRedoManager();
manager.Subscribe(sheet);
return (manager, sheet);
}
public static void Edit(UndoRedoManager manager, Action change)
{
change();
manager.Record();
}
}