A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

75 lines
2.1 KiB

using System;
using Avalonia.LogicalTree;
using Avalonia.Styling.Activators;
#nullable enable
namespace Avalonia.Styling
{
internal class DescendantSelector : Selector
{
private readonly Selector _parent;
private string? _selectorString;
public DescendantSelector(Selector? parent)
{
_parent = parent ?? throw new InvalidOperationException("Descendant selector must be preceeded by a selector.");
}
/// <inheritdoc/>
public override bool IsCombinator => true;
/// <inheritdoc/>
public override bool InTemplate => _parent.InTemplate;
/// <inheritdoc/>
public override Type? TargetType => null;
public override string ToString(Style? owner)
{
if (_selectorString == null)
{
_selectorString = _parent.ToString(owner) + ' ';
}
return _selectorString;
}
protected override SelectorMatch Evaluate(IStyleable control, IStyle? parent, bool subscribe)
{
var c = (ILogical)control;
var descendantMatches = new OrActivatorBuilder();
while (c != null)
{
c = c.LogicalParent;
if (c is IStyleable)
{
var match = _parent.Match((IStyleable)c, parent, subscribe);
if (match.Result == SelectorMatchResult.Sometimes)
{
descendantMatches.Add(match.Activator);
}
else if (match.IsMatch)
{
return SelectorMatch.AlwaysThisInstance;
}
}
}
if (descendantMatches.Count > 0)
{
return new SelectorMatch(descendantMatches.Get());
}
else
{
return SelectorMatch.NeverThisInstance;
}
}
protected override Selector? MovePrevious() => null;
protected override Selector? MovePreviousOrParent() => _parent;
}
}