33 行
1.4 KiB
C#
33 行
1.4 KiB
C#
using YukkuriMovieMaker.Commons;
|
|
|
|
namespace UndoTree;
|
|
|
|
internal class UndoTreeFilter : Bindable
|
|
{
|
|
static readonly IReadOnlySet<object> NoSelection = new HashSet<object>(ReferenceEqualityComparer.Instance);
|
|
|
|
public string SearchText { get; set => Set(ref field, value ?? string.Empty, nameof(SearchText), nameof(IsActive)); } = string.Empty;
|
|
|
|
public IReadOnlySet<object> Selection { get; set => Set(ref field, value, nameof(Selection), nameof(IsActive)); } = NoSelection;
|
|
|
|
public UndoTreeOwners? Owners { get; set; }
|
|
|
|
public bool IsActive => HasText || Selection.Count > 0;
|
|
|
|
bool HasText => SearchText.Trim().Length > 0;
|
|
|
|
public bool IsMatch(UndoTreeNodeViewModel node)
|
|
=> (!HasText || MatchesText(node)) && (Selection.Count == 0 || MatchesSelection(node));
|
|
|
|
bool MatchesText(UndoTreeNodeViewModel node)
|
|
{
|
|
var text = SearchText.Trim();
|
|
return node.Text.Contains(text, StringComparison.CurrentCultureIgnoreCase)
|
|
|| node.Tag.Contains(text, StringComparison.CurrentCultureIgnoreCase)
|
|
|| node.Commands.Any(command => command.Text.Contains(text, StringComparison.CurrentCultureIgnoreCase) || command.Detail.Contains(text, StringComparison.CurrentCultureIgnoreCase));
|
|
}
|
|
|
|
bool MatchesSelection(UndoTreeNodeViewModel node)
|
|
=> node.Commands.Any(command => command.Target is { } target && (Selection.Contains(target) || Owners?.OwnersOf(target).Any(Selection.Contains) == true));
|
|
}
|