Browse Source

Add an OrActivatorBuilder because we have one for And.

pull/3636/head
Steven Kirk 7 years ago
parent
commit
8ec7988e66
  1. 38
      src/Avalonia.Styling/Styling/Activators/OrActivatorBuilder.cs
  2. 27
      src/Avalonia.Styling/Styling/DescendentSelector.cs
  3. 27
      src/Avalonia.Styling/Styling/OrSelector.cs

38
src/Avalonia.Styling/Styling/Activators/OrActivatorBuilder.cs

@ -0,0 +1,38 @@
#nullable enable
namespace Avalonia.Styling.Activators
{
internal struct OrActivatorBuilder
{
private IStyleActivator? _single;
private OrActivator? _multiple;
public int Count => _multiple?.Count ?? (_single is object ? 1 : 0);
public void Add(IStyleActivator? activator)
{
if (activator == null)
{
return;
}
if (_single is null && _multiple is null)
{
_single = activator;
}
else
{
if (_multiple is null)
{
_multiple = new OrActivator();
_multiple.Add(_single!);
_single = null;
}
_multiple.Add(activator);
}
}
public IStyleActivator Get() => _single ?? _multiple!;
}
}

27
src/Avalonia.Styling/Styling/DescendentSelector.cs

@ -41,8 +41,7 @@ namespace Avalonia.Styling
protected override SelectorMatch Evaluate(IStyleable control, bool subscribe)
{
var c = (ILogical)control;
IStyleActivator? descendentMatch = null;
OrActivator? descendantMatches = null;
var descendantMatches = new OrActivatorBuilder();
while (c != null)
{
@ -54,21 +53,7 @@ namespace Avalonia.Styling
if (match.Result == SelectorMatchResult.Sometimes)
{
if (descendentMatch is null && descendantMatches is null)
{
descendentMatch = match.Activator;
}
else
{
if (descendantMatches is null)
{
descendantMatches = new OrActivator();
descendantMatches.Add(descendentMatch!);
descendentMatch = null;
}
descendantMatches.Add(match.Activator!);
}
descendantMatches.Add(match.Activator);
}
else if (match.IsMatch)
{
@ -77,13 +62,9 @@ namespace Avalonia.Styling
}
}
if (descendantMatches is object)
{
return new SelectorMatch(descendantMatches);
}
else if (descendentMatch is object)
if (descendantMatches.Count > 0)
{
return new SelectorMatch(descendentMatch);
return new SelectorMatch(descendantMatches.Get());
}
else
{

27
src/Avalonia.Styling/Styling/OrSelector.cs

@ -70,8 +70,7 @@ namespace Avalonia.Styling
protected override SelectorMatch Evaluate(IStyleable control, bool subscribe)
{
IStyleActivator? activator = null;
OrActivator? activators = null;
var activators = new OrActivatorBuilder();
var neverThisInstance = false;
foreach (var selector in _selectors)
@ -87,32 +86,14 @@ namespace Avalonia.Styling
neverThisInstance = true;
break;
case SelectorMatchResult.Sometimes:
if (activator is null && activators is null)
{
activator = match.Activator;
}
else
{
if (activators is null)
{
activators = new OrActivator();
activators.Add(activator!);
activator = null;
}
activators.Add(match.Activator!);
}
activators.Add(match.Activator!);
break;
}
}
if (activators is object)
if (activators.Count > 0)
{
return new SelectorMatch(activators);
}
else if (activator is object)
{
return new SelectorMatch(activator);
return new SelectorMatch(activators.Get());
}
else if (neverThisInstance)
{

Loading…
Cancel
Save