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, IReadOnlyList? steps = null) { Id = id; Handle = handle; Parent = parent; Label = label; Commands = commands; Time = time; Visit = visit; Steps = steps; Keys = commands.Select(command => command.Key).OfType().Distinct().ToArray(); ChangesOwnership = commands.Any(command => command.ChangesOwnership); Toggle = this; } 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 bool ChangesOwnership { get; } public IReadOnlyList? Steps { get; } public string ProjectHash { get; internal set; } = string.Empty; 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 PathFromRoot() { var path = new List(); for (var current = this; current is not null; current = current.Parent) path.Add(current); path.Reverse(); return path; } public IReadOnlyList StepsBelow(UndoTreeNodeViewModel ancestor) => PathFromRoot().Skip(ancestor.Depth + 1).ToArray(); public IReadOnlyList PrimaryChain() { var chain = new List(); 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 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, BranchMarks); 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 string BranchMarks { get; set => Set(ref field, value ?? string.Empty, nameof(BranchMarks), 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 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 UndoTreeNodeViewModel Toggle { 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 bool CanRestore { get; set => Set(ref field, value, nameof(CanRestore), nameof(Opacity)); } public string? RestoreHint { get; set => Set(ref field, value); } public double Opacity => IsArchived && !CanRestore ? UndoTreeLayout.UnavailableOpacity : IsCurrent || IsOnPath ? UndoTreeLayout.PathOpacity : IsRedoable ? UndoTreeLayout.RedoOpacity : UndoTreeLayout.OtherBranchOpacity; public event EventHandler? ExpandRequested; public event EventHandler? CollapseRequested; }