From 8ec7988e6697db23a41d9290882b69e472d74982 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 26 Feb 2020 22:28:30 +0100 Subject: [PATCH] Add an OrActivatorBuilder because we have one for And. --- .../Styling/Activators/OrActivatorBuilder.cs | 38 +++++++++++++++++++ .../Styling/DescendentSelector.cs | 27 ++----------- src/Avalonia.Styling/Styling/OrSelector.cs | 27 ++----------- 3 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 src/Avalonia.Styling/Styling/Activators/OrActivatorBuilder.cs diff --git a/src/Avalonia.Styling/Styling/Activators/OrActivatorBuilder.cs b/src/Avalonia.Styling/Styling/Activators/OrActivatorBuilder.cs new file mode 100644 index 0000000000..ee7731e0ea --- /dev/null +++ b/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!; + } +} diff --git a/src/Avalonia.Styling/Styling/DescendentSelector.cs b/src/Avalonia.Styling/Styling/DescendentSelector.cs index 08b25f4057..896197d183 100644 --- a/src/Avalonia.Styling/Styling/DescendentSelector.cs +++ b/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 { diff --git a/src/Avalonia.Styling/Styling/OrSelector.cs b/src/Avalonia.Styling/Styling/OrSelector.cs index 9c76a38f45..b121658826 100644 --- a/src/Avalonia.Styling/Styling/OrSelector.cs +++ b/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) {