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

138 行
4.4 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 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 = (IsCompareBase ? $"[{Texts.BaseMark}]" : string.Empty) + (IsSaved ? $"[{Texts.SavedMark}]" : string.Empty) + (Tag.Length > 0 ? $"[{Tag}]" : string.Empty);
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 * 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;
}