136 行
3.6 KiB
C#
136 行
3.6 KiB
C#
using YukkuriMovieMaker.UndoRedo;
|
|
|
|
namespace UndoTree;
|
|
|
|
internal sealed class UndoRedoMirror : IUndoRedoHistory, IDisposable
|
|
{
|
|
readonly UndoRedoManager manager;
|
|
readonly IReadOnlyList<IUndoRedoable> roots;
|
|
readonly List<Entry> undo = [];
|
|
readonly List<Entry> redo = [];
|
|
Entry pending = new();
|
|
bool eventSeen;
|
|
bool cleared;
|
|
|
|
public UndoRedoMirror(UndoRedoManager manager, IReadOnlyList<IUndoRedoable> roots)
|
|
{
|
|
this.manager = manager;
|
|
this.roots = roots;
|
|
manager.Recorded += Manager_Recorded;
|
|
manager.Undoed += Manager_Undoed;
|
|
manager.Redoed += Manager_Redoed;
|
|
manager.HistoryChanged += Manager_HistoryChanged;
|
|
foreach (var root in roots)
|
|
root.UndoRedoCommandCreated += Root_UndoRedoCommandCreated;
|
|
}
|
|
|
|
public UndoRedoHistorySource Source => UndoRedoHistorySource.Events;
|
|
|
|
public bool CanReplay => false;
|
|
|
|
public bool CanRewriteUndo => false;
|
|
|
|
public bool CanRewriteRedo => false;
|
|
|
|
public bool IsConsistent => (undo.Count > 0) == manager.IsUndoable && (redo.Count > 0) == manager.IsRedoable;
|
|
|
|
public IReadOnlyList<object> Undo => undo.ToArray();
|
|
|
|
public IReadOnlyList<object> Redo => redo.ToArray();
|
|
|
|
public IReadOnlyList<IUndoRedoCommandBase> CommandsOf(object handle) => ((Entry)handle).Commands.ToArray();
|
|
|
|
public bool TakeCleared()
|
|
{
|
|
var result = cleared;
|
|
cleared = false;
|
|
return result;
|
|
}
|
|
|
|
public bool TrySetUndo(IReadOnlyList<object> oldestFirst) => false;
|
|
|
|
public bool TrySetRedo(IReadOnlyList<object> nextFirst) => false;
|
|
|
|
public void Dispose()
|
|
{
|
|
manager.Recorded -= Manager_Recorded;
|
|
manager.Undoed -= Manager_Undoed;
|
|
manager.Redoed -= Manager_Redoed;
|
|
manager.HistoryChanged -= Manager_HistoryChanged;
|
|
foreach (var root in roots)
|
|
root.UndoRedoCommandCreated -= Root_UndoRedoCommandCreated;
|
|
}
|
|
|
|
void Root_UndoRedoCommandCreated(object? sender, UndoRedoEventArgs e) => pending.Add(e.Command);
|
|
|
|
void Manager_Recorded(object? sender, EventArgs e)
|
|
{
|
|
undo.Add(pending);
|
|
redo.Clear();
|
|
while (undo.Count > manager.Capacity)
|
|
undo.RemoveAt(0);
|
|
|
|
pending = new Entry();
|
|
eventSeen = true;
|
|
}
|
|
|
|
void Manager_Undoed(object? sender, EventArgs e)
|
|
{
|
|
Move(undo, redo);
|
|
pending = new Entry();
|
|
eventSeen = true;
|
|
}
|
|
|
|
void Manager_Redoed(object? sender, EventArgs e)
|
|
{
|
|
Move(redo, undo);
|
|
pending = new Entry();
|
|
eventSeen = true;
|
|
}
|
|
|
|
void Manager_HistoryChanged(object? sender, EventArgs e)
|
|
{
|
|
if (!eventSeen)
|
|
{
|
|
undo.Clear();
|
|
redo.Clear();
|
|
pending = new Entry();
|
|
cleared = true;
|
|
}
|
|
|
|
eventSeen = false;
|
|
}
|
|
|
|
static void Move(List<Entry> from, List<Entry> to)
|
|
{
|
|
if (from.Count == 0)
|
|
return;
|
|
|
|
to.Add(from[^1]);
|
|
from.RemoveAt(from.Count - 1);
|
|
}
|
|
|
|
sealed class Entry
|
|
{
|
|
public List<IUndoRedoCommandBase> Commands { get; } = [];
|
|
|
|
public void Add(IUndoRedoCommandBase command)
|
|
{
|
|
if (command.IsEmpty)
|
|
return;
|
|
|
|
var target = Commands.FirstOrDefault(x => x.CanConnect(command));
|
|
if (target is null)
|
|
{
|
|
Commands.Add(command);
|
|
return;
|
|
}
|
|
|
|
Commands.Remove(target);
|
|
var connected = target.Connect(command);
|
|
if (!connected.IsEmpty)
|
|
Commands.Add(connected);
|
|
}
|
|
}
|
|
}
|