using System.Windows; using YukkuriMovieMaker.Commons; namespace UndoTree; internal class UndoTreeNodeViewModel : Bindable { internal UndoTreeNodeViewModel(int id, object? handle, UndoTreeNodeViewModel? parent, string label, IReadOnlyList 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().Distinct().ToArray(); } public int Id { get; } public object? Handle { get; internal set; } public UndoTreeNodeViewModel? Parent { get; internal set; } public List Children { get; } = []; public UndoTreeNodeViewModel? LastVisited { get; internal set; } public IReadOnlyList Commands { get; } public IReadOnlyList Keys { get; } public string Label { get; } public DateTime Time { get; } public long Visit { get; internal set; } public bool IsRoot => Parent is null; public int Depth { get { var depth = 0; for (var node = Parent; node is not null; node = node.Parent) depth++; return depth; } } public UndoTreeNodeViewModel? Primary => LastVisited is { } visited && Children.Contains(visited) ? visited : Children.Count > 0 ? Children[^1] : null; public IEnumerable SideBranches { get { var primary = Primary; return Children.Where(child => !ReferenceEquals(child, primary)); } } public string Text => IsRoot ? Texts.InitialState : Label; 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 * 16, 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 ? 1 : IsRedoable ? 0.75 : 0.55; public event EventHandler? ExpandRequested; public event EventHandler? CollapseRequested; }