Browse Source

Reduce allocations in TypeNameAndClassSelector.Classes

pull/10478/head
Julien Lebosquain 4 years ago
parent
commit
45ba9c98df
No known key found for this signature in database GPG Key ID: 1833CAD10ACC46FD
  1. 15
      src/Avalonia.Base/Styling/TypeNameAndClassSelector.cs

15
src/Avalonia.Base/Styling/TypeNameAndClassSelector.cs

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Styling.Activators; using Avalonia.Styling.Activators;
using Avalonia.Utilities; using Avalonia.Utilities;
@ -15,7 +14,7 @@ namespace Avalonia.Styling
internal class TypeNameAndClassSelector : Selector internal class TypeNameAndClassSelector : Selector
{ {
private readonly Selector? _previous; private readonly Selector? _previous;
private readonly Lazy<List<string>> _classes = new Lazy<List<string>>(() => new List<string>()); private List<string>? _classes;
private Type? _targetType; private Type? _targetType;
private string? _selectorString; private string? _selectorString;
@ -81,7 +80,7 @@ namespace Avalonia.Styling
/// <summary> /// <summary>
/// The style classes which the selector matches. /// The style classes which the selector matches.
/// </summary> /// </summary>
public IList<string> Classes => _classes.Value; public IList<string> Classes => _classes ??= new();
/// <inheritdoc/> /// <inheritdoc/>
public override string ToString(Style? owner) public override string ToString(Style? owner)
@ -122,16 +121,16 @@ namespace Avalonia.Styling
return SelectorMatch.NeverThisInstance; return SelectorMatch.NeverThisInstance;
} }
if (_classes.IsValueCreated && _classes.Value.Count > 0) if (_classes is { Count: > 0 })
{ {
if (subscribe) if (subscribe)
{ {
var observable = new StyleClassActivator((Classes)control.Classes, _classes.Value); var observable = new StyleClassActivator(control.Classes, _classes);
return new SelectorMatch(observable); return new SelectorMatch(observable);
} }
if (!StyleClassActivator.AreClassesMatching(control.Classes, Classes)) if (!StyleClassActivator.AreClassesMatching(control.Classes, _classes))
{ {
return SelectorMatch.NeverThisInstance; return SelectorMatch.NeverThisInstance;
} }
@ -172,9 +171,9 @@ namespace Avalonia.Styling
builder.Append(Name); builder.Append(Name);
} }
if (_classes.IsValueCreated && _classes.Value.Count > 0) if (_classes is { Count: > 0 })
{ {
foreach (var c in Classes) foreach (var c in _classes)
{ {
if (!c.StartsWith(":")) if (!c.StartsWith(":"))
{ {

Loading…
Cancel
Save