ファイル
YMM4-UndoTree/UndoTree/UndoTreeNodeViewModel.cs
T

184 行
5.6 KiB
C#

using System.Windows;
using YukkuriMovieMaker.Commons;
namespace UndoTree;
internal class UndoTreeNodeViewModel : Bindable
{
internal UndoTreeNodeViewModel(int id, object? handle, UndoTreeNodeViewModel? parent, string label, IReadOnlyList<UndoTreeCommandInfo> commands, DateTime time, long visit)
{
Id = id;
Handle = handle;
Parent = parent;
Label = label;
Commands = commands;
Time = time;
Visit = visit;
Keys = commands.Select(command => command.Key).OfType<UndoTreeChangeKey>().Distinct().ToArray();
ChangesOwnership = commands.Any(command => command.ChangesOwnership);
}
public int Id { get; }
public object? Handle { get; internal set; }
public UndoTreeNodeViewModel? Parent { get; internal set; }
public List<UndoTreeNodeViewModel> Children { get; } = [];
public UndoTreeNodeViewModel? LastVisited { get; internal set; }
public IReadOnlyList<UndoTreeCommandInfo> Commands { get; }
public IReadOnlyList<UndoTreeChangeKey> Keys { get; }
public bool ChangesOwnership { get; }
public string Label { get; }
public DateTime Time { get; }
public long Visit { get; internal set; }
public bool IsRoot => Parent is null;
public bool IsArchived { get; init; }
public int Depth
{
get
{
var depth = 0;
for (var node = Parent; node is not null; node = node.Parent)
depth++;
return depth;
}
}
public static UndoTreeNodeViewModel CommonAncestor(UndoTreeNodeViewModel first, UndoTreeNodeViewModel second)
{
var firstDepth = first.Depth;
var secondDepth = second.Depth;
while (firstDepth > secondDepth)
{
first = first.Parent!;
firstDepth--;
}
while (secondDepth > firstDepth)
{
second = second.Parent!;
secondDepth--;
}
while (!ReferenceEquals(first, second))
{
first = first.Parent!;
second = second.Parent!;
}
return first;
}
public IReadOnlyList<UndoTreeNodeViewModel> PathFromRoot()
{
var path = new List<UndoTreeNodeViewModel>();
for (var current = this; current is not null; current = current.Parent)
path.Add(current);
path.Reverse();
return path;
}
public IReadOnlyList<UndoTreeNodeViewModel> StepsBelow(UndoTreeNodeViewModel ancestor) => PathFromRoot().Skip(ancestor.Depth + 1).ToArray();
public IReadOnlyList<UndoTreeNodeViewModel> PrimaryChain()
{
var chain = new List<UndoTreeNodeViewModel>();
for (var next = Primary; next is not null; next = next.Primary)
chain.Add(next);
return chain;
}
public UndoTreeNodeViewModel? Primary
=> LastVisited is { } visited && Children.Contains(visited) ? visited : Children.Count > 0 ? Children[^1] : null;
public IEnumerable<UndoTreeNodeViewModel> SideBranches
{
get
{
var primary = Primary;
return Children.Where(child => !ReferenceEquals(child, primary));
}
}
public string Text => IsRoot ? Texts.InitialState : Label;
public string DisplayText
{
get
{
var text = IsRunHidden ? string.Format(Texts.FoldedRun, Text, FoldedCount + 1) : Text;
var marks = UndoTreeText.Marks(IsCompareBase, IsSaved, Tag);
return marks.Length > 0 ? $"{marks} {text}" : text;
}
}
public string Tag { get; set => Set(ref field, value ?? string.Empty, nameof(Tag), nameof(DisplayText)); } = string.Empty;
public bool IsCompareBase { get; set => Set(ref field, value, nameof(IsCompareBase), nameof(DisplayText)); }
public bool IsSaved { get; set => Set(ref field, value, nameof(IsSaved), nameof(DisplayText)); }
public bool IsEditingTag { get; set => Set(ref field, value); }
public string EditingTag { get; set => Set(ref field, value ?? string.Empty); } = string.Empty;
internal IReadOnlyList<UndoTreeNodeViewModel> Run { get; set; } = [];
public int FoldedCount { get; set => Set(ref field, value, nameof(FoldedCount), nameof(DisplayText)); }
public bool IsRunHidden { get; set => Set(ref field, value, nameof(IsRunHidden), nameof(DisplayText)); }
public string TimeText => Time == DateTime.MinValue ? string.Empty : Time.ToString("T");
public int Level { get; set => Set(ref field, value, nameof(Level), nameof(Margin)); }
public Thickness Margin => new(Level * UndoTreeLayout.IndentWidth, 0, 0, 0);
public bool IsExpanded
{
get;
set
{
if (!Set(ref field, value))
return;
if (value)
ExpandRequested?.Invoke(this, EventArgs.Empty);
else
CollapseRequested?.Invoke(this, EventArgs.Empty);
}
}
public bool HasSideBranches { get; set => Set(ref field, value); }
public bool CanExpand { get; set => Set(ref field, value); }
public bool IsSelected { get; set => Set(ref field, value); }
public bool IsCurrent { get; set => Set(ref field, value, nameof(IsCurrent), nameof(Opacity)); }
public bool IsOnPath { get; set => Set(ref field, value, nameof(IsOnPath), nameof(Opacity)); }
public bool IsRedoable { get; set => Set(ref field, value, nameof(IsRedoable), nameof(Opacity)); }
public double Opacity => IsCurrent || IsOnPath ? UndoTreeLayout.PathOpacity : IsRedoable ? UndoTreeLayout.RedoOpacity : UndoTreeLayout.OtherBranchOpacity;
public event EventHandler? ExpandRequested;
public event EventHandler? CollapseRequested;
}